import { VantComponent } from '../common/component';
import { isDef } from '../common/utils';
import { pickerProps } from '../picker/shared';
const currentYear = new Date().getFullYear();
function isValidDate(date) {
return isDef(date) && !isNaN(new Date(date).getTime());
}
function range(num, min, max) {
return Math.min(Math.max(num, min), max);
}
function padZero(val) {
return `00${val}`.slice(-2);
}
function times(n, iteratee) {
let index = -1;
const result = Array(n < 0 ? 0 : n);
while (++index < n) {
result[index] = iteratee(index);
}
return result;
}
function getTrueValue(formattedValue) {
if (!formattedValue)
return;
while (isNaN(parseInt(formattedValue, 10))) {
formattedValue = formattedValue.slice(1);
}
return parseInt(formattedValue, 10);
}
function getMonthEndDay(year, month) {
return 32 - new Date(year, month - 1, 32).getDate();
}
const defaultFormatter = (_, value) => value;
VantComponent({
classes: ['active-class', 'toolbar-class', 'column-class'],
props: Object.assign({}, pickerProps, { formatter: {
type: Function,
value: defaultFormatter
}, value: null, type: {
type: String,
value: 'datetime'
}, showToolbar: {
type: Boolean,
value: true
}, minDate: {
type: Number,
value: new Date(currentYear - 10, 0, 1).getTime()
}, maxDate: {
type: Number,
value: new Date(currentYear + 10, 11, 31).getTime()
}, minHour: {
type: Number,
value: 0
}, maxHour: {
type: Number,
value: 23
}, minMinute: {
type: Number,
value: 0
}, maxMinute: {
type: Number,
value: 59
} }),
data: {
innerValue: Date.now(),
columns: []
},
watch: {
value: 'updateValue',
type: 'updateValue',
minDate: 'updateValue',
maxDate: 'updateValue',
minHour: 'updateValue',
maxHour: 'updateValue',
minMinute: 'updateValue',
maxMinute: 'updateValue'
},
methods: {
updateValue() {
const { data } = this;
const val = this.correctValue(this.data.value);
const isEqual = val === data.innerValue;
if (!isEqual) {
this.updateColumnValue(val).then(() => {
this.$emit('input', val);
});
}
else {
this.updateColumns();
}
},
getPicker() {
if (this.picker == null) {
this.picker = this.selectComponent('.van-datetime-picker');
const { picker } = this;
const { setColumnValues } = picker;
picker.setColumnValues = (...args) => setColumnValues.apply(picker, [...args, false]);
}
return this.picker;
},
updateColumns() {
const { formatter = defaultFormatter } = this.data;
const results = this.getRanges().map(({ type, range }) => {
const values = times(range[1] - range[0] + 1, index => {
let value = range[0] + index;
value = type === 'year' ? `${value}` : padZero(value);
return formatter(type, value);
});
return { values };
});
return this.set({ columns: results });
},
getRanges() {
const { data } = this;
if (data.type === 'time') {
return [
{
type: 'hour',
range: [data.minHour, data.maxHour]
},
{
type: 'minute',
range: [data.minMinute, data.maxMinute]
}
];
}
const { maxYear, maxDate, maxMonth, maxHour, maxMinute } = this.getBoundary('max', data.innerValue);
const { minYear, minDate, minMonth, minHour, minMinute } = this.getBoundary('min', data.innerValue);
const 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 (data.type === 'date')
result.splice(3, 2);
if (data.type === 'year-month')
result.splice(2, 3);
return result;
},
correctValue(value) {
const { data } = this;
// validate value
const isDateType = data.type !== 'time';
if (isDateType && !isValidDate(value)) {
value = data.minDate;
}
else if (!isDateType && !value) {
const { minHour } = data;
value = `${padZero(minHour)}:00`;
}
// time type
if (!isDateType) {
let [hour, minute] = value.split(':');
hour = padZero(range(hour, data.minHour, data.maxHour));
minute = padZero(range(minute, data.minMinute, data.maxMinute));
return `${hour}:${minute}`;
}
// date type
value = Math.max(value, data.minDate);
value = Math.min(value, data.maxDate);
return value;
},
getBoundary(type, innerValue) {
const value = new Date(innerValue);
const boundary = new Date(this.data[`${type}Date`]);
const year = boundary.getFullYear();
let month = 1;
let date = 1;
let hour = 0;
let 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 {
[`${type}Year`]: year,
[`${type}Month`]: month,
[`${type}Date`]: date,
[`${type}Hour`]: hour,
[`${type}Minute`]: minute
};
},
onCancel() {
this.$emit('cancel');
},
onConfirm() {
this.$emit('confirm', this.data.innerValue);
},
onChange() {
const { data } = this;
let value;
const picker = this.getPicker();
if (data.type === 'time') {
const indexes = picker.getIndexes();
value = `${indexes[0] + data.minHour}:${indexes[1] + data.minMinute}`;
}
else {
const values = picker.getValues();
const year = getTrueValue(values[0]);
const month = getTrueValue(values[1]);
const maxDate = getMonthEndDay(year, month);
let date = getTrueValue(values[2])
龙年行大运
- 粉丝: 1386
- 资源: 3960
最新资源
- 张伯伦打球视频资源mp4.zip
- TCP socket 协议异步文件传输,包括 sever 端与 client 端两个部分的代码
- RS422与ARINC429通讯转换模块:全双工接口支持,独立设置速率,可定制硬件设计,支持FPGA代码移植,RS422-ARINC429通讯转模块 RS422支持全双工通讯接口,通讯速率可设置,A
- 汽车BCM程序源代码解析:涵盖内外灯光、雨刮、遥控等系统,适合汽车电路研究爱好者学习参考,汽车BCM程序源代码,国产车BCM程序源代码,喜好汽车电路控制系统研究的值得入手 外部灯光:前照灯、小
- 基于多时间尺度滚动优化的多能源微网双层调度策略:最小化成本与避免变压器过载的优化方案 ,MATLAB代码:基于多时间尺度滚动优化的多能源微网双层调度模型红旗红旗红旗火火 关键词:多
- 西门子300PLC分时产量计数程序:灵活启动条件,间接寻址设计,直观易懂,集成系统时间更改功能的触摸屏应用 ,西门子300PLC 分时产量计数程序,在地址不冲突情况下,改个启动计数条件,就可以拿来在项
- 公告管理系统 - JavaEE项目源码.zip
- 铌酸锂薄膜电光调制特性研究:LNOI绝缘体上的新突破,LNOI绝缘体上铌酸锂薄膜电光调制特性 ,关键词:LNOI绝缘体; 铌酸锂薄膜; 电光调制特性; 薄膜电光性能; 绝缘体上薄膜技术; 调制特性研
- 428652838510dbab3ed98f19b4b8af42.zip
- "Apollo 7.0:升级版行为预测模块的思维导图与注释代码详解",Apollo7.0-行为预测模块思维导图及该模块注释代码 , Apollo 7.0为Apollo最新版本,此次重点针对感知和预测算
- 西门子UG三轴后处理系统UG840D与UG828D界面简洁,刀具信息齐全,输出精准,支持圆弧输出与防错提示等功能 ,西门子UG后处理三轴后处理840D828D系统 界面简洁,没那么多字幕 无使用限制
- 基于Simulink的风电调频技术研究:双馈风机与虚拟同步机控制下的风电场运行及三机九节点仿真分析,风电渗透达20%,phasor模型仿真高效实现 ,simulink 风电调频,双馈风机调频,VSG同
- 软硬网协同赋能:医院数智化转型的信息工程实践与探索报告
- gnome-online-accounts-3.28.2-1.el7.x64-86.rpm.tar.gz
- java开发练习-猜数游戏 - 源代码
- gnome-online-accounts-devel-3.28.2-1.el7.x64-86.rpm.tar.gz
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈