snappymail/dev/Common/Booter.js

336 lines
6.6 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-07-02 06:49:59 +08:00
import Promise from 'Promise';
2016-05-06 23:14:40 +08:00
import {jassl} from 'Common/Jassl';
2016-06-17 07:23:49 +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
2016-07-16 05:29:42 +08:00
/* eslint-disable */
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-07-16 05:29:42 +08:00
/* eslint-enable */
2016-05-06 23:14:40 +08:00
2016-08-11 07:16:58 +08:00
const useJsNextBundle = (function() {
/* eslint-disable */
2016-10-19 01:52:43 +08:00
// try {
//
// (function() {
// eval(`
// // let + const
//const x = 5; let y = 4; var z = 4;
//
// // Arrow Function
//const f = () => 'rainloop';
//
// // Default + Rest + Spread
//const d = (test = 1, ...t) => 'rainloop';
//d(...[1, 2, 3]);
//
//// Destructuring
//let [a, b] = [1, 2];
//({a, b} = {a: 1, b: 2});
//
//// Class
//class Q1 { constructor() {} }
//
//// Class extends + super
//class Q2 extends Q1 { constructor() { super() } }
//
//`);
// }());
//
// return true;
// }
// catch (e) {}
2016-08-11 07:16:58 +08:00
return false;
/* eslint-enable */
}());
2016-06-30 08:02:45 +08:00
/**
* @param {string} id
* @param {string} name
* @returns {string}
*/
2016-05-24 05:56:48 +08:00
function getComputedStyle(id, name)
{
const element = window.document.getElementById(id);
2016-10-19 01:52:43 +08:00
return element && element.currentStyle ? element.currentStyle[name] :
2016-06-17 07:23:49 +08:00
(window.getComputedStyle ? window.getComputedStyle(element, null).getPropertyValue(name) : null);
2016-05-24 05:56:48 +08:00
}
2016-06-30 08:02:45 +08:00
/**
* @param {string} styles
* @returns {void}
*/
2016-05-20 08:04:15 +08:00
function includeStyle(styles)
{
2016-07-02 06:49:59 +08:00
window.document.write(unescape('%3Csty' + 'le%3E' + styles + '"%3E%3C/' + 'sty' + 'le%3E')); // eslint-disable-line no-useless-concat
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}
*/
2016-05-20 08:04:15 +08:00
function includeScr(src)
{
2016-07-02 06:49:59 +08:00
window.document.write(unescape('%3Csc' + 'ript type="text/jav' + 'ascr' + 'ipt" data-cfasync="false" sr' + 'c="' + src + '"%3E%3C/' + 'scr' + 'ipt%3E')); // eslint-disable-line no-useless-concat
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}
*/
2016-05-20 08:04:15 +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
require('style-loader!Styles/@Boot.css');
2016-05-24 05:56:48 +08:00
if (app)
2016-05-20 08:04:15 +08:00
{
app.innerHTML = require('Html/Layout.html').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}
*/
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 += '/';
includeScr(src + (window.__rlah ? window.__rlah() || '0' : '0') + '/' + window.Math.random().toString().substr(2) + '/');
}
2016-05-06 23:14:40 +08:00
2016-06-30 08:02:45 +08:00
/**
* @returns {object}
*/
2016-05-20 08:04:15 +08:00
function getRainloopBootData()
{
let result = {};
const meta = window.document.getElementById('app-boot-data');
2016-05-06 23:14:40 +08:00
2016-05-20 08:04:15 +08:00
if (meta && meta.getAttribute)
{
result = JSON.parse(meta.getAttribute('content')) || {};
}
return result;
}
2016-06-30 08:02:45 +08:00
/**
* @param {string} additionalError
* @returns {void}
*/
2016-05-20 08:04:15 +08:00
function showError(additionalError)
{
2016-05-06 23:14:40 +08:00
const
oR = window.document.getElementById('rl-loading'),
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
if (oR)
{
oR.style.display = 'none';
}
if (oL)
{
oL.style.display = 'block';
}
if (oLA && additionalError)
{
oLA.style.display = 'block';
oLA.innerHTML = additionalError;
}
2016-05-20 08:04:15 +08:00
if (progressJs)
2016-05-06 23:14:40 +08:00
{
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}
*/
2016-05-20 08:04:15 +08:00
function showDescriptionAndLoading(description)
{
2016-05-06 23:14:40 +08:00
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
if (oElDesc && description)
{
oElDesc.innerHTML = description;
}
if (oE && oE.style)
{
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}
*/
2016-05-20 08:04:15 +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
});
}
2016-05-20 08:04:15 +08:00
else
{
showError(additionalError);
}
}
2016-05-06 23:14:40 +08:00
2016-06-30 08:02:45 +08:00
/**
* @returns {void}
*/
2016-05-20 08:04:15 +08:00
function runApp()
{
const appData = window.__rlah_data();
2016-05-06 23:14:40 +08:00
if (jassl && progressJs && appData && appData.TemplatesLink && appData.LangLink &&
2016-08-11 07:16:58 +08:00
appData.StaticLibJsLink && appData.StaticAppJsLink && appData.StaticAppJsNextLink && appData.StaticEditorJsLink)
2016-05-06 23:14:40 +08:00
{
2016-05-20 08:04:15 +08:00
const p = progressJs;
2016-05-06 23:14:40 +08:00
p.setOptions({theme: 'rainloop'});
p.start().set(5);
2016-05-20 08:04:15 +08:00
const
libs = jassl(appData.StaticLibJsLink).then(() => {
2016-05-20 08:04:15 +08:00
if (window.$)
{
window.$('#rl-check').remove();
if (appData.IncludeBackground)
{
window.$('#rl-bg').attr('style', 'background-image: none !important;')
.backstretch(appData.IncludeBackground.replace('{{USER}}',
(window.__rlah ? (window.__rlah() || '0') : '0')), {fade: 100, centeredX: true, centeredY: true})
2016-06-30 08:02:45 +08:00
.removeAttr('style');
2016-05-20 08:04:15 +08:00
}
2016-05-06 23:14:40 +08:00
}
2016-05-20 08:04:15 +08:00
}),
2016-07-02 06:49:59 +08:00
common = Promise.all([
2016-08-30 06:10:24 +08:00
// jassl('https://code.jquery.com/jquery-migrate-3.0.0.js'),
jassl(appData.TemplatesLink),
jassl(appData.LangLink)
2016-06-30 08:02:45 +08:00
]);
2016-05-20 08:04:15 +08:00
2016-07-02 06:49:59 +08:00
Promise.all([libs, common])
2016-05-20 08:04:15 +08:00
.then(() => {
2016-05-06 23:14:40 +08:00
p.set(30);
return jassl(useJsNextBundle ? appData.StaticAppJsNextLink : 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 ? 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
});
2016-05-06 23:14:40 +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();
2016-06-17 07:23:49 +08:00
if (RL_APP_DATA_STORAGE)
2016-05-20 08:04:15 +08:00
{
2016-06-17 07:23:49 +08:00
if (RL_APP_DATA_STORAGE.NewThemeLink)
{
(window.document.getElementById('app-theme-link') || {}).href = RL_APP_DATA_STORAGE.NewThemeLink;
}
2016-05-20 08:04:15 +08:00
2016-06-17 07:23:49 +08:00
if (RL_APP_DATA_STORAGE.IncludeCss)
{
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() {
if (!window.navigator || !window.navigator.cookieEnabled)
{
window.document.location.replace('./?/NoCookie');
}
const root = window.document.documentElement;
2016-05-24 05:56:48 +08:00
if ('none' !== getComputedStyle('rl-check', 'display'))
{
root.className += ' no-css';
}
2016-08-11 07:16:58 +08:00
if (useJsNextBundle)
{
root.className += ' js-next';
}
2016-05-20 08:04:15 +08:00
if (includeLayout())
{
includeAppScr(getRainloopBootData());
}
};