snappymail/dev/App/Abstract.js

270 lines
6.4 KiB
JavaScript
Raw Normal View History

import ko from 'ko';
2016-06-07 05:57:52 +08:00
import {
2019-07-05 03:19:24 +08:00
$win,
$htmlCL,
2019-07-05 03:19:24 +08:00
leftPanelDisabled,
leftPanelType,
bMobileDevice
2016-06-07 05:57:52 +08:00
} from 'Common/Globals';
import { pString, detectDropdownVisibility, windowResizeCallback } from 'Common/Utils';
2016-07-08 07:22:58 +08:00
2020-08-14 04:58:41 +08:00
import { KeyState } from 'Common/Enums';
2019-07-05 03:19:24 +08:00
import { root, rootAdmin, rootUser, populateAuthSuffix } from 'Common/Links';
import { initOnStartOrLangChange, initNotificationLanguage } from 'Common/Translator';
2016-08-24 06:17:50 +08:00
import * as Settings from 'Storage/Settings';
2015-11-19 01:32:29 +08:00
import LanguageStore from 'Stores/Language';
import ThemeStore from 'Stores/Theme';
2019-07-05 03:19:24 +08:00
import { routeOff, setHash } from 'Knoin/Knoin';
import { AbstractBoot } from 'Knoin/AbstractBoot';
2015-11-19 01:32:29 +08:00
2019-07-05 03:19:24 +08:00
class AbstractApp extends AbstractBoot {
2014-08-20 23:03:12 +08:00
/**
* @param {RemoteStorage|AdminRemoteStorage} Remote
2014-08-20 23:03:12 +08:00
*/
2019-10-17 06:43:04 +08:00
constructor() {
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.isLocalAutocomplete = true;
this.lastErrorTime = 0;
addEventListener(
'resize',
(()=>{
const iH = $win.height(),
iW = $win.height();
if ($win.__sizes[0] !== iH || $win.__sizes[1] !== iW) {
$win.__sizes[0] = iH;
$win.__sizes[1] = iW;
dispatchEvent(new CustomEvent('resize.real'));
2019-07-05 03:19:24 +08:00
}
}).throttle(50)
2019-07-05 03:19:24 +08:00
);
const $doc = document;
$doc.addEventListener('keydown', (event) => {
if (event && event.ctrlKey) {
$htmlCL.add('rl-ctrl-key-pressed');
}
});
$doc.addEventListener('keyup', (event) => {
if (event && !event.ctrlKey) {
$htmlCL.remove('rl-ctrl-key-pressed');
}
});
const fn = (()=>dispatchEvent(new CustomEvent('rl.auto-logout-refresh'))).debounce(5000);
$doc.addEventListener('mousemove', fn);
$doc.addEventListener('keypress', fn);
$doc.addEventListener('click', fn);
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
});
}
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) {
2019-07-05 03:19:24 +08:00
if (bMobileDevice) {
open(link, '_self');
focus();
2019-07-05 03:19:24 +08:00
} else {
const oLink = document.createElement('a');
oLink.href = link;
document.body.appendChild(oLink).click();
oLink.remove();
// open(link, '_self');
2014-08-20 23:03:12 +08:00
}
return true;
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 = pString(title);
2019-07-05 03:19:24 +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
document.title = title;
2015-11-19 01:32:29 +08:00
}
2014-08-20 23:03:12 +08:00
2015-11-19 01:32:29 +08:00
redirectToAdminPanel() {
setTimeout(() => {
location.href = rootAdmin();
2020-08-14 04:58:41 +08:00
}, 100);
2015-11-19 01:32:29 +08:00
}
2015-11-19 01:32:29 +08:00
clearClientSideToken() {
2019-07-05 03:19:24 +08:00
if (window.__rlah_clear) {
__rlah_clear();
2014-10-06 02:37:31 +08:00
}
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) {
2019-07-05 03:19:24 +08:00
if (window.__rlah_set) {
__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'));
2019-07-05 03:19:24 +08:00
if (logout) {
2014-10-06 02:37:31 +08:00
this.clearClientSideToken();
}
2019-07-05 03:19:24 +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
if (logout && location.href !== customLogoutLink) {
setTimeout(() => {
if (inIframe && parent) {
parent.location.href = customLogoutLink;
2019-07-05 03:19:24 +08:00
} else {
location.href = customLogoutLink;
2014-08-20 23:03:12 +08:00
}
$win.trigger('rl.tooltips.diactivate');
2020-08-14 04:58:41 +08:00
}, 100);
2019-07-05 03:19:24 +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
setTimeout(() => {
if (inIframe && parent) {
parent.location.reload();
2019-07-05 03:19:24 +08:00
} else {
location.reload();
2014-08-20 23:03:12 +08:00
}
$win.trigger('rl.tooltips.diactivate');
2020-08-14 04:58:41 +08:00
}, 100);
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() {
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() {
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);
if (Settings.appSettingsGet('materialDesign') && !bMobileDevice) {
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);
2019-07-05 03:19:24 +08:00
} else {
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);
2020-08-14 04:58:41 +08:00
setTimeout(windowResizeCallback, 1000);
2014-08-20 23:03:12 +08:00
2019-07-05 03:19:24 +08:00
if (!mobile) {
$htmlCL.add('rl-desktop');
2016-05-01 09:07:10 +08:00
ssm.addState({
id: 'mobile',
query: '(max-width: 767px)',
2016-05-01 09:07:10 +08:00
onEnter: () => {
$htmlCL.add('ssm-state-mobile');
leftPanelDisabled(true);
2016-05-01 09:07:10 +08:00
},
onLeave: () => {
$htmlCL.remove('ssm-state-mobile');
leftPanelDisabled(false);
2016-05-01 09:07:10 +08:00
}
});
ssm.addState({
id: 'tablet',
query: '(min-width: 768px) and (max-width: 999px)',
2020-08-14 04:58:41 +08:00
onEnter: () => $htmlCL.add('ssm-state-tablet'),
onLeave: () => $htmlCL.remove('ssm-state-tablet')
2016-05-01 09:07:10 +08:00
});
ssm.addState({
id: 'desktop',
query: '(min-width: 1000px) and (max-width: 1400px)',
2020-08-14 04:58:41 +08:00
onEnter: () => $htmlCL.add('ssm-state-desktop'),
onLeave: () => $htmlCL.remove('ssm-state-desktop')
2016-05-01 09:07:10 +08:00
});
ssm.addState({
id: 'desktop-large',
query: '(min-width: 1401px)',
2020-08-14 04:58:41 +08:00
onEnter: () => $htmlCL.add('ssm-state-desktop-large'),
onLeave: () => $htmlCL.remove('ssm-state-desktop-large')
2016-05-01 09:07:10 +08:00
});
2019-07-05 03:19:24 +08:00
} else {
$htmlCL.add('ssm-state-mobile', 'rl-mobile');
leftPanelDisabled(true);
2016-05-01 09:07:10 +08:00
}
2016-06-07 05:57:52 +08:00
leftPanelDisabled.subscribe((bValue) => {
$htmlCL.toggle('rl-left-panel-disabled', bValue);
$htmlCL.toggle('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) => {
$htmlCL.toggle('rl-left-panel-none', 'none' === sValue);
$htmlCL.toggle('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();
2016-04-21 01:12:51 +08:00
}
2015-11-19 01:32:29 +08:00
}
2014-04-27 08:54:22 +08:00
2019-07-05 03:19:24 +08:00
export { AbstractApp, AbstractApp as default };