fineui是帆软报表和BI产品线所使用的前端框架。
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

374 lines
12 KiB

import {
shortcut,
i18nText,
createWidget,
HTapeLayout,
CenterLayout,
HorizontalFillLayout,
isEmptyString,
parseDateTime,
isPositiveInteger,
checkDateVoid,
isNotEmptyString,
isNotNull,
print,
checkDateLegal,
isNull,
parseInt,
getDate,
getQuarter
} from "@/core";
import { Trigger, TextButton } from "@/base";
import { TriggerIconButton, SignEditor } from "@/case";
import { DynamicDateHelper } from "../dynamicdate";
// TODO:需要等待yearmonth完成才能将BI.DynamicYearMonthTrigger替换
// import { DynamicYearMonthTrigger } from "../yearmonth/trigger.yearmonth";
import { DynamicYearQuarterCombo } from "./combo.yearquarter";
@shortcut()
export class DynamicYearQuarterTrigger extends Trigger {
static xtype = "bi.dynamic_year_quarter_trigger";
_const = { hgap: 4, vgap: 2, iconWidth: 24 };
static EVENT_FOCUS = "EVENT_FOCUS";
static EVENT_ERROR = "EVENT_ERROR";
static EVENT_START = "EVENT_START";
static EVENT_CONFIRM = "EVENT_CONFIRM";
static EVENT_STOP = "EVENT_STOP";
static EVENT_KEY_DOWN = "EVENT_KEY_DOWN";
static EVENT_VALID = "EVENT_VALID";
props() {
return {
extraCls: "bi-year-quarter-trigger",
min: "1900-01-01", // 最小日期
max: "2099-12-31", // 最大日期
height: 24,
watermark: {
year: i18nText("BI-Basic_Unrestricted"),
quarter: i18nText("BI-Basic_Unrestricted"),
},
};
}
_init() {
super._init(...arguments);
const o = this.options;
this.yearEditor = this._createEditor(true);
this.quarterEditor = this._createEditor(false);
// 暂时的解决方法
// const height = o.height + 2;
createWidget({
element: this,
type: HTapeLayout.xtype,
items: [
{
type: CenterLayout.xtype,
items: [
{
type: HorizontalFillLayout.xtype,
columnSize: ["fill", ""],
items: [
this.yearEditor,
{
el: {
type: TextButton.xtype,
text: i18nText("BI-Multi_Date_Year"),
},
}
],
},
{
type: HorizontalFillLayout.xtype,
columnSize: ["fill", ""],
items: [
this.quarterEditor,
{
el: {
type: TextButton.xtype,
text: i18nText("BI-Multi_Date_Quarter"),
},
}
],
}
],
},
{
el: {
type: TriggerIconButton.xtype,
width: this._const.iconWidth,
},
width: this._const.iconWidth,
}
],
});
this.setValue(o.value);
}
_createEditor(isYear) {
const o = this.options,
c = this._const;
const editor = createWidget({
type: SignEditor.xtype,
simple: o.simple,
height: o.height,
validationChecker: v => {
if (isYear) {
let month = this.quarterEditor.getValue();
if (isEmptyString(month)) {
month =
parseInt(v, 10) ===
parseDateTime(o.min, "%Y-%X-%d").getFullYear()
? parseDateTime(o.min, "%Y-%X-%d").getMonth() +
1
: 1;
} else {
month = (parseInt(month, 10) - 1) * 3 + 1;
}
return (
v === "" ||
(isPositiveInteger(v) &&
!checkDateVoid(v, month, 1, o.min, o.max)[0])
);
}
const year = this.yearEditor.getValue();
return (
v === "" ||
(isPositiveInteger(v) &&
v >= 1 &&
v <= 4 &&
(isEmptyString(year)
? true
: !checkDateVoid(
this.yearEditor.getValue(),
(v - 1) * 3 + 1,
1,
o.min,
o.max
)[0]))
);
},
quitChecker: () => false,
errorText: v => {
const year = isYear ? v : this.yearEditor.getValue();
const quarter = isYear ? this.quarterEditor.getValue() : v;
if (
!isPositiveInteger(year) ||
!isPositiveInteger(quarter) ||
quarter > 4
) {
return i18nText("BI-Year_Trigger_Invalid_Text");
}
const start = parseDateTime(o.min, "%Y-%X-%d");
const end = parseDateTime(o.max, "%Y-%X-%d");
return i18nText(
"BI-Basic_Year_Quarter_Range_Error",
start.getFullYear(),
getQuarter(start),
end.getFullYear(),
getQuarter(end)
);
},
watermark: isYear ? o.watermark?.year : o.watermark?.quarter,
hgap: c.hgap,
vgap: c.vgap,
allowBlank: true,
});
editor.on(SignEditor.EVENT_KEY_DOWN, () => {
this.fireEvent(DynamicYearQuarterTrigger.EVENT_KEY_DOWN);
});
editor.on(SignEditor.EVENT_FOCUS, () => {
this.fireEvent(DynamicYearQuarterTrigger.EVENT_FOCUS);
});
editor.on(SignEditor.EVENT_STOP, () => {
this.fireEvent(DynamicYearQuarterTrigger.EVENT_STOP);
});
editor.on(SignEditor.EVENT_CONFIRM, () => {
this._doEditorConfirm(editor);
this.fireEvent(DynamicYearQuarterTrigger.EVENT_CONFIRM);
});
editor.on(SignEditor.EVENT_SPACE, () => {
if (editor.isValid()) {
editor.blur();
}
});
editor.on(SignEditor.EVENT_START, () => {
this.fireEvent(DynamicYearQuarterTrigger.EVENT_START);
});
editor.on(SignEditor.EVENT_ERROR, () => {
this.fireEvent(DynamicYearQuarterTrigger.EVENT_ERROR);
});
editor.on(SignEditor.EVENT_VALID, () => {
const year = this.yearEditor.getValue();
const quarter = this.quarterEditor.getValue();
if (isNotEmptyString(year) && isNotEmptyString(quarter)) {
if (
isPositiveInteger(year) &&
quarter >= 1 &&
quarter <= 4 &&
!checkDateVoid(
year,
(quarter - 1) * 3 + 1,
1,
o.min,
o.max
)[0]
) {
this.fireEvent(BI.DynamicYearMonthTrigger.EVENT_VALID);
}
}
});
editor.on(SignEditor.EVENT_CHANGE, () => {
if (isYear) {
this._autoSwitch(editor);
}
});
return editor;
}
_doEditorConfirm(editor) {
const value = editor.getValue();
if (isNotNull(value)) {
editor.setValue(value);
}
const quarterValue = this.quarterEditor.getValue();
this.storeValue = {
type: DynamicYearQuarterCombo.Static,
value: {
year: this.yearEditor.getValue(),
quarter: isEmptyString(this.quarterEditor.getValue())
? ""
: quarterValue,
},
};
this.setTitle(this._getStaticTitle(this.storeValue.value));
}
_yearCheck(v) {
const date = print(parseDateTime(v, "%Y-%X-%d"), "%Y-%X-%d");
return (
print(parseDateTime(v, "%Y"), "%Y") === v &&
date >= this.options.min &&
date <= this.options.max
);
}
_autoSwitch(editor) {
const v = editor.getValue();
if (isNotEmptyString(v) && checkDateLegal(v)) {
if (v.length === 4 && this._yearCheck(v)) {
this._doEditorConfirm(editor);
this.fireEvent(DynamicYearQuarterTrigger.EVENT_CONFIRM);
this.quarterEditor.focus();
}
}
}
_getStaticTitle(value) {
value = value || {};
const hasYear = !(isNull(value.year) || isEmptyString(value.year));
const hasMonth = !(isNull(value.quarter) || isEmptyString(value.quarter));
switch ((hasYear << 1) | hasMonth) {
// !hasYear && !hasMonth
case 0:
return "";
// !hasYear && hasMonth
case 1:
return value.quarter;
// hasYear && !hasMonth
case 2:
return value.year;
// hasYear && hasMonth
case 3:
default:
return `${value.year}-${value.quarter}`;
}
}
_getText(obj) {
let value = "";
if (isNotNull(obj.year) && parseInt(obj.year) !== 0) {
value +=
Math.abs(obj.year) +
i18nText("BI-Basic_Year") +
(obj.year < 0
? i18nText("BI-Basic_Front")
: i18nText("BI-Basic_Behind"));
}
if (isNotNull(obj.quarter) && parseInt(obj.quarter) !== 0) {
value +=
Math.abs(obj.quarter) +
i18nText("BI-Basic_Single_Quarter") +
(obj.quarter < 0
? i18nText("BI-Basic_Front")
: i18nText("BI-Basic_Behind"));
}
return value;
}
_setInnerValue(date, text) {
const dateStr = print(date, "%Y-%Q");
this.yearEditor.setValue(date.getFullYear());
this.quarterEditor.setValue(getQuarter(date));
this.setTitle(isEmptyString(text) ? dateStr : `${text}:${dateStr}`);
}
setMinDate(minDate) {
if (isNotEmptyString(this.options.min)) {
this.options.min = minDate;
}
}
setMaxDate(maxDate) {
if (isNotEmptyString(this.options.max)) {
this.options.max = maxDate;
}
}
setValue(v) {
let type, value, text, quarter;
let date = getDate();
this.storeValue = v;
if (isNotNull(v)) {
type = v.type || DynamicYearQuarterCombo.Static;
value = v.value || v;
}
switch (type) {
case DynamicYearQuarterCombo.Dynamic:
text = this._getText(value);
date = DynamicDateHelper.getCalculation(value);
this._setInnerValue(date, text);
break;
case DynamicYearQuarterCombo.Static:
default:
value = value || {};
quarter = isNull(value.quarter) ? null : value.quarter;
this.yearEditor.setValue(value.year);
this.quarterEditor.setValue(quarter);
this.setTitle(this._getStaticTitle(value));
break;
}
}
getValue() {
return this.storeValue;
}
getKey() {
return `${this.yearEditor.getValue()}-${this.quarterEditor.getValue()}`;
}
isStateValid() {
return this.yearEditor.isValid() && this.quarterEditor.isValid();
}
}