snappymail/dev/Common/Booter.js

188 lines
4.2 KiB
JavaScript
Raw Normal View History

2019-07-05 03:19:24 +08:00
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
const doc = document;
2020-08-10 17:32:05 +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_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
/**
* @returns {void}
*/
2020-07-28 17:23:06 +08:00
function showError() {
2020-08-10 17:32:05 +08:00
const oR = doc.getElementById('rl-loading'),
oL = doc.getElementById('rl-loading-error');
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';
}
2020-08-07 22:28:30 +08:00
if (window.progressJs) {
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 {boolean} withError
* @returns {void}
*/
2020-07-28 17:23:06 +08:00
function runMainBoot(withError) {
2019-07-05 03:19:24 +08:00
if (window.__APP_BOOT && !withError) {
2020-08-10 17:32:05 +08:00
window.__APP_BOOT(() => showError());
2019-07-05 03:19:24 +08:00
} else {
2020-07-28 17:23:06 +08:00
showError();
2016-05-20 08:04:15 +08:00
}
}
2016-05-06 23:14:40 +08:00
function writeCSS(css) {
const style = doc.createElement('style');
style.type = 'text/css';
style.textContent = css;
// style.appendChild(doc.createTextNode(styles));
doc.head.appendChild(style);
}
2020-08-10 17:32:05 +08:00
function loadScript(src) {
if (!src) {
throw new Error('src should not be empty.');
}
return new Promise((resolve, reject) => {
2020-08-10 17:32:05 +08:00
const script = doc.createElement('script');
script.onload = () => resolve();
script.onerror = () => reject(new Error(src));
script.src = src;
// script.type = 'text/javascript';
doc.head.appendChild(script);
// doc.body.appendChild(element);
});
}
2016-06-30 08:02:45 +08:00
/**
2020-07-28 17:23:06 +08:00
* @param {mixed} data
2016-06-30 08:02:45 +08:00
* @returns {void}
*/
2020-07-28 17:23:06 +08:00
window.__initAppData = data => {
RL_APP_DATA_STORAGE = data;
window.__rlah_set();
if (RL_APP_DATA_STORAGE) {
const css = RL_APP_DATA_STORAGE.IncludeCss,
theme = RL_APP_DATA_STORAGE.NewThemeLink,
description= RL_APP_DATA_STORAGE.LoadingDescriptionEsc || '',
oE = doc.getElementById('rl-loading'),
oElDesc = doc.getElementById('rl-loading-desc');
if (theme) {
(doc.getElementById('app-theme-link') || {}).href = theme;
}
css && writeCSS(css);
2020-07-28 17:23:06 +08:00
if (oElDesc && description) {
oElDesc.innerHTML = description;
}
if (oE && oE.style) {
oE.style.opacity = 0;
setTimeout(() => oE.style.opacity = 1, 300);
2020-07-28 17:23:06 +08:00
}
}
const appData = window.__rlah_data(), p = progressJs;
2016-05-06 23:14:40 +08:00
2019-07-05 03:19:24 +08:00
if (
2020-07-28 17:23:06 +08:00
p &&
2019-07-05 03:19:24 +08:00
appData &&
appData.TemplatesLink &&
2019-07-05 03:19:24 +08:00
appData.LangLink &&
appData.StaticLibJsLink &&
appData.StaticAppJsLink &&
appData.StaticEditorJsLink
) {
p.set(5);
2016-05-06 23:14:40 +08:00
2020-08-10 17:32:05 +08:00
const libs = () =>
loadScript(appData.StaticLibJsLink).then(() => {
2020-07-28 17:23:06 +08:00
doc.getElementById('rl-check').remove();
if (appData.IncludeBackground) {
const img = appData.IncludeBackground.replace('{{USER}}', getHash() || '0');
if (img) {
2020-07-28 17:23:06 +08:00
doc.documentElement.classList.add('UserBackground');
doc.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 Promise.all([loadScript(appData.TemplatesLink), loadScript(appData.LangLink)]);
2018-07-10 05:05:02 +08:00
})
.then(() => {
p.set(30);
return loadScript(appData.StaticAppJsLink);
})
2016-07-02 06:49:59 +08:00
.then(() => {
2016-05-06 23:14:40 +08:00
p.set(50);
return appData.PluginsLink ? loadScript(appData.PluginsLink) : 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(() => loadScript(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
}
2020-07-28 17:23:06 +08:00
};
2016-05-20 08:04:15 +08:00
2016-06-30 08:02:45 +08:00
/**
* @returns {void}
*/
2020-07-28 17:23:06 +08:00
window.__runBoot = () => {
2020-08-10 17:32:05 +08:00
const app = doc.getElementById('rl-app');
2016-05-20 08:04:15 +08:00
if (!navigator || !navigator.cookieEnabled) {
2020-07-28 17:23:06 +08:00
doc.location.replace('./?/NoCookie');
2016-06-17 07:23:49 +08:00
}
2016-05-20 08:04:15 +08:00
// require('Styles/@Boot.css');
writeCSS('#rl-content{display:none;}.internal-hiddden{display:none !important;}');
2016-05-20 08:04:15 +08:00
2020-07-28 17:23:06 +08:00
if (app) {
const layout = require('Html/Layout.html'),
meta = doc.getElementById('app-boot-data'),
2020-08-10 17:32:05 +08:00
options = meta ? JSON.parse(meta.getAttribute('content')) || {} : {};
2020-07-28 17:23:06 +08:00
app.innerHTML = ((layout && layout.default ? layout.default : layout) || '').replace(/[\r\n\t]+/g, '');
2016-05-20 08:04:15 +08:00
2020-08-10 17:32:05 +08:00
loadScript('./?/'
2020-07-28 17:23:06 +08:00
+ (options.admin ? 'Admin' : '')
+ 'AppData@'
+ (options.mobile ? 'mobile' : 'no-mobile')
+ (options.mobileDevice ? '-1' : '-0')
+ '/'
+ (getHash() || '0')
2020-07-28 17:23:06 +08:00
+ '/'
+ Math.random().toString().substr(2)
2020-08-10 17:32:05 +08:00
+ '/').then(() => {});
2016-05-20 08:04:15 +08:00
}
};