snappymail/dev/App/Abstract.js

317 lines
7.7 KiB
JavaScript
Raw Normal View History

2016-07-02 06:49:59 +08:00
import window from 'window';
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 {
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';
2019-10-17 06:43:04 +08:00
import { isNormal, pString, detectDropdownVisibility, windowResizeCallback } from 'Common/Utils';
2016-07-08 07:22:58 +08:00
2019-07-05 03:19:24 +08:00
import { KeyState, Magics } from 'Common/Enums';
import { root, rootAdmin, rootUser, populateAuthSuffix } from 'Common/Links';
import { initOnStartOrLangChange, initNotificationLanguage } from 'Common/Translator';
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';
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;
window.addEventListener('resize', () => {
Events.pub('window.resize');
});
2019-07-05 03:19:24 +08:00
Events.sub(
'window.resize',
_.throttle(() => {
const iH = $win.height(),
iW = $win.height();
2019-07-05 03:19:24 +08:00
if ($win.__sizes[0] !== iH || $win.__sizes[1] !== iW) {
$win.__sizes[0] = iH;
$win.__sizes[1] = iW;
2019-07-05 03:19:24 +08:00
Events.pub('window.resize.real');
}
}, Magics.Time50ms)
);
// DEBUG
// Events.sub({
// 'window.resize': function() {
// window.console.log('window.resize');
// },
// 'window.resize.real': function() {
// window.console.log('window.resize.real');
// }
// });
const $doc = window.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 = _.debounce(() => {
Events.pub('rl.auto-logout-refresh');
}, Magics.Time5s);
$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) {
2015-11-19 01:32:29 +08:00
window.open(link, '_self');
2014-08-20 23:03:12 +08:00
window.focus();
2019-07-05 03:19:24 +08:00
} else {
const oLink = window.document.createElement('a');
oLink.href = link;
window.document.body.appendChild(oLink).click();
oLink.remove();
// window.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) {
2019-07-05 03:19:24 +08:00
title = isNormal(title) && 0 < title.length ? '' + title : '';
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;
}
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() {
2019-07-05 03:19:24 +08:00
if (window.__rlah_clear) {
2014-10-06 02:37:31 +08:00
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) {
2019-07-05 03:19:24 +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'));
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
2019-07-05 03:19:24 +08:00
if (logout && window.location.href !== customLogoutLink) {
2015-11-19 01:32:29 +08:00
_.delay(() => {
2019-07-05 03:19:24 +08:00
if (inIframe && window.parent) {
2015-11-19 01:32:29 +08:00
window.parent.location.href = customLogoutLink;
2019-07-05 03:19:24 +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);
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
2015-11-19 01:32:29 +08:00
_.delay(() => {
2019-07-05 03:19:24 +08:00
if (inIframe && window.parent) {
2014-08-20 23:03:12 +08:00
window.parent.location.reload();
2019-07-05 03:19:24 +08:00
} else {
2014-08-20 23:03:12 +08:00
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);
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 {
// 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
});
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');
2016-05-01 09:07:10 +08:00
Events.pub('ssm.mobile-enter');
},
onLeave: () => {
$htmlCL.remove('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: () => {
$htmlCL.add('ssm-state-tablet');
2016-05-01 09:07:10 +08:00
},
2016-08-24 06:17:50 +08:00
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)',
2016-05-01 09:07:10 +08:00
onEnter: () => {
$htmlCL.add('ssm-state-desktop');
2016-05-01 09:07:10 +08:00
},
onLeave: () => {
$htmlCL.remove('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: () => {
$htmlCL.add('ssm-state-desktop-large');
2016-05-01 09:07:10 +08:00
},
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');
2016-05-01 09:07:10 +08:00
Events.pub('ssm.mobile-enter');
}
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 };