lulouis-vant
Version:
Lightweight Mobile UI Components built on Vue
318 lines (275 loc) • 8.76 kB
JavaScript
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.default = void 0;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _utils = require("../utils");
var _picker = _interopRequireDefault(require("../picker"));
var _picker2 = _interopRequireDefault(require("../mixins/picker"));
var _utils2 = require("./utils");
var _use = (0, _utils.use)('datetime-picker'),
sfc = _use[0],
bem = _use[1];
var currentYear = new Date().getFullYear();
var _default2 = sfc({
mixins: [_picker2.default],
props: {
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: _utils2.isValidDate
},
maxDate: {
type: Date,
default: function _default() {
return new Date(currentYear + 10, 11, 31);
},
validator: _utils2.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 = (0, _utils2.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 && !(0, _utils2.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 = (0, _utils2.padZero)((0, _utils.range)(hour, this.minHour, this.maxHour));
minute = (0, _utils2.padZero)((0, _utils.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 = (0, _utils2.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 = (0, _utils2.getTrueValue)(values[0]);
var month = (0, _utils2.getTrueValue)(values[1]);
var maxDate = (0, _utils2.getMonthEndDay)(year, month);
var date = (0, _utils2.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 = (0, _utils2.getTrueValue)(values[3]);
minute = (0, _utils2.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', (0, _utils2.padZero)(value.getMonth() + 1)), formatter('day', (0, _utils2.padZero)(value.getDate()))];
if (this.type === 'datetime') {
values.push(formatter('hour', (0, _utils2.padZero)(value.getHours())), formatter('minute', (0, _utils2.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(_picker2.default.props).forEach(function (key) {
props[key] = _this4[key];
});
return h(_picker.default, {
"class": bem(),
"ref": "picker",
"attrs": {
"columns": this.columns
},
"on": {
"change": this.onChange,
"confirm": this.onConfirm,
"cancel": function cancel() {
_this4.$emit('cancel');
}
},
"props": (0, _extends2.default)({}, props)
});
}
});
exports.default = _default2;