UNPKG

@attivio/suit

Version:

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

135 lines (120 loc) 5.45 kB
var _class, _temp; 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; } import React from 'react'; import DateFormat from '../api/DateFormat'; import DateUtils from '../util/DateUtils'; 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 = DateFormat.LONG_YEAR; } else if (betweenMS >= DateRangeDisplay.MONTHS_MS) { format = DateFormat.LONG_MONTH; } else if (betweenMS >= DateRangeDisplay.DAYS_MS) { format = DateFormat.DAY_OF_MONTH; } else if (betweenMS > DateRangeDisplay.HOURS_MS) { format = DateFormat.HOUR; } else if (betweenMS > DateRangeDisplay.SECONDS_MS) { format = DateFormat.SHORT_TIME; } else { // If start and end are the same, show a full date and time. format = DateFormat.MEDIUM; } } else { format = DateFormat.MEDIUM; } var startDate = ''; // Crazy hoop-jumping to deal with flow not remembering the previous if (thius.props.start) if (this.props.start) { startDate = DateUtils.formatDate(this.props.start, format); } if (this.props.end) { var endDate = DateUtils.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 React.createElement( 'span', { style: style }, this.props.label, ' ', React.createElement( 'span', { style: { color: '#2e75b3' } }, range ) ); }; return DateRangeDisplay; }(React.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); export { DateRangeDisplay as default };