snappymail/dev/Screen/AbstractSettings.js

192 lines
5.2 KiB
JavaScript
Raw Normal View History

2016-07-07 07:11:13 +08:00
import ko from 'ko';
2019-07-05 03:19:24 +08:00
import { VIEW_MODELS } from 'Common/Globals';
import { pString } from 'Common/Utils';
2019-07-05 03:19:24 +08:00
import { settings } from 'Common/Links';
2016-07-07 07:11:13 +08:00
2019-07-05 03:19:24 +08:00
import { setHash } from 'Knoin/Knoin';
import { AbstractScreen } from 'Knoin/AbstractScreen';
2016-07-07 07:11:13 +08:00
2019-07-05 03:19:24 +08:00
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([]);
this.oCurrentSubScreen = null;
this.oViewModelPlace = null;
this.setupSettings();
}
/**
* @param {Function=} fCallback
*/
setupSettings(fCallback = null) {
2019-07-05 03:19:24 +08:00
if (fCallback) {
2016-07-07 07:11:13 +08:00
fCallback();
}
}
onRoute(subName) {
2019-07-05 03:19:24 +08:00
let settingsScreen = null,
2016-07-07 07:11:13 +08:00
RoutedSettingsViewModel = null,
viewModelDom = null;
2016-07-07 07:11:13 +08:00
RoutedSettingsViewModel = VIEW_MODELS.settings.find(
SettingsViewModel =>
2019-07-05 03:19:24 +08:00
SettingsViewModel && SettingsViewModel.__rlSettingsData && subName === SettingsViewModel.__rlSettingsData.Route
2016-07-07 07:11:13 +08:00
);
2019-07-05 03:19:24 +08:00
if (RoutedSettingsViewModel) {
if (
VIEW_MODELS['settings-removed'].find(
DisabledSettingsViewModel =>
2019-07-05 03:19:24 +08:00
DisabledSettingsViewModel && DisabledSettingsViewModel === RoutedSettingsViewModel
)
) {
2016-07-07 07:11:13 +08:00
RoutedSettingsViewModel = null;
}
2019-07-05 03:19:24 +08:00
if (
RoutedSettingsViewModel &&
VIEW_MODELS['settings-disabled'].find(
DisabledSettingsViewModel =>
2019-07-05 03:19:24 +08:00
DisabledSettingsViewModel && DisabledSettingsViewModel === RoutedSettingsViewModel
)
) {
2016-07-07 07:11:13 +08:00
RoutedSettingsViewModel = null;
}
}
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 {
2020-08-27 21:45:47 +08:00
if (this.oViewModelPlace) {
settingsScreen = new RoutedSettingsViewModel();
2016-07-07 07:11:13 +08:00
2020-08-27 21:45:47 +08:00
viewModelDom = jQuery('<div></div>');
viewModelDom[0].classList.add('rl-settings-view-model');
viewModelDom[0].hidden = true;
this.oViewModelPlace.append(viewModelDom[0]);
2016-07-07 07:11:13 +08:00
2020-08-27 21:45:47 +08:00
settingsScreen.viewModelDom = viewModelDom[0];
2016-07-07 07:11:13 +08:00
settingsScreen.__rlSettingsData = RoutedSettingsViewModel.__rlSettingsData;
2016-07-07 07:11:13 +08:00
2020-08-27 21:45:47 +08:00
RoutedSettingsViewModel.__dom = viewModelDom[0];
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
const tmpl = { name: RoutedSettingsViewModel.__rlSettingsData.Template };
ko.applyBindingAccessorsToNode(
viewModelDom[0],
{
i18nInit: true,
2019-07-05 03:19:24 +08:00
template: () => tmpl
},
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) {
const o = this;
setTimeout(() => {
2016-07-07 07:11:13 +08:00
// hide
if (o.oCurrentSubScreen) {
o.oCurrentSubScreen.onHide && o.oCurrentSubScreen.onHide();
2020-08-27 21:45:47 +08:00
o.oCurrentSubScreen.viewModelDom.hidden = true;
2016-07-07 07:11:13 +08:00
}
// --
o.oCurrentSubScreen = settingsScreen;
2016-07-07 07:11:13 +08:00
// show
if (o.oCurrentSubScreen) {
o.oCurrentSubScreen.onBeforeShow && o.oCurrentSubScreen.onBeforeShow();
2020-08-27 21:45:47 +08:00
o.oCurrentSubScreen.viewModelDom.hidden = false;
o.oCurrentSubScreen.onShow && o.oCurrentSubScreen.onShow();
o.oCurrentSubScreen.onShowWithDelay && setTimeout(() => o.oCurrentSubScreen.onShowWithDelay(), 200);
2016-07-07 07:11:13 +08:00
o.menu().forEach(item => {
2019-07-05 03:19:24 +08:00
item.selected(
settingsScreen &&
settingsScreen.__rlSettingsData &&
item.route === settingsScreen.__rlSettingsData.Route
);
2016-07-07 07:11:13 +08:00
});
2020-08-27 21:45:47 +08:00
document.querySelector('#rl-content .b-settings .b-content').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 {
2016-07-08 07:22:58 +08:00
setHash(settings(), false, true);
2016-07-07 07:11:13 +08:00
}
}
onHide() {
2019-07-05 03:19:24 +08:00
if (this.oCurrentSubScreen && this.oCurrentSubScreen.viewModelDom) {
this.oCurrentSubScreen.onHide && this.oCurrentSubScreen.onHide();
2020-08-27 21:45:47 +08:00
this.oCurrentSubScreen.viewModelDom.hidden = true;
2016-07-07 07:11:13 +08:00
}
}
onBuild() {
VIEW_MODELS.settings.forEach(SettingsViewModel => {
2019-07-05 03:19:24 +08:00
if (
SettingsViewModel &&
SettingsViewModel.__rlSettingsData &&
!VIEW_MODELS['settings-removed'].find(
RemoveSettingsViewModel => RemoveSettingsViewModel && RemoveSettingsViewModel === SettingsViewModel
2019-07-05 03:19:24 +08:00
)
) {
2016-07-07 07:11:13 +08:00
this.menu.push({
route: SettingsViewModel.__rlSettingsData.Route,
label: SettingsViewModel.__rlSettingsData.Label,
selected: ko.observable(false),
disabled: !!VIEW_MODELS['settings-disabled'].find(
DisabledSettingsViewModel => DisabledSettingsViewModel && DisabledSettingsViewModel === SettingsViewModel
2019-07-05 03:19:24 +08:00
)
2016-07-07 07:11:13 +08:00
});
}
});
2020-08-27 21:45:47 +08:00
this.oViewModelPlace = document.getElementById('rl-settings-subscreen');
2016-07-07 07:11:13 +08:00
}
routes() {
const DefaultViewModel = VIEW_MODELS.settings.find(
SettingsViewModel =>
2019-07-05 03:19:24 +08:00
SettingsViewModel && SettingsViewModel.__rlSettingsData && SettingsViewModel.__rlSettingsData.IsDefault
2017-08-07 23:09:14 +08:00
),
2019-07-05 03:19:24 +08:00
defaultRoute =
DefaultViewModel && DefaultViewModel.__rlSettingsData ? DefaultViewModel.__rlSettingsData.Route : 'general',
rules = {
2016-07-07 07:11:13 +08:00
subname: /^(.*)$/,
normalize_: (rquest, vals) => {
vals.subname = undefined === 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
}
}
2019-07-05 03:19:24 +08:00
export { AbstractSettingsScreen, AbstractSettingsScreen as default };