snappymail/dev/App/Abstract.jsx

366 lines
8.4 KiB
React
Raw Normal View History

2015-11-19 01:32:29 +08:00
import {window, _, $, key} from 'common';
import Globals from 'Common/Globals';
import * as Enums from 'Common/Enums';
import Utils from 'Common/Utils';
import Links from 'Common/Links';
import Events from 'Common/Events';
import Translator from 'Common/Translator';
import Settings from 'Storage/Settings';
import {AbstractBoot} from 'Knoin/AbstractBoot';
class AbstractApp extends AbstractBoot
{
googlePreviewSupportedCache = null;
isLocalAutocomplete = true;
iframe = null;
2014-08-20 23:03:12 +08:00
/**
* @param {RemoteStorage|AdminRemoteStorage} Remote
2014-08-20 23:03:12 +08:00
*/
2015-11-19 01:32:29 +08:00
constructor(Remote)
{
2015-11-19 01:32:29 +08:00
super();
2014-08-20 23:03:12 +08:00
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'),
Utils.microtime() - Globals.startMicrotime
2014-08-20 23:03:12 +08:00
);
}
2014-08-20 23:03:12 +08:00
});
Globals.$win.on('resize', function () {
Events.pub('window.resize');
});
Events.sub('window.resize', _.throttle(function () {
var
iH = Globals.$win.height(),
iW = Globals.$win.height()
;
if (Globals.$win.__sizes[0] !== iH || Globals.$win.__sizes[1] !== iW)
{
Globals.$win.__sizes[0] = iH;
Globals.$win.__sizes[1] = iW;
Events.pub('window.resize.real');
}
}, 50));
2015-04-14 02:45:09 +08:00
// DEBUG
// Events.sub({
// 'window.resize': function () {
// window.console.log('window.resize');
// },
// 'window.resize.real': function () {
// window.console.log('window.resize.real');
// }
// });
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
}
});
2015-02-19 03:52:52 +08:00
Globals.$doc.on('mousemove keypress click', _.debounce(function () {
Events.pub('rl.auto-logout-refresh');
}, 5000));
2015-03-16 05:58:50 +08:00
key('esc, enter', Enums.KeyState.All, _.bind(function () {
Utils.detectDropdownVisibility();
}, this));
}
2015-11-19 01:32:29 +08:00
remote() {
2014-08-22 23:08:56 +08:00
return null;
2015-11-19 01:32:29 +08:00
}
2014-08-22 23:08:56 +08:00
2015-11-19 01:32:29 +08:00
data() {
2014-08-22 23:08:56 +08:00
return null;
2015-11-19 01:32:29 +08:00
}
2014-08-22 23:08:56 +08:00
2014-08-20 23:03:12 +08:00
/**
2015-11-19 01:32:29 +08:00
* @param {string} link
2014-08-20 23:03:12 +08:00
* @return {boolean}
*/
2015-11-19 01:32:29 +08:00
download(link) {
2014-08-20 23:03:12 +08:00
if (Globals.sUserAgent && (Globals.sUserAgent.indexOf('chrome') > -1 || Globals.sUserAgent.indexOf('chrome') > -1))
2014-08-20 23:03:12 +08:00
{
2015-11-19 01:32:29 +08:00
const oLink = window.document.createElement('a');
oLink['href'] = link;
2014-08-20 23:03:12 +08:00
if (window.document['createEvent'])
{
2015-11-19 01:32:29 +08:00
const oE = window.document['createEvent']('MouseEvents');
2014-08-20 23:03:12 +08:00
if (oE && oE['initEvent'] && oLink['dispatchEvent'])
{
oE['initEvent']('click', true, true);
oLink['dispatchEvent'](oE);
return true;
}
}
}
if (Globals.bMobileDevice)
{
2015-11-19 01:32:29 +08:00
window.open(link, '_self');
2014-08-20 23:03:12 +08:00
window.focus();
}
else
{
2015-11-19 01:32:29 +08:00
this.iframe.attr('src', link);
// window.document.location.href = link;
2014-08-20 23:03:12 +08:00
}
return true;
2015-11-19 01:32:29 +08:00
}
/**
* @return {boolean}
*/
2015-11-19 01:32:29 +08:00
googlePreviewSupported() {
if (null === this.googlePreviewSupportedCache)
{
this.googlePreviewSupportedCache = !!Settings.settingsGet('AllowGoogleSocial') &&
!!Settings.settingsGet('AllowGoogleSocialPreview');
}
2014-11-01 01:25:02 +08:00
return this.googlePreviewSupportedCache;
2015-11-19 01:32:29 +08:00
}
/**
2015-11-19 01:32:29 +08:00
* @param {string} title
*/
2015-11-19 01:32:29 +08:00
setWindowTitle(title) {
title = ((Utils.isNormal(title) && 0 < title.length) ? '' + title : '');
2015-10-15 02:28:58 +08:00
if (Settings.settingsGet('Title'))
{
2015-11-19 01:32:29 +08:00
title += (title ? ' - ' : '') + Settings.settingsGet('Title');
2015-10-15 02:28:58 +08:00
}
2014-08-20 23:03:12 +08:00
2015-11-19 01:32:29 +08:00
window.document.title = title + ' ...';
window.document.title = title;
}
2014-08-20 23:03:12 +08:00
2015-11-19 01:32:29 +08:00
redirectToAdminPanel() {
_.delay(() => window.location.href = Links.rootAdmin(), 100);
}
2015-11-19 01:32:29 +08:00
clearClientSideToken() {
2014-10-06 02:37:31 +08:00
if (window.__rlah_clear)
{
window.__rlah_clear();
}
2015-11-19 01:32:29 +08:00
}
2014-10-06 02:37:31 +08:00
2015-05-06 00:41:15 +08:00
/**
2015-11-19 01:32:29 +08:00
* @param {string} key
2015-05-06 00:41:15 +08:00
*/
2015-11-19 01:32:29 +08:00
setClientSideToken(key) {
2015-05-06 00:41:15 +08:00
if (window.__rlah_set)
{
2015-11-19 01:32:29 +08:00
window.__rlah_set(key);
2015-05-06 00:41:15 +08:00
2015-11-19 01:32:29 +08:00
require('Storage/Settings').settingsSet('AuthAccountHash', key);
2015-05-06 00:41:15 +08:00
require('Common/Links').populateAuthSuffix();
}
2015-11-19 01:32:29 +08:00
}
2015-05-06 00:41:15 +08:00
2014-08-20 23:03:12 +08:00
/**
2015-11-19 01:32:29 +08:00
* @param {boolean=} admin = false
* @param {boolean=} logout = false
* @param {boolean=} close = false
2014-08-20 23:03:12 +08:00
*/
2015-11-19 01:32:29 +08:00
loginAndLogoutReload(admin = false, logout = false, close = false) {
const
kn = require('Knoin/Knoin'),
2015-11-19 01:32:29 +08:00
inIframe = !!Settings.settingsGet('InIframe')
2014-08-20 23:03:12 +08:00
;
2015-11-19 01:32:29 +08:00
let customLogoutLink = Utils.pString(Settings.settingsGet('CustomLogoutLink'))
2015-11-19 01:32:29 +08:00
if (logout)
2014-10-06 02:37:31 +08:00
{
this.clearClientSideToken();
}
2015-11-19 01:32:29 +08:00
if (logout && close && window.close)
2014-08-20 23:03:12 +08:00
{
window.close();
}
2015-11-19 01:32:29 +08:00
customLogoutLink = customLogoutLink || (admin ? Links.rootAdmin() : Links.rootUser());
2015-03-28 06:06:56 +08:00
2015-11-19 01:32:29 +08:00
if (logout && window.location.href !== customLogoutLink)
2014-08-20 23:03:12 +08:00
{
2015-11-19 01:32:29 +08:00
_.delay(() => {
if (inIframe && window.parent)
2014-08-20 23:03:12 +08:00
{
2015-11-19 01:32:29 +08:00
window.parent.location.href = customLogoutLink;
2014-08-20 23:03:12 +08:00
}
else
{
2015-11-19 01:32:29 +08:00
window.location.href = customLogoutLink;
2014-08-20 23:03:12 +08:00
}
}, 100);
}
else
{
kn.routeOff();
kn.setHash(Links.root(), true);
2014-08-20 23:03:12 +08:00
kn.routeOff();
2015-11-19 01:32:29 +08:00
_.delay(() => {
if (inIframe && window.parent)
2014-08-20 23:03:12 +08:00
{
window.parent.location.reload();
}
else
{
window.location.reload();
}
}, 100);
}
2015-11-19 01:32:29 +08:00
}
2015-11-19 01:32:29 +08:00
historyBack() {
2014-08-20 23:03:12 +08:00
window.history.back();
2015-11-19 01:32:29 +08:00
}
2014-08-20 23:03:12 +08:00
2015-11-19 01:32:29 +08:00
bootstart() {
// Utils.log('Ps' + 'ss, hac' + 'kers! The' + 're\'s not' + 'hing inte' + 'resting :' + ')');
2015-07-30 02:13:49 +08:00
2014-08-22 23:08:56 +08:00
Events.pub('rl.bootstart');
2014-08-25 15:10:51 +08:00
2015-11-19 01:32:29 +08:00
const
2014-10-29 06:05:50 +08:00
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('Radio', require('Component/Radio'));
2015-07-30 02:13:49 +08:00
ko.components.register('TextArea', require('Component/TextArea'));
ko.components.register('x-script', require('Component/Script'));
// ko.components.register('svg-icon', require('Component/SvgIcon'));
if (/**false && /**/Settings.settingsGet('MaterialDesign') && Globals.bAnimationSupported)
{
ko.components.register('Checkbox', require('Component/MaterialDesign/Checkbox'));
ko.components.register('CheckboxSimple', require('Component/Checkbox'));
}
else
{
2014-11-06 03:40:20 +08:00
// ko.components.register('Checkbox', require('Component/Classic/Checkbox'));
// ko.components.register('CheckboxSimple', require('Component/Classic/Checkbox'));
ko.components.register('Checkbox', require('Component/Checkbox'));
ko.components.register('CheckboxSimple', 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,
2015-11-19 01:32:29 +08:00
'onEnter': () => {
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
},
2015-11-19 01:32:29 +08:00
'onLeave': () => {
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,
2015-11-19 01:32:29 +08:00
'onEnter': () => {
2014-09-02 00:05:32 +08:00
Globals.$html.addClass('ssm-state-desktop');
2014-08-20 23:03:12 +08:00
},
2015-11-19 01:32:29 +08:00
'onLeave': () => {
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,
2015-11-19 01:32:29 +08:00
'onEnter': () => {
2014-09-02 00:05:32 +08:00
Globals.$html.addClass('ssm-state-desktop-large');
2014-08-20 23:03:12 +08:00
},
2015-11-19 01:32:29 +08:00
'onLeave': () => {
2014-09-02 00:05:32 +08:00
Globals.$html.removeClass('ssm-state-desktop-large');
2013-12-13 07:23:47 +08:00
}
});
2015-11-19 01:32:29 +08:00
Events.sub('ssm.mobile-enter', () => {
2014-08-22 23:08:56 +08:00
Globals.leftPanelDisabled(true);
2014-08-20 23:03:12 +08:00
});
2015-11-19 01:32:29 +08:00
Events.sub('ssm.mobile-leave', () => {
2014-08-22 23:08:56 +08:00
Globals.leftPanelDisabled(false);
2014-08-20 23:03:12 +08:00
});
2015-11-19 01:32:29 +08:00
Globals.leftPanelDisabled.subscribe((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
2015-11-19 01:32:29 +08:00
Globals.leftPanelType.subscribe((sValue) => {
Globals.$html.toggleClass('rl-left-panel-none', 'none' === sValue);
Globals.$html.toggleClass('rl-left-panel-short', 'short' === sValue);
});
2014-08-20 23:03:12 +08:00
ssm.ready();
require('Stores/Language').populate();
require('Stores/Theme').populate();
require('Stores/Social').populate();
2014-08-20 23:03:12 +08:00
};
2015-11-19 01:32:29 +08:00
}
2014-04-27 08:54:22 +08:00
2015-11-19 01:32:29 +08:00
export {AbstractApp, AbstractApp as default};