2016-07-07 05:03:30 +08:00
|
|
|
import ko from 'ko';
|
|
|
|
|
2019-07-05 03:19:24 +08:00
|
|
|
import { FilterConditionField, FilterConditionType } from 'Common/Enums';
|
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
|
|
|
|
2019-07-05 03:19:24 +08:00
|
|
|
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
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
this.template = ko.computed(() => {
|
|
|
|
let template = '';
|
2019-07-05 03:19:24 +08:00
|
|
|
switch (this.field()) {
|
2020-11-26 19:34:54 +08:00
|
|
|
case FilterConditionField.Body:
|
|
|
|
template = 'SettingsFiltersConditionBody';
|
|
|
|
break;
|
2016-07-07 05:03:30 +08:00
|
|
|
case FilterConditionField.Size:
|
2016-08-17 06:01:20 +08:00
|
|
|
template = 'SettingsFiltersConditionSize';
|
2016-07-07 05:03:30 +08:00
|
|
|
break;
|
|
|
|
case FilterConditionField.Header:
|
2016-08-17 06:01:20 +08:00
|
|
|
template = 'SettingsFiltersConditionMore';
|
2016-07-07 05:03:30 +08:00
|
|
|
break;
|
|
|
|
default:
|
2016-08-17 06:01:20 +08:00
|
|
|
template = 'SettingsFiltersConditionDefault';
|
2016-07-07 05:03:30 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
return template;
|
2016-07-07 05:03:30 +08:00
|
|
|
}, this);
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-05 03:19:24 +08:00
|
|
|
export { FilterConditionModel, FilterConditionModel as default };
|