Browse Source

KERNEL-551 feat: 日期和时间控件支持范围和自定义显示格式

es6
windy 6 years ago
parent
commit
857315e691
  1. 4
      changelog.md
  2. 4
      src/core/func/date.js
  3. 2
      src/widget/dynamicdate/dynamicdate.combo.js
  4. 2
      src/widget/dynamicdatetime/dynamicdatetime.combo.js
  5. 60
      src/widget/dynamicdatetime/dynamicdatetime.trigger.js

4
changelog.md

@ -1,4 +1,8 @@
# 更新日志 # 更新日志
2.0(2019-05)
- 日期/时间/日期区间/时间区间支持自定义日期选择范围
- 日期/时间/日期区间/时间区间支持自定义日期显示格式
2.0(2019-04) 2.0(2019-04)
- 新增`bi.multi_tree_list_combo`控件, 此下拉树勾选节点时不会影响父子节点的勾选状态 - 新增`bi.multi_tree_list_combo`控件, 此下拉树勾选节点时不会影响父子节点的勾选状态
- 新增`bi.multi_tree_insert_combo`控件, 此下拉树可以插入不存在的新值 - 新增`bi.multi_tree_insert_combo`控件, 此下拉树可以插入不存在的新值

4
src/core/func/date.js

@ -171,8 +171,8 @@ _.extend(BI, {
s["%H"] = (hr < 10) ? ("0" + hr) : hr; // hour, range 00 to 23 (24h format) s["%H"] = (hr < 10) ? ("0" + hr) : hr; // hour, range 00 to 23 (24h format)
s["%I"] = (ir < 10) ? ("0" + ir) : ir; // hour, range 01 to 12 (12h format) s["%I"] = (ir < 10) ? ("0" + ir) : ir; // hour, range 01 to 12 (12h format)
s["%j"] = (dy < 100) ? ((dy < 10) ? ("00" + dy) : ("0" + dy)) : dy; // day of the year (range 001 to 366) s["%j"] = (dy < 100) ? ((dy < 10) ? ("00" + dy) : ("0" + dy)) : dy; // day of the year (range 001 to 366)
s["%k"] = hr; // hour, range 0 to 23 (24h format) s["%k"] = hr + ""; // hour, range 0 to 23 (24h format)
s["%l"] = ir; // hour, range 1 to 12 (12h format) s["%l"] = ir + ""; // hour, range 1 to 12 (12h format)
s["%X"] = (m < 9) ? ("0" + (1 + m)) : (1 + m); // month, range 01 to 12 s["%X"] = (m < 9) ? ("0" + (1 + m)) : (1 + m); // month, range 01 to 12
s["%x"] = m + 1; // month, range 1 to 12 s["%x"] = m + 1; // month, range 1 to 12
s["%M"] = (min < 10) ? ("0" + min) : min; // minute, range 00 to 59 s["%M"] = (min < 10) ? ("0" + min) : min; // minute, range 00 to 59

2
src/widget/dynamicdate/dynamicdate.combo.js

@ -11,7 +11,7 @@ BI.DynamicDateCombo = BI.inherit(BI.Single, {
height: 22, height: 22,
minDate: "1900-01-01", minDate: "1900-01-01",
maxDate: "2099-12-31", maxDate: "2099-12-31",
format: "%Y-%X-%d" format: ""
}, },

2
src/widget/dynamicdatetime/dynamicdatetime.combo.js

@ -11,7 +11,7 @@ BI.DynamicDateTimeCombo = BI.inherit(BI.Single, {
height: 22, height: 22,
minDate: "1900-01-01", minDate: "1900-01-01",
maxDate: "2099-12-31", maxDate: "2099-12-31",
format: "%Y-%X-%d" format: ""
}, },

60
src/widget/dynamicdatetime/dynamicdatetime.trigger.js

@ -13,7 +13,7 @@ BI.DynamicDateTimeTrigger = BI.inherit(BI.Trigger, {
min: "1900-01-01", // 最小日期 min: "1900-01-01", // 最小日期
max: "2099-12-31", // 最大日期 max: "2099-12-31", // 最大日期
height: 24, height: 24,
format: "%Y-%X-%d %H:%M:%S" // 显示的日期格式化方式 format: "" // 显示的日期格式化方式
}, },
_init: function () { _init: function () {
@ -26,7 +26,7 @@ BI.DynamicDateTimeTrigger = BI.inherit(BI.Trigger, {
validationChecker: function (v) { validationChecker: function (v) {
var formatStr = self._getStandardDateStr(v); var formatStr = self._getStandardDateStr(v);
var date = formatStr.match(/\d+/g); var date = formatStr.match(/\d+/g);
self._isDefaultDateFormat() && self._autoAppend(v, date); !BI.isKey(o.format) && self._autoAppend(v, date);
return self._dateCheck(formatStr) && BI.checkDateLegal(formatStr) && self._checkVoid({ return self._dateCheck(formatStr) && BI.checkDateLegal(formatStr) && self._checkVoid({
year: date[0] | 0, year: date[0] | 0,
month: date[1] | 0, month: date[1] | 0,
@ -42,7 +42,7 @@ BI.DynamicDateTimeTrigger = BI.inherit(BI.Trigger, {
watermark: BI.i18nText("BI-Basic_Unrestricted"), watermark: BI.i18nText("BI-Basic_Unrestricted"),
errorText: function () { errorText: function () {
var str = ""; var str = "";
if (self._isDefaultDateFormat()) { if (BI.isKey(o.format)) {
str = self.editor.isEditing() ? BI.i18nText("BI-Basic_Date_Time_Error_Text") : BI.i18nText("BI-Year_Trigger_Invalid_Text"); str = self.editor.isEditing() ? BI.i18nText("BI-Basic_Date_Time_Error_Text") : BI.i18nText("BI-Year_Trigger_Invalid_Text");
} }
return str; return str;
@ -55,7 +55,7 @@ BI.DynamicDateTimeTrigger = BI.inherit(BI.Trigger, {
case BI.DynamicDateCombo.Dynamic: case BI.DynamicDateCombo.Dynamic:
var text = self._getText(value); var text = self._getText(value);
var date = BI.DynamicDateHelper.getCalculation(value); var date = BI.DynamicDateHelper.getCalculation(value);
var dateStr = BI.print(date, o.format); var dateStr = BI.print(date, self._getFormatString());
return BI.isEmptyString(text) ? dateStr : (text + ":" + dateStr); return BI.isEmptyString(text) ? dateStr : (text + ":" + dateStr);
case BI.DynamicDateCombo.Static: case BI.DynamicDateCombo.Static:
default: default:
@ -63,7 +63,7 @@ BI.DynamicDateTimeTrigger = BI.inherit(BI.Trigger, {
return ""; return "";
} }
return BI.print(BI.getDate(value.year, (value.month - 1), value.day, value.hour || 0, value.minute || 0, return BI.print(BI.getDate(value.year, (value.month - 1), value.day, value.hour || 0, value.minute || 0,
value.second || 0), o.format); value.second || 0), self._getFormatString());
} }
} }
}); });
@ -125,13 +125,10 @@ BI.DynamicDateTimeTrigger = BI.inherit(BI.Trigger, {
this.setValue(o.value); this.setValue(o.value);
}, },
_isDefaultDateFormat: function () {
return this.options.format === this._const.compareFormat;
},
_getStandardDateStr: function (v) { _getStandardDateStr: function (v) {
var result = [0, 1, 2, 3, 4, 5]; var result = [];
var formatArray = this.options.format.match(/%./g); var hasSecond = false;
var formatArray = this._getFormatString().match(/%./g);
BI.each(formatArray, function (idx, v) { BI.each(formatArray, function (idx, v) {
switch (v) { switch (v) {
case "%Y": case "%Y":
@ -146,33 +143,40 @@ BI.DynamicDateTimeTrigger = BI.inherit(BI.Trigger, {
case "%e": case "%e":
result[2] = idx; result[2] = idx;
break; break;
case "%H":
case "%h":
result[3] = idx;
break;
case "%M":
case "%m":
result[4] = idx;
break;
case "%S": case "%S":
case "%s": hasSecond = true;
break;
default: default:
result[5] = idx;
break; break;
} }
}); });
var dateArray = v.match(/\d+/g); var dateArray = v.match(/\d+/g);
var newArray = []; var newArray = [];
BI.each(dateArray, function (idx) { // 处理乱序的年月日
BI.each(dateArray.slice(0, 3), function (idx) {
newArray[idx] = dateArray[result[idx]]; newArray[idx] = dateArray[result[idx]];
}); });
return newArray.slice(0, 3).join("-") + " " + newArray.slice(3).join(":"); // 拼接时分秒和pm
var suffixArray = dateArray.slice(3);
// hh:mm
if(suffixArray.length === 2 && !hasSecond) {
suffixArray.push("00");
}
var suffixString = suffixArray.join(":");
var dateString = newArray.slice(0, 3).join("-");
if (BI.isNotEmptyString(suffixString)) {
dateString += " " + suffixString;
}
return dateString;
},
_getFormatString: function () {
return this.options.format || this._const.compareFormat;
}, },
_dateCheck: function (date) { _dateCheck: function (date) {
var o = this.options; return BI.print(BI.parseDateTime(date, "%Y-%x-%d %H:%M:%S"), "%Y-%x-%d %H:%M:%S") === date ||
return BI.print(BI.parseDateTime(date, o.format), o.format) === date ||
BI.print(BI.parseDateTime(date, "%Y-%x-%d %H:%M:%S"), "%Y-%x-%d %H:%M:%S") === date ||
BI.print(BI.parseDateTime(date, "%Y-%X-%d %H:%M:%S"), "%Y-%X-%d %H:%M:%S") === date || BI.print(BI.parseDateTime(date, "%Y-%X-%d %H:%M:%S"), "%Y-%X-%d %H:%M:%S") === date ||
BI.print(BI.parseDateTime(date, "%Y-%x-%e %H:%M:%S"), "%Y-%x-%e %H:%M:%S") === date || BI.print(BI.parseDateTime(date, "%Y-%x-%e %H:%M:%S"), "%Y-%x-%e %H:%M:%S") === date ||
BI.print(BI.parseDateTime(date, "%Y-%X-%e %H:%M:%S"), "%Y-%X-%e %H:%M:%S") === date || BI.print(BI.parseDateTime(date, "%Y-%X-%e %H:%M:%S"), "%Y-%X-%e %H:%M:%S") === date ||
@ -217,7 +221,7 @@ BI.DynamicDateTimeTrigger = BI.inherit(BI.Trigger, {
}, },
_setInnerValue: function (date) { _setInnerValue: function (date) {
var dateStr = BI.print(date, "%Y-%X-%e %H:%M:%S"); var dateStr = BI.print(date, this._getFormatString());
this.editor.setState(dateStr); this.editor.setState(dateStr);
this.editor.setValue(dateStr); this.editor.setValue(dateStr);
}, },
@ -294,7 +298,7 @@ BI.DynamicDateTimeTrigger = BI.inherit(BI.Trigger, {
this.editor.setValue(""); this.editor.setValue("");
} else { } else {
var dateStr = BI.print(BI.getDate(value.year, (value.month - 1), value.day, value.hour || 0, value.minute || 0, var dateStr = BI.print(BI.getDate(value.year, (value.month - 1), value.day, value.hour || 0, value.minute || 0,
value.second || 0), this.options.format); value.second || 0), this._getFormatString());
this.editor.setState(dateStr); this.editor.setState(dateStr);
this.editor.setValue(dateStr); this.editor.setValue(dateStr);
} }

Loading…
Cancel
Save