snappymail/dev/Knoin/Knoin.js

461 lines
11 KiB
JavaScript
Raw Normal View History

2014-09-05 06:49:03 +08:00
(function () {
2014-08-25 23:49:01 +08:00
'use strict';
2014-08-20 23:03:12 +08:00
var
2014-08-25 23:49:01 +08:00
_ = require('_'),
$ = require('$'),
2014-08-25 23:49:01 +08:00
ko = require('ko'),
hasher = require('hasher'),
crossroads = require('crossroads'),
2014-09-05 06:49:03 +08:00
Globals = require('Common/Globals'),
Plugins = require('Common/Plugins'),
Utils = require('Common/Utils')
2014-08-20 23:03:12 +08:00
;
/**
* @constructor
*/
function Knoin()
{
2014-08-20 23:03:12 +08:00
this.oScreens = {};
2014-08-27 23:59:44 +08:00
this.sDefaultScreenName = '';
2014-08-20 23:03:12 +08:00
this.oCurrentScreen = null;
}
2014-08-20 23:03:12 +08:00
Knoin.prototype.oScreens = {};
2014-08-27 23:59:44 +08:00
Knoin.prototype.sDefaultScreenName = '';
2014-08-20 23:03:12 +08:00
Knoin.prototype.oCurrentScreen = null;
Knoin.prototype.hideLoading = function ()
{
2014-08-20 23:03:12 +08:00
$('#rl-loading').hide();
};
2014-08-20 23:03:12 +08:00
/**
* @param {Object} thisObject
*/
Knoin.prototype.constructorEnd = function (thisObject)
{
if (Utils.isFunc(thisObject['__constructor_end']))
{
thisObject['__constructor_end'].call(thisObject);
}
};
2014-08-21 23:08:34 +08:00
/**
* @param {string|Array} mName
2014-08-21 23:08:34 +08:00
* @param {Function} ViewModelClass
*/
Knoin.prototype.extendAsViewModel = function (mName, ViewModelClass)
2014-08-21 23:08:34 +08:00
{
if (ViewModelClass)
{
if (Utils.isArray(mName))
2014-08-21 23:08:34 +08:00
{
ViewModelClass.__names = mName;
}
else
{
ViewModelClass.__names = [mName];
2014-08-21 23:08:34 +08:00
}
ViewModelClass.__name = ViewModelClass.__names[0];
2014-08-21 23:08:34 +08:00
}
};
/**
* @param {Function} SettingsViewModelClass
* @param {string} sLabelName
* @param {string} sTemplate
* @param {string} sRoute
* @param {boolean=} bDefault
*/
Knoin.prototype.addSettingsViewModel = function (SettingsViewModelClass, sTemplate, sLabelName, sRoute, bDefault)
{
SettingsViewModelClass.__rlSettingsData = {
'Label': sLabelName,
'Template': sTemplate,
'Route': sRoute,
'IsDefault': !!bDefault
};
Globals.aViewModels['settings'].push(SettingsViewModelClass);
};
/**
* @param {Function} SettingsViewModelClass
*/
Knoin.prototype.removeSettingsViewModel = function (SettingsViewModelClass)
{
Globals.aViewModels['settings-removed'].push(SettingsViewModelClass);
};
/**
* @param {Function} SettingsViewModelClass
*/
Knoin.prototype.disableSettingsViewModel = function (SettingsViewModelClass)
{
Globals.aViewModels['settings-disabled'].push(SettingsViewModelClass);
};
2014-08-20 23:03:12 +08:00
Knoin.prototype.routeOff = function ()
{
hasher.changed.active = false;
};
Knoin.prototype.routeOn = function ()
{
hasher.changed.active = true;
};
/**
* @param {string} sScreenName
* @return {?Object}
*/
Knoin.prototype.screen = function (sScreenName)
{
return ('' !== sScreenName && !Utils.isUnd(this.oScreens[sScreenName])) ? this.oScreens[sScreenName] : null;
};
/**
* @param {Function} ViewModelClass
* @param {Object=} oScreen
*/
Knoin.prototype.buildViewModel = function (ViewModelClass, oScreen)
{
if (ViewModelClass && !ViewModelClass.__builded)
{
2014-08-20 23:03:12 +08:00
var
2014-08-22 23:08:56 +08:00
kn = this,
2014-08-20 23:03:12 +08:00
oViewModel = new ViewModelClass(oScreen),
sPosition = oViewModel.viewModelPosition(),
oViewModelPlace = $('#rl-content #rl-' + sPosition.toLowerCase()),
oViewModelDom = null
;
ViewModelClass.__builded = true;
ViewModelClass.__vm = oViewModel;
2014-05-23 00:08:38 +08:00
2014-08-20 23:03:12 +08:00
oViewModel.viewModelName = ViewModelClass.__name;
oViewModel.viewModelNames = ViewModelClass.__names;
2014-08-20 23:03:12 +08:00
if (oViewModelPlace && 1 === oViewModelPlace.length)
{
2014-08-20 23:03:12 +08:00
oViewModelDom = $('<div></div>').addClass('rl-view-model').addClass('RL-' + oViewModel.viewModelTemplate()).hide();
oViewModelDom.appendTo(oViewModelPlace);
2014-08-20 23:03:12 +08:00
oViewModel.viewModelDom = oViewModelDom;
ViewModelClass.__dom = oViewModelDom;
2014-08-20 23:03:12 +08:00
if ('Popups' === sPosition)
{
oViewModel.cancelCommand = oViewModel.closeCommand = Utils.createCommand(oViewModel, function () {
2014-08-22 23:08:56 +08:00
kn.hideScreenPopup(ViewModelClass);
2014-08-20 23:03:12 +08:00
});
2014-08-20 23:03:12 +08:00
oViewModel.modalVisibility.subscribe(function (bValue) {
2014-08-20 23:03:12 +08:00
var self = this;
if (bValue)
{
this.viewModelDom.show();
this.storeAndSetKeyScope();
2014-08-22 23:08:56 +08:00
Globals.popupVisibilityNames.push(this.viewModelName);
oViewModel.viewModelDom.css('z-index', 3000 + Globals.popupVisibilityNames().length + 10);
2014-08-20 23:03:12 +08:00
Utils.delegateRun(this, 'onFocus', [], 500);
}
else
{
Utils.delegateRun(this, 'onHide');
this.restoreKeyScope();
2014-05-21 00:00:59 +08:00
_.each(this.viewModelNames, function (sName) {
Plugins.runHook('view-model-on-hide', [sName, self]);
});
2014-08-22 23:08:56 +08:00
Globals.popupVisibilityNames.remove(this.viewModelName);
2014-08-20 23:03:12 +08:00
oViewModel.viewModelDom.css('z-index', 2000);
2014-08-20 23:03:12 +08:00
Globals.tooltipTrigger(!Globals.tooltipTrigger());
_.delay(function () {
self.viewModelDom.hide();
}, 300);
}
}, oViewModel);
}
_.each(ViewModelClass.__names, function (sName) {
Plugins.runHook('view-model-pre-build', [sName, oViewModel, oViewModelDom]);
});
2014-08-20 23:03:12 +08:00
ko.applyBindingAccessorsToNode(oViewModelDom[0], {
'i18nInit': true,
'template': function () { return {'name': oViewModel.viewModelTemplate()};}
}, oViewModel);
Utils.delegateRun(oViewModel, 'onBuild', [oViewModelDom]);
if (oViewModel && 'Popups' === sPosition)
{
oViewModel.registerPopupKeyDown();
}
2014-05-23 00:08:38 +08:00
_.each(ViewModelClass.__names, function (sName) {
Plugins.runHook('view-model-post-build', [sName, oViewModel, oViewModelDom]);
});
2014-08-20 23:03:12 +08:00
}
else
{
2014-08-20 23:03:12 +08:00
Utils.log('Cannot find view model position: ' + sPosition);
}
}
2014-08-20 23:03:12 +08:00
return ViewModelClass ? ViewModelClass.__vm : null;
};
2014-08-20 23:03:12 +08:00
/**
* @param {Function} ViewModelClassToHide
*/
Knoin.prototype.hideScreenPopup = function (ViewModelClassToHide)
{
2014-08-20 23:03:12 +08:00
if (ViewModelClassToHide && ViewModelClassToHide.__vm && ViewModelClassToHide.__dom)
{
ViewModelClassToHide.__vm.modalVisibility(false);
}
};
2014-08-20 23:03:12 +08:00
/**
* @param {Function} ViewModelClassToShow
* @param {Array=} aParameters
*/
Knoin.prototype.showScreenPopup = function (ViewModelClassToShow, aParameters)
{
2014-08-20 23:03:12 +08:00
if (ViewModelClassToShow)
{
2014-08-20 23:03:12 +08:00
this.buildViewModel(ViewModelClassToShow);
if (ViewModelClassToShow.__vm && ViewModelClassToShow.__dom)
{
2014-08-20 23:03:12 +08:00
ViewModelClassToShow.__vm.modalVisibility(true);
Utils.delegateRun(ViewModelClassToShow.__vm, 'onShow', aParameters || []);
_.each(ViewModelClassToShow.__names, function (sName) {
Plugins.runHook('view-model-on-show', [sName, ViewModelClassToShow.__vm, aParameters || []]);
});
}
}
2014-08-20 23:03:12 +08:00
};
/**
* @param {Function} ViewModelClassToShow
* @return {boolean}
*/
Knoin.prototype.isPopupVisible = function (ViewModelClassToShow)
{
return ViewModelClassToShow && ViewModelClassToShow.__vm ? ViewModelClassToShow.__vm.modalVisibility() : false;
};
/**
* @param {string} sScreenName
* @param {string} sSubPart
*/
Knoin.prototype.screenOnRoute = function (sScreenName, sSubPart)
{
var
self = this,
oScreen = null,
oCross = null
;
2014-08-20 23:03:12 +08:00
if ('' === Utils.pString(sScreenName))
{
2014-08-20 23:03:12 +08:00
sScreenName = this.sDefaultScreenName;
}
2014-08-20 23:03:12 +08:00
if ('' !== sScreenName)
{
oScreen = this.screen(sScreenName);
if (!oScreen)
{
oScreen = this.screen(this.sDefaultScreenName);
if (oScreen)
{
2014-08-20 23:03:12 +08:00
sSubPart = sScreenName + '/' + sSubPart;
sScreenName = this.sDefaultScreenName;
}
}
2014-08-20 23:03:12 +08:00
if (oScreen && oScreen.__started)
{
if (!oScreen.__builded)
{
2014-08-20 23:03:12 +08:00
oScreen.__builded = true;
2014-08-20 23:03:12 +08:00
if (Utils.isNonEmptyArray(oScreen.viewModels()))
{
2014-08-20 23:03:12 +08:00
_.each(oScreen.viewModels(), function (ViewModelClass) {
this.buildViewModel(ViewModelClass, oScreen);
}, this);
}
2014-08-20 23:03:12 +08:00
Utils.delegateRun(oScreen, 'onBuild');
}
2014-08-20 23:03:12 +08:00
_.defer(function () {
2014-08-20 23:03:12 +08:00
// hide screen
if (self.oCurrentScreen)
{
Utils.delegateRun(self.oCurrentScreen, 'onHide');
if (Utils.isNonEmptyArray(self.oCurrentScreen.viewModels()))
{
_.each(self.oCurrentScreen.viewModels(), function (ViewModelClass) {
if (ViewModelClass.__vm && ViewModelClass.__dom &&
'Popups' !== ViewModelClass.__vm.viewModelPosition())
{
ViewModelClass.__dom.hide();
ViewModelClass.__vm.viewModelVisibility(false);
Utils.delegateRun(ViewModelClass.__vm, 'onHide');
}
});
}
}
// --
2014-08-20 23:03:12 +08:00
self.oCurrentScreen = oScreen;
2014-08-20 23:03:12 +08:00
// show screen
if (self.oCurrentScreen)
{
2014-08-20 23:03:12 +08:00
Utils.delegateRun(self.oCurrentScreen, 'onShow');
2014-08-22 23:08:56 +08:00
Plugins.runHook('screen-on-show', [self.oCurrentScreen.screenName(), self.oCurrentScreen]);
2014-08-20 23:03:12 +08:00
if (Utils.isNonEmptyArray(self.oCurrentScreen.viewModels()))
{
_.each(self.oCurrentScreen.viewModels(), function (ViewModelClass) {
2014-08-20 23:03:12 +08:00
if (ViewModelClass.__vm && ViewModelClass.__dom &&
'Popups' !== ViewModelClass.__vm.viewModelPosition())
{
ViewModelClass.__dom.show();
ViewModelClass.__vm.viewModelVisibility(true);
2014-08-20 23:03:12 +08:00
Utils.delegateRun(ViewModelClass.__vm, 'onShow');
Utils.delegateRun(ViewModelClass.__vm, 'onFocus', [], 200);
_.each(ViewModelClass.__names, function (sName) {
Plugins.runHook('view-model-on-show', [sName, ViewModelClass.__vm]);
});
2014-08-20 23:03:12 +08:00
}
}, self);
}
}
2014-08-20 23:03:12 +08:00
// --
2014-08-27 23:59:44 +08:00
oCross = oScreen.__cross ? oScreen.__cross() : null;
2014-08-20 23:03:12 +08:00
if (oCross)
{
oCross.parse(sSubPart);
}
});
}
}
2014-08-20 23:03:12 +08:00
};
2014-08-20 23:03:12 +08:00
/**
* @param {Array} aScreensClasses
*/
Knoin.prototype.startScreens = function (aScreensClasses)
{
$('#rl-content').css({
'visibility': 'hidden'
});
2014-08-20 23:03:12 +08:00
_.each(aScreensClasses, function (CScreen) {
2014-08-20 23:03:12 +08:00
var
oScreen = new CScreen(),
sScreenName = oScreen ? oScreen.screenName() : ''
;
2014-08-20 23:03:12 +08:00
if (oScreen && '' !== sScreenName)
{
2014-08-20 23:03:12 +08:00
if ('' === this.sDefaultScreenName)
{
this.sDefaultScreenName = sScreenName;
}
this.oScreens[sScreenName] = oScreen;
}
2014-08-20 23:03:12 +08:00
}, this);
_.each(this.oScreens, function (oScreen) {
if (oScreen && !oScreen.__started && oScreen.__start)
{
oScreen.__started = true;
oScreen.__start();
2014-08-22 23:08:56 +08:00
Plugins.runHook('screen-pre-start', [oScreen.screenName(), oScreen]);
2014-08-20 23:03:12 +08:00
Utils.delegateRun(oScreen, 'onStart');
2014-08-22 23:08:56 +08:00
Plugins.runHook('screen-post-start', [oScreen.screenName(), oScreen]);
2014-08-20 23:03:12 +08:00
}
}, this);
2014-08-20 23:03:12 +08:00
var oCross = crossroads.create();
oCross.addRoute(/^([a-zA-Z0-9\-]*)\/?(.*)$/, _.bind(this.screenOnRoute, this));
2014-08-20 23:03:12 +08:00
hasher.initialized.add(oCross.parse, oCross);
hasher.changed.add(oCross.parse, oCross);
hasher.init();
$('#rl-content').css({
'visibility': 'visible'
});
_.delay(function () {
2014-09-02 00:05:32 +08:00
Globals.$html.removeClass('rl-started-trigger').addClass('rl-started');
2014-08-20 23:03:12 +08:00
}, 50);
};
2014-08-20 23:03:12 +08:00
/**
* @param {string} sHash
* @param {boolean=} bSilence = false
* @param {boolean=} bReplace = false
*/
Knoin.prototype.setHash = function (sHash, bSilence, bReplace)
{
sHash = '#' === sHash.substr(0, 1) ? sHash.substr(1) : sHash;
sHash = '/' === sHash.substr(0, 1) ? sHash.substr(1) : sHash;
bReplace = Utils.isUnd(bReplace) ? false : !!bReplace;
if (Utils.isUnd(bSilence) ? false : !!bSilence)
{
hasher.changed.active = false;
hasher[bReplace ? 'replaceHash' : 'setHash'](sHash);
hasher.changed.active = true;
}
2014-08-20 23:03:12 +08:00
else
{
hasher.changed.active = true;
hasher[bReplace ? 'replaceHash' : 'setHash'](sHash);
hasher.setHash(sHash);
}
};
2014-08-20 23:03:12 +08:00
module.exports = new Knoin();
2014-09-05 06:49:03 +08:00
}());