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,
|
2021-01-19 06:52:15 +08:00
|
|
|
saveErrorText: '',
|
2021-01-19 23:01:30 +08:00
|
|
|
rawActive: false,
|
2021-01-20 18:00:13 +08:00
|
|
|
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() {
|
2021-01-20 19:04:27 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-01-20 00:42:38 +08:00
|
|
|
if (!script.exists() && SieveStore.scripts.find(item => item.name() === script.name())) {
|
2021-01-19 23:59:43 +08:00
|
|
|
script.nameError(true);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-01-20 19:04:27 +08:00
|
|
|
self.saving = true;
|
|
|
|
self.saveError(false);
|
2021-01-21 17:39:19 +08:00
|
|
|
|
|
|
|
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) => {
|
2021-01-20 19:04:27 +08:00
|
|
|
self.saving = false;
|
2021-01-18 23:47:10 +08:00
|
|
|
|
|
|
|
if (StorageResultType.Success === result && data && data.Result) {
|
2021-01-20 22:34:33 +08:00
|
|
|
script.exists() || SieveStore.scripts.push(script);
|
2021-01-20 00:42:38 +08:00
|
|
|
script.exists(true);
|
2021-01-19 23:01:30 +08:00
|
|
|
script.hasChanges(false);
|
2021-01-18 23:47:10 +08:00
|
|
|
} else {
|
2021-01-20 19:04:27 +08:00
|
|
|
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
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2021-01-20 17:10:59 +08:00
|
|
|
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());
|
2021-01-20 18:00:13 +08:00
|
|
|
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 };
|