snappymail/dev/Screens/AbstractSettings.js

201 lines
5.6 KiB
JavaScript
Raw Normal View History

/* RainLoop Webmail (c) RainLoop Team | Licensed under CC BY-NC-SA 3.0 */
2014-08-25 23:49:01 +08:00
(function (module, require) {
'use strict';
2014-08-20 23:03:12 +08:00
var
2014-08-25 23:49:01 +08:00
$ = require('$'),
_ = require('_'),
ko = require('ko'),
2014-08-25 15:10:51 +08:00
2014-08-25 23:49:01 +08:00
Globals = require('Globals'),
Utils = require('Utils'),
LinkBuilder = require('LinkBuilder'),
2014-08-21 23:08:34 +08:00
2014-08-25 23:49:01 +08:00
kn = require('kn'),
KnoinAbstractScreen = require('KnoinAbstractScreen')
;
2014-08-20 23:03:12 +08:00
/**
* @param {Array} aViewModels
* @constructor
* @extends KnoinAbstractScreen
*/
function AbstractSettings(aViewModels)
{
2014-08-20 23:03:12 +08:00
KnoinAbstractScreen.call(this, 'settings', aViewModels);
this.menu = ko.observableArray([]);
this.oCurrentSubScreen = null;
this.oViewModelPlace = null;
}
2014-08-20 23:03:12 +08:00
_.extend(AbstractSettings.prototype, KnoinAbstractScreen.prototype);
AbstractSettings.prototype.onRoute = function (sSubName)
{
2014-08-20 23:03:12 +08:00
var
self = this,
oSettingsScreen = null,
RoutedSettingsViewModel = null,
oViewModelPlace = null,
oViewModelDom = null
;
RoutedSettingsViewModel = _.find(Globals.aViewModels['settings'], function (SettingsViewModel) {
return SettingsViewModel && SettingsViewModel.__rlSettingsData &&
sSubName === SettingsViewModel.__rlSettingsData.Route;
});
if (RoutedSettingsViewModel)
{
2014-08-20 23:03:12 +08:00
if (_.find(Globals.aViewModels['settings-removed'], function (DisabledSettingsViewModel) {
return DisabledSettingsViewModel && DisabledSettingsViewModel === RoutedSettingsViewModel;
}))
{
2014-08-20 23:03:12 +08:00
RoutedSettingsViewModel = null;
}
2014-08-20 23:03:12 +08:00
if (RoutedSettingsViewModel && _.find(Globals.aViewModels['settings-disabled'], function (DisabledSettingsViewModel) {
return DisabledSettingsViewModel && DisabledSettingsViewModel === RoutedSettingsViewModel;
}))
{
2014-08-20 23:03:12 +08:00
RoutedSettingsViewModel = null;
}
}
2014-08-20 23:03:12 +08:00
if (RoutedSettingsViewModel)
{
2014-08-20 23:03:12 +08:00
if (RoutedSettingsViewModel.__builded && RoutedSettingsViewModel.__vm)
{
oSettingsScreen = RoutedSettingsViewModel.__vm;
}
else
{
oViewModelPlace = this.oViewModelPlace;
if (oViewModelPlace && 1 === oViewModelPlace.length)
{
2014-08-20 23:03:12 +08:00
RoutedSettingsViewModel = /** @type {?Function} */ RoutedSettingsViewModel;
oSettingsScreen = new RoutedSettingsViewModel();
2014-08-20 23:03:12 +08:00
oViewModelDom = $('<div></div>').addClass('rl-settings-view-model').hide();
oViewModelDom.appendTo(oViewModelPlace);
2014-08-20 23:03:12 +08:00
oSettingsScreen.viewModelDom = oViewModelDom;
oSettingsScreen.__rlSettingsData = RoutedSettingsViewModel.__rlSettingsData;
2014-08-20 23:03:12 +08:00
RoutedSettingsViewModel.__dom = oViewModelDom;
RoutedSettingsViewModel.__builded = true;
RoutedSettingsViewModel.__vm = oSettingsScreen;
2014-08-20 23:03:12 +08:00
ko.applyBindingAccessorsToNode(oViewModelDom[0], {
'i18nInit': true,
'template': function () { return {'name': RoutedSettingsViewModel.__rlSettingsData.Template}; }
}, oSettingsScreen);
Utils.delegateRun(oSettingsScreen, 'onBuild', [oViewModelDom]);
}
else
{
Utils.log('Cannot find sub settings view model position: SettingsSubScreen');
}
2014-08-20 23:03:12 +08:00
}
2014-08-20 23:03:12 +08:00
if (oSettingsScreen)
{
_.defer(function () {
// hide
if (self.oCurrentSubScreen)
{
Utils.delegateRun(self.oCurrentSubScreen, 'onHide');
self.oCurrentSubScreen.viewModelDom.hide();
}
// --
self.oCurrentSubScreen = oSettingsScreen;
// show
if (self.oCurrentSubScreen)
{
self.oCurrentSubScreen.viewModelDom.show();
Utils.delegateRun(self.oCurrentSubScreen, 'onShow');
Utils.delegateRun(self.oCurrentSubScreen, 'onFocus', [], 200);
_.each(self.menu(), function (oItem) {
oItem.selected(oSettingsScreen && oSettingsScreen.__rlSettingsData && oItem.route === oSettingsScreen.__rlSettingsData.Route);
});
$('#rl-content .b-settings .b-content .content').scrollTop(0);
}
// --
Utils.windowResize();
});
}
}
2014-08-20 23:03:12 +08:00
else
{
2014-08-22 23:08:56 +08:00
kn.setHash(LinkBuilder.settings(), false, true);
2014-08-20 23:03:12 +08:00
}
};
2014-08-20 23:03:12 +08:00
AbstractSettings.prototype.onHide = function ()
{
2014-08-20 23:03:12 +08:00
if (this.oCurrentSubScreen && this.oCurrentSubScreen.viewModelDom)
{
2014-08-20 23:03:12 +08:00
Utils.delegateRun(this.oCurrentSubScreen, 'onHide');
this.oCurrentSubScreen.viewModelDom.hide();
}
2014-08-20 23:03:12 +08:00
};
AbstractSettings.prototype.onBuild = function ()
{
_.each(Globals.aViewModels['settings'], function (SettingsViewModel) {
if (SettingsViewModel && SettingsViewModel.__rlSettingsData &&
!_.find(Globals.aViewModels['settings-removed'], function (RemoveSettingsViewModel) {
return RemoveSettingsViewModel && RemoveSettingsViewModel === SettingsViewModel;
}))
{
this.menu.push({
'route': SettingsViewModel.__rlSettingsData.Route,
'label': SettingsViewModel.__rlSettingsData.Label,
'selected': ko.observable(false),
'disabled': !!_.find(Globals.aViewModels['settings-disabled'], function (DisabledSettingsViewModel) {
return DisabledSettingsViewModel && DisabledSettingsViewModel === SettingsViewModel;
})
});
}
}, this);
2014-08-20 23:03:12 +08:00
this.oViewModelPlace = $('#rl-content #rl-settings-subscreen');
};
2014-08-20 23:03:12 +08:00
AbstractSettings.prototype.routes = function ()
{
var
DefaultViewModel = _.find(Globals.aViewModels['settings'], function (SettingsViewModel) {
return SettingsViewModel && SettingsViewModel.__rlSettingsData && SettingsViewModel.__rlSettingsData['IsDefault'];
}),
sDefaultRoute = DefaultViewModel ? DefaultViewModel.__rlSettingsData['Route'] : 'general',
oRules = {
'subname': /^(.*)$/,
'normalize_': function (oRequest, oVals) {
oVals.subname = Utils.isUnd(oVals.subname) ? sDefaultRoute : Utils.pString(oVals.subname);
return [oVals.subname];
}
}
2014-08-20 23:03:12 +08:00
;
return [
['{subname}/', oRules],
['{subname}', oRules],
['', oRules]
];
};
module.exports = AbstractSettings;
2014-08-25 23:49:01 +08:00
}(module, require));