Get a working Sieve scripts manager

This commit is contained in:
djmaze 2021-01-19 16:01:30 +01:00
parent a31834458b
commit e3125ebfff
24 changed files with 439 additions and 304 deletions

View file

@ -3,12 +3,13 @@ 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 FilterStore from 'Stores/User/Filter';
import FilterModel from 'Model/Filter';
import SieveStore from 'Stores/User/Sieve';
import { popup, showScreenPopup, command } from 'Knoin/Knoin';
import { popup, showScreenPopup/*, command*/ } from 'Knoin/Knoin';
import { AbstractViewNext } from 'Knoin/AbstractViewNext';
@popup({
@ -19,78 +20,46 @@ class SieveScriptPopupView extends AbstractViewNext {
constructor() {
super();
// this.filters = FilterStore.filters;
this.filters = ko.observableArray([]);
this.filters.loading = ko.observable(false).extend({ throttle: 200 });
this.filters.saving = ko.observable(false).extend({ throttle: 200 });
this.modules = FilterStore.modules;
this.script = {
filters: FilterStore.filters,
saving: () => FilterStore.filters.saving()
};
ko.addObservablesTo(this, {
isNew: true,
serverError: false,
serverErrorDesc: '',
saveError: false,
saveErrorText: '',
haveChanges: false,
filterRaw: ''
rawActive: false,
script: null
});
this.serverError.subscribe(value => value || this.serverErrorDesc(''), this);
// this.filterRaw = FilterStore.raw;
this.filterRaw.capa = FilterStore.capa;
this.filterRaw.active = ko.observable(false);
this.filterRaw.allow = ko.observable(false);
this.filterRaw.error = ko.observable(false);
this.sieveCapabilities = SieveStore.capa.join(' ');
this.saving = false;
this.filterForDeletion = ko.observable(null).deleteAccessHelper();
this.filters.subscribe(() => this.haveChanges(true));
this.filterRaw.subscribe(() => {
this.haveChanges(true);
this.filterRaw.error(false);
});
this.haveChanges.subscribe(() => this.saveErrorText(''));
this.filterRaw.active.subscribe(() => {
this.haveChanges(true);
this.filterRaw.error(false);
});
}
@command((self) => self.haveChanges())
// @command()
saveScriptCommand() {
if (!this.filters.saving()) {
if (this.filterRaw.active() && !this.filterRaw().trim()) {
this.filterRaw.error(true);
let script = this.script();
if (!this.saving/* && script.hasChanges()*/) {
if (!script.verify()) {
return false;
}
this.filters.saving(true);
this.saveErrorText('');
this.saving = true;
this.saveError(false);
Remote.filtersSave(
Remote.filtersScriptSave(
(result, data) => {
this.filters.saving(false);
this.saving = false;
if (StorageResultType.Success === result && data && data.Result) {
this.haveChanges(false);
} else if (data && data.ErrorCode) {
this.saveErrorText(data.ErrorMessageAdditional || getNotification(data.ErrorCode));
script.hasChanges(false);
} else {
this.saveErrorText(getNotification(Notification.CantSaveFilters));
this.saveError(true);
this.saveErrorText((data && data.ErrorCode)
? (data.ErrorMessageAdditional || getNotification(data.ErrorCode))
: getNotification(Notification.CantSaveFilters)
);
}
},
this.filters(),
this.filterRaw(),
this.filterRaw.active()
script
);
}
@ -98,19 +67,18 @@ class SieveScriptPopupView extends AbstractViewNext {
}
deleteFilter(filter) {
this.filters.remove(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);
this.filterRaw.active(false);
},
false
]);
@ -118,19 +86,16 @@ class SieveScriptPopupView extends AbstractViewNext {
editFilter(filter) {
const clonedFilter = filter.cloneSelf();
showScreenPopup(require('View/Popup/Filter'), [
clonedFilter,
() => {
const filters = this.filters(),
const script = this.script(),
filters = script.filters(),
index = filters.indexOf(filter);
if (-1 < index && filters[index]) {
if (-1 < index) {
delegateRunOnDestroy(filters[index]);
filters[index] = clonedFilter;
this.filters(filters);
this.haveChanges(true);
script.filters(filters);
}
},
true
@ -141,36 +106,22 @@ class SieveScriptPopupView extends AbstractViewNext {
oDom.addEventListener('click', event => {
const el = event.target.closestWithin('.filter-item .e-action', oDom),
filter = el && ko.dataFor(el);
filter && this.editFilter(ko.dataFor(el));
filter && this.editFilter(filter);
});
}
onShow(oScript, fTrueCallback, bEdit) {
this.clearPopup();
this.fTrueCallback = fTrueCallback;
this.filters(oScript.filters());
this.filterRaw(oScript.body());
this.filterRaw.active(!oScript.allowFilters());
this.filterRaw.error(false);
this.script(oScript);
this.rawActive(!oScript.allowFilters());
this.isNew(!bEdit);
if (!bEdit && oScript) {
// oScript.nameFocused(true);
}
this.saveError(false);
}
onShowWithDelay() {
// Sometimes not everything is translated, try again
i18nToNodes(this.viewModelDom);
}
clearPopup() {
this.isNew(true);
this.fTrueCallback = null;
this.filters([]);
}
}
export { SieveScriptPopupView, SieveScriptPopupView as default };