import { CtlInput } from './CtlInput' import { parseYvanPropChangeVJson } from '../../CtlUtils' import { CtlDateDefault, CtlDateTimeDefault } from '../../CtlDefaultValue' import * as YvanUI from '../../YvanUIExtend' export class CtlDatePicker extends CtlInput { static create(module: any, vjson: any): CtlDatePicker { const that = new CtlDatePicker(vjson) that._module = module const baseConfig: any = {} if (vjson.type === 'year') { baseConfig.format = '%Y' baseConfig.timepicker = false _.defaultsDeep(vjson, CtlDateDefault) } else if (vjson.type === 'month') { baseConfig.format = '%Y-%m' baseConfig.timepicker = false _.defaultsDeep(vjson, CtlDateDefault) } else { if (vjson.view === 'datetime') { // 日期+时间输入 baseConfig.format = '%Y-%m-%d %H:%i:%s' baseConfig.timepicker = true _.defaultsDeep(vjson, CtlDateTimeDefault) } else { // 日期输入 baseConfig.format = '%Y-%m-%d' baseConfig.timepicker = false _.defaultsDeep(vjson, CtlDateDefault) } } // 基础属性先执行 that._create(vjson, that) const yvanProp = parseYvanPropChangeVJson(vjson, []) // 将 yvanProp 合并至当前 Ctl 对象 _.assign(that, yvanProp) _.merge(vjson, that._webixConfig, { editable: true, stringResult: true, view: 'datepicker', ...baseConfig, on: {} }) return that } /*============================ 公共属性部分 ============================*/ /** * 设置值 (如果不符合规定的格式 会清空) */ set value(nv: any) { if (isNaN(Date.parse(nv)) && this.vjson.formatter) { if (typeof this.vjson.formatter === 'string') { // formatter 是字符串,从全局 YvanUI.formatter 找方法 if (YvanUI.formatter.hasOwnProperty(this.vjson.formatter)) { const formatterFunc = YvanUI.formatter[this.vjson.formatter] nv = formatterFunc(String(nv)) } } } if (!this._webix) { this._webixConfig.value = nv } else { this._webix.setValue(nv) } } /** * 获取值(可能取到空值) */ get value(): any { const value = this._webix ? this._webix.getValue() : this._webixConfig.value if (this.vjson.view === 'datetime') { // 日期+时间输入 return _.toString(value).substr(0, 19) } else { // 日期输入 return _.toString(value).substr(0, 10) } } /*============================ 私有部分 ============================*/ //更改 onChange 或实体时的值 protected valueProcess(value: any): any { const moment: any = _.get(window, 'moment') if (_.isDate(value)) { value = moment(value) if (this.vjson.type === 'year') { // 年份 value = value.isValid() ? value.format('YYYY') : '' } else if (this.vjson.type === 'month') { // 年份+月份 value = value.isValid() ? value.format('YYYY-MM') : '' } else { if (this.vjson.view === 'datetime') { // 日期+时间输入 value = value.isValid() ? value.format('YYYY-MM-DD HH:mm:ss') : '' } else { // 日期输入 value = value.isValid() ? value.format('YYYY-MM-DD') : '' } } return value } return value } }