UNPKG

@attivio/suit

Version:

Attivio SUIT, the Search UI Toolkit, is a library for creating search clients for searching the Attivio platform.

150 lines (128 loc) 5.94 kB
'use strict'; exports.__esModule = true; exports.default = undefined; var _class, _temp; var _react = require('react'); var _react2 = _interopRequireDefault(_react); var _DateFormat = require('../api/DateFormat'); var _DateFormat2 = _interopRequireDefault(_DateFormat); var _DateUtils = require('../util/DateUtils'); var _DateUtils2 = _interopRequireDefault(_DateUtils); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } var DateRangeDisplay = (_temp = _class = function (_React$Component) { _inherits(DateRangeDisplay, _React$Component); function DateRangeDisplay() { _classCallCheck(this, DateRangeDisplay); return _possibleConstructorReturn(this, _React$Component.apply(this, arguments)); } DateRangeDisplay.getIntervalRange = function getIntervalRange(interval) { // Convert interval from milliseconds to a duration var range = void 0; // Try years... var years = Math.floor(interval / DateRangeDisplay.YEARS_MS); if (years >= 1) { range = years === 1 ? 'year' : years + ' years'; } else { var months = Math.floor(interval / DateRangeDisplay.MONTHS_MS); if (months >= 1) { range = months === 1 ? 'month' : months + ' months'; } else { var weeks = Math.floor(interval / DateRangeDisplay.WEEKS_MS); if (weeks >= 1) { range = weeks === 1 ? 'week' : weeks + ' weeks'; } else { var days = Math.floor(interval / DateRangeDisplay.DAYS_MS); if (days >= 1) { range = days === 1 ? 'day' : days + ' days'; } else { var hours = Math.floor(interval / DateRangeDisplay.HOURS_MS); if (hours >= 1) { range = hours === 1 ? 'hour' : hours + ' hours'; } else { var minutes = Math.floor(interval / DateRangeDisplay.MINUTES_MS); if (minutes >= 1) { range = minutes === 1 ? 'minute' : minutes + ' minutes'; } else { var seconds = Math.floor(interval / DateRangeDisplay.SECONDS_MS); if (seconds >= 1) { range = seconds === 1 ? 'second' : seconds + ' seconds'; } else { range = interval === 1 ? 'millisecond' : interval + ' milliseconds'; } } } } } } } return 'The last ' + range; }; DateRangeDisplay.prototype.render = function render() { var range = void 0; var format = void 0; if (this.props.start) { var startMS = this.props.start.getTime(); if (this.props.end) { var betweenMS = this.props.end.getTime() - startMS; if (betweenMS >= DateRangeDisplay.YEARS_MS) { format = _DateFormat2.default.LONG_YEAR; } else if (betweenMS >= DateRangeDisplay.MONTHS_MS) { format = _DateFormat2.default.LONG_MONTH; } else if (betweenMS >= DateRangeDisplay.DAYS_MS) { format = _DateFormat2.default.DAY_OF_MONTH; } else if (betweenMS > DateRangeDisplay.HOURS_MS) { format = _DateFormat2.default.HOUR; } else if (betweenMS > DateRangeDisplay.SECONDS_MS) { format = _DateFormat2.default.SHORT_TIME; } else { // If start and end are the same, show a full date and time. format = _DateFormat2.default.MEDIUM; } } else { format = _DateFormat2.default.MEDIUM; } var startDate = ''; // Crazy hoop-jumping to deal with flow not remembering the previous if (thius.props.start) if (this.props.start) { startDate = _DateUtils2.default.formatDate(this.props.start, format); } if (this.props.end) { var endDate = _DateUtils2.default.formatDate(this.props.end, format); if (startDate === endDate) { range = startDate; } else { range = startDate + ' - ' + endDate; } } else { range = startDate; } } else if (this.props.interval > 0) { range = DateRangeDisplay.getIntervalRange(this.props.interval); } else { range = 'Not specified'; } var style = Object.assign({ fontSize: '1.2em', fontWeight: 'bold' }, this.props.style); return _react2.default.createElement( 'span', { style: style }, this.props.label, ' ', _react2.default.createElement( 'span', { style: { color: '#2e75b3' } }, range ) ); }; return DateRangeDisplay; }(_react2.default.Component), _class.defaultProps = { label: 'Date:', interval: 0, start: null, end: null, style: {} }, _class.SECONDS_MS = 1000, _class.MINUTES_MS = 1000 * 60, _class.HOURS_MS = 1000 * 60 * 60, _class.DAYS_MS = 1000 * 60 * 60 * 24, _class.WEEKS_MS = 1000 * 60 * 60 * 24 * 7, _class.MONTHS_MS = 1000 * 60 * 60 * 24 * 30, _class.YEARS_MS = 1000 * 60 * 60 * 24 * 365, _class.displayName = 'DateRangeDisplay', _temp); exports.default = DateRangeDisplay; module.exports = exports['default'];