snappymail/dev/boot.js

207 lines
5.2 KiB
JavaScript
Raw Normal View History

(win => {
const
doc = document,
2020-09-13 20:13:16 +08:00
eId = id => doc.getElementById(id),
htmlCL = doc.documentElement.classList,
app = eId('rl-app'),
options = app && app.dataset.boot && JSON.parse(app.dataset.boot) || {},
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 = doc.cookie.match('(^|;) ?'+cookieName+'=([^;]*)(;|$)');
2020-09-03 22:34:23 +08:00
data = data ? decodeURIComponent(data[2]) : null;
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";
}
};
}
},
STORAGE_KEY = '__rlA',
TIME_KEY = '__rlT',
AUTH_KEY = 'AuthAccountHash',
storage = () => window.sessionStorage,
timestamp = () => Math.round(Date.now() / 1000),
setTimestamp = () => storage().setItem(TIME_KEY, timestamp()),
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
p.end();
},
writeCSS = css => {
const style = doc.createElement('style');
style.type = 'text/css';
style.textContent = css;
doc.head.append(style);
},
loadScript = src => {
if (!src) {
throw new Error('src should not be empty.');
}
return new Promise((resolve, reject) => {
const script = doc.createElement('script');
2020-09-23 17:20:00 +08:00
script.onload = () => {
p.set(pStep += step);
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);
});
},
2020-09-23 17:20:00 +08:00
step = 100 / 7,
2020-09-03 22:34:23 +08:00
p = win.progressJs = {
set: percent => progress.style.width = Math.min(percent, 100) + '%',
2020-09-03 22:34:23 +08:00
end: () => {
if (container) {
p.set(100);
2020-09-03 23:14:44 +08:00
setTimeout(() => {
container.remove();
container = progress = null;
}, 600);
2020-09-03 22:34:23 +08:00
}
}
};
if (!navigator || !navigator.cookieEnabled) {
2020-09-13 20:13:16 +08:00
doc.location.href = './?/NoCookie';
}
2020-09-03 22:34:23 +08:00
2020-09-23 17:20:00 +08:00
let pStep = 0,
container = doc.querySelector('.progressjs'),
progress = doc.querySelector('.progressjs-inner'),
2020-09-23 17:20:00 +08:00
RL_APP_DATA = {};
win.rl = {
2020-09-03 22:34:23 +08:00
hash: {
// getHash
get: () => storage().getItem(STORAGE_KEY) || null,
// setHash
set: () => {
2020-09-23 17:20:00 +08:00
storage().setItem(STORAGE_KEY, RL_APP_DATA && RL_APP_DATA[AUTH_KEY]
? RL_APP_DATA[AUTH_KEY] : '');
2020-09-03 22:34:23 +08:00
setTimestamp();
},
// clearHash
clear: () => {
storage().setItem(STORAGE_KEY, '');
setTimestamp();
},
// checkTimestamp
check: () => {
if (timestamp() > (parseInt(storage().getItem(TIME_KEY) || 0, 10) || 0) + 3600000) {
// 60m
rl.hash.clear();
2020-09-03 22:34:23 +08:00
return true;
}
return false;
}
2020-09-03 22:34:23 +08:00
},
2020-09-23 17:20:00 +08:00
data: () => RL_APP_DATA,
adminArea: () => options.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 = null == 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
rl.hash.set();
2020-09-23 17:20:00 +08:00
if (appData) {
if (appData.NewThemeLink) {
eId('app-theme-link').href = appData.NewThemeLink;
}
2020-09-23 17:20:00 +08:00
appData.IncludeCss && writeCSS(appData.IncludeCss);
2020-09-23 17:20:00 +08:00
if (appData.IncludeBackground) {
const img = appData.IncludeBackground.replace('{{USER}}', rl.hash.get() || '0');
if (img) {
htmlCL.add('UserBackground');
doc.body.style.backgroundImage = "url("+img+")";
}
}
2020-09-23 17:20:00 +08:00
loadScript(appData.StaticLibJsLink)
.then(() => Promise.all([loadScript(appData.TemplatesLink), loadScript(appData.LangLink)]))
.then(() => loadScript(appData.StaticAppJsLink))
.then(() => appData.PluginsLink ? loadScript(appData.PluginsLink) : Promise.resolve())
.then(() =>
// Enable the old CKEditor?
(appData.Auth && appData.StaticEditorJsLink)
? loadScript(appData.StaticEditorJsLink)
: 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();
}
}
};
p.set(1);
Storage('local');
Storage('session');
// init section
setInterval(setTimestamp, 60000); // 1m
2020-09-13 20:13:16 +08:00
htmlCL.add(options.mobileDevice ? 'mobile' : 'no-mobile');
2020-09-09 22:53:36 +08:00
2020-09-14 18:39:30 +08:00
[eId('app-css'),eId('app-theme-link')].forEach(css => css.href = css.dataset.href);
loadScript('./?/'
+ (options.admin ? 'Admin' : '')
+ 'AppData@'
+ (options.mobile ? 'mobile' : 'no-mobile')
+ (options.mobileDevice ? '-1' : '-0')
+ '/'
+ (rl.hash.get() || '0')
+ '/'
+ Math.random().toString().substr(2)
+ '/').then(() => {});
})(this);