snappymail/dev/Sieve/View/Script.js

156 lines
3.3 KiB
JavaScript
Raw Normal View History

import { FilterModel } from 'Sieve/Model/Filter';
import { SieveScriptModel } from 'Sieve/Model/Script';
2021-01-18 23:47:10 +08:00
import { FilterPopupView } from 'Sieve/View/Filter';
import { parseScript } from 'Sieve/Parser';
2022-03-09 19:33:31 +08:00
import { availableActions, availableControls, availableTests } from 'Sieve/Commands';
import {
capa,
scripts,
getNotification,
Remote
} from 'Sieve/Utils';
export class SieveScriptPopupView extends rl.pluginPopupView {
2021-01-18 23:47:10 +08:00
constructor() {
super('SieveScript');
2021-01-18 23:47:10 +08:00
this.addObservables({
2021-01-19 23:01:30 +08:00
saveError: false,
errorText: '',
2021-01-19 23:01:30 +08:00
rawActive: false,
script: null,
saving: false,
sieveCapabilities: '',
availableActions: '',
availableControls: '',
availableTests: ''
2021-01-18 23:47:10 +08:00
});
this.filterForDeletion = ko.observable(null).askDeleteHelper();
2021-01-18 23:47:10 +08:00
}
validateScript() {
try {
this.errorText('');
parseScript(this.script().body());
} catch (e) {
this.errorText(e.message);
}
}
2022-02-21 22:36:34 +08:00
saveScript() {
let self = this,
script = self.script();
if (!self.saving()/* && script.hasChanges()*/) {
this.errorText('');
self.saveError(false);
2021-01-19 23:01:30 +08:00
if (!script.verify()) {
2022-02-26 08:02:12 +08:00
return;
2021-01-18 23:47:10 +08:00
}
if (!script.exists() && scripts.find(item => item.name() === script.name())) {
2021-01-19 23:59:43 +08:00
script.nameError(true);
2022-02-26 08:02:12 +08:00
return;
2021-01-19 23:59:43 +08:00
}
try {
parseScript(script.body());
} catch (e) {
this.errorText(e.message);
return;
}
self.saving(true);
if (script.allowFilters()) {
script.body(script.filtersToRaw());
}
2021-01-18 23:47:10 +08:00
Remote.request('FiltersScriptSave',
(iError, data) => {
self.saving(false);
2021-01-18 23:47:10 +08:00
if (iError) {
self.saveError(true);
self.errorText(data?.ErrorMessageAdditional || getNotification(iError));
} else {
script.exists() || scripts.push(script);
script.exists(true);
2021-01-19 23:01:30 +08:00
script.hasChanges(false);
// this.close();
2021-01-18 23:47:10 +08:00
}
},
script.toJson()
2021-01-18 23:47:10 +08:00
);
}
}
deleteFilter(filter) {
2021-01-19 23:01:30 +08:00
this.script().filters.remove(filter);
2021-01-18 23:47:10 +08:00
}
addFilter() {
2021-01-19 23:01:30 +08:00
/* this = SieveScriptModel */
2021-01-18 23:47:10 +08:00
const filter = new FilterModel();
filter.generateID();
FilterPopupView.showModal([
2021-01-18 23:47:10 +08:00
filter,
2022-07-22 03:57:29 +08:00
() => this.filters.push(filter.assignTo())
2021-01-18 23:47:10 +08:00
]);
}
editFilter(filter) {
2022-07-22 03:57:29 +08:00
const clonedFilter = filter.assignTo();
FilterPopupView.showModal([
2021-01-18 23:47:10 +08:00
clonedFilter,
() => {
clonedFilter.assignTo(filter);
const script = this.script();
script.hasChanges(script.body() != script.filtersToRaw());
},
2021-01-18 23:47:10 +08:00
true
]);
}
toggleFiltersRaw() {
const script = this.script(), notRaw = !this.rawActive();
notRaw && script.body(script.filtersToRaw());
2022-02-26 08:02:12 +08:00
this.rawActive(notRaw);
}
2021-01-18 23:47:10 +08:00
onBuild(oDom) {
oDom.addEventListener('click', event => {
2021-09-10 15:30:06 +08:00
const el = event.target.closestWithin('td.e-action', oDom),
2021-01-18 23:47:10 +08:00
filter = el && ko.dataFor(el);
2021-01-19 23:01:30 +08:00
filter && this.editFilter(filter);
2021-01-18 23:47:10 +08:00
});
}
beforeShow(oScript) {
// onShow(oScript) {
this.sieveCapabilities(capa.join(' '));
this.availableActions([...Object.keys(availableActions())].join(', '));
this.availableControls([...Object.keys(availableControls())].join(', '));
this.availableTests([...Object.keys(availableTests())].join(', '));
oScript = oScript || new SieveScriptModel();
2021-01-19 23:01:30 +08:00
this.script(oScript);
this.rawActive(!oScript.allowFilters());
2021-01-19 23:01:30 +08:00
this.saveError(false);
this.errorText('');
2022-03-09 19:33:31 +08:00
/*
// TODO: Sieve GUI
let tree = parseScript(oScript.body(), oScript.name());
console.dir(tree);
console.log(tree.join('\r\n'));
*/
2021-01-18 23:47:10 +08:00
}
}