snappymail/dev/Model/SieveScript.js
2021-01-19 17:42:38 +01:00

85 lines
1.8 KiB
JavaScript

import ko from 'ko';
import { AbstractModel } from 'Knoin/AbstractModel';
import { FilterModel } from 'Model/Filter';
class SieveScriptModel extends AbstractModel
{
constructor() {
super();
this.addObservables({
name: '',
active: false,
body: '',
exists: false,
nameError: false,
bodyError: false,
deleteAccess: false,
canBeDeleted: true,
hasChanges: false
});
this.filters = ko.observableArray([]);
// this.saving = ko.observable(false).extend({ throttle: 200 });
this.addSubscribables({
name: () => this.hasChanges(true),
filters: () => this.hasChanges(true),
body: () => this.hasChanges(true)
});
}
setFilters() {
/*let tree = */window.Sieve.parseScript(this.body);
// this.filters = ko.observableArray(tree);
}
verify() {
this.nameError(!this.name().trim());
this.bodyError(this.allowFilters() ? !this.filters().length : !this.body().trim());
return !this.nameError() && !this.bodyError();
}
toJson() {
return {
name: this.name(),
active: this.active() ? '1' : '0',
body: this.body(),
filters: this.filters().map(item => item.toJson())
};
}
/**
* Only 'rainloop.user' script supports filters
*/
allowFilters() {
return 'rainloop.user' === this.name();
}
/**
* @static
* @param {FetchJsonScript} json
* @returns {?SieveScriptModel}
*/
static reviveFromJson(json) {
const script = super.reviveFromJson(json);
if (script) {
if (script.allowFilters() && Array.isNotEmpty(json.filters)) {
script.filters(
json.filters.map(aData => FilterModel.reviveFromJson(aData)).filter(v => v)
);
} else {
script.filters([]);
}
script.canBeDeleted(0 !== json.name.indexOf('rainloop.user'));
script.exists(true);
script.hasChanges(false);
}
return script;
}
}
export { SieveScriptModel, SieveScriptModel as default };