snappymail/dev/Screen/AbstractSettings.js

152 lines
3.9 KiB
JavaScript
Raw Normal View History

2016-07-07 07:11:13 +08:00
import ko from 'ko';
import { pString } from 'Common/Utils';
2019-07-05 03:19:24 +08:00
import { settings } from 'Common/Links';
import { createElement/*, elementById*/, fireEvent } from 'Common/Globals';
2016-07-07 07:11:13 +08:00
2019-07-05 03:19:24 +08:00
import { AbstractScreen } from 'Knoin/AbstractScreen';
import { i18nToNodes } from 'Common/Translator';
2016-07-07 07:11:13 +08:00
2021-01-26 18:46:30 +08:00
const VIEW_MODELS = [];
2021-01-22 23:32:08 +08:00
export class AbstractSettingsScreen extends AbstractScreen {
2016-07-07 07:11:13 +08:00
/**
* @param {Array} viewModels
*/
2019-07-05 03:19:24 +08:00
constructor(viewModels) {
2016-07-07 07:11:13 +08:00
super('settings', viewModels);
this.menu = ko.observableArray();
2016-07-07 07:11:13 +08:00
this.oCurrentSubScreen = null;
}
onRoute(subName) {
2019-07-05 03:19:24 +08:00
let settingsScreen = null,
viewModelDom = null,
RoutedSettingsViewModel = VIEW_MODELS.find(
SettingsViewModel => subName === SettingsViewModel.route
);
2016-07-07 07:11:13 +08:00
2019-07-05 03:19:24 +08:00
if (RoutedSettingsViewModel) {
2023-02-24 18:45:26 +08:00
// const vmPlace = elementById('V-SettingsPane') || elementById('V-AdminPane);
2023-10-24 23:07:14 +08:00
const vmPlace = this.viewModels[1].__vm.viewModelDom,
SettingsViewModelClass = RoutedSettingsViewModel.vmc;
if (SettingsViewModelClass.__vm) {
settingsScreen = SettingsViewModelClass.__vm;
viewModelDom = settingsScreen.viewModelDom;
} else if (vmPlace) {
viewModelDom = createElement('div',{
id: 'V-Settings-' + SettingsViewModelClass.name.replace(/(User|Admin)Settings/,''),
hidden: ''
})
vmPlace.append(viewModelDom);
settingsScreen = new SettingsViewModelClass();
settingsScreen.viewModelDom = viewModelDom;
settingsScreen.viewModelTemplateID = RoutedSettingsViewModel.template;
SettingsViewModelClass.__vm = settingsScreen;
fireEvent('rl-view-model.create', settingsScreen);
ko.applyBindingAccessorsToNode(
viewModelDom,
{
template: () => ({ name: RoutedSettingsViewModel.template })
},
settingsScreen
);
2022-09-02 17:52:07 +08:00
settingsScreen.onBuild?.(viewModelDom);
fireEvent('rl-view-model', settingsScreen);
2019-07-05 03:19:24 +08:00
} else {
console.log('Cannot find sub settings view model position: SettingsSubScreen');
2016-07-07 07:11:13 +08:00
}
2019-07-05 03:19:24 +08:00
if (settingsScreen) {
setTimeout(() => {
2016-07-07 07:11:13 +08:00
// hide
this.onHide();
2016-07-07 07:11:13 +08:00
// --
this.oCurrentSubScreen = settingsScreen;
2016-07-07 07:11:13 +08:00
// show
2022-09-02 17:52:07 +08:00
settingsScreen.beforeShow?.();
i18nToNodes(viewModelDom);
viewModelDom.hidden = false;
2022-09-02 17:52:07 +08:00
settingsScreen.onShow?.();
this.menu.forEach(item => {
item.selected(
item.route === RoutedSettingsViewModel.route
);
});
(vmPlace || {}).scrollTop = 0;
2016-07-07 07:11:13 +08:00
// --
}, 1);
2016-07-07 07:11:13 +08:00
}
2019-07-05 03:19:24 +08:00
} else {
2022-03-03 23:28:05 +08:00
hasher.replaceHash(settings());
2016-07-07 07:11:13 +08:00
}
}
onHide() {
let subScreen = this.oCurrentSubScreen;
if (subScreen) {
2022-09-02 17:52:07 +08:00
subScreen.onHide?.();
2023-04-24 18:03:03 +08:00
subScreen.viewModelDom.hidden = true;
2016-07-07 07:11:13 +08:00
}
}
onBuild() {
// TODO: issue on account switch
// When current domain has sieve but the new has not, or current has not and the new has
// SettingsViewModel.disabled() || this.menu.push()
VIEW_MODELS.forEach(SettingsViewModel => this.menu.push(SettingsViewModel));
2016-07-07 07:11:13 +08:00
}
routes() {
2021-01-26 18:46:30 +08:00
const DefaultViewModel = VIEW_MODELS.find(
SettingsViewModel => SettingsViewModel.isDefault
2017-08-07 23:09:14 +08:00
),
defaultRoute = DefaultViewModel?.route || 'general',
rules = {
2016-07-07 07:11:13 +08:00
subname: /^(.*)$/,
normalize_: (rquest, vals) => {
2021-10-19 17:31:44 +08:00
vals.subname = null == vals.subname ? defaultRoute : pString(vals.subname);
return [vals.subname];
2016-07-07 07:11:13 +08:00
}
};
2019-12-25 03:05:46 +08:00
return [
['{subname}/', rules],
['{subname}', rules],
['', rules]
];
2016-07-07 07:11:13 +08:00
}
}
2021-01-26 18:46:30 +08:00
/**
* @param {Function} SettingsViewModelClass
* @param {string} template
* @param {string} labelName
* @param {string} route
* @param {boolean=} isDefault = false
* @returns {void}
*/
export function settingsAddViewModel(SettingsViewModelClass, template, labelName, route, isDefault = false) {
let name = SettingsViewModelClass.name.replace(/(User|Admin)Settings/, '');
VIEW_MODELS.push({
vmc: SettingsViewModelClass,
label: labelName || 'SETTINGS_LABELS/' + name.toUpperCase(),
route: route || name.toLowerCase(),
selected: ko.observable(false),
template: template || SettingsViewModelClass.name,
isDefault: !!isDefault
});
2021-01-26 18:46:30 +08:00
}