snappymail/dev/App/Abstract.jsx

399 lines
8.8 KiB
React
Raw Normal View History

2015-11-19 01:32:29 +08:00
import {window, _, $, key} from 'common';
2016-06-07 05:57:52 +08:00
import {
$win, $html, $doc,
startMicrotime, leftPanelDisabled, leftPanelType,
sUserAgent, bMobileDevice, bAnimationSupported
} from 'Common/Globals';
import {KeyState} from 'Common/Enums';
import {noop, isNormal, pString, inArray, microtime, timestamp, detectDropdownVisibility, windowResizeCallback} from 'Common/Utils';
2016-06-16 07:36:44 +08:00
import * as Links from 'Common/Links';
import * as Settings from 'Storage/Settings';
import * as Events from 'Common/Events';
2016-06-17 07:23:49 +08:00
import {initOnStartOrLangChange, initNotificationLanguage} from 'Common/Translator';
2015-11-19 01:32:29 +08:00
import {AbstractBoot} from 'Knoin/AbstractBoot';
class AbstractApp extends AbstractBoot
{
googlePreviewSupportedCache = null;
isLocalAutocomplete = true;
iframe = null;
2016-06-07 05:57:52 +08:00
lastErrorTime = 0;
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
2016-05-24 05:56:48 +08:00
this.iframe = $('<iframe class="internal-hiddden" />').appendTo('body');
2014-08-20 23:03:12 +08:00
2016-06-07 05:57:52 +08:00
$win.on('error', (event) => {
if (event && event.originalEvent && event.originalEvent.message &&
-1 === inArray(event.originalEvent.message, [
2014-08-20 23:03:12 +08:00
'Script error.', 'Uncaught Error: Error calling method on NPObject.'
]))
{
2016-06-07 05:57:52 +08:00
const time = timestamp();
if (this.lastErrorTime >= time)
{
return;
}
this.lastErrorTime = time;
2014-08-22 23:08:56 +08:00
Remote.jsError(
2016-06-07 05:57:52 +08:00
noop,
event.originalEvent.message,
event.originalEvent.filename,
event.originalEvent.lineno,
2014-08-20 23:03:12 +08:00
window.location && window.location.toString ? window.location.toString() : '',
2016-06-07 05:57:52 +08:00
$html.attr('class'),
microtime() - startMicrotime
2014-08-20 23:03:12 +08:00
);
}
2014-08-20 23:03:12 +08:00
});
2016-06-07 05:57:52 +08:00
$win.on('resize', function () {
Events.pub('window.resize');
});
Events.sub('window.resize', _.throttle(function () {
var
2016-06-07 05:57:52 +08:00
iH = $win.height(),
iW = $win.height()
;
2016-06-07 05:57:52 +08:00
if ($win.__sizes[0] !== iH || $win.__sizes[1] !== iW)
{
2016-06-07 05:57:52 +08:00
$win.__sizes[0] = iH;
$win.__sizes[1] = iW;
Events.pub('window.resize.real');
}
}, 50));
2016-04-21 01:12:51 +08:00
// DEBUG
// Events.sub({
// 'window.resize': function () {
// window.console.log('window.resize');
// },
// 'window.resize.real': function () {
// window.console.log('window.resize.real');
// }
// });
2016-06-07 05:57:52 +08:00
$doc.on('keydown', function (oEvent) {
2014-08-20 23:03:12 +08:00
if (oEvent && oEvent.ctrlKey)
{
2016-06-07 05:57:52 +08:00
$html.addClass('rl-ctrl-key-pressed');
2014-08-20 23:03:12 +08:00
}
}).on('keyup', function (oEvent) {
if (oEvent && !oEvent.ctrlKey)
{
2016-06-07 05:57:52 +08:00
$html.removeClass('rl-ctrl-key-pressed');
2014-08-20 23:03:12 +08:00
}
});
2015-02-19 03:52:52 +08:00
2016-06-07 05:57:52 +08:00
$doc.on('mousemove keypress click', _.debounce(function () {
2015-02-19 03:52:52 +08:00
Events.pub('rl.auto-logout-refresh');
}, 5000));
2015-03-16 05:58:50 +08:00
2016-06-07 05:57:52 +08:00
key('esc, enter', KeyState.All, _.bind(function () {
detectDropdownVisibility();
2015-03-16 05:58:50 +08:00
}, 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
2016-04-30 07:42:18 +08:00
getApplicationConfiguration(name, default_) {
return this.applicationConfiguration[name] || default_;
}
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
2016-06-07 05:57:52 +08:00
if (sUserAgent && (sUserAgent.indexOf('chrome') > -1 || 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');
2016-04-21 01:12:51 +08:00
oLink.href = link;
2014-08-20 23:03:12 +08:00
2016-04-21 01:12:51 +08:00
if (window.document && window.document.createEvent)
2014-08-20 23:03:12 +08:00
{
2016-04-21 01:12:51 +08:00
const oE = window.document.createEvent.MouseEvents;
if (oE && oE.initEvent && oLink.dispatchEvent)
2014-08-20 23:03:12 +08:00
{
2016-04-21 01:12:51 +08:00
oE.initEvent('click', true, true);
oLink.dispatchEvent(oE);
2014-08-20 23:03:12 +08:00
return true;
}
}
}
2016-06-07 05:57:52 +08:00
if (bMobileDevice)
2014-08-20 23:03:12 +08:00
{
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) {
2016-06-07 05:57:52 +08:00
title = ((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
/**
2016-04-21 01:12:51 +08:00
* @param {string} token
2015-05-06 00:41:15 +08:00
*/
2016-04-21 01:12:51 +08:00
setClientSideToken(token) {
2015-05-06 00:41:15 +08:00
if (window.__rlah_set)
{
2016-04-21 01:12:51 +08:00
window.__rlah_set(token);
2015-05-06 00:41:15 +08:00
2016-04-21 01:12:51 +08:00
require('Storage/Settings').settingsSet('AuthAccountHash', token);
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'),
2016-05-03 04:42:29 +08:00
mobile = Settings.appSettingsGet('mobile'),
2016-04-30 07:42:18 +08:00
inIframe = !!Settings.appSettingsGet('inIframe')
2014-08-20 23:03:12 +08:00
;
2016-06-07 05:57:52 +08:00
let customLogoutLink = pString(Settings.appSettingsGet('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();
}
2016-05-03 04:42:29 +08:00
customLogoutLink = customLogoutLink || (admin ? Links.rootAdmin(mobile) : Links.rootUser(mobile));
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() {
2016-06-07 05:57:52 +08:00
// 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
2016-05-01 09:07:10 +08:00
mobile = Settings.appSettingsGet('mobile'),
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'));
2016-06-07 05:57:52 +08:00
if (Settings.appSettingsGet('materialDesign') && 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
2016-06-17 07:23:49 +08:00
initOnStartOrLangChange(initNotificationLanguage);
2016-06-07 05:57:52 +08:00
_.delay(windowResizeCallback, 1000);
2014-08-20 23:03:12 +08:00
2015-11-19 01:32:29 +08:00
Events.sub('ssm.mobile-enter', () => {
2016-06-07 05:57:52 +08:00
leftPanelDisabled(true);
2014-08-20 23:03:12 +08:00
});
2015-11-19 01:32:29 +08:00
Events.sub('ssm.mobile-leave', () => {
2016-06-07 05:57:52 +08:00
leftPanelDisabled(false);
2014-08-20 23:03:12 +08:00
});
2016-05-01 09:07:10 +08:00
if (!mobile)
{
ssm.addState({
id: 'mobile',
maxWidth: 767,
onEnter: () => {
2016-06-07 05:57:52 +08:00
$html.addClass('ssm-state-mobile');
2016-05-01 09:07:10 +08:00
Events.pub('ssm.mobile-enter');
},
onLeave: () => {
2016-06-07 05:57:52 +08:00
$html.removeClass('ssm-state-mobile');
2016-05-01 09:07:10 +08:00
Events.pub('ssm.mobile-leave');
}
});
ssm.addState({
id: 'tablet',
minWidth: 768,
maxWidth: 999,
onEnter: function() {
2016-06-07 05:57:52 +08:00
$html.addClass('ssm-state-tablet');
2016-05-01 09:07:10 +08:00
},
onLeave: function() {
2016-06-07 05:57:52 +08:00
$html.removeClass('ssm-state-tablet');
2016-05-01 09:07:10 +08:00
}
});
ssm.addState({
id: 'desktop',
minWidth: 1000,
maxWidth: 1400,
onEnter: () => {
2016-06-07 05:57:52 +08:00
$html.addClass('ssm-state-desktop');
2016-05-01 09:07:10 +08:00
},
onLeave: () => {
2016-06-07 05:57:52 +08:00
$html.removeClass('ssm-state-desktop');
2016-05-01 09:07:10 +08:00
}
});
ssm.addState({
id: 'desktop-large',
minWidth: 1400,
onEnter: () => {
2016-06-07 05:57:52 +08:00
$html.addClass('ssm-state-desktop-large');
2016-05-01 09:07:10 +08:00
},
onLeave: () => {
2016-06-07 05:57:52 +08:00
$html.removeClass('ssm-state-desktop-large');
2016-05-01 09:07:10 +08:00
}
});
}
else
{
2016-06-07 05:57:52 +08:00
$html.addClass('ssm-state-mobile').addClass('rl-mobile');
2016-05-01 09:07:10 +08:00
Events.pub('ssm.mobile-enter');
}
2016-06-07 05:57:52 +08:00
leftPanelDisabled.subscribe((bValue) => {
$html.toggleClass('rl-left-panel-disabled', bValue);
$html.toggleClass('rl-left-panel-enabled', !bValue);
2014-08-20 23:03:12 +08:00
});
2014-04-27 08:54:22 +08:00
2016-06-07 05:57:52 +08:00
leftPanelType.subscribe((sValue) => {
$html.toggleClass('rl-left-panel-none', 'none' === sValue);
$html.toggleClass('rl-left-panel-short', 'short' === sValue);
});
2016-06-07 05:57:52 +08:00
leftPanelDisabled.valueHasMutated();
2016-04-29 04:32:54 +08:00
2014-08-20 23:03:12 +08:00
ssm.ready();
require('Stores/Language').populate();
require('Stores/Theme').populate();
require('Stores/Social').populate();
2016-04-21 01:12:51 +08:00
}
2015-11-19 01:32:29 +08:00
}
2014-04-27 08:54:22 +08:00
2016-04-21 01:12:51 +08:00
export {AbstractApp, AbstractApp as default};