snappymail/dev/View/Popup/SieveScript.js
2021-01-20 12:04:27 +01:00

145 lines
3.4 KiB
JavaScript

import ko from 'ko';
import { delegateRunOnDestroy } from 'Common/UtilsUser';
import { StorageResultType, Notification } from 'Common/Enums';
import { getNotification } from 'Common/Translator';
import { i18nToNodes } from 'Common/Translator';
import Remote from 'Remote/User/Fetch';
import FilterModel from 'Model/Filter';
import SieveStore from 'Stores/User/Sieve';
import { popup, showScreenPopup/*, command*/ } from 'Knoin/Knoin';
import { AbstractViewNext } from 'Knoin/AbstractViewNext';
@popup({
name: 'View/Popup/SieveScript',
templateID: 'PopupsSieveScript'
})
class SieveScriptPopupView extends AbstractViewNext {
constructor() {
super();
ko.addObservablesTo(this, {
saveError: false,
saveErrorText: '',
rawActive: false,
allowToggle: false,
script: null
});
this.sieveCapabilities = SieveStore.capa.join(' ');
this.saving = false;
this.filterForDeletion = ko.observable(null).deleteAccessHelper();
}
// @command()
saveScriptCommand() {
let self = this,
script = self.script();
if (!self.saving/* && script.hasChanges()*/) {
if (!script.verify()) {
return false;
}
if (!script.exists() && SieveStore.scripts.find(item => item.name() === script.name())) {
script.nameError(true);
return false;
}
self.saving = true;
self.saveError(false);
// script.body(script.filtersToRaw());
Remote.filtersScriptSave(
(result, data) => {
self.saving = false;
if (StorageResultType.Success === result && data && data.Result) {
script.exists(true);
script.hasChanges(false);
SieveStore.scripts.push(script);
} else {
self.saveError(true);
self.saveErrorText((data && data.ErrorCode)
? (data.ErrorMessageAdditional || getNotification(data.ErrorCode))
: getNotification(Notification.CantSaveFilters)
);
}
},
script
);
}
return true;
}
deleteFilter(filter) {
this.script().filters.remove(filter);
delegateRunOnDestroy(filter);
}
addFilter() {
/* this = SieveScriptModel */
const filter = new FilterModel();
filter.generateID();
showScreenPopup(require('View/Popup/Filter'), [
filter,
() => {
this.filters.push(filter);
},
false
]);
}
editFilter(filter) {
const clonedFilter = filter.cloneSelf();
showScreenPopup(require('View/Popup/Filter'), [
clonedFilter,
() => {
const script = this.script(),
filters = script.filters(),
index = filters.indexOf(filter);
if (-1 < index) {
delegateRunOnDestroy(filters[index]);
filters[index] = clonedFilter;
script.filters(filters);
}
},
true
]);
}
toggleFiltersRaw() {
if (!this.rawActive()) {
let script = this.script(),
changed = script.hasChanges();
script.body(script.filtersToRaw());
script.hasChanges(changed);
}
this.rawActive(!this.rawActive());
}
onBuild(oDom) {
oDom.addEventListener('click', event => {
const el = event.target.closestWithin('.filter-item .e-action', oDom),
filter = el && ko.dataFor(el);
filter && this.editFilter(filter);
});
}
onShow(oScript) {
this.script(oScript);
this.rawActive(!oScript.allowFilters());
this.allowToggle(oScript.allowFilters());
this.saveError(false);
}
onShowWithDelay() {
// Sometimes not everything is translated, try again
i18nToNodes(this.viewModelDom);
}
}
export { SieveScriptPopupView, SieveScriptPopupView as default };