snappymail/dev/Model/FilterCondition.js

105 lines
1.9 KiB
JavaScript
Raw Normal View History

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
this.template = koComputable(() => {
const template = 'SettingsFiltersCondition';
2019-07-05 03:19:24 +08:00
switch (this.field()) {
case FilterConditionField.Body:
return template + 'Body';
2016-07-07 05:03:30 +08:00
case FilterConditionField.Size:
return template + 'Size';
2016-07-07 05:03:30 +08:00
case FilterConditionField.Header:
return template + 'More';
2016-07-07 05:03:30 +08:00
default:
return template + 'Default';
2016-07-07 05:03:30 +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-07-07 05:03:30 +08:00
}
verify() {
if (!this.value()) {
2020-10-25 18:46:58 +08:00
this.valueError(true);
2016-07-07 05:03:30 +08:00
return false;
}
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;
}
// static reviveFromJson(json) {}
2016-07-07 05:03:30 +08:00
toJson() {
return {
// '@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;
}
}