snappymail/dev/Common/Booter.js

254 lines
5.4 KiB
JavaScript
Raw Normal View History

2016-05-06 23:14:40 +08:00
import window from 'window';
2016-05-20 08:04:15 +08:00
import progressJs from 'progressJs';
2016-05-06 23:14:40 +08:00
2019-07-05 03:19:24 +08:00
import { jassl } from 'Common/Jassl';
import { getHash, setHash, clearHash } from 'Storage/RainLoop';
2016-05-20 08:04:15 +08:00
2016-06-17 07:23:49 +08:00
let RL_APP_DATA_STORAGE = null;
2016-05-06 23:14:40 +08:00
2018-10-06 07:13:58 +08:00
/* eslint-disable camelcase,spaced-comment */
2016-06-17 07:23:49 +08:00
window.__rlah = () => getHash();
window.__rlah_set = () => setHash();
window.__rlah_clear = () => clearHash();
window.__rlah_data = () => RL_APP_DATA_STORAGE;
2016-05-06 23:14:40 +08:00
2016-06-30 08:02:45 +08:00
/**
* @param {string} styles
* @returns {void}
*/
2019-07-05 03:19:24 +08:00
function includeStyle(styles) {
const doc = window.document, style = doc.createElement('style');
2020-03-16 04:14:52 +08:00
style.type = 'text/css';
style.textContent = styles;
// style.appendChild(doc.createTextNode(styles));
doc.head.appendChild(style);
2016-05-20 08:04:15 +08:00
}
2016-05-06 23:14:40 +08:00
2016-06-30 08:02:45 +08:00
/**
* @param {string} src
* @returns {void}
*/
2019-07-05 03:19:24 +08:00
function includeScr(src) {
const doc = window.document, script = doc.createElement('script');
2020-03-16 04:14:52 +08:00
script.type = 'text/javascript';
script.src = src;
doc.head.appendChild(script);
2016-05-20 08:04:15 +08:00
}
2016-05-07 22:42:36 +08:00
2016-06-30 08:02:45 +08:00
/**
* @returns {boolean}
*/
2019-07-05 03:19:24 +08:00
function includeLayout() {
2016-06-17 07:23:49 +08:00
const app = window.document.getElementById('rl-app');
2016-05-20 08:04:15 +08:00
2019-03-28 06:01:26 +08:00
require('Styles/@Boot.css');
2016-05-24 05:56:48 +08:00
2019-07-05 03:19:24 +08:00
if (app) {
2019-03-28 06:01:26 +08:00
const layout = require('Html/Layout.html');
app.innerHTML = ((layout && layout.default ? layout.default : layout) || '').replace(/[\r\n\t]+/g, '');
2016-05-20 08:04:15 +08:00
return true;
}
return false;
}
2016-06-30 08:02:45 +08:00
/**
2016-10-19 01:52:43 +08:00
* @param {boolean} admin = false
* @param {boolean} mobile = false
* @param {boolean} mobileDevice = false
2016-06-30 08:02:45 +08:00
* @returns {void}
*/
2019-07-05 03:19:24 +08:00
function includeAppScr({ admin = false, mobile = false, mobileDevice = false }) {
2016-05-20 08:04:15 +08:00
let src = './?/';
2016-06-30 08:02:45 +08:00
src += admin ? 'Admin' : '';
2016-05-20 08:04:15 +08:00
src += 'AppData@';
2016-06-30 08:02:45 +08:00
src += mobile ? 'mobile' : 'no-mobile';
src += mobileDevice ? '-1' : '-0';
2016-05-20 08:04:15 +08:00
src += '/';
2019-07-05 03:19:24 +08:00
includeScr(
src +
(window.__rlah ? window.__rlah() || '0' : '0') +
'/' +
window.Math.random()
.toString()
.substr(2) +
'/'
);
2016-05-20 08:04:15 +08:00
}
2016-05-06 23:14:40 +08:00
2016-06-30 08:02:45 +08:00
/**
* @returns {object}
*/
2019-07-05 03:19:24 +08:00
function getRainloopBootData() {
2016-05-20 08:04:15 +08:00
let result = {};
const meta = window.document.getElementById('app-boot-data');
2016-05-06 23:14:40 +08:00
2019-07-05 03:19:24 +08:00
if (meta && meta.getAttribute) {
2016-05-20 08:04:15 +08:00
result = JSON.parse(meta.getAttribute('content')) || {};
}
return result;
}
2016-06-30 08:02:45 +08:00
/**
* @param {string} additionalError
* @returns {void}
*/
2019-07-05 03:19:24 +08:00
function showError(additionalError) {
const oR = window.document.getElementById('rl-loading'),
2016-05-06 23:14:40 +08:00
oL = window.document.getElementById('rl-loading-error'),
2016-06-30 08:02:45 +08:00
oLA = window.document.getElementById('rl-loading-error-additional');
2016-05-06 23:14:40 +08:00
2019-07-05 03:19:24 +08:00
if (oR) {
2016-05-06 23:14:40 +08:00
oR.style.display = 'none';
}
2019-07-05 03:19:24 +08:00
if (oL) {
2016-05-06 23:14:40 +08:00
oL.style.display = 'block';
}
2019-07-05 03:19:24 +08:00
if (oLA && additionalError) {
2016-05-06 23:14:40 +08:00
oLA.style.display = 'block';
oLA.innerHTML = additionalError;
}
2019-07-05 03:19:24 +08:00
if (progressJs) {
2016-05-20 08:04:15 +08:00
progressJs.set(100).end();
2016-05-06 23:14:40 +08:00
}
2016-05-20 08:04:15 +08:00
}
2016-05-06 23:14:40 +08:00
2016-06-30 08:02:45 +08:00
/**
* @param {string} description
* @returns {void}
*/
2019-07-05 03:19:24 +08:00
function showDescriptionAndLoading(description) {
const oE = window.document.getElementById('rl-loading'),
2016-06-30 08:02:45 +08:00
oElDesc = window.document.getElementById('rl-loading-desc');
2016-05-06 23:14:40 +08:00
2019-07-05 03:19:24 +08:00
if (oElDesc && description) {
2016-05-06 23:14:40 +08:00
oElDesc.innerHTML = description;
}
2019-07-05 03:19:24 +08:00
if (oE && oE.style) {
2016-05-06 23:14:40 +08:00
oE.style.opacity = 0;
window.setTimeout(() => {
oE.style.opacity = 1;
}, 300);
}
2016-05-20 08:04:15 +08:00
}
2016-05-06 23:14:40 +08:00
2016-06-30 08:02:45 +08:00
/**
* @param {boolean} withError
* @param {string} additionalError
* @returns {void}
*/
2019-07-05 03:19:24 +08:00
function runMainBoot(withError, additionalError) {
if (window.__APP_BOOT && !withError) {
2016-06-07 05:57:52 +08:00
window.__APP_BOOT(() => {
showError(additionalError);
2016-05-06 23:14:40 +08:00
});
2019-07-05 03:19:24 +08:00
} else {
2016-05-20 08:04:15 +08:00
showError(additionalError);
}
}
2016-05-06 23:14:40 +08:00
2016-06-30 08:02:45 +08:00
/**
* @returns {void}
*/
2019-07-05 03:19:24 +08:00
function runApp() {
2016-05-20 08:04:15 +08:00
const appData = window.__rlah_data();
2016-05-06 23:14:40 +08:00
2019-07-05 03:19:24 +08:00
if (
jassl &&
progressJs &&
appData &&
appData.TemplatesLink &&
2019-07-05 03:19:24 +08:00
appData.LangLink &&
appData.StaticLibJsLink &&
appData.StaticAppJsLink &&
appData.StaticEditorJsLink
) {
2016-05-20 08:04:15 +08:00
const p = progressJs;
2016-05-06 23:14:40 +08:00
2019-07-05 03:19:24 +08:00
p.setOptions({ theme: 'rainloop' });
2016-05-06 23:14:40 +08:00
p.start().set(5);
2019-07-05 03:19:24 +08:00
const libs = () =>
jassl(appData.StaticLibJsLink).then(() => {
window.document.getElementById('rl-check').remove();
if (appData.IncludeBackground) {
const img = appData.IncludeBackground.replace('{{USER}}', window.__rlah ? window.__rlah() || '0' : '0');
if (img) {
window.document.documentElement.classList.add('UserBackground');
window.document.body.style.backgroundImage = "url("+img+")";
2019-07-05 03:19:24 +08:00
}
2016-05-06 23:14:40 +08:00
}
2019-07-05 03:19:24 +08:00
});
2018-07-10 05:05:02 +08:00
libs()
.then(() => {
p.set(20);
return window.Promise.all([jassl(appData.TemplatesLink), jassl(appData.LangLink)]);
2018-07-10 05:05:02 +08:00
})
2016-07-02 06:49:59 +08:00
.then(() => {
2016-05-06 23:14:40 +08:00
p.set(50);
return appData.PluginsLink ? jassl(appData.PluginsLink) : window.Promise.resolve();
2016-07-02 06:49:59 +08:00
})
.then(() => {
2016-05-06 23:14:40 +08:00
p.set(70);
2016-05-20 08:04:15 +08:00
runMainBoot(false);
2016-07-02 06:49:59 +08:00
})
.catch((e) => {
2016-05-20 08:04:15 +08:00
runMainBoot(true);
throw e;
2016-07-02 06:49:59 +08:00
})
.then(() => jassl(appData.StaticEditorJsLink))
2016-07-02 06:49:59 +08:00
.then(() => {
2016-05-06 23:14:40 +08:00
if (window.CKEDITOR && window.__initEditor) {
window.__initEditor();
window.__initEditor = null;
}
2016-06-30 08:02:45 +08:00
});
2019-07-05 03:19:24 +08:00
} else {
2016-05-20 08:04:15 +08:00
runMainBoot(true);
2016-05-06 23:14:40 +08:00
}
2016-05-20 08:04:15 +08:00
}
2016-06-30 08:02:45 +08:00
/**
* @param {mixed} data
* @returns {void}
*/
2016-05-20 08:04:15 +08:00
window.__initAppData = function(data) {
2016-06-17 07:23:49 +08:00
RL_APP_DATA_STORAGE = data;
2016-05-20 08:04:15 +08:00
window.__rlah_set();
2019-07-05 03:19:24 +08:00
if (RL_APP_DATA_STORAGE) {
if (RL_APP_DATA_STORAGE.NewThemeLink) {
2016-06-17 07:23:49 +08:00
(window.document.getElementById('app-theme-link') || {}).href = RL_APP_DATA_STORAGE.NewThemeLink;
}
2016-05-20 08:04:15 +08:00
2019-07-05 03:19:24 +08:00
if (RL_APP_DATA_STORAGE.IncludeCss) {
2016-06-17 07:23:49 +08:00
includeStyle(RL_APP_DATA_STORAGE.IncludeCss);
}
2016-05-20 08:04:15 +08:00
2016-06-17 07:23:49 +08:00
showDescriptionAndLoading(RL_APP_DATA_STORAGE.LoadingDescriptionEsc || '');
}
2016-05-20 08:04:15 +08:00
runApp();
2016-05-06 23:14:40 +08:00
};
2016-05-20 08:04:15 +08:00
2016-06-30 08:02:45 +08:00
/**
* @returns {void}
*/
2016-05-20 08:04:15 +08:00
window.__runBoot = function() {
2019-07-05 03:19:24 +08:00
if (!window.navigator || !window.navigator.cookieEnabled) {
2016-05-20 08:04:15 +08:00
window.document.location.replace('./?/NoCookie');
}
2019-07-05 03:19:24 +08:00
if (includeLayout()) {
2016-05-20 08:04:15 +08:00
includeAppScr(getRainloopBootData());
}
};