2016-07-07 07:11:13 +08:00
|
|
|
import ko from 'ko';
|
|
|
|
|
2020-08-19 18:34:05 +08:00
|
|
|
import { pString } from 'Common/Utils';
|
2019-07-05 03:19:24 +08:00
|
|
|
import { settings } from 'Common/Links';
|
2023-05-09 18:31:29 +08:00
|
|
|
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';
|
2022-04-05 16:31:55 +08:00
|
|
|
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);
|
|
|
|
|
2021-01-22 19:23:20 +08:00
|
|
|
this.menu = ko.observableArray();
|
2016-07-07 07:11:13 +08:00
|
|
|
|
|
|
|
this.oCurrentSubScreen = null;
|
|
|
|
}
|
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
onRoute(subName) {
|
2019-07-05 03:19:24 +08:00
|
|
|
let settingsScreen = null,
|
2021-09-23 02:17:44 +08:00
|
|
|
viewModelDom = null,
|
|
|
|
RoutedSettingsViewModel = VIEW_MODELS.find(
|
2023-04-24 18:01:09 +08:00
|
|
|
SettingsViewModel => subName === SettingsViewModel.route
|
2021-09-23 02:17:44 +08:00
|
|
|
);
|
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,
|
2023-04-24 18:01:09 +08:00
|
|
|
SettingsViewModelClass = RoutedSettingsViewModel.vmc;
|
|
|
|
if (SettingsViewModelClass.__vm) {
|
|
|
|
settingsScreen = SettingsViewModelClass.__vm;
|
|
|
|
viewModelDom = settingsScreen.viewModelDom;
|
2022-04-05 16:31:55 +08:00
|
|
|
} else if (vmPlace) {
|
|
|
|
viewModelDom = createElement('div',{
|
2023-04-24 18:01:09 +08:00
|
|
|
id: 'V-Settings-' + SettingsViewModelClass.name.replace(/(User|Admin)Settings/,''),
|
2022-04-05 16:31:55 +08:00
|
|
|
hidden: ''
|
|
|
|
})
|
|
|
|
vmPlace.append(viewModelDom);
|
|
|
|
|
2023-04-24 18:01:09 +08:00
|
|
|
settingsScreen = new SettingsViewModelClass();
|
2022-04-05 16:31:55 +08:00
|
|
|
settingsScreen.viewModelDom = viewModelDom;
|
2023-05-09 18:31:29 +08:00
|
|
|
settingsScreen.viewModelTemplateID = RoutedSettingsViewModel.template;
|
2022-04-05 16:31:55 +08:00
|
|
|
|
2023-04-24 18:01:09 +08:00
|
|
|
SettingsViewModelClass.__vm = settingsScreen;
|
2022-04-05 16:31:55 +08:00
|
|
|
|
2023-05-09 18:31:29 +08:00
|
|
|
fireEvent('rl-view-model.create', settingsScreen);
|
|
|
|
|
2022-04-05 16:31:55 +08:00
|
|
|
ko.applyBindingAccessorsToNode(
|
|
|
|
viewModelDom,
|
|
|
|
{
|
2023-04-24 18:01:09 +08:00
|
|
|
template: () => ({ name: RoutedSettingsViewModel.template })
|
2022-04-05 16:31:55 +08:00
|
|
|
},
|
|
|
|
settingsScreen
|
|
|
|
);
|
|
|
|
|
2022-09-02 17:52:07 +08:00
|
|
|
settingsScreen.onBuild?.(viewModelDom);
|
2023-05-09 18:31:29 +08:00
|
|
|
|
|
|
|
fireEvent('rl-view-model', settingsScreen);
|
2019-07-05 03:19:24 +08:00
|
|
|
} else {
|
2022-04-05 16:31:55 +08:00
|
|
|
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) {
|
2020-07-30 03:49:41 +08:00
|
|
|
setTimeout(() => {
|
2016-07-07 07:11:13 +08:00
|
|
|
// hide
|
2021-09-23 02:17:44 +08:00
|
|
|
this.onHide();
|
2016-07-07 07:11:13 +08:00
|
|
|
// --
|
|
|
|
|
2021-09-23 02:17:44 +08:00
|
|
|
this.oCurrentSubScreen = settingsScreen;
|
2016-07-07 07:11:13 +08:00
|
|
|
|
|
|
|
// show
|
2022-09-02 17:52:07 +08:00
|
|
|
settingsScreen.beforeShow?.();
|
2023-04-24 18:01:09 +08:00
|
|
|
i18nToNodes(viewModelDom);
|
|
|
|
viewModelDom.hidden = false;
|
2022-09-02 17:52:07 +08:00
|
|
|
settingsScreen.onShow?.();
|
2021-09-23 02:17:44 +08:00
|
|
|
|
|
|
|
this.menu.forEach(item => {
|
|
|
|
item.selected(
|
2023-04-24 18:01:09 +08:00
|
|
|
item.route === RoutedSettingsViewModel.route
|
2021-09-23 02:17:44 +08:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2022-04-05 16:31:55 +08:00
|
|
|
(vmPlace || {}).scrollTop = 0;
|
2016-07-07 07:11:13 +08:00
|
|
|
// --
|
2020-07-30 03:49:41 +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() {
|
2021-09-23 02:17:44 +08:00
|
|
|
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() {
|
2023-04-24 18:01:09 +08:00
|
|
|
// 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(
|
2023-04-24 18:01:09 +08:00
|
|
|
SettingsViewModel => SettingsViewModel.isDefault
|
2017-08-07 23:09:14 +08:00
|
|
|
),
|
2023-04-24 18:01:09 +08:00
|
|
|
defaultRoute = DefaultViewModel?.route || 'general',
|
2016-08-17 06:01:20 +08:00
|
|
|
rules = {
|
2016-07-07 07:11:13 +08:00
|
|
|
subname: /^(.*)$/,
|
2016-08-17 06:01:20 +08:00
|
|
|
normalize_: (rquest, vals) => {
|
2021-10-19 17:31:44 +08:00
|
|
|
vals.subname = null == vals.subname ? defaultRoute : pString(vals.subname);
|
2016-08-17 06:01:20 +08:00
|
|
|
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) {
|
2022-03-08 07:51:32 +08:00
|
|
|
let name = SettingsViewModelClass.name.replace(/(User|Admin)Settings/, '');
|
2023-04-24 18:01:09 +08:00
|
|
|
VIEW_MODELS.push({
|
|
|
|
vmc: SettingsViewModelClass,
|
2022-12-24 00:54:13 +08:00
|
|
|
label: labelName || 'SETTINGS_LABELS/' + name.toUpperCase(),
|
2022-03-08 07:51:32 +08:00
|
|
|
route: route || name.toLowerCase(),
|
2021-09-23 02:17:44 +08:00
|
|
|
selected: ko.observable(false),
|
2022-03-08 07:51:32 +08:00
|
|
|
template: template || SettingsViewModelClass.name,
|
2021-09-23 02:17:44 +08:00
|
|
|
isDefault: !!isDefault
|
2023-04-24 18:01:09 +08:00
|
|
|
});
|
2021-01-26 18:46:30 +08:00
|
|
|
}
|