snappymail/dev/View/Popup/SieveScript.js

147 lines
3.3 KiB
JavaScript
Raw Normal View History

2021-01-18 23:47:10 +08:00
import ko from 'ko';
import { getNotification, i18nToNodes } from 'Common/Translator';
import { addObservablesTo } from 'Common/Utils';
import { delegateRunOnDestroy } from 'Common/UtilsUser';
2021-01-18 23:47:10 +08:00
import Remote from 'Remote/User/Fetch';
2021-01-23 00:29:01 +08:00
import { FilterModel } from 'Model/Filter';
import { SieveUserStore } from 'Stores/User/Sieve';
2021-01-18 23:47:10 +08:00
import { showScreenPopup/*, decorateKoCommands*/ } from 'Knoin/Knoin';
import { AbstractViewPopup } from 'Knoin/AbstractViews';
import { FilterPopupView } from 'View/Popup/Filter';
class SieveScriptPopupView extends AbstractViewPopup {
2021-01-18 23:47:10 +08:00
constructor() {
super('SieveScript');
2021-01-18 23:47:10 +08:00
addObservablesTo(this, {
2021-01-19 23:01:30 +08:00
saveError: false,
saveErrorText: '',
2021-01-19 23:01:30 +08:00
rawActive: false,
allowToggle: false,
2021-01-19 23:01:30 +08:00
script: null
2021-01-18 23:47:10 +08:00
});
this.sieveCapabilities = SieveUserStore.capa.join(' ');
2021-01-19 23:01:30 +08:00
this.saving = false;
2021-01-18 23:47:10 +08:00
this.filterForDeletion = ko.observable(null).deleteAccessHelper();
/*
decorateKoCommands(this, {
saveScriptCommand: 1
});
*/
2021-01-18 23:47:10 +08:00
}
saveScriptCommand() {
let self = this,
script = self.script();
if (!self.saving/* && script.hasChanges()*/) {
2021-01-19 23:01:30 +08:00
if (!script.verify()) {
2021-01-18 23:47:10 +08:00
return false;
}
if (!script.exists() && SieveUserStore.scripts.find(item => item.name() === script.name())) {
2021-01-19 23:59:43 +08:00
script.nameError(true);
return false;
}
self.saving = true;
self.saveError(false);
if (self.allowToggle()) {
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.saveErrorText((data && data.ErrorMessageAdditional) || getNotification(iError));
} else {
script.exists() || SieveUserStore.scripts.push(script);
script.exists(true);
2021-01-19 23:01:30 +08:00
script.hasChanges(false);
2021-01-18 23:47:10 +08:00
}
},
script.toJson()
2021-01-18 23:47:10 +08:00
);
}
return true;
}
deleteFilter(filter) {
2021-01-19 23:01:30 +08:00
this.script().filters.remove(filter);
2021-01-18 23:47:10 +08:00
delegateRunOnDestroy(filter);
}
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();
showScreenPopup(FilterPopupView, [
2021-01-18 23:47:10 +08:00
filter,
() => {
this.filters.push(filter);
},
false
]);
}
editFilter(filter) {
const clonedFilter = filter.cloneSelf();
showScreenPopup(FilterPopupView, [
2021-01-18 23:47:10 +08:00
clonedFilter,
() => {
2021-01-19 23:01:30 +08:00
const script = this.script(),
filters = script.filters(),
2021-01-18 23:47:10 +08:00
index = filters.indexOf(filter);
2021-01-19 23:01:30 +08:00
if (-1 < index) {
2021-01-18 23:47:10 +08:00
delegateRunOnDestroy(filters[index]);
filters[index] = clonedFilter;
2021-01-19 23:01:30 +08:00
script.filters(filters);
2021-01-18 23:47:10 +08:00
}
},
true
]);
}
toggleFiltersRaw() {
if (!this.rawActive()) {
let script = this.script(),
changed = script.hasChanges();
script.body(script.filtersToRaw());
script.hasChanges(changed);
}
this.rawActive(!this.rawActive());
}
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
});
}
2021-01-19 23:59:43 +08:00
onShow(oScript) {
2021-01-19 23:01:30 +08:00
this.script(oScript);
this.rawActive(!oScript.allowFilters());
this.allowToggle(oScript.allowFilters());
2021-01-19 23:01:30 +08:00
this.saveError(false);
2021-01-18 23:47:10 +08:00
}
onShowWithDelay() {
2021-01-19 23:01:30 +08:00
// Sometimes not everything is translated, try again
i18nToNodes(this.viewModelDom);
2021-01-18 23:47:10 +08:00
}
}
export { SieveScriptPopupView, SieveScriptPopupView as default };