snappymail/dev/Model/FilterCondition.js

94 lines
2 KiB
JavaScript
Raw Normal View History

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';
import { pString } from 'Common/Utils';
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
this.field = ko.observable(FilterConditionField.From);
this.type = ko.observable(FilterConditionType.Contains);
this.value = ko.observable('');
this.value.error = ko.observable(false);
this.valueSecond = ko.observable('');
this.valueSecond.error = ko.observable(false);
this.template = ko.computed(() => {
let template = '';
2019-07-05 03:19:24 +08:00
switch (this.field()) {
2016-07-07 05:03:30 +08:00
case FilterConditionField.Size:
template = 'SettingsFiltersConditionSize';
2016-07-07 05:03:30 +08:00
break;
case FilterConditionField.Header:
template = 'SettingsFiltersConditionMore';
2016-07-07 05:03:30 +08:00
break;
default:
template = 'SettingsFiltersConditionDefault';
2016-07-07 05:03:30 +08:00
break;
}
return template;
2016-07-07 05:03:30 +08:00
}, this);
this.field.subscribe(() => {
2016-07-07 05:03:30 +08:00
this.value('');
this.valueSecond('');
});
2016-07-07 05:03:30 +08:00
this.regDisposables([this.template]);
}
verify() {
if (!this.value()) {
2016-07-07 05:03:30 +08:00
this.value.error(true);
return false;
}
if (FilterConditionField.Header === this.field() && !this.valueSecond()) {
2016-07-07 05:03:30 +08:00
this.valueSecond.error(true);
return false;
}
return true;
}
parse(json) {
2019-07-05 03:19:24 +08:00
if (json && json.Field && json.Type) {
2016-07-07 05:03:30 +08:00
this.field(pString(json.Field));
this.type(pString(json.Type));
this.value(pString(json.Value));
this.valueSecond(pString(json.ValueSecond));
return true;
}
return false;
}
toJson() {
return {
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 };