snappymail/dev/boot.js

106 lines
2.7 KiB
JavaScript
Raw Normal View History

[].flat||document.location.replace('./?/BadBrowser');
(win => {
const
doc = document,
2021-07-26 21:06:28 +08:00
eId = id => doc.getElementById('rl-'+id),
app = eId('app'),
css = eId('css'),
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;
},
showError = msg => {
let div = eId('loading-error');
div.append(' ' + msg);
2021-07-26 21:06:28 +08:00
eId('loading').hidden = true;
div.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();
script.onerror = () => reject(new Error('Failed loading ' + src));
2020-09-03 22:34:23 +08:00
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);
});
2021-07-26 21:06:28 +08:00
},
layout = getCookie('rllayout'),
sName = 'localStorage';
if (!navigator || !navigator.cookieEnabled) {
2020-09-13 20:13:16 +08:00
doc.location.href = './?/NoCookie';
}
2020-09-03 22:34:23 +08:00
doc.documentElement.classList.toggle('rl-mobile', 'mobile' === layout || (!layout && 1000 > innerWidth));
2021-07-26 21:06:28 +08:00
let RL_APP_DATA = {};
win.rl = {
adminArea: () => admin,
settings: {
2021-09-23 21:30:08 +08:00
get: name => RL_APP_DATA[name],
2020-09-23 17:20:00 +08:00
set: (name, value) => RL_APP_DATA[name] = value,
2021-09-23 21:30:08 +08:00
app: name => RL_APP_DATA.System[name],
capa: name => name && (RL_APP_DATA.Capa || []).includes(name)
2020-09-23 17:20:00 +08:00
},
2021-09-17 21:09:47 +08:00
setWindowTitle: title =>
doc.title = RL_APP_DATA.Title ? (title ? title + ' - ' : '') + RL_APP_DATA.Title : (title ? '' + title : ''),
2016-05-06 23:14:40 +08:00
2020-09-23 17:20:00 +08:00
initData: appData => {
2021-09-23 21:30:08 +08:00
const cb = () => rl.app.bootstart();
2020-09-23 17:20:00 +08:00
RL_APP_DATA = appData;
2021-09-23 21:30:08 +08:00
loadScript(appData.StaticLibJsLink)
2020-09-23 17:20:00 +08:00
.then(() => loadScript(appData.StaticAppJsLink))
.then(() => appData.PluginsLink ? loadScript(appData.PluginsLink) : Promise.resolve())
2021-09-23 21:30:08 +08:00
.then(() => ('loading' !== doc.readyState) ? cb() : doc.addEventListener('DOMContentLoaded', cb))
2020-09-23 17:20:00 +08:00
.catch(e => {
showError(e.message);
throw e;
2021-09-23 21:30:08 +08:00
});
},
setData: appData => {
RL_APP_DATA = appData;
rl.app.refresh();
}
};
2021-07-26 21:06:28 +08:00
// Storage
try {
win[sName].setItem(sName, '');
win[sName].getItem(sName);
win[sName].removeItem(sName);
} catch (e) {
console.error(e);
// initialise if there's already data
let data = getCookie(sName);
data = data ? JSON.parse(data) : {};
win[sName] = {
2021-09-17 21:09:47 +08:00
getItem: key => data[key] == null ? null : data[key],
2021-07-26 21:06:28 +08:00
setItem: (key, value) => {
data[key] = ''+value; // forces the value to a string
doc.cookie = sName+'='+encodeURIComponent(JSON.stringify(data))
+"; expires="+((new Date(Date.now()+(365*24*60*60*1000))).toGMTString())
+"; path=/; samesite=strict";
}
};
}
2021-07-26 21:06:28 +08:00
css.href = css.dataset.href;
loadScript(`./?/${admin ? 'Admin' : ''}AppData/0/${Math.random().toString().slice(2)}/`)
.then(() => 0);
})(this);