snappymail/dev/Screen/AbstractSettings.js

145 lines
3.6 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';
2021-12-28 21:48:20 +08:00
import { createElement, elementById } from 'Common/Globals';
2016-07-07 07:11:13 +08:00
2019-07-05 03:19:24 +08:00
import { AbstractScreen } from 'Knoin/AbstractScreen';
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.__rlSettingsData.route
);
2016-07-07 07:11:13 +08:00
2019-07-05 03:19:24 +08:00
if (RoutedSettingsViewModel) {
if (RoutedSettingsViewModel.__builded && RoutedSettingsViewModel.__vm) {
settingsScreen = RoutedSettingsViewModel.__vm;
2019-07-05 03:19:24 +08:00
} else {
const vmPlace = elementById('rl-settings-subscreen');
if (vmPlace) {
2021-12-28 21:48:20 +08:00
viewModelDom = createElement('div',{
id: 'V-Settings-' + RoutedSettingsViewModel.name.replace(/(User|Admin)Settings/,''),
hidden: ''
})
vmPlace.append(viewModelDom);
2016-07-07 07:11:13 +08:00
settingsScreen = new RoutedSettingsViewModel();
settingsScreen.viewModelDom = viewModelDom;
2016-07-07 07:11:13 +08:00
RoutedSettingsViewModel.__dom = viewModelDom;
2016-07-07 07:11:13 +08:00
RoutedSettingsViewModel.__builded = true;
RoutedSettingsViewModel.__vm = settingsScreen;
2016-07-07 07:11:13 +08:00
2019-07-05 03:19:24 +08:00
ko.applyBindingAccessorsToNode(
viewModelDom,
2019-07-05 03:19:24 +08:00
{
i18nInit: true,
template: () => ({ name: RoutedSettingsViewModel.__rlSettingsData.template })
2019-07-05 03:19:24 +08:00
},
settingsScreen
);
2016-07-07 07:11:13 +08:00
settingsScreen.onBuild && settingsScreen.onBuild(viewModelDom);
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
settingsScreen.onBeforeShow && settingsScreen.onBeforeShow();
settingsScreen.viewModelDom.hidden = false;
settingsScreen.onShow && settingsScreen.onShow();
this.menu.forEach(item => {
item.selected(
item.route === RoutedSettingsViewModel.__rlSettingsData.route
);
});
elementById('rl-settings-subscreen').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 {
rl.route.setHash(settings(), false, true);
2016-07-07 07:11:13 +08:00
}
}
onHide() {
let subScreen = this.oCurrentSubScreen;
if (subScreen) {
subScreen.onHide && subScreen.onHide();
subScreen.viewModelDom.hidden = true;
2016-07-07 07:11:13 +08:00
}
}
onBuild() {
VIEW_MODELS.forEach(SettingsViewModel => this.menu.push(SettingsViewModel.__rlSettingsData));
2016-07-07 07:11:13 +08:00
}
routes() {
2021-01-26 18:46:30 +08:00
const DefaultViewModel = VIEW_MODELS.find(
SettingsViewModel => SettingsViewModel.__rlSettingsData.isDefault
2017-08-07 23:09:14 +08:00
),
2019-07-05 03:19:24 +08:00
defaultRoute =
DefaultViewModel ? DefaultViewModel.__rlSettingsData.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) {
SettingsViewModelClass.__rlSettingsData = {
label: labelName,
route: route,
selected: ko.observable(false),
template: template,
isDefault: !!isDefault
2021-01-26 18:46:30 +08:00
};
VIEW_MODELS.push(SettingsViewModelClass);
}