snappymail/dev/App/Abstract.js

416 lines
9.4 KiB
JavaScript
Raw Normal View History

2016-07-02 06:49:59 +08:00
import window from 'window';
import $ from '$';
import _ from '_';
import ko from 'ko';
2016-07-02 06:49:59 +08:00
import key from 'key';
import ssm from 'ssm';
2016-06-07 05:57:52 +08:00
import {
$win, $html, $doc,
startMicrotime, leftPanelDisabled, leftPanelType,
sUserAgent, bMobileDevice, bAnimationSupported
} from 'Common/Globals';
2016-07-08 07:22:58 +08:00
import {
noop, isNormal, pString, inArray, microtime, timestamp,
detectDropdownVisibility, windowResizeCallback
} from 'Common/Utils';
2016-08-24 06:17:50 +08:00
import {KeyState, Magics} from 'Common/Enums';
import {root, rootAdmin, rootUser, populateAuthSuffix} from 'Common/Links';
2016-06-17 07:23:49 +08:00
import {initOnStartOrLangChange, initNotificationLanguage} from 'Common/Translator';
2016-07-08 07:22:58 +08:00
import {toggle as toggleCmd} from 'Common/Cmd';
2016-08-24 06:17:50 +08:00
import * as Events from 'Common/Events';
import * as Settings from 'Storage/Settings';
2015-11-19 01:32:29 +08:00
import LanguageStore from 'Stores/Language';
import ThemeStore from 'Stores/Theme';
import SocialStore from 'Stores/Social';
2016-07-08 07:22:58 +08:00
import {routeOff, setHash} from 'Knoin/Knoin';
2015-11-19 01:32:29 +08:00
import {AbstractBoot} from 'Knoin/AbstractBoot';
class AbstractApp extends AbstractBoot
{
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-06-28 04:54:38 +08:00
this.googlePreviewSupportedCache = null;
this.isLocalAutocomplete = true;
this.iframe = null;
this.lastErrorTime = 0;
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-07-08 07:22:58 +08:00
$win.on('resize', () => {
Events.pub('window.resize');
});
2016-07-08 07:22:58 +08:00
Events.sub('window.resize', _.throttle(() => {
const
2016-06-07 05:57:52 +08:00
iH = $win.height(),
2016-06-30 08:02:45 +08:00
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');
}
2016-08-24 06:17:50 +08:00
}, Magics.Time50ms));
// DEBUG
// Events.sub({
// 'window.resize': function() {
// window.console.log('window.resize');
// },
// 'window.resize.real': function() {
// window.console.log('window.resize.real');
// }
// });
2016-07-08 07:22:58 +08:00
$doc.on('keydown', (event) => {
if (event && event.ctrlKey)
2014-08-20 23:03:12 +08:00
{
2016-06-07 05:57:52 +08:00
$html.addClass('rl-ctrl-key-pressed');
2014-08-20 23:03:12 +08:00
}
2016-07-08 07:22:58 +08:00
}).on('keyup', (event) => {
if (event && !event.ctrlKey)
2014-08-20 23:03:12 +08:00
{
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-07-08 07:22:58 +08:00
$doc.on('mousemove keypress click', _.debounce(() => {
2015-02-19 03:52:52 +08:00
Events.pub('rl.auto-logout-refresh');
2016-08-24 06:17:50 +08:00
}, Magics.Time5s));
2015-03-16 05:58:50 +08:00
2016-07-08 07:22:58 +08:00
key('esc, enter', KeyState.All, () => {
2016-06-07 05:57:52 +08:00
detectDropdownVisibility();
2016-07-08 07:22:58 +08:00
});
2016-08-22 05:30:34 +08:00
if (Settings.appSettingsGet('allowCmdInterface'))
{
key('ctrl+shift+`', KeyState.All, () => {
toggleCmd();
});
}
}
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
2016-06-30 08:02:45 +08:00
* @returns {boolean}
2014-08-20 23:03:12 +08:00
*/
2015-11-19 01:32:29 +08:00
download(link) {
2014-08-20 23:03:12 +08:00
2016-06-30 08:02:45 +08:00
if (sUserAgent && (-1 < sUserAgent.indexOf('chrome') || -1 < sUserAgent.indexOf('chrome')))
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
}
/**
2016-06-30 08:02:45 +08:00
* @returns {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() {
2016-07-01 06:50:11 +08:00
_.delay(() => {
2016-08-24 06:17:50 +08:00
window.location.href = rootAdmin();
}, Magics.Time100ms);
2015-11-19 01:32:29 +08:00
}
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-08-24 06:17:50 +08:00
Settings.settingsSet('AuthAccountHash', token);
populateAuthSuffix();
2015-05-06 00:41:15 +08:00
}
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 inIframe = !!Settings.appSettingsGet('inIframe');
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-08-24 06:17:50 +08:00
customLogoutLink = customLogoutLink || (admin ? rootAdmin() : 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(() => {
2015-11-19 01:32:29 +08:00
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
}
$win.trigger('rl.tooltips.diactivate');
2016-08-24 06:17:50 +08:00
}, Magics.Time100ms);
2014-08-20 23:03:12 +08:00
}
else
{
2016-07-08 07:22:58 +08:00
routeOff();
2016-08-24 06:17:50 +08:00
setHash(root(), true);
2016-07-08 07:22:58 +08:00
routeOff();
2014-08-20 23:03:12 +08:00
2015-11-19 01:32:29 +08:00
_.delay(() => {
2015-11-19 01:32:29 +08:00
if (inIframe && window.parent)
2014-08-20 23:03:12 +08:00
{
window.parent.location.reload();
}
else
{
window.location.reload();
}
$win.trigger('rl.tooltips.diactivate');
2016-08-24 06:17:50 +08:00
}, Magics.Time100ms);
2014-08-20 23:03:12 +08:00
}
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
const mobile = Settings.appSettingsGet('mobile');
2014-10-29 06:05:50 +08:00
2017-03-01 02:44:13 +08:00
ko.components.register('SaveTrigger', require('Component/SaveTrigger').default);
ko.components.register('Input', require('Component/Input').default);
ko.components.register('Select', require('Component/Select').default);
ko.components.register('Radio', require('Component/Radio').default);
ko.components.register('TextArea', require('Component/TextArea').default);
ko.components.register('Date', require('Component/Date').default);
2017-03-01 02:44:13 +08:00
ko.components.register('x-script', require('Component/Script').default);
// ko.components.register('svg-icon', require('Component/SvgIcon').default);
2016-06-07 05:57:52 +08:00
if (Settings.appSettingsGet('materialDesign') && bAnimationSupported)
{
2017-03-01 02:44:13 +08:00
ko.components.register('Checkbox', require('Component/MaterialDesign/Checkbox').default);
ko.components.register('CheckboxSimple', require('Component/Checkbox').default);
}
else
{
// ko.components.register('Checkbox', require('Component/Classic/Checkbox').default);
// ko.components.register('CheckboxSimple', require('Component/Classic/Checkbox').default);
2017-03-01 02:44:13 +08:00
ko.components.register('Checkbox', require('Component/Checkbox').default);
ko.components.register('CheckboxSimple', require('Component/Checkbox').default);
}
2014-08-20 23:03:12 +08:00
2016-06-17 07:23:49 +08:00
initOnStartOrLangChange(initNotificationLanguage);
2016-08-24 06:17:50 +08:00
_.delay(windowResizeCallback, Magics.Time1s);
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',
query: '(max-width: 767px)',
2016-05-01 09:07:10 +08:00
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',
query: '(min-width: 768px) and (max-width: 999px)',
2016-08-24 06:17:50 +08:00
onEnter: () => {
2016-06-07 05:57:52 +08:00
$html.addClass('ssm-state-tablet');
2016-05-01 09:07:10 +08:00
},
2016-08-24 06:17:50 +08:00
onLeave: () => {
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',
query: '(min-width: 1000px) and (max-width: 1400px)',
2016-05-01 09:07:10 +08:00
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',
query: '(min-width: 1401px)',
2016-05-01 09:07:10 +08:00
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
LanguageStore.populate();
ThemeStore.populate();
SocialStore.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};