snappymail/dev/boot.js

239 lines
5.7 KiB
JavaScript
Raw Normal View History

(win => {
const
doc = document,
2020-09-09 22:53:36 +08:00
html = doc.documentElement,
2020-09-03 22:34:23 +08:00
app = doc.getElementById('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 {
win[name].setItem('storage', '');
win[name].getItem('storage');
win[name].removeItem('storage');
} 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 = () => {
doc.getElementById('rl-loading').hidden = true;
doc.getElementById('rl-loading-error').hidden = false;
2020-09-03 22:34:23 +08:00
p.end();
},
runMainBoot = withError => {
if (win.__APP_BOOT && !withError) {
win.__APP_BOOT(() => showError());
} else {
showError();
}
},
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');
script.onload = () => resolve();
script.onerror = () => reject(new Error(src));
script.src = src;
doc.head.append(script);
});
},
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) {
doc.location.replace('./?/NoCookie');
}
2020-09-03 22:34:23 +08:00
let container = doc.querySelector('.progressjs'),
progress = doc.querySelector('.progressjs-inner'),
RL_APP_DATA_STORAGE = {};
win.rl = {
2020-09-03 22:34:23 +08:00
hash: {
// getHash
get: () => storage().getItem(STORAGE_KEY) || null,
// setHash
set: () => {
storage().setItem(STORAGE_KEY, RL_APP_DATA_STORAGE && RL_APP_DATA_STORAGE[AUTH_KEY]
? RL_APP_DATA_STORAGE[AUTH_KEY] : '');
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
},
data: () => RL_APP_DATA_STORAGE,
adminArea: () => options.admin,
settings: {
get: name => null == RL_APP_DATA_STORAGE[name] ? null : RL_APP_DATA_STORAGE[name],
set: (name, value) => RL_APP_DATA_STORAGE[name] = value,
app: name => {
const APP_SETTINGS = RL_APP_DATA_STORAGE.System || {};
return null == APP_SETTINGS[name] ? null : APP_SETTINGS[name];
},
capa: name => null != name && Array.isArray(RL_APP_DATA_STORAGE.Capa) && RL_APP_DATA_STORAGE.Capa.includes(name)
},
setWindowTitle: title => {
title = null == title ? '' : '' + title;
if (RL_APP_DATA_STORAGE.Title) {
title += (title ? ' - ' : '') + RL_APP_DATA_STORAGE.Title;
}
doc.title = null == title ? '' : '' + title;
}
};
2016-05-06 23:14:40 +08:00
/**
2020-09-03 22:34:23 +08:00
* @param {mixed} appData
* @returns {void}
*/
2020-09-03 22:34:23 +08:00
win.__initAppData = appData => {
RL_APP_DATA_STORAGE = appData;
rl.hash.set();
2020-09-03 22:34:23 +08:00
if (appData) {
if (appData.NewThemeLink) {
2020-09-09 22:53:36 +08:00
doc.getElementById('app-theme-link').href = appData.NewThemeLink;
}
appData.IncludeCss && writeCSS(appData.IncludeCss);
if (appData.IncludeBackground) {
const img = appData.IncludeBackground.replace('{{USER}}', rl.hash.get() || '0');
if (img) {
html.classList.add('UserBackground');
doc.body.style.backgroundImage = "url("+img+")";
}
}
}
if (
appData &&
appData.TemplatesLink &&
appData.LangLink &&
appData.StaticLibJsLink &&
appData.StaticAppJsLink
) {
p.set(5);
loadScript(appData.StaticLibJsLink)
.then(() => {
p.set(20);
return Promise.all([loadScript(appData.TemplatesLink), loadScript(appData.LangLink)]);
})
.then(() => {
p.set(30);
return loadScript(appData.StaticAppJsLink);
})
.then(() => {
p.set(50);
return appData.PluginsLink ? loadScript(appData.PluginsLink) : Promise.resolve();
})
.then(() => {
p.set(70);
runMainBoot(false);
})
.catch((e) => {
runMainBoot(true);
throw e;
})
.then(() => {
if (appData.Auth) {
loadScript(appData.StaticEditorJsLink).then(() => {
win.__initEditor && win.__initEditor();
win.__initEditor = null;
});
}
});
} else {
runMainBoot(true);
}
};
p.set(1);
Storage('local');
Storage('session');
// init section
setInterval(setTimestamp, 60000); // 1m
2020-09-09 22:53:36 +08:00
html.classList.add(options.mobileDevice ? 'mobile' : 'no-mobile');
['app-css','app-theme-link'].forEach(css => {
css = doc.getElementById(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);