| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303 |
- import _extends from "@babel/runtime/helpers/esm/extends";
- import { use, range } from '../utils';
- import Picker from '../picker';
- import { pickerProps } from '../picker/shared';
- import { times, padZero, isValidDate, getTrueValue, getMonthEndDay } from './utils';
- var _use = use('datetime-picker'),
- sfc = _use[0],
- bem = _use[1];
- var currentYear = new Date().getFullYear();
- export default sfc({
- props: _extends({}, pickerProps, {
- value: null,
- minHour: Number,
- minMinute: Number,
- type: {
- type: String,
- "default": 'datetime'
- },
- showToolbar: {
- type: Boolean,
- "default": true
- },
- format: {
- type: String,
- "default": 'YYYY.MM.DD HH时 mm分'
- },
- formatter: {
- type: Function,
- "default": function _default(type, value) {
- return value;
- }
- },
- minDate: {
- type: Date,
- "default": function _default() {
- return new Date(currentYear - 10, 0, 1);
- },
- validator: isValidDate
- },
- maxDate: {
- type: Date,
- "default": function _default() {
- return new Date(currentYear + 10, 11, 31);
- },
- validator: isValidDate
- },
- maxHour: {
- type: Number,
- "default": 23
- },
- maxMinute: {
- type: Number,
- "default": 59
- }
- }),
- data: function data() {
- return {
- innerValue: this.correctValue(this.value)
- };
- },
- watch: {
- value: function value(val) {
- val = this.correctValue(val);
- var isEqual = this.type === 'time' ? val === this.innerValue : val.valueOf() === this.innerValue.valueOf();
- if (!isEqual) {
- this.innerValue = val;
- if (this.type === 'time') {
- this.updateColumnValue(val);
- }
- }
- },
- innerValue: function innerValue(val) {
- this.$emit('input', val);
- },
- columns: function columns() {
- this.updateColumnValue(this.innerValue);
- }
- },
- computed: {
- ranges: function ranges() {
- if (this.type === 'time') {
- return [{
- type: 'hour',
- range: [this.minHour, this.maxHour]
- }, {
- type: 'minute',
- range: [this.minMinute, this.maxMinute]
- }];
- }
- var _this$getBoundary = this.getBoundary('max', this.innerValue),
- maxYear = _this$getBoundary.maxYear,
- maxDate = _this$getBoundary.maxDate,
- maxMonth = _this$getBoundary.maxMonth,
- maxHour = _this$getBoundary.maxHour,
- maxMinute = _this$getBoundary.maxMinute;
- var _this$getBoundary2 = this.getBoundary('min', this.innerValue),
- minYear = _this$getBoundary2.minYear,
- minDate = _this$getBoundary2.minDate,
- minMonth = _this$getBoundary2.minMonth,
- minHour = _this$getBoundary2.minHour,
- minMinute = _this$getBoundary2.minMinute;
- var result = [{
- type: 'year',
- range: [minYear, maxYear]
- }, {
- type: 'month',
- range: [minMonth, maxMonth]
- }, {
- type: 'day',
- range: [minDate, maxDate]
- }, {
- type: 'hour',
- range: [minHour, maxHour]
- }, {
- type: 'minute',
- range: [minMinute, maxMinute]
- }];
- if (this.type === 'date') result.splice(3, 2);
- if (this.type === 'year-month') result.splice(2, 3);
- return result;
- },
- columns: function columns() {
- var _this = this;
- var results = this.ranges.map(function (_ref) {
- var type = _ref.type,
- rangeArr = _ref.range;
- var values = times(rangeArr[1] - rangeArr[0] + 1, function (index) {
- var value = rangeArr[0] + index;
- value = value < 10 ? "0" + value : "" + value;
- return _this.formatter(type, value);
- });
- return {
- values: values
- };
- });
- return results;
- }
- },
- mounted: function mounted() {
- this.updateColumnValue(this.innerValue);
- },
- methods: {
- correctValue: function correctValue(value) {
- // validate value
- var isDateType = this.type !== 'time';
- if (isDateType && !isValidDate(value)) {
- value = this.minDate;
- } else if (!value) {
- var minHour = this.minHour;
- value = (minHour > 10 ? minHour : '0' + minHour) + ":00";
- } // time type
- if (!isDateType) {
- var _value$split = value.split(':'),
- hour = _value$split[0],
- minute = _value$split[1];
- hour = padZero(range(hour, this.minHour, this.maxHour));
- minute = padZero(range(minute, this.minMinute, this.maxMinute));
- return hour + ":" + minute;
- } // date type
- value = Math.max(value, this.minDate.getTime());
- value = Math.min(value, this.maxDate.getTime());
- return new Date(value);
- },
- getBoundary: function getBoundary(type, value) {
- var _ref2;
- var boundary = this[type + "Date"];
- var year = boundary.getFullYear();
- var month = 1;
- var date = 1;
- var hour = 0;
- var minute = 0;
- if (type === 'max') {
- month = 12;
- date = getMonthEndDay(value.getFullYear(), value.getMonth() + 1);
- hour = 23;
- minute = 59;
- }
- if (value.getFullYear() === year) {
- month = boundary.getMonth() + 1;
- if (value.getMonth() + 1 === month) {
- date = boundary.getDate();
- if (value.getDate() === date) {
- hour = boundary.getHours();
- if (value.getHours() === hour) {
- minute = boundary.getMinutes();
- }
- }
- }
- }
- return _ref2 = {}, _ref2[type + "Year"] = year, _ref2[type + "Month"] = month, _ref2[type + "Date"] = date, _ref2[type + "Hour"] = hour, _ref2[type + "Minute"] = minute, _ref2;
- },
- onConfirm: function onConfirm() {
- this.$emit('confirm', this.innerValue);
- },
- onChange: function onChange(picker) {
- var _this2 = this;
- var value;
- if (this.type === 'time') {
- var indexes = picker.getIndexes();
- value = indexes[0] + this.minHour + ":" + (indexes[1] + this.minMinute);
- } else {
- var values = picker.getValues();
- var year = getTrueValue(values[0]);
- var month = getTrueValue(values[1]);
- var maxDate = getMonthEndDay(year, month);
- var date = getTrueValue(values[2]);
- if (this.type === 'year-month') {
- date = 1;
- }
- date = date > maxDate ? maxDate : date;
- var hour = 0;
- var minute = 0;
- if (this.type === 'datetime') {
- hour = getTrueValue(values[3]);
- minute = getTrueValue(values[4]);
- }
- value = new Date(year, month - 1, date, hour, minute);
- }
- this.innerValue = this.correctValue(value);
- this.$nextTick(function () {
- _this2.$nextTick(function () {
- _this2.$emit('change', picker);
- });
- });
- },
- updateColumnValue: function updateColumnValue(value) {
- var _this3 = this;
- var values = [];
- var formatter = this.formatter;
- if (this.type === 'time') {
- var pair = value.split(':');
- values = [formatter('hour', pair[0]), formatter('minute', pair[1])];
- } else {
- values = [formatter('year', "" + value.getFullYear()), formatter('month', padZero(value.getMonth() + 1)), formatter('day', padZero(value.getDate()))];
- if (this.type === 'datetime') {
- values.push(formatter('hour', padZero(value.getHours())), formatter('minute', padZero(value.getMinutes())));
- }
- if (this.type === 'year-month') {
- values = values.slice(0, 2);
- }
- }
- this.$nextTick(function () {
- _this3.$refs.picker.setValues(values);
- });
- }
- },
- render: function render(h) {
- var _this4 = this;
- var props = {};
- Object.keys(pickerProps).forEach(function (key) {
- props[key] = _this4[key];
- });
- return h(Picker, {
- "class": bem(),
- "ref": "picker",
- "attrs": {
- "columns": this.columns
- },
- "on": {
- "change": this.onChange,
- "confirm": this.onConfirm,
- "cancel": function cancel() {
- _this4.$emit('cancel');
- }
- },
- "props": _extends({}, props)
- });
- }
- });
|