snappymail/dev/boot.js

122 lines
3.1 KiB
JavaScript
Raw Normal View History

(win => {
const
doc = document,
2020-09-13 20:13:16 +08:00
eId = id => doc.getElementById(id),
app = eId('rl-app'),
admin = app && '1' == app.dataset.admin,
2020-09-03 22:34:23 +08:00
getCookie = name => {
let data = doc.cookie.match('(^|;) ?'+name+'=([^;]*)(;|$)');
return data ? decodeURIComponent(data[2]) : null;
},
2020-09-03 22:34:23 +08:00
Storage = type => {
let name = type+'Storage';
try {
2020-09-22 15:50:52 +08:00
win[name].setItem(name, '');
win[name].getItem(name);
win[name].removeItem(name);
2020-09-03 22:34:23 +08:00
} catch (e) {
console.error(e);
const cookieName = encodeURIComponent(name+('session' === type ? win.name || (win.name = Date.now()) : ''));
// initialise if there's already data
let data = getCookie(cookieName);
2020-09-03 22:34:23 +08:00
data = data ? JSON.parse(data) : {};
win[name] = {
getItem: key => data[key] === undefined ? null : data[key],
setItem: function (key, value) {
data[key] = ''+value; // forces the value to a string
doc.cookie = cookieName+'='+encodeURIComponent(JSON.stringify(data))
2020-09-03 22:34:23 +08:00
+"; expires="+('local' === type ? (new Date(Date.now()+(365*24*60*60*1000))).toGMTString() : '')
+"; path=/; samesite=strict";
}
};
}
return win[name];
2020-09-03 22:34:23 +08:00
},
showError = () => {
2020-09-13 20:13:16 +08:00
eId('rl-loading').hidden = true;
eId('rl-loading-error').hidden = false;
2020-09-03 22:34:23 +08:00
},
loadScript = src => {
if (!src) {
throw new Error('src should not be empty.');
}
return new Promise((resolve, reject) => {
const script = doc.createElement('script');
script.onload = () => resolve();
2020-09-03 22:34:23 +08:00
script.onerror = () => reject(new Error(src));
script.src = src;
2020-09-23 17:20:00 +08:00
// script.async = true;
2020-09-03 22:34:23 +08:00
doc.head.append(script);
});
};
if (!navigator || !navigator.cookieEnabled) {
2020-09-13 20:13:16 +08:00
doc.location.href = './?/NoCookie';
}
2020-09-03 22:34:23 +08:00
const layout = getCookie('rllayout');
doc.documentElement.classList.toggle('rl-mobile', 'mobile' === layout || (!layout && 1000 > innerWidth));
let progress = eId('progressjs'),
2020-09-23 17:20:00 +08:00
RL_APP_DATA = {};
if (progress) {
progress.remove();
progress = null;
}
win.rl = {
2020-09-23 17:20:00 +08:00
data: () => RL_APP_DATA,
adminArea: () => admin,
settings: {
2020-09-23 17:20:00 +08:00
get: name => null == RL_APP_DATA[name] ? null : RL_APP_DATA[name],
set: (name, value) => RL_APP_DATA[name] = value,
app: name => {
2020-09-23 17:20:00 +08:00
const APP_SETTINGS = RL_APP_DATA.System || {};
return null == APP_SETTINGS[name] ? null : APP_SETTINGS[name];
},
2020-09-23 17:20:00 +08:00
capa: name => null != name && Array.isArray(RL_APP_DATA.Capa) && RL_APP_DATA.Capa.includes(name)
},
setWindowTitle: title => {
title = null == title ? '' : '' + title;
2020-09-23 17:20:00 +08:00
if (RL_APP_DATA.Title) {
title += (title ? ' - ' : '') + RL_APP_DATA.Title;
}
doc.title = title;
2020-09-23 17:20:00 +08:00
},
2016-05-06 23:14:40 +08:00
2020-09-23 17:20:00 +08:00
initData: appData => {
RL_APP_DATA = appData;
2020-09-23 17:20:00 +08:00
if (appData) {
loadScript(appData.StaticLibJsLink)
.then(() => loadScript(appData.StaticAppJsLink))
.then(() => appData.PluginsLink ? loadScript(appData.PluginsLink) : Promise.resolve())
.then(() => win.__APP_BOOT ? win.__APP_BOOT(showError) : showError())
.catch(e => {
2020-09-22 15:50:52 +08:00
showError();
throw e;
});
2020-09-23 17:20:00 +08:00
} else {
showError();
}
}
};
Storage('local');
eId('app-css').href = eId('app-css').dataset.href;
loadScript(`./?/${admin ? 'Admin' : ''}AppData/0/${Math.random().toString().substr(2)}/`)
.then(() => {});
})(this);