snappymail/dev/Model/FilterCondition.js
djmaze 188a40b196 Basic JSON object properties revival now handled by AbstractModel
This will be better for future use of JSON.stringify() and JSON.parse()
For now the difference between the PHP JSON being PascalCase and the JS object properties being camelCase is handled by AbstractModel
2020-10-20 17:39:00 +02:00

81 lines
1.7 KiB
JavaScript

import ko from 'ko';
import { FilterConditionField, FilterConditionType } from 'Common/Enums';
import { AbstractModel } from 'Knoin/AbstractModel';
class FilterConditionModel extends AbstractModel {
constructor() {
super();
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 = '';
switch (this.field()) {
case FilterConditionField.Size:
template = 'SettingsFiltersConditionSize';
break;
case FilterConditionField.Header:
template = 'SettingsFiltersConditionMore';
break;
default:
template = 'SettingsFiltersConditionDefault';
break;
}
return template;
}, this);
this.field.subscribe(() => {
this.value('');
this.valueSecond('');
});
this.regDisposables([this.template]);
}
verify() {
if (!this.value()) {
this.value.error(true);
return false;
}
if (FilterConditionField.Header === this.field() && !this.valueSecond()) {
this.valueSecond.error(true);
return false;
}
return true;
}
// static reviveFromJson(json) {}
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;
}
}
export { FilterConditionModel, FilterConditionModel as default };