2021-12-31 20:30:05 +08:00
|
|
|
import { koComputable } from 'External/ko';
|
2016-07-07 05:03:30 +08:00
|
|
|
|
2019-07-05 03:19:24 +08:00
|
|
|
import { AbstractModel } from 'Knoin/AbstractModel';
|
2016-07-07 05:03:30 +08:00
|
|
|
|
2021-01-25 05:58:06 +08:00
|
|
|
/**
|
|
|
|
* @enum {string}
|
|
|
|
*/
|
|
|
|
export const FilterConditionField = {
|
|
|
|
From: 'From',
|
|
|
|
Recipient: 'Recipient',
|
|
|
|
Subject: 'Subject',
|
|
|
|
Header: 'Header',
|
|
|
|
Body: 'Body',
|
|
|
|
Size: 'Size'
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @enum {string}
|
|
|
|
*/
|
|
|
|
export const FilterConditionType = {
|
|
|
|
Contains: 'Contains',
|
|
|
|
NotContains: 'NotContains',
|
|
|
|
EqualTo: 'EqualTo',
|
|
|
|
NotEqualTo: 'NotEqualTo',
|
|
|
|
Regex: 'Regex',
|
|
|
|
Over: 'Over',
|
|
|
|
Under: 'Under',
|
|
|
|
Text: 'Text',
|
|
|
|
Raw: 'Raw'
|
|
|
|
};
|
|
|
|
|
2021-01-22 23:32:08 +08:00
|
|
|
export class FilterConditionModel extends AbstractModel {
|
2016-07-16 05:29:42 +08:00
|
|
|
constructor() {
|
2020-10-19 01:19:45 +08:00
|
|
|
super();
|
2016-07-07 05:03:30 +08:00
|
|
|
|
2020-10-25 18:46:58 +08:00
|
|
|
this.addObservables({
|
|
|
|
field: FilterConditionField.From,
|
|
|
|
type: FilterConditionType.Contains,
|
|
|
|
value: '',
|
|
|
|
valueError: false,
|
|
|
|
|
|
|
|
valueSecond: '',
|
|
|
|
valueSecondError: false
|
|
|
|
});
|
2016-07-07 05:03:30 +08:00
|
|
|
|
2021-12-31 20:30:05 +08:00
|
|
|
this.template = koComputable(() => {
|
2021-01-26 05:29:55 +08:00
|
|
|
const template = 'SettingsFiltersCondition';
|
2019-07-05 03:19:24 +08:00
|
|
|
switch (this.field()) {
|
2020-11-26 19:34:54 +08:00
|
|
|
case FilterConditionField.Body:
|
2021-01-26 05:29:55 +08:00
|
|
|
return template + 'Body';
|
2016-07-07 05:03:30 +08:00
|
|
|
case FilterConditionField.Size:
|
2021-01-26 05:29:55 +08:00
|
|
|
return template + 'Size';
|
2016-07-07 05:03:30 +08:00
|
|
|
case FilterConditionField.Header:
|
2021-01-26 05:29:55 +08:00
|
|
|
return template + 'More';
|
2016-07-07 05:03:30 +08:00
|
|
|
default:
|
2021-01-26 05:29:55 +08:00
|
|
|
return template + 'Default';
|
2016-07-07 05:03:30 +08:00
|
|
|
}
|
2021-02-10 19:12:36 +08:00
|
|
|
});
|
2016-07-07 05:03:30 +08:00
|
|
|
|
2020-10-25 18:46:58 +08:00
|
|
|
this.addSubscribables({
|
|
|
|
field: () => {
|
|
|
|
this.value('');
|
|
|
|
this.valueSecond('');
|
|
|
|
}
|
2016-08-17 06:01:20 +08:00
|
|
|
});
|
2016-07-07 05:03:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
verify() {
|
2020-07-28 23:20:14 +08:00
|
|
|
if (!this.value()) {
|
2020-10-25 18:46:58 +08:00
|
|
|
this.valueError(true);
|
2016-07-07 05:03:30 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-07-28 23:20:14 +08:00
|
|
|
if (FilterConditionField.Header === this.field() && !this.valueSecond()) {
|
2020-10-25 18:46:58 +08:00
|
|
|
this.valueSecondError(true);
|
2016-07-07 05:03:30 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-10-20 23:39:00 +08:00
|
|
|
// static reviveFromJson(json) {}
|
2016-07-07 05:03:30 +08:00
|
|
|
|
|
|
|
toJson() {
|
|
|
|
return {
|
2021-01-20 17:10:59 +08:00
|
|
|
// '@Object': 'Object/FilterCondition',
|
2016-07-07 05:03:30 +08:00
|
|
|
Field: this.field(),
|
|
|
|
Type: this.type(),
|
|
|
|
Value: this.value(),
|
|
|
|
ValueSecond: this.valueSecond()
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
cloneSelf() {
|
|
|
|
const filterCond = new FilterConditionModel();
|
|
|
|
|
|
|
|
filterCond.field(this.field());
|
|
|
|
filterCond.type(this.type());
|
|
|
|
filterCond.value(this.value());
|
|
|
|
filterCond.valueSecond(this.valueSecond());
|
|
|
|
|
|
|
|
return filterCond;
|
|
|
|
}
|
|
|
|
}
|