2021-01-18 23:47:10 +08:00
|
|
|
import ko from 'ko';
|
|
|
|
|
2021-03-16 17:59:47 +08:00
|
|
|
import { getNotification, i18nToNodes } from 'Common/Translator';
|
2022-02-17 16:36:29 +08:00
|
|
|
import { addObservablesTo } from 'External/ko';
|
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-02-17 21:40:21 +08:00
|
|
|
import { SieveUserStore } from 'Stores/User/Sieve';
|
2021-01-18 23:47:10 +08:00
|
|
|
|
2022-02-21 22:36:34 +08:00
|
|
|
import { showScreenPopup } from 'Knoin/Knoin';
|
2021-01-24 17:25:23 +08:00
|
|
|
import { AbstractViewPopup } from 'Knoin/AbstractViews';
|
|
|
|
|
2021-01-26 05:00:13 +08:00
|
|
|
import { FilterPopupView } from 'View/Popup/Filter';
|
|
|
|
|
2022-03-09 19:33:31 +08:00
|
|
|
//import { parseScript } from 'Sieve/Parser';
|
|
|
|
|
2022-02-24 21:01:41 +08:00
|
|
|
export class SieveScriptPopupView extends AbstractViewPopup {
|
2021-01-18 23:47:10 +08:00
|
|
|
constructor() {
|
2021-01-24 17:25:23 +08:00
|
|
|
super('SieveScript');
|
2021-01-18 23:47:10 +08:00
|
|
|
|
2021-03-16 17:59:47 +08:00
|
|
|
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-02-17 21:40:21 +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
|
|
|
|
2022-01-28 06:07:34 +08:00
|
|
|
this.filterForDeletion = ko.observable(null).askDeleteHelper();
|
2021-01-18 23:47:10 +08:00
|
|
|
}
|
|
|
|
|
2022-02-21 22:36:34 +08:00
|
|
|
saveScript() {
|
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()) {
|
2022-02-26 08:02:12 +08:00
|
|
|
return;
|
2021-01-18 23:47:10 +08:00
|
|
|
}
|
|
|
|
|
2021-02-17 21:40:21 +08:00
|
|
|
if (!script.exists() && SieveUserStore.scripts.find(item => item.name() === script.name())) {
|
2021-01-19 23:59:43 +08:00
|
|
|
script.nameError(true);
|
2022-02-26 08:02:12 +08:00
|
|
|
return;
|
2021-01-19 23:59:43 +08:00
|
|
|
}
|
|
|
|
|
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-12-03 06:15:24 +08:00
|
|
|
Remote.request('FiltersScriptSave',
|
2021-03-16 16:46:23 +08:00
|
|
|
(iError, data) => {
|
2021-01-20 19:04:27 +08:00
|
|
|
self.saving = false;
|
2021-01-18 23:47:10 +08:00
|
|
|
|
2021-03-18 19:33:13 +08:00
|
|
|
if (iError) {
|
|
|
|
self.saveError(true);
|
|
|
|
self.saveErrorText((data && data.ErrorMessageAdditional) || getNotification(iError));
|
|
|
|
} else {
|
2021-02-17 21:40:21 +08:00
|
|
|
script.exists() || SieveUserStore.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
|
|
|
}
|
|
|
|
},
|
2021-12-03 06:15:24 +08:00
|
|
|
script.toJson()
|
2021-01-18 23:47:10 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
deleteFilter(filter) {
|
2021-01-19 23:01:30 +08:00
|
|
|
this.script().filters.remove(filter);
|
2021-01-18 23:47:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
2021-01-26 05:00:13 +08:00
|
|
|
showScreenPopup(FilterPopupView, [
|
2021-01-18 23:47:10 +08:00
|
|
|
filter,
|
2022-02-26 08:02:12 +08:00
|
|
|
() => this.filters.push(filter)
|
2021-01-18 23:47:10 +08:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
editFilter(filter) {
|
|
|
|
const clonedFilter = filter.cloneSelf();
|
2021-01-26 05:00:13 +08:00
|
|
|
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
|
|
|
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() {
|
2022-02-26 08:02:12 +08:00
|
|
|
let script = this.script(), notRaw = !this.rawActive();
|
|
|
|
if (notRaw) {
|
2021-01-20 17:10:59 +08:00
|
|
|
script.body(script.filtersToRaw());
|
2022-02-26 08:02:12 +08:00
|
|
|
script.hasChanges(script.hasChanges());
|
2021-01-20 17:10:59 +08:00
|
|
|
}
|
2022-02-26 08:02:12 +08:00
|
|
|
this.rawActive(notRaw);
|
2021-01-20 17:10:59 +08:00
|
|
|
}
|
|
|
|
|
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) {
|
2022-02-26 08:02:12 +08:00
|
|
|
let raw = !oScript.allowFilters();
|
2021-01-19 23:01:30 +08:00
|
|
|
this.script(oScript);
|
2022-02-26 08:02:12 +08:00
|
|
|
this.rawActive(raw);
|
|
|
|
this.allowToggle(!raw);
|
2021-01-19 23:01:30 +08:00
|
|
|
this.saveError(false);
|
2022-03-09 19:33:31 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
// TODO: Sieve GUI
|
|
|
|
let tree = parseScript(oScript.body(), oScript.name());
|
|
|
|
console.dir(tree);
|
|
|
|
console.log(tree.join('\r\n'));
|
|
|
|
*/
|
2021-01-18 23:47:10 +08:00
|
|
|
}
|
|
|
|
|
2022-02-25 05:40:17 +08:00
|
|
|
afterShow() {
|
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
|
|
|
}
|
|
|
|
}
|