snappymail/dev/App/Abstract.js

321 lines
7 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
window = require('window'),
_ = require('_'),
$ = require('$'),
2014-08-25 23:49:01 +08:00
2014-09-05 06:49:03 +08:00
Globals = require('Common/Globals'),
Utils = require('Common/Utils'),
2014-10-06 02:37:31 +08:00
Links = require('Common/Links'),
2014-09-05 06:49:03 +08:00
Events = require('Common/Events'),
Translator = require('Common/Translator'),
2014-08-21 23:08:34 +08:00
Settings = require('Storage/Settings'),
2014-08-21 23:08:34 +08:00
AbstractBoot = require('Knoin/AbstractBoot')
;
2014-08-20 23:03:12 +08:00
/**
* @constructor
* @param {RemoteStorage|AdminRemoteStorage} Remote
* @extends AbstractBoot
2014-08-20 23:03:12 +08:00
*/
2014-08-22 23:08:56 +08:00
function AbstractApp(Remote)
{
AbstractBoot.call(this);
2014-08-20 23:03:12 +08:00
this.isLocalAutocomplete = true;
this.iframe = $('<iframe style="display:none" src="javascript:;" />').appendTo('body');
2014-09-02 00:05:32 +08:00
Globals.$win.on('error', function (oEvent) {
2014-08-20 23:03:12 +08:00
if (oEvent && oEvent.originalEvent && oEvent.originalEvent.message &&
-1 === Utils.inArray(oEvent.originalEvent.message, [
'Script error.', 'Uncaught Error: Error calling method on NPObject.'
]))
{
2014-08-22 23:08:56 +08:00
Remote.jsError(
2014-08-20 23:03:12 +08:00
Utils.emptyFunction,
oEvent.originalEvent.message,
oEvent.originalEvent.filename,
oEvent.originalEvent.lineno,
window.location && window.location.toString ? window.location.toString() : '',
2014-09-02 00:05:32 +08:00
Globals.$html.attr('class'),
2014-08-20 23:03:12 +08:00
Utils.microtime() - Globals.now
);
}
2014-08-20 23:03:12 +08:00
});
2014-09-02 00:05:32 +08:00
Globals.$doc.on('keydown', function (oEvent) {
2014-08-20 23:03:12 +08:00
if (oEvent && oEvent.ctrlKey)
{
2014-09-02 00:05:32 +08:00
Globals.$html.addClass('rl-ctrl-key-pressed');
2014-08-20 23:03:12 +08:00
}
}).on('keyup', function (oEvent) {
if (oEvent && !oEvent.ctrlKey)
{
2014-09-02 00:05:32 +08:00
Globals.$html.removeClass('rl-ctrl-key-pressed');
2014-08-20 23:03:12 +08:00
}
});
}
_.extend(AbstractApp.prototype, AbstractBoot.prototype);
2014-08-20 23:03:12 +08:00
2014-08-22 23:08:56 +08:00
AbstractApp.prototype.remote = function ()
{
return null;
};
AbstractApp.prototype.data = function ()
{
return null;
};
2014-08-20 23:03:12 +08:00
/**
* @param {string} sLink
* @return {boolean}
*/
AbstractApp.prototype.download = function (sLink)
{
2014-08-20 23:03:12 +08:00
var
oE = null,
oLink = null,
sUserAgent = window.navigator.userAgent.toLowerCase()
;
if (sUserAgent && (sUserAgent.indexOf('chrome') > -1 || sUserAgent.indexOf('chrome') > -1))
{
oLink = window.document.createElement('a');
oLink['href'] = sLink;
if (window.document['createEvent'])
{
oE = window.document['createEvent']('MouseEvents');
if (oE && oE['initEvent'] && oLink['dispatchEvent'])
{
oE['initEvent']('click', true, true);
oLink['dispatchEvent'](oE);
return true;
}
}
}
if (Globals.bMobileDevice)
{
window.open(sLink, '_self');
window.focus();
}
else
{
this.iframe.attr('src', sLink);
// window.document.location.href = sLink;
}
return true;
};
AbstractApp.prototype.googlePreviewSupportedCache = null;
/**
* @return {boolean}
*/
AbstractApp.prototype.googlePreviewSupported = function ()
{
if (null === this.googlePreviewSupportedCache)
{
this.googlePreviewSupportedCache = !!Settings.settingsGet('AllowGoogleSocial') &&
!!Settings.settingsGet('AllowGoogleSocialPreview');
}
2014-11-01 01:25:02 +08:00
return this.googlePreviewSupportedCache;
};
/**
* @param {string} sTitle
*/
2014-08-20 23:03:12 +08:00
AbstractApp.prototype.setTitle = function (sTitle)
{
2014-08-20 23:03:12 +08:00
sTitle = ((Utils.isNormal(sTitle) && 0 < sTitle.length) ? sTitle + ' - ' : '') +
2014-08-27 23:59:44 +08:00
Settings.settingsGet('Title') || '';
2014-08-20 23:03:12 +08:00
window.document.title = sTitle + ' ...';
2014-08-20 23:03:12 +08:00
window.document.title = sTitle;
};
AbstractApp.prototype.redirectToAdminPanel = function ()
{
_.delay(function () {
2014-10-06 02:37:31 +08:00
window.location.href = Links.rootAdmin();
}, 100);
};
2014-10-06 02:37:31 +08:00
AbstractApp.prototype.clearClientSideToken = function ()
{
if (window.__rlah_clear)
{
window.__rlah_clear();
}
};
2014-08-20 23:03:12 +08:00
/**
* @param {boolean=} bLogout = false
* @param {boolean=} bClose = false
*/
AbstractApp.prototype.loginAndLogoutReload = function (bLogout, bClose)
{
var
kn = require('Knoin/Knoin'),
2014-08-27 23:59:44 +08:00
sCustomLogoutLink = Utils.pString(Settings.settingsGet('CustomLogoutLink')),
bInIframe = !!Settings.settingsGet('InIframe')
2014-08-20 23:03:12 +08:00
;
2014-08-20 23:03:12 +08:00
bLogout = Utils.isUnd(bLogout) ? false : !!bLogout;
bClose = Utils.isUnd(bClose) ? false : !!bClose;
2014-10-06 02:37:31 +08:00
if (bLogout)
{
this.clearClientSideToken();
}
2014-08-20 23:03:12 +08:00
if (bLogout && bClose && window.close)
{
window.close();
}
sCustomLogoutLink = sCustomLogoutLink || './';
if (bLogout && window.location.href !== sCustomLogoutLink)
2014-08-20 23:03:12 +08:00
{
_.delay(function () {
if (bInIframe && window.parent)
{
window.parent.location.href = sCustomLogoutLink;
}
else
{
window.location.href = sCustomLogoutLink;
}
}, 100);
}
else
{
kn.routeOff();
2014-10-06 02:37:31 +08:00
kn.setHash(Links.root(), true);
2014-08-20 23:03:12 +08:00
kn.routeOff();
_.delay(function () {
if (bInIframe && window.parent)
{
window.parent.location.reload();
}
else
{
window.location.reload();
}
}, 100);
}
};
2014-08-20 23:03:12 +08:00
AbstractApp.prototype.historyBack = function ()
{
window.history.back();
};
AbstractApp.prototype.bootstart = function ()
{
2014-08-22 23:08:56 +08:00
Events.pub('rl.bootstart');
2014-08-25 15:10:51 +08:00
2014-10-29 06:05:50 +08:00
var
ssm = require('ssm'),
ko = require('ko')
;
ko.components.register('SaveTrigger', require('Component/SaveTrigger'));
ko.components.register('Input', require('Component/Input'));
ko.components.register('Select', require('Component/Select'));
ko.components.register('TextArea', require('Component/TextArea'));
ko.components.register('Radio', require('Component/Radio'));
2014-11-01 01:25:02 +08:00
if (Settings.settingsGet('MaterialDesign') && Globals.bAnimationSupported)
{
ko.components.register('Checkbox', require('Component/MaterialDesign/Checkbox'));
}
else
{
2014-11-06 03:40:20 +08:00
// ko.components.register('Checkbox', require('Component/Classic/Checkbox'));
ko.components.register('Checkbox', require('Component/Checkbox'));
}
2014-08-20 23:03:12 +08:00
Translator.initOnStartOrLangChange(Translator.initNotificationLanguage, Translator);
_.delay(Utils.windowResizeCallback, 1000);
2014-08-20 23:03:12 +08:00
ssm.addState({
'id': 'mobile',
'maxWidth': 767,
'onEnter': function() {
2014-09-02 00:05:32 +08:00
Globals.$html.addClass('ssm-state-mobile');
2014-08-22 23:08:56 +08:00
Events.pub('ssm.mobile-enter');
2014-08-20 23:03:12 +08:00
},
'onLeave': function() {
2014-09-02 00:05:32 +08:00
Globals.$html.removeClass('ssm-state-mobile');
2014-08-22 23:08:56 +08:00
Events.pub('ssm.mobile-leave');
}
2014-08-20 23:03:12 +08:00
});
2013-12-13 07:23:47 +08:00
2014-08-20 23:03:12 +08:00
ssm.addState({
'id': 'tablet',
'minWidth': 768,
'maxWidth': 999,
'onEnter': function() {
2014-09-02 00:05:32 +08:00
Globals.$html.addClass('ssm-state-tablet');
2014-08-20 23:03:12 +08:00
},
'onLeave': function() {
2014-09-02 00:05:32 +08:00
Globals.$html.removeClass('ssm-state-tablet');
2014-08-20 23:03:12 +08:00
}
});
2013-12-13 07:23:47 +08:00
2014-08-20 23:03:12 +08:00
ssm.addState({
'id': 'desktop',
'minWidth': 1000,
'maxWidth': 1400,
'onEnter': function() {
2014-09-02 00:05:32 +08:00
Globals.$html.addClass('ssm-state-desktop');
2014-08-20 23:03:12 +08:00
},
'onLeave': function() {
2014-09-02 00:05:32 +08:00
Globals.$html.removeClass('ssm-state-desktop');
2014-08-20 23:03:12 +08:00
}
});
2013-12-13 07:23:47 +08:00
2014-08-20 23:03:12 +08:00
ssm.addState({
'id': 'desktop-large',
'minWidth': 1400,
'onEnter': function() {
2014-09-02 00:05:32 +08:00
Globals.$html.addClass('ssm-state-desktop-large');
2014-08-20 23:03:12 +08:00
},
'onLeave': function() {
2014-09-02 00:05:32 +08:00
Globals.$html.removeClass('ssm-state-desktop-large');
2013-12-13 07:23:47 +08:00
}
});
2014-08-22 23:08:56 +08:00
Events.sub('ssm.mobile-enter', function () {
Globals.leftPanelDisabled(true);
2014-08-20 23:03:12 +08:00
});
2014-08-22 23:08:56 +08:00
Events.sub('ssm.mobile-leave', function () {
Globals.leftPanelDisabled(false);
2014-08-20 23:03:12 +08:00
});
2014-08-22 23:08:56 +08:00
Globals.leftPanelDisabled.subscribe(function (bValue) {
2014-09-02 00:05:32 +08:00
Globals.$html.toggleClass('rl-left-panel-disabled', bValue);
2014-08-20 23:03:12 +08:00
});
2014-04-27 08:54:22 +08:00
2014-08-20 23:03:12 +08:00
ssm.ready();
};
2014-04-27 08:54:22 +08:00
2014-08-20 23:03:12 +08:00
module.exports = AbstractApp;
2014-04-27 08:54:22 +08:00
2014-09-05 06:49:03 +08:00
}());