import { shortcut, i18nText, createWidget, isKey, checkDateLegal, parseDateTime, bind, isNotNull, isNotEmptyString, isEqual, isEmptyObject, getDate, isEmptyString, isNull, each, checkDateVoid, print } from "@/core"; import { Trigger } from "@/base"; import { SignEditor } from "@/case"; import { DynamicDateCombo } from "./dynamicdate.combo"; import { DynamicDateHelper } from "./dynamicdate.caculate"; @shortcut() export class DynamicDateTrigger extends Trigger { static xtype = "bi.dynamic_date_trigger" _const = { hgap: 4, vgap: 2, yearLength: 4, yearMonthLength: 6, yearFullMonthLength: 7, compareFormat: "%Y-%X-%d", iconWidth: 24, }; static EVENT_BLUR = "EVENT_BLUR" static EVENT_FOCUS = "EVENT_FOCUS" static EVENT_START = "EVENT_START" static EVENT_STOP = "EVENT_STOP" static EVENT_CONFIRM = "EVENT_CONFIRM" static EVENT_CHANGE = "EVENT_CHANGE" static EVENT_VALID = "EVENT_VALID" static EVENT_ERROR = "EVENT_ERROR" static EVENT_TRIGGER_CLICK = "EVENT_TRIGGER_CLICK" static EVENT_KEY_DOWN = "EVENT_KEY_DOWN" props() { return { extraCls: "bi-date-trigger", min: "1900-01-01", // 最小日期 max: "2099-12-31", // 最大日期 height: 24, iconWidth: 24, format: "", // 显示的日期格式化方式 allowEdit: true, // 是否允许编辑 watermark: i18nText("BI-Basic_Unrestricted"), }; } _init() { super._init(...arguments); const o = this.options, c = this._const; this.storeTriggerValue = ""; this.editor = createWidget({ type: "bi.sign_editor", simple: o.simple, height: o.height, validationChecker: v => { const formatStr = this._getStandardDateStr(v); const date = formatStr.match(/\d+/g); !isKey(o.format) && this._autoAppend(v, date); return this._dateCheck(formatStr) && checkDateLegal(formatStr) && this._checkVoid({ year: date[0] | 0, month: date[1] | 0, day: date[2] | 0, }); }, quitChecker () { return false; }, hgap: c.hgap, vgap: c.vgap, allowBlank: true, watermark: o.watermark, errorText: v => { let str = ""; if (!isKey(o.format)) { if (!this._dateCheck(v)) { str = this.editor.isEditing() ? i18nText("BI-Date_Trigger_Error_Text") : i18nText("BI-Year_Trigger_Invalid_Text"); } else { const start = parseDateTime(o.min, "%Y-%X-%d"); const end = parseDateTime(o.max, "%Y-%X-%d"); str = i18nText("BI-Basic_Date_Range_Error", start.getFullYear(), start.getMonth() + 1, start.getDate(), end.getFullYear(), end.getMonth() + 1, end.getDate() ); } } return str; }, title: bind(this._getTitle, this), }); this.editor.on(SignEditor.EVENT_KEY_DOWN, (...args) => { this.fireEvent(DynamicDateTrigger.EVENT_KEY_DOWN, ...args); }); this.editor.on(SignEditor.EVENT_FOCUS, () => { this.storeTriggerValue = this.getKey(); this.fireEvent(DynamicDateTrigger.EVENT_FOCUS); }); this.editor.on(SignEditor.EVENT_BLUR, () => { this.fireEvent(DynamicDateTrigger.EVENT_BLUR); }); this.editor.on(SignEditor.EVENT_STOP, () => { this.fireEvent(DynamicDateTrigger.EVENT_STOP); }); this.editor.on(SignEditor.EVENT_VALID, () => { this.fireEvent(DynamicDateTrigger.EVENT_VALID); }); this.editor.on(SignEditor.EVENT_ERROR, () => { this.fireEvent(DynamicDateTrigger.EVENT_ERROR); }); this.editor.on(SignEditor.EVENT_CONFIRM, () => { const value = this.editor.getValue(); if (isNotNull(value)) { this.editor.setState(value); } if (isNotEmptyString(value) && !isEqual(this.storeTriggerValue, this.getKey())) { const formatStr = this._getStandardDateStr(value); const date = formatStr.match(/\d+/g); this.storeValue = { type: DynamicDateCombo.Static, value: { year: date[0] | 0, month: date[1] | 0, day: date[2] | 0, }, }; } this.fireEvent(DynamicDateTrigger.EVENT_CONFIRM); }); this.editor.on(SignEditor.EVENT_SPACE, () => { if (this.editor.isValid()) { this.editor.blur(); } }); this.editor.on(SignEditor.EVENT_START, () => { this.fireEvent(DynamicDateTrigger.EVENT_START); }); this.editor.on(SignEditor.EVENT_CHANGE, () => { this.fireEvent(DynamicDateTrigger.EVENT_CHANGE); }); createWidget({ type: "bi.htape", element: this, columnSize: ["", this._const.iconWidth], items: [{ el: this.editor, }, { el: { type: "bi.icon_button", cls: "bi-trigger-icon-button date-font", width: this._const.iconWidth, }, width: this._const.iconWidth, }], }); !o.allowEdit && createWidget({ type: "bi.absolute", element: this, items: [{ el: { type: "bi.text", title: bind(this._getTitle, this), }, left: 0, right: o.iconWidth, top: 0, bottom: 0, }], }); this.setValue(o.value); } _getTitle() { const storeValue = this.storeValue || {}; if (isEmptyObject(storeValue)) { return this.options.watermark; } const type = storeValue.type || DynamicDateCombo.Static; const value = storeValue.value; let text, date, dateStr; switch (type) { case DynamicDateCombo.Dynamic: text = this._getText(value); date = getDate(); date = DynamicDateHelper.getCalculation(value); dateStr = print(date, this._getFormatString()); return isEmptyString(text) ? dateStr : (`${text}:${dateStr}`); case DynamicDateCombo.Static: default: if (isNull(value) || isNull(value.day)) { return ""; } return print(getDate(value.year, (value.month - 1), value.day), this._getFormatString()); } } _getStandardDateStr(v) { const c = this._const; let result = [0, 1, 2]; const formatArray = this._getFormatString().match(/%./g); each(formatArray, (idx, v) => { switch (v) { case "%Y": case "%y": result[0] = idx; break; case "%X": case "%x": result[1] = idx; break; case "%d": case "%e": default: result[2] = idx; break; } }); // 这边不能直接用\d+去切日期, 因为format格式可能是20190607这样的没有分割符的 = = // 先看一下是否是合法的, 如果合法就变成标准格式的走原来的流程, 不合法不关心 const date = parseDateTime(v, this._getFormatString()); if (print(date, this._getFormatString()) === v) { v = print(date, c.compareFormat); result = [0, 1, 2]; } const dateArray = v.match(/\d+/g); const newArray = []; each(dateArray, idx => { newArray[idx] = dateArray[result[idx]]; }); // 这边之所以不直接返回join结果是因为年的格式可能只有2位,所以需要format一下 if (newArray.length === result.length && newArray[0].length === 2) { return print(parseDateTime(newArray.join("-"), c.compareFormat), c.compareFormat); } // 这边format成-20-也没关系, 反正都是不合法的 return newArray.join("-"); } _getFormatString() { return this.options.format || this._const.compareFormat; } _dateCheck(date) { return print(parseDateTime(date, "%Y-%x-%d"), "%Y-%x-%d") === date || print(parseDateTime(date, "%Y-%X-%d"), "%Y-%X-%d") === date || print(parseDateTime(date, "%Y-%x-%e"), "%Y-%x-%e") === date || print(parseDateTime(date, "%Y-%X-%e"), "%Y-%X-%e") === date; } _checkVoid(obj) { return !checkDateVoid(obj.year, obj.month, obj.day, this.options.min, this.options.max)[0]; } _autoAppend(v, dateObj) { const splitMonth = v.split("-")[1]; if (isNotNull(dateObj) && checkDateLegal(v)) { switch (v.length) { case this._const.yearLength: if (this._yearCheck(v)) { this.editor.setValue(`${v}-`); } break; case this._const.yearMonthLength: case this._const.yearFullMonthLength: if ((isNotNull(splitMonth) && splitMonth.length === 2) || this._monthCheck(v)) { this.editor.setValue(`${v}-`); } break; default: } } } _yearCheck(v) { const date = print(parseDateTime(v, this._getFormatString()), this._const.compareFormat); return print(parseDateTime(v, "%Y"), "%Y") === v && date >= this.options.min && date <= this.options.max; } _monthCheck(v) { const date = parseDateTime(v, this._getFormatString()); const dateStr = print(date, this._const.compareFormat); return (date.getMonth() >= 0 && (print(parseDateTime(v, "%Y-%X"), "%Y-%X") === v || print(parseDateTime(v, "%Y-%x"), "%Y-%x") === v)) && dateStr >= this.options.min && dateStr <= this.options.max; } _setInnerValue(date) { const dateStr = print(date, this._getFormatString()); this.editor.setState(dateStr); this.editor.setValue(dateStr); } _getText(obj) { return DynamicDateHelper.getDescription(obj); } setValue(v) { let type, value; let date = getDate(); this.storeValue = v; if (isNotNull(v)) { type = v.type || DynamicDateCombo.Static; value = v.value || v; } const text = this._getText(value); switch (type) { case DynamicDateCombo.Dynamic: date = DynamicDateHelper.getCalculation(value); this._setInnerValue(date, text); break; case DynamicDateCombo.Static: default: if (isNull(value) || isNull(value.day)) { this.editor.setState(""); this.editor.setValue(""); } else { const dateStr = print(getDate(value.year, (value.month - 1), value.day), this._getFormatString()); this.editor.setState(dateStr); this.editor.setValue(dateStr); } break; } } setMinDate(minDate) { if (isNotEmptyString(this.options.min)) { this.options.min = minDate; } } setMaxDate(maxDate) { if (isNotEmptyString(this.options.max)) { this.options.max = maxDate; } } getKey() { return this.editor.getValue(); } getValue() { return this.storeValue; } focus() { this.editor.focus(); } blur() { this.editor.blur(); } setWaterMark(v) { this.editor.setWaterMark(v); } }