choerodon-ui
Version:
An enterprise-class UI design language and React-based implementation
540 lines (482 loc) • 18.3 kB
JavaScript
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 throttle from 'lodash/throttle';
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 { preventDefault, 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() {
var _this;
_classCallCheck(this, TimesView);
_this = _super.apply(this, arguments);
_this.throttleHandleWheel = throttle(_this.handleWheel, 80);
return _this;
}
_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.throttleHandleWheel, {
passive: false
});
}
}
}, {
key: "componentWillUnmount",
value: function componentWillUnmount() {
if (this.panel) {
this.panel.removeEventListener('wheel', this.throttleHandleWheel);
}
}
}, {
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.changeCursorDate(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.changeCursorDate(date, ViewMode.time);
return;
}
}
this.changeCursorDate(date.subtract(unitStep, unit), ViewMode.time);
}
}
}, {
key: "handleKeyDownDown",
value: function handleKeyDownDown(e) {
stopEvent(e);
var unit = this.getCurrentUnit();
if (unit === TimeUnit.a) {
this.changeCursorDate(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.changeCursorDate(date, ViewMode.time);
return;
}
}
this.changeCursorDate(date.add(unitStep, unit), ViewMode.time);
}
}
}, {
key: "handleKeyDownPageUp",
value: function handleKeyDownPageUp(e) {
stopEvent(e);
var unit = this.getCurrentUnit();
if (unit === TimeUnit.a) {
this.changeCursorDate(this.getCloneDate().set(TimeUnit.hour, 0));
} else {
this.changeCursorDate(this.getCloneDate().set(unit, 0), ViewMode.time);
}
}
}, {
key: "handleKeyDownPageDown",
value: function handleKeyDownPageDown(e) {
stopEvent(e);
var unit = this.getCurrentUnit();
if (unit === TimeUnit.a) {
this.changeCursorDate(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.changeCursorDate(this.getCloneDate().set(unit, size - unitStep), ViewMode.time);
}
}
}, {
key: "handleTimeCellClick",
value: function handleTimeCellClick(date, unit) {
this.changeUnit(unit);
this.changeSelectedDate(date);
}
}, {
key: "handleWheel",
value: function handleWheel(e) {
e.preventDefault();
if (e.deltaY > 1) {
this.handleKeyDownDown(e);
} else if (e.deltaY < -1) {
this.handleKeyDownUp(e);
}
}
}, {
key: "handleMouseEnterPanel",
value: function handleMouseEnterPanel() {
// 控件滚动时阻止页面发生滚动
window.addEventListener('wheel', preventDefault, {
passive: false
});
}
}, {
key: "handleMouseLeavePanel",
value: function handleMouseLeavePanel() {
// 鼠标离开控件清除监听
window.removeEventListener('wheel', preventDefault);
}
}, {
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() {
if (this.props.datetimeSide) {
return;
}
var prefixCls = this.prefixCls,
_this$props2 = this.props,
disabledNow = _this$props2.disabledNow,
okButton = _this$props2.okButton;
var footerProps = {
className: classNames("".concat(prefixCls, "-footer-now-btn"), _defineProperty({}, "".concat(prefixCls, "-now-disabled"), disabledNow)),
onClick: !disabledNow ? this.choose.bind(this, moment(), false) : noop,
hidden: this.props.datetimeSide
};
return /*#__PURE__*/React.createElement("div", {
className: "".concat(prefixCls, "-footer")
}, /*#__PURE__*/React.createElement("a", _extends({}, footerProps), $l('DatePicker', 'now')), okButton && /*#__PURE__*/React.createElement("a", {
className: "".concat(prefixCls, "-footer-view-select"),
onClick: this.choose.bind(this, this.props.date, false)
}, $l('DatePicker', 'ok')));
}
}, {
key: "renderPanel",
value: function renderPanel() {
var className = this.getPanelClass();
return /*#__PURE__*/React.createElement("div", {
ref: this.savePanel,
className: className,
onMouseEnter: this.handleMouseEnterPanel,
onMouseLeave: this.handleMouseLeavePanel
}, /*#__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$props3 = this.props,
date = _this$props3.date,
_this$props3$renderer = _this$props3.renderer,
renderer = _this$props3$renderer === void 0 ? this.renderCell : _this$props3$renderer,
_this$props3$isValidD = _this$props3.isValidDate,
isValidDate = _this$props3$isValidD === void 0 ? alwaysValidDate : _this$props3$isValidD,
step = _this$props3.step,
onDateMouseLeave = _this$props3.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, ViewMode.time);
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, expand) {
var mode = this.props.mode;
_get(_getPrototypeOf(TimesView.prototype), "choose", this).call(this, date, expand);
if (mode !== ViewMode.time) {
this.changeCursorDate(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