mirror of
https://github.com/the-djmaze/snappymail.git
synced 2026-07-08 22:18:28 +03:00
Split Sieve/Filters code from app.js so that i can work on the new Sieve GUI
This commit is contained in:
parent
ec6a79d9d6
commit
d6dc4d291c
23 changed files with 460 additions and 490 deletions
|
|
@ -1,146 +0,0 @@
|
|||
import ko from 'ko';
|
||||
import { koComputable } from 'External/ko';
|
||||
|
||||
import { FilterAction } from 'Model/Filter';
|
||||
import { FilterConditionField, FilterConditionType } from 'Model/FilterCondition';
|
||||
import { SettingsGet } from 'Common/Globals';
|
||||
import { defaultOptionsAfterRender } from 'Common/Utils';
|
||||
import { i18n, initOnStartOrLangChange } from 'Common/Translator';
|
||||
|
||||
import { SieveUserStore } from 'Stores/User/Sieve';
|
||||
|
||||
import { AbstractViewPopup } from 'Knoin/AbstractViews';
|
||||
|
||||
import { folderListOptionsBuilder } from 'Common/Folders';
|
||||
|
||||
export class FilterPopupView extends AbstractViewPopup {
|
||||
constructor() {
|
||||
super('Filter');
|
||||
|
||||
this.addObservables({
|
||||
isNew: true,
|
||||
filter: null,
|
||||
allowMarkAsRead: false,
|
||||
selectedFolderValue: ''
|
||||
});
|
||||
|
||||
this.defaultOptionsAfterRender = defaultOptionsAfterRender;
|
||||
this.folderSelectList = koComputable(() =>
|
||||
folderListOptionsBuilder(
|
||||
[SettingsGet('SieveAllowFileintoInbox') ? '' : 'INBOX'],
|
||||
[['', '']],
|
||||
item => item ? item.localName() : ''
|
||||
)
|
||||
);
|
||||
|
||||
this.selectedFolderValue.subscribe(() => this.filter().actionValueError(false));
|
||||
|
||||
['actionTypeOptions','fieldOptions','typeOptions','typeOptionsSize','typeOptionsBody'].forEach(
|
||||
key => this[key] = ko.observableArray()
|
||||
);
|
||||
|
||||
initOnStartOrLangChange(this.populateOptions.bind(this));
|
||||
|
||||
SieveUserStore.capa.subscribe(this.populateOptions, this);
|
||||
}
|
||||
|
||||
saveFilter() {
|
||||
if (FilterAction.MoveTo === this.filter().actionType()) {
|
||||
this.filter().actionValue(this.selectedFolderValue());
|
||||
}
|
||||
|
||||
if (this.filter().verify()) {
|
||||
this.fTrueCallback();
|
||||
this.close();
|
||||
}
|
||||
}
|
||||
|
||||
populateOptions() {
|
||||
this.actionTypeOptions([]);
|
||||
|
||||
let i18nFilter = key => i18n('POPUPS_FILTER/SELECT_' + key);
|
||||
|
||||
this.fieldOptions([
|
||||
{ id: FilterConditionField.From, name: i18n('GLOBAL/FROM') },
|
||||
{ id: FilterConditionField.Recipient, name: i18nFilter('FIELD_RECIPIENTS') },
|
||||
{ id: FilterConditionField.Subject, name: i18n('GLOBAL/SUBJECT') },
|
||||
{ id: FilterConditionField.Size, name: i18nFilter('FIELD_SIZE') },
|
||||
{ id: FilterConditionField.Header, name: i18nFilter('FIELD_HEADER') }
|
||||
]);
|
||||
|
||||
this.typeOptions([
|
||||
{ id: FilterConditionType.Contains, name: i18nFilter('TYPE_CONTAINS') },
|
||||
{ id: FilterConditionType.NotContains, name: i18nFilter('TYPE_NOT_CONTAINS') },
|
||||
{ id: FilterConditionType.EqualTo, name: i18nFilter('TYPE_EQUAL_TO') },
|
||||
{ id: FilterConditionType.NotEqualTo, name: i18nFilter('TYPE_NOT_EQUAL_TO') }
|
||||
]);
|
||||
|
||||
// this.actionTypeOptions.push({id: FilterAction.None,
|
||||
// name: i18n('GLOBAL/NONE')});
|
||||
const modules = SieveUserStore.capa;
|
||||
if (modules) {
|
||||
this.allowMarkAsRead(modules.includes('imap4flags'));
|
||||
|
||||
if (modules.includes('fileinto')) {
|
||||
this.actionTypeOptions.push({
|
||||
id: FilterAction.MoveTo,
|
||||
name: i18nFilter('ACTION_MOVE_TO')
|
||||
});
|
||||
this.actionTypeOptions.push({
|
||||
id: FilterAction.Forward,
|
||||
name: i18nFilter('ACTION_FORWARD_TO')
|
||||
});
|
||||
}
|
||||
|
||||
if (modules.includes('reject')) {
|
||||
this.actionTypeOptions.push({ id: FilterAction.Reject, name: i18nFilter('ACTION_REJECT') });
|
||||
}
|
||||
|
||||
if (modules.includes('vacation')) {
|
||||
this.actionTypeOptions.push({
|
||||
id: FilterAction.Vacation,
|
||||
name: i18nFilter('ACTION_VACATION_MESSAGE')
|
||||
});
|
||||
}
|
||||
|
||||
if (modules.includes('body')) {
|
||||
this.fieldOptions.push({ id: FilterConditionField.Body, name: i18nFilter('FIELD_BODY') });
|
||||
}
|
||||
|
||||
if (modules.includes('regex')) {
|
||||
this.typeOptions.push({ id: FilterConditionType.Regex, name: 'Regex' });
|
||||
}
|
||||
}
|
||||
|
||||
this.actionTypeOptions.push({ id: FilterAction.Discard, name: i18nFilter('ACTION_DISCARD') });
|
||||
|
||||
this.typeOptionsSize([
|
||||
{ id: FilterConditionType.Over, name: i18nFilter('TYPE_OVER') },
|
||||
{ id: FilterConditionType.Under, name: i18nFilter('TYPE_UNDER') }
|
||||
]);
|
||||
|
||||
this.typeOptionsBody([
|
||||
{ id: FilterConditionType.Text, name: i18nFilter('TYPE_TEXT') },
|
||||
{ id: FilterConditionType.Raw, name: i18nFilter('TYPE_RAW') }
|
||||
]);
|
||||
}
|
||||
|
||||
removeCondition(oConditionToDelete) {
|
||||
this.filter().removeCondition(oConditionToDelete);
|
||||
}
|
||||
|
||||
onShow(oFilter, fTrueCallback, bEdit) {
|
||||
this.isNew(!bEdit);
|
||||
|
||||
this.fTrueCallback = fTrueCallback;
|
||||
this.filter(oFilter);
|
||||
|
||||
this.selectedFolderValue(oFilter.actionValue());
|
||||
|
||||
bEdit || oFilter.nameFocused(true);
|
||||
}
|
||||
|
||||
afterShow() {
|
||||
this.isNew() && this.filter().nameFocused(true);
|
||||
}
|
||||
}
|
||||
|
|
@ -36,7 +36,7 @@ export class LanguagesPopupView extends AbstractViewPopup {
|
|||
this.languages().forEach(item => item.selected(item.key === currentLang));
|
||||
}
|
||||
|
||||
onBeforeShow() {
|
||||
beforeShow() {
|
||||
this.fLang = null;
|
||||
this.userLanguage('');
|
||||
|
||||
|
|
|
|||
|
|
@ -1,140 +0,0 @@
|
|||
import ko from 'ko';
|
||||
|
||||
import { getNotification, i18nToNodes } from 'Common/Translator';
|
||||
import { addObservablesTo } from 'External/ko';
|
||||
|
||||
import Remote from 'Remote/User/Fetch';
|
||||
import { FilterModel } from 'Model/Filter';
|
||||
import { SieveUserStore } from 'Stores/User/Sieve';
|
||||
|
||||
import { showScreenPopup } from 'Knoin/Knoin';
|
||||
import { AbstractViewPopup } from 'Knoin/AbstractViews';
|
||||
|
||||
import { FilterPopupView } from 'View/Popup/Filter';
|
||||
|
||||
//import { parseScript } from 'Sieve/Parser';
|
||||
|
||||
export class SieveScriptPopupView extends AbstractViewPopup {
|
||||
constructor() {
|
||||
super('SieveScript');
|
||||
|
||||
addObservablesTo(this, {
|
||||
saveError: false,
|
||||
saveErrorText: '',
|
||||
rawActive: false,
|
||||
allowToggle: false,
|
||||
script: null
|
||||
});
|
||||
|
||||
this.sieveCapabilities = SieveUserStore.capa.join(' ');
|
||||
this.saving = false;
|
||||
|
||||
this.filterForDeletion = ko.observable(null).askDeleteHelper();
|
||||
}
|
||||
|
||||
saveScript() {
|
||||
let self = this,
|
||||
script = self.script();
|
||||
if (!self.saving/* && script.hasChanges()*/) {
|
||||
if (!script.verify()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!script.exists() && SieveUserStore.scripts.find(item => item.name() === script.name())) {
|
||||
script.nameError(true);
|
||||
return;
|
||||
}
|
||||
|
||||
self.saving = true;
|
||||
self.saveError(false);
|
||||
|
||||
if (self.allowToggle()) {
|
||||
script.body(script.filtersToRaw());
|
||||
}
|
||||
|
||||
Remote.request('FiltersScriptSave',
|
||||
(iError, data) => {
|
||||
self.saving = false;
|
||||
|
||||
if (iError) {
|
||||
self.saveError(true);
|
||||
self.saveErrorText((data && data.ErrorMessageAdditional) || getNotification(iError));
|
||||
} else {
|
||||
script.exists() || SieveUserStore.scripts.push(script);
|
||||
script.exists(true);
|
||||
script.hasChanges(false);
|
||||
}
|
||||
},
|
||||
script.toJson()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
deleteFilter(filter) {
|
||||
this.script().filters.remove(filter);
|
||||
}
|
||||
|
||||
addFilter() {
|
||||
/* this = SieveScriptModel */
|
||||
const filter = new FilterModel();
|
||||
filter.generateID();
|
||||
showScreenPopup(FilterPopupView, [
|
||||
filter,
|
||||
() => this.filters.push(filter)
|
||||
]);
|
||||
}
|
||||
|
||||
editFilter(filter) {
|
||||
const clonedFilter = filter.cloneSelf();
|
||||
showScreenPopup(FilterPopupView, [
|
||||
clonedFilter,
|
||||
() => {
|
||||
const script = this.script(),
|
||||
filters = script.filters(),
|
||||
index = filters.indexOf(filter);
|
||||
if (-1 < index) {
|
||||
filters[index] = clonedFilter;
|
||||
script.filters(filters);
|
||||
}
|
||||
},
|
||||
true
|
||||
]);
|
||||
}
|
||||
|
||||
toggleFiltersRaw() {
|
||||
let script = this.script(), notRaw = !this.rawActive();
|
||||
if (notRaw) {
|
||||
script.body(script.filtersToRaw());
|
||||
script.hasChanges(script.hasChanges());
|
||||
}
|
||||
this.rawActive(notRaw);
|
||||
}
|
||||
|
||||
onBuild(oDom) {
|
||||
oDom.addEventListener('click', event => {
|
||||
const el = event.target.closestWithin('td.e-action', oDom),
|
||||
filter = el && ko.dataFor(el);
|
||||
filter && this.editFilter(filter);
|
||||
});
|
||||
}
|
||||
|
||||
onShow(oScript) {
|
||||
let raw = !oScript.allowFilters();
|
||||
this.script(oScript);
|
||||
this.rawActive(raw);
|
||||
this.allowToggle(!raw);
|
||||
this.saveError(false);
|
||||
|
||||
/*
|
||||
// TODO: Sieve GUI
|
||||
let tree = parseScript(oScript.body(), oScript.name());
|
||||
console.dir(tree);
|
||||
console.log(tree.join('\r\n'));
|
||||
*/
|
||||
}
|
||||
|
||||
afterShow() {
|
||||
// Sometimes not everything is translated, try again
|
||||
i18nToNodes(this.viewModelDom);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue