UNPKG

vant-fork

Version:

Lightweight Mobile UI Components built on Vue

357 lines (315 loc) 9.88 kB
import Picker from '../picker'; import create from '../utils/create'; import { range } from '../utils'; var currentYear = new Date().getFullYear(); var isValidDate = function isValidDate(date) { return Object.prototype.toString.call(date) === '[object Date]' && !isNaN(date.getTime()); }; export default create({ render: function render() { var _vm = this; var _h = _vm.$createElement; var _c = _vm._self._c || _h; return _c('picker', { ref: "picker", attrs: { "title": _vm.title, "columns": _vm.columns, "item-height": _vm.itemHeight, "show-toolbar": _vm.showToolbar, "visible-item-count": _vm.visibleItemCount, "confirm-button-text": _vm.confirmButtonText, "cancel-button-text": _vm.cancelButtonText }, on: { "change": _vm.onChange, "confirm": _vm.onConfirm, "cancel": function cancel($event) { _vm.$emit('cancel'); } } }); }, name: 'datetime-picker', components: { Picker: Picker }, props: { value: {}, title: String, itemHeight: Number, visibleItemCount: Number, confirmButtonText: String, cancelButtonText: String, 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 }, minHour: { type: Number, default: 0 }, maxHour: { type: Number, default: 23 }, minMinute: { type: Number, default: 0 }, 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; }, 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, range = _ref.range; var values = _this.times(range[1] - range[0] + 1, function (index) { var value = range[0] + index; value = value < 10 ? "0" + value : "" + value; return _this.formatter(type, value); }); return { values: values }; }); return results; } }, methods: { pad: function pad(val) { return ("00" + val).slice(-2); }, 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 = this.pad(range(hour, this.minHour, this.maxHour)); minute = this.pad(range(minute, this.minMinute, this.maxMinute)); return hour + ":" + minute; } // date type var _this$getBoundary3 = this.getBoundary('max', value), maxYear = _this$getBoundary3.maxYear, maxDate = _this$getBoundary3.maxDate, maxMonth = _this$getBoundary3.maxMonth, maxHour = _this$getBoundary3.maxHour, maxMinute = _this$getBoundary3.maxMinute; var _this$getBoundary4 = this.getBoundary('min', value), minYear = _this$getBoundary4.minYear, minDate = _this$getBoundary4.minDate, minMonth = _this$getBoundary4.minMonth, minHour = _this$getBoundary4.minHour, minMinute = _this$getBoundary4.minMinute; var minDay = new Date(minYear, minMonth - 1, minDate, minHour, minMinute); var maxDay = new Date(maxYear, maxMonth - 1, maxDate, maxHour, maxMinute); value = Math.max(value, minDay); value = Math.min(value, maxDay); return new Date(value); }, times: function times(n, iteratee) { var index = -1; var result = Array(n); while (++index < n) { result[index] = iteratee(index); } return result; }, 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 = this.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; }, getTrueValue: function getTrueValue(formattedValue) { if (!formattedValue) return; while (isNaN(parseInt(formattedValue, 10))) { formattedValue = formattedValue.slice(1); } return parseInt(formattedValue, 10); }, getMonthEndDay: function getMonthEndDay(year, month) { return 32 - new Date(year, month - 1, 32).getDate(); }, onConfirm: function onConfirm() { this.$emit('confirm', this.innerValue); }, onChange: function onChange(picker) { var _this2 = this; var values = picker.getValues(); var value; if (this.type === 'time') { value = values.join(':'); } else { var year = this.getTrueValue(values[0]); var month = this.getTrueValue(values[1]); var maxDate = this.getMonthEndDay(year, month); var date = this.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 = this.getTrueValue(values[3]); minute = this.getTrueValue(values[4]); } value = new Date(year, month - 1, date, hour, minute); } value = this.correctValue(value); this.innerValue = value; this.$nextTick(function () { _this2.$nextTick(function () { _this2.$emit('change', picker); }); }); }, updateColumnValue: function updateColumnValue(value) { var _this3 = this; var values = []; var formatter = this.formatter, pad = this.pad; if (this.type === 'time') { var currentValue = value.split(':'); values = [formatter('hour', currentValue[0]), formatter('minute', currentValue[1])]; } else { values = [formatter('year', "" + value.getFullYear()), formatter('month', pad(value.getMonth() + 1)), formatter('day', pad(value.getDate()))]; if (this.type === 'datetime') { values.push(formatter('hour', pad(value.getHours())), formatter('minute', pad(value.getMinutes()))); } if (this.type === 'year-month') { values = values.slice(0, 2); } } this.$nextTick(function () { _this3.$refs.picker.setValues(values); }); } }, mounted: function mounted() { this.updateColumnValue(this.innerValue); } });