UNPKG

choerodon-ui

Version:

An enterprise-class UI design language and React-based implementation

513 lines (457 loc) 17.4 kB
import _extends from "@babel/runtime/helpers/extends"; import _objectSpread from "@babel/runtime/helpers/objectSpread2"; import _classCallCheck from "@babel/runtime/helpers/classCallCheck"; import _createClass from "@babel/runtime/helpers/createClass"; import _get from "@babel/runtime/helpers/get"; import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf"; import _inherits from "@babel/runtime/helpers/inherits"; import _createSuper from "@babel/runtime/helpers/createSuper"; import _defineProperty from "@babel/runtime/helpers/defineProperty"; var _stepMapping; import { __decorate } from "tslib"; import React from 'react'; import moment from 'moment'; import classNames from 'classnames'; import { action, computed, observable } from 'mobx'; import { observer } from 'mobx-react'; import noop from 'lodash/noop'; import autobind from '../_util/autobind'; import { TimeUnit, ViewMode } from './enum'; import DaysView, { alwaysValidDate } from './DaysView'; import { FieldType } from '../data-set/enum'; import { $l } from '../locale-context'; import { stopEvent } from '../_util/EventManager'; var stepMapping = (_stepMapping = {}, _defineProperty(_stepMapping, TimeUnit.hour, 'hour'), _defineProperty(_stepMapping, TimeUnit.minute, 'minute'), _defineProperty(_stepMapping, TimeUnit.second, 'second'), _stepMapping); var TimesView = /*#__PURE__*/function (_DaysView) { _inherits(TimesView, _DaysView); var _super = _createSuper(TimesView); function TimesView() { _classCallCheck(this, TimesView); return _super.apply(this, arguments); } _createClass(TimesView, [{ key: "getViewClassName", value: function getViewClassName() { var prefixCls = this.prefixCls; return "".concat(prefixCls, "-time"); } }, { key: "showHour", get: function get() { var format = this.observableProps.format; return format.indexOf('H') > -1 || format.indexOf('h') > -1 || format.indexOf('k') > -1; } }, { key: "showMinute", get: function get() { var format = this.observableProps.format; return format.indexOf('m') > -1; } }, { key: "showSecond", get: function get() { var format = this.observableProps.format; return format.indexOf('s') > -1; } }, { key: "use12Hours", get: function get() { var format = this.observableProps.format; return format.indexOf('h') > -1 || format.indexOf('a') > -1 || format.indexOf('A') > -1; } }, { key: "timeUnitQueue", get: function get() { var showHour = this.showHour, showMinute = this.showMinute, showSecond = this.showSecond, use12Hours = this.use12Hours; var queue = []; if (showHour) { queue.push(TimeUnit.hour); } if (showMinute) { queue.push(TimeUnit.minute); } if (showSecond) { queue.push(TimeUnit.second); } if (use12Hours) { queue.push(TimeUnit.a); } return queue; } }, { key: "barStyle", get: function get() { return { width: "".concat(100 / this.timeUnitQueue.length, "%") }; } }, { key: "activeStyle", get: function get() { var timeUnitQueue = this.timeUnitQueue; var width = 100 / timeUnitQueue.length; return { width: "".concat(width, "%"), left: "".concat(timeUnitQueue.indexOf(this.getCurrentUnit()) * width, "%") }; } }, { key: "savePanel", value: function savePanel(node) { this.panel = node; } }, { key: "getObservableProps", value: function getObservableProps(props, context) { return _objectSpread(_objectSpread({}, _get(_getPrototypeOf(TimesView.prototype), "getObservableProps", this).call(this, props, context)), {}, { format: props.format }); } }, { key: "componentDidMount", value: function componentDidMount() { _get(_getPrototypeOf(TimesView.prototype), "componentDidMount", this).call(this); if (this.panel) { // 兼容Firefox wheel为通用事件 this.panel.addEventListener('wheel', this.handleWheel, { passive: false }); } } }, { key: "componentWillUnmount", value: function componentWillUnmount() { if (this.panel) { this.panel.removeEventListener('wheel', this.handleWheel); } } }, { key: "handleDateTimeSelect", value: function handleDateTimeSelect() { this.changeViewMode(ViewMode.dateTime); } }, { key: "handleKeyDownHome", value: function handleKeyDownHome(e) { this.handleKeyDownPageUp(e); } }, { key: "handleKeyDownEnd", value: function handleKeyDownEnd(e) { this.handleKeyDownPageDown(e); } }, { key: "handleKeyDownLeft", value: function handleKeyDownLeft(e) { stopEvent(e); if (e.altKey) { if (this.props.mode !== ViewMode.time) { this.changeViewMode(ViewMode.dateTime); } } else { this.changeUnit(this.getPrevUnit()); } } }, { key: "handleKeyDownRight", value: function handleKeyDownRight(e) { stopEvent(e); if (!e.altKey) { this.changeUnit(this.getNextUnit()); } } }, { key: "handleKeyDownUp", value: function handleKeyDownUp(e) { stopEvent(e); var unit = this.getCurrentUnit(); if (unit === TimeUnit.a) { this.changeSelectedDate(this.getCloneDate().subtract(12, TimeUnit.hour)); } else { var step = this.props.step; var unitStep = step[stepMapping[unit]] || 1; var date = this.getCloneDate(); var parentUnit = unit === TimeUnit.second ? TimeUnit.minute : unit === TimeUnit.minute ? TimeUnit.hour : null; if (parentUnit) { var parentStep = step[stepMapping[parentUnit]]; if (parentStep) { var preValue = date.get(parentUnit); date.subtract(unitStep, unit); if (preValue !== date.get(parentUnit)) { date.subtract(parentStep - 1, parentUnit); } this.changeSelectedDate(date); return; } } this.changeSelectedDate(date.subtract(unitStep, unit)); } } }, { key: "handleKeyDownDown", value: function handleKeyDownDown(e) { stopEvent(e); var unit = this.getCurrentUnit(); if (unit === TimeUnit.a) { this.changeSelectedDate(this.getCloneDate().add(12, TimeUnit.hour)); } else { var step = this.props.step; var unitStep = step[stepMapping[unit]] || 1; var date = this.getCloneDate(); var parentUnit = unit === TimeUnit.second ? TimeUnit.minute : unit === TimeUnit.minute ? TimeUnit.hour : null; if (parentUnit) { var parentStep = step[stepMapping[parentUnit]]; if (parentStep) { var preValue = date.get(parentUnit); date.add(unitStep, unit); if (preValue !== date.get(parentUnit)) { date.add(parentStep - 1, parentUnit); } this.changeSelectedDate(date); return; } } this.changeSelectedDate(date.add(unitStep, unit)); } } }, { key: "handleKeyDownPageUp", value: function handleKeyDownPageUp(e) { stopEvent(e); var unit = this.getCurrentUnit(); if (unit === TimeUnit.a) { this.changeSelectedDate(this.getCloneDate().set(TimeUnit.hour, 0)); } else { this.changeSelectedDate(this.getCloneDate().set(unit, 0)); } } }, { key: "handleKeyDownPageDown", value: function handleKeyDownPageDown(e) { stopEvent(e); var unit = this.getCurrentUnit(); if (unit === TimeUnit.a) { this.changeSelectedDate(this.getCloneDate().set(TimeUnit.hour, 12)); } else { var step = this.props.step; var unitStep = step[stepMapping[unit]] || 1; var size = unit === TimeUnit.hour ? this.use12Hours ? 12 : 24 : 60; this.changeSelectedDate(this.getCloneDate().set(unit, size - unitStep)); } } }, { key: "handleTimeCellClick", value: function handleTimeCellClick(date, unit) { this.changeUnit(unit); this.changeSelectedDate(date); } }, { key: "handleWheel", value: function handleWheel(e) { e.preventDefault(); if (e.deltaY > 0) { this.handleKeyDownDown(e); } else if (e.deltaY < 0) { this.handleKeyDownUp(e); } } }, { key: "renderHeader", value: function renderHeader() { var prefixCls = this.prefixCls, _this$props = this.props, date = _this$props.date, mode = _this$props.mode, format = this.observableProps.format; if (mode === ViewMode.time) { return /*#__PURE__*/React.createElement("div", { className: "".concat(prefixCls, "-header") }, /*#__PURE__*/React.createElement("span", { className: "".concat(prefixCls, "-view-select") }, date.format(format))); } return /*#__PURE__*/React.createElement("div", { className: "".concat(prefixCls, "-header") }, /*#__PURE__*/React.createElement("a", { className: "".concat(prefixCls, "-view-select"), onClick: this.handleMonthSelect }, date.localeData().monthsShort(date)), /*#__PURE__*/React.createElement("a", { className: "".concat(prefixCls, "-view-select"), onClick: this.handleDateTimeSelect }, date.date()), /*#__PURE__*/React.createElement("a", { className: "".concat(prefixCls, "-view-select"), onClick: this.handleYearSelect }, date.year())); } }, { key: "renderFooter", value: function renderFooter() { var prefixCls = this.prefixCls, disabledNow = this.props.disabledNow; var footerProps = { className: classNames("".concat(prefixCls, "-footer-now-btn"), _defineProperty({}, "".concat(prefixCls, "-now-disabled"), disabledNow)), onClick: !disabledNow ? this.choose.bind(this, moment()) : noop, hidden: this.props.datetimeSide }; return /*#__PURE__*/React.createElement("div", { className: "".concat(prefixCls, "-footer") }, /*#__PURE__*/React.createElement("a", _extends({}, footerProps), $l('DatePicker', 'now')), /*#__PURE__*/React.createElement("a", { className: "".concat(prefixCls, "-footer-view-select"), onClick: this.choose.bind(this, this.props.date) }, $l('DatePicker', 'ok'))); } }, { key: "renderPanel", value: function renderPanel() { var className = this.getPanelClass(); return /*#__PURE__*/React.createElement("div", { ref: this.savePanel, className: className }, /*#__PURE__*/React.createElement("div", { className: "".concat(className, "-inner") }, this.renderPanelBody())); } }, { key: "renderPanelBody", value: function renderPanelBody() { var showHour = this.showHour, showMinute = this.showMinute, showSecond = this.showSecond, use12Hours = this.use12Hours, activeStyle = this.activeStyle; return [showHour && this.getTimeBar(TimeUnit.hour), showMinute && this.getTimeBar(TimeUnit.minute), showSecond && this.getTimeBar(TimeUnit.second), use12Hours && this.getTimeBar(TimeUnit.a), /*#__PURE__*/React.createElement("div", { key: "active", style: activeStyle, className: "".concat(this.prefixCls, "-time-focus-active") })]; } }, { key: "renderCell", value: function renderCell(props) { return /*#__PURE__*/React.createElement("li", _extends({}, props)); } }, { key: "getTimeBar", value: function getTimeBar(unit) { var prefixCls = this.prefixCls, use12Hours = this.use12Hours, _this$props2 = this.props, date = _this$props2.date, _this$props2$renderer = _this$props2.renderer, renderer = _this$props2$renderer === void 0 ? this.renderCell : _this$props2$renderer, _this$props2$isValidD = _this$props2.isValidDate, isValidDate = _this$props2$isValidD === void 0 ? alwaysValidDate : _this$props2$isValidD, step = _this$props2.step, onDateMouseLeave = _this$props2.onDateMouseLeave, format = this.observableProps.format; var isUpperCase = format.indexOf('A') > -1; var items = []; var selected = date.clone(); var finalUnit = unit === TimeUnit.a ? TimeUnit.hour : unit; var selectedValue = selected.get(finalUnit); var size = unit === TimeUnit.a ? 13 : unit === TimeUnit.hour ? use12Hours ? 12 : 24 : 60; var begin = unit === TimeUnit.a ? selectedValue % 12 : unit === TimeUnit.hour && use12Hours && selectedValue > 11 ? 12 : 0; var pre = date.clone().set(finalUnit, begin); var last = pre.clone().add(size, finalUnit); while (pre.isBefore(last)) { var _classNames2; var current = pre.clone(); var isDisabled = !isValidDate(current, selected); var text = unit === TimeUnit.a ? current.format(isUpperCase ? 'A' : 'a') : String(pre.get(unit) - (use12Hours && finalUnit === TimeUnit.hour && pre.get(unit) > 11 ? 12 : 0) || (use12Hours && finalUnit === TimeUnit.hour ? 12 : 0)); var className = classNames("".concat(prefixCls, "-cell"), (_classNames2 = {}, _defineProperty(_classNames2, "".concat(prefixCls, "-selected"), unit === TimeUnit.a ? current.get(TimeUnit.hour) === selectedValue : current.isSame(selected, finalUnit)), _defineProperty(_classNames2, "".concat(prefixCls, "-disabled"), isDisabled), _classNames2)); var props = { key: text, className: className, children: /*#__PURE__*/React.createElement("div", { className: "".concat(prefixCls, "-cell-inner") }, text) }; if (!isDisabled) { props.onClick = this.handleTimeCellClick.bind(this, current, unit); props.onMouseEnter = this.handleDateMouseEnter.bind(this, current); props.onMouseLeave = onDateMouseLeave; } items.push(renderer(props, text, current, selected)); pre.add(unit === TimeUnit.a ? 12 : step[stepMapping[unit]] || 1, finalUnit); } var top = unit === TimeUnit.a ? -Math.floor(selectedValue / 12) : (unit === TimeUnit.hour && use12Hours ? -selectedValue % 12 : -selectedValue) / (step[stepMapping[unit]] || 1); return /*#__PURE__*/React.createElement("div", { key: unit, className: "".concat(prefixCls, "-time-list"), onMouseEnter: this.changeUnit.bind(this, unit), style: this.barStyle }, /*#__PURE__*/React.createElement("ul", { style: { top: "".concat((top + 3.5) * 100, "%") } }, items), /*#__PURE__*/React.createElement("div", { className: "".concat(prefixCls, "-time-focus") })); } }, { key: "getPanelClass", value: function getPanelClass() { return "".concat(this.prefixCls, "-time-panel"); } }, { key: "getCurrentUnit", value: function getCurrentUnit() { var currentUnit = this.currentUnit; return currentUnit || this.timeUnitQueue[0]; } }, { key: "getPrevUnit", value: function getPrevUnit() { var timeUnitQueue = this.timeUnitQueue; return timeUnitQueue[timeUnitQueue.indexOf(this.getCurrentUnit()) - 1]; } }, { key: "getNextUnit", value: function getNextUnit() { var timeUnitQueue = this.timeUnitQueue; return timeUnitQueue[timeUnitQueue.indexOf(this.getCurrentUnit()) + 1]; } }, { key: "changeUnit", value: function changeUnit(unit) { if (unit !== undefined && unit !== this.currentUnit) { this.currentUnit = unit; } } }, { key: "choose", value: function choose(date) { var mode = this.props.mode; _get(_getPrototypeOf(TimesView.prototype), "choose", this).call(this, date); if (mode !== ViewMode.time) { this.changeSelectedDate(date); this.changeViewMode(mode); } } }]); return TimesView; }(DaysView); TimesView.displayName = 'TimesView'; TimesView.defaultProps = _objectSpread({ datetimeSide: false }, DaysView.defaultProps); TimesView.type = FieldType.time; __decorate([observable], TimesView.prototype, "currentUnit", void 0); __decorate([computed], TimesView.prototype, "showHour", null); __decorate([computed], TimesView.prototype, "showMinute", null); __decorate([computed], TimesView.prototype, "showSecond", null); __decorate([computed], TimesView.prototype, "use12Hours", null); __decorate([computed], TimesView.prototype, "timeUnitQueue", null); __decorate([computed], TimesView.prototype, "barStyle", null); __decorate([computed], TimesView.prototype, "activeStyle", null); __decorate([autobind], TimesView.prototype, "savePanel", null); __decorate([autobind], TimesView.prototype, "handleDateTimeSelect", null); __decorate([autobind], TimesView.prototype, "handleWheel", null); __decorate([autobind], TimesView.prototype, "renderCell", null); __decorate([action], TimesView.prototype, "changeUnit", null); TimesView = __decorate([observer], TimesView); export default TimesView; //# sourceMappingURL=TimesView.js.map