snappymail/dev/View/Popup/SieveScript.js

149 lines
3.4 KiB
JavaScript
Raw Normal View History

2021-01-18 23:47:10 +08:00
import ko from 'ko';
import { delegateRunOnDestroy } from 'Common/UtilsUser';
import { StorageResultType, Notification } from 'Common/Enums';
import { getNotification } from 'Common/Translator';
2021-01-19 23:01:30 +08:00
import { i18nToNodes } from 'Common/Translator';
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';
2021-01-19 23:01:30 +08:00
import SieveStore from 'Stores/User/Sieve';
2021-01-18 23:47:10 +08:00
2021-01-19 23:01:30 +08:00
import { popup, showScreenPopup/*, command*/ } from 'Knoin/Knoin';
2021-01-18 23:47:10 +08:00
import { AbstractViewNext } from 'Knoin/AbstractViewNext';
@popup({
name: 'View/Popup/SieveScript',
templateID: 'PopupsSieveScript'
})
class SieveScriptPopupView extends AbstractViewNext {
constructor() {
super();
ko.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
});
2021-01-19 23:01:30 +08:00
this.sieveCapabilities = SieveStore.capa.join(' ');
this.saving = false;
2021-01-18 23:47:10 +08:00
this.filterForDeletion = ko.observable(null).deleteAccessHelper();
}
2021-01-19 23:01:30 +08:00
// @command()
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() && SieveStore.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
2021-01-19 23:01:30 +08:00
Remote.filtersScriptSave(
2021-01-18 23:47:10 +08:00
(result, data) => {
self.saving = false;
2021-01-18 23:47:10 +08:00
if (StorageResultType.Success === result && data && data.Result) {
script.exists() || SieveStore.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
} else {
self.saveError(true);
self.saveErrorText((data && data.ErrorCode)
2021-01-19 23:01:30 +08:00
? (data.ErrorMessageAdditional || getNotification(data.ErrorCode))
: getNotification(Notification.CantSaveFilters)
);
2021-01-18 23:47:10 +08:00
}
},
2021-01-19 23:01:30 +08:00
script
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(require('View/Popup/Filter'), [
filter,
() => {
this.filters.push(filter);
},
false
]);
}
editFilter(filter) {
const clonedFilter = filter.cloneSelf();
showScreenPopup(require('View/Popup/Filter'), [
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 => {
const el = event.target.closestWithin('.filter-item .e-action', oDom),
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 };