UNPKG

@seafile/seafile-calendar

Version:
277 lines (246 loc) 9.8 kB
import _classCallCheck from 'babel-runtime/helpers/classCallCheck'; import _possibleConstructorReturn from 'babel-runtime/helpers/possibleConstructorReturn'; import _inherits from 'babel-runtime/helpers/inherits'; import React from 'react'; import PropTypes from 'prop-types'; import dayjs from 'dayjs'; var CalendarRightPanel = function (_React$Component) { _inherits(CalendarRightPanel, _React$Component); function CalendarRightPanel(props) { _classCallCheck(this, CalendarRightPanel); var _this = _possibleConstructorReturn(this, _React$Component.call(this, props)); _this.centerScroll = function (container, index) { if (!container || index < 0) return; var firstItem = container.querySelector('li'); var itemHeight = firstItem && firstItem.offsetHeight || 32; var containerHeight = container.clientHeight || 0; var maxScroll = Math.max(0, container.scrollHeight - containerHeight); var target = index * itemHeight - (containerHeight / 2 - itemHeight / 2); if (target < 0) target = 0; if (target > maxScroll) target = maxScroll; container.scrollTop = target; }; _this.onSelectMinute = function (minute) { var base = _this.props.selectedValue || _this.state.highlightTime || _this.props.value || dayjs(); var selectedHour = _this.getSelectedHour(); var h = selectedHour !== null ? parseInt(selectedHour, 10) : dayjs().hour(); var m = parseInt(minute, 10); var current = base.clone().hour(h).minute(m); _this.skipScrollUpdates = 2; _this.setState({ highlightTime: current }); _this.props.onSelect(current); if (_this.props.onClickRightPanelTime) { _this.props.onClickRightPanelTime(); } }; _this.onSelectHour = function (hour) { var base = _this.props.selectedValue || _this.state.highlightTime || dayjs(); var h = parseInt(hour, 10); var selectedMinute = _this.getSelectedMinute(); var m = selectedMinute !== null ? parseInt(selectedMinute, 10) : dayjs().minute(); var current = base.clone().hour(h).minute(m); _this.skipScrollUpdates = 2; _this.setState({ highlightTime: current }); _this.props.onSelect(current); }; _this.getHours = function () { return Array.from({ length: 24 }, function (_, i) { return String(i).padStart(2, '0'); }); }; _this.getMinutes = function () { return Array.from({ length: 60 }, function (_, i) { return String(i).padStart(2, '0'); }); }; _this.getSelectedHour = function () { var highlightTime = _this.state.highlightTime; var selectedValue = _this.props.selectedValue; var v = highlightTime || selectedValue || null; return v ? v.format('HH') : null; }; _this.getSelectedMinute = function () { var highlightTime = _this.state.highlightTime; var selectedValue = _this.props.selectedValue; var v = highlightTime || selectedValue || null; return v ? v.format('mm') : null; }; var format = Array.isArray(_this.props.format) ? _this.props.format[0] : _this.props.format; _this.state = { highlightTime: _this.props.selectedValue || null, localeFormat: format }; _this.hoursRef = React.createRef(); _this.minutesRef = React.createRef(); _this.hours = _this.getHours(); _this.minutes = _this.getMinutes(); _this.skipScrollUpdates = 0; return _this; } CalendarRightPanel.getDerivedStateFromProps = function getDerivedStateFromProps(nextProps, prevState) { if (nextProps.selectedValue) { if (!prevState.highlightTime || !prevState.highlightTime.isSame(nextProps.selectedValue)) { return { highlightTime: nextProps.selectedValue }; } } return null; }; CalendarRightPanel.prototype.componentDidMount = function componentDidMount() { var _this2 = this; var _props = this.props, defaultMinutesTime = _props.defaultMinutesTime, selectedValue = _props.selectedValue; var baseTime = defaultMinutesTime || (selectedValue ? selectedValue.format('HH:mm') : dayjs().format('HH:mm')); var base = baseTime.split(':'); var hIdx = this.hours.findIndex(function (h) { return h === base[0]; }); var mIdx = this.minutes.findIndex(function (m) { return m === base[1]; }); var hourIndex = hIdx > -1 ? hIdx : 0; var minuteIndex = mIdx > -1 ? mIdx : 0; if (typeof window !== 'undefined' && window.requestAnimationFrame) { window.requestAnimationFrame(function () { _this2.centerScroll(_this2.hoursRef.current, hourIndex); _this2.centerScroll(_this2.minutesRef.current, minuteIndex); }); } else { this.centerScroll(this.hoursRef.current, hourIndex); this.centerScroll(this.minutesRef.current, minuteIndex); } }; CalendarRightPanel.prototype.componentDidUpdate = function componentDidUpdate(prevProps, prevState) { var _this3 = this; var prevV = prevState.highlightTime || prevProps.selectedValue || dayjs(); var currV = this.state.highlightTime || this.props.selectedValue || dayjs(); var prevH = prevV ? prevV.format('HH') : '00'; var prevM = prevV ? prevV.format('mm') : '00'; var currH = currV ? currV.format('HH') : '00'; var currM = currV ? currV.format('mm') : '00'; if (this.skipScrollUpdates > 0) { this.skipScrollUpdates -= 1; return; } var hChanged = prevH !== currH; var mChanged = prevM !== currM; if (hChanged || mChanged) { var scrollHours = function scrollHours() { if (hChanged) { var hIdx = _this3.hours.findIndex(function (h) { return h === currH; }); var hourIndex = hIdx > -1 ? hIdx : 0; _this3.centerScroll(_this3.hoursRef.current, hourIndex); } if (mChanged) { var mIdx = _this3.minutes.findIndex(function (m) { return m === currM; }); var minuteIndex = mIdx > -1 ? mIdx : 0; _this3.centerScroll(_this3.minutesRef.current, minuteIndex); } }; if (typeof window !== 'undefined' && window.requestAnimationFrame) { window.requestAnimationFrame(scrollHours); } else { scrollHours(); } } }; CalendarRightPanel.prototype.render = function render() { var _this4 = this; var prefixCls = this.props.prefixCls; var selectedHour = this.getSelectedHour(); var selectedMinute = this.getSelectedMinute(); var currentHour = dayjs().format('HH'); var currentMinute = dayjs().format('mm'); var displayHour = selectedHour || currentHour; var displayMinute = selectedMinute || currentMinute; return React.createElement( 'div', { className: prefixCls + '-right-panel' }, React.createElement( 'div', { className: prefixCls + '-right-panel-header ' + prefixCls + '-header' }, displayHour, ':', displayMinute ), React.createElement( 'div', { className: prefixCls + '-right-panel-body' }, React.createElement( 'div', { className: prefixCls + '-right-panel-col', ref: this.hoursRef }, React.createElement( 'ul', null, this.hours.map(function (h) { var isSelected = selectedHour && h === selectedHour; var isCurrent = !selectedHour && h === currentHour; var className = prefixCls + '-right-panel-item-text ' + (isSelected ? prefixCls + '-right-panel-item-selected' : '') + ' ' + (isCurrent ? prefixCls + '-right-panel-item-current' : ''); return React.createElement( 'li', { key: h, onClick: function onClick() { return _this4.onSelectHour(h); }, className: prefixCls + '-right-panel-item', title: h }, React.createElement( 'span', { className: className }, h ) ); }) ) ), React.createElement( 'div', { className: prefixCls + '-right-panel-col', ref: this.minutesRef }, React.createElement( 'ul', null, this.minutes.map(function (m) { var isSelected = selectedMinute && m === selectedMinute; var isCurrent = !selectedMinute && m === currentMinute; var className = prefixCls + '-right-panel-item-text ' + (isSelected ? prefixCls + '-right-panel-item-selected' : '') + ' ' + (isCurrent ? prefixCls + '-right-panel-item-current' : ''); return React.createElement( 'li', { key: m, onClick: function onClick() { return _this4.onSelectMinute(m); }, className: prefixCls + '-right-panel-item', title: m }, React.createElement( 'span', { className: className }, m ) ); }) ) ) ) ); }; return CalendarRightPanel; }(React.Component); CalendarRightPanel.propTypes = { prefixCls: PropTypes.string, value: PropTypes.object, selectedValue: PropTypes.object, onSelect: PropTypes.func, onClickRightPanelTime: PropTypes.func, locale: PropTypes.object, defaultMinutesTime: PropTypes.string, format: PropTypes.oneOfType([PropTypes.string, PropTypes.arrayOf(PropTypes.string)]) }; export default CalendarRightPanel;