2021-09-23 21:30:08 +08:00
|
|
|
import { dropdownVisibility, Settings } from 'Common/Globals';
|
2019-07-05 03:19:24 +08:00
|
|
|
import { i18n } from 'Common/Translator';
|
2016-06-07 05:57:52 +08:00
|
|
|
|
2020-09-17 02:35:29 +08:00
|
|
|
import { root } from 'Common/Links';
|
|
|
|
|
2021-04-27 03:56:11 +08:00
|
|
|
export default App => {
|
2015-11-19 01:32:29 +08:00
|
|
|
|
2020-08-30 16:30:50 +08:00
|
|
|
addEventListener('click', ()=>rl.Dropdowns.detectVisibility());
|
2015-11-19 01:32:29 +08:00
|
|
|
|
2020-09-04 20:36:24 +08:00
|
|
|
rl.app = App;
|
2021-03-24 21:14:21 +08:00
|
|
|
rl.logoutReload = App.logoutReload;
|
2015-11-19 01:32:29 +08:00
|
|
|
|
2016-06-17 07:23:49 +08:00
|
|
|
rl.i18n = i18n;
|
2015-11-19 01:32:29 +08:00
|
|
|
|
2021-01-27 17:59:15 +08:00
|
|
|
rl.Enums = {
|
2021-03-16 16:46:23 +08:00
|
|
|
StorageResultType: {
|
|
|
|
Success: 0,
|
|
|
|
Error: 1,
|
2021-03-18 19:33:13 +08:00
|
|
|
Abort: 2
|
2021-03-16 16:46:23 +08:00
|
|
|
}
|
2021-01-27 17:59:15 +08:00
|
|
|
};
|
2015-11-19 01:32:29 +08:00
|
|
|
|
2020-08-24 17:14:35 +08:00
|
|
|
rl.Dropdowns = [];
|
2020-10-26 21:34:59 +08:00
|
|
|
rl.Dropdowns.register = function(element) { this.push(element); };
|
2020-08-24 17:14:35 +08:00
|
|
|
rl.Dropdowns.detectVisibility = (() =>
|
2020-10-26 21:34:59 +08:00
|
|
|
dropdownVisibility(!!rl.Dropdowns.find(item => item.classList.contains('show')))
|
2020-08-24 17:14:35 +08:00
|
|
|
).debounce(50);
|
|
|
|
|
2020-09-17 02:35:29 +08:00
|
|
|
rl.route = {
|
|
|
|
root: () => {
|
|
|
|
rl.route.setHash(root(), true);
|
|
|
|
rl.route.off();
|
|
|
|
},
|
|
|
|
reload: () => {
|
2020-09-17 05:28:29 +08:00
|
|
|
rl.route.root();
|
2021-03-10 18:44:48 +08:00
|
|
|
setTimeout(() => (Settings.app('inIframe') ? parent : window).location.reload(), 100);
|
2020-09-17 02:35:29 +08:00
|
|
|
},
|
2021-08-13 16:01:01 +08:00
|
|
|
off: () => hasher.active = false,
|
|
|
|
on: () => hasher.active = true,
|
2020-09-17 02:35:29 +08:00
|
|
|
/**
|
|
|
|
* @param {string} sHash
|
|
|
|
* @param {boolean=} silence = false
|
|
|
|
* @param {boolean=} replace = false
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
setHash: (hash, silence = false, replace = false) => {
|
|
|
|
hash = hash.replace(/^[#/]+/, '');
|
2021-08-20 21:40:07 +08:00
|
|
|
hasher.active = !silence;
|
|
|
|
hasher[replace ? 'replaceHash' : 'setHash'](hash);
|
2020-09-17 02:35:29 +08:00
|
|
|
if (silence) {
|
2021-08-13 16:01:01 +08:00
|
|
|
hasher.active = true;
|
2020-09-17 02:35:29 +08:00
|
|
|
} else {
|
|
|
|
hasher.setHash(hash);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-11-01 21:57:58 +08:00
|
|
|
rl.fetch = (resource, init, postData) => {
|
2020-09-14 18:39:15 +08:00
|
|
|
init = Object.assign({
|
|
|
|
mode: 'same-origin',
|
|
|
|
cache: 'no-cache',
|
|
|
|
redirect: 'error',
|
|
|
|
referrerPolicy: 'no-referrer',
|
2021-01-05 20:58:50 +08:00
|
|
|
credentials: 'same-origin',
|
|
|
|
headers: {}
|
2020-09-14 18:39:15 +08:00
|
|
|
}, init);
|
|
|
|
if (postData) {
|
|
|
|
init.method = 'POST';
|
2021-01-05 20:58:50 +08:00
|
|
|
init.headers['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8';
|
2021-07-23 17:40:03 +08:00
|
|
|
const buildFormData = (formData, data, parentKey) => {
|
2020-09-14 18:39:15 +08:00
|
|
|
if (data && typeof data === 'object' && !(data instanceof Date || data instanceof File)) {
|
|
|
|
Object.keys(data).forEach(key =>
|
|
|
|
buildFormData(formData, data[key], parentKey ? `${parentKey}[${key}]` : key)
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
formData.set(parentKey, data == null ? '' : data);
|
|
|
|
}
|
2021-07-23 17:40:03 +08:00
|
|
|
return formData;
|
2020-09-14 18:39:15 +08:00
|
|
|
};
|
2021-07-23 17:40:03 +08:00
|
|
|
postData = (postData instanceof FormData)
|
|
|
|
? postData
|
|
|
|
: buildFormData(new FormData(), postData);
|
|
|
|
postData.set('XToken', Settings.app('token'));
|
|
|
|
// init.body = JSON.stringify(Object.fromEntries(postData));
|
|
|
|
init.body = new URLSearchParams(postData);
|
2020-09-14 18:39:15 +08:00
|
|
|
}
|
|
|
|
|
2021-11-01 21:57:58 +08:00
|
|
|
return fetch(resource, init);
|
|
|
|
};
|
|
|
|
|
|
|
|
rl.fetchJSON = (resource, init, postData) => {
|
|
|
|
init = Object.assign({ headers: {} }, init);
|
|
|
|
init.headers.Accept = 'application/json';
|
|
|
|
return rl.fetch(resource, init, postData).then(response => {
|
2021-01-05 20:58:50 +08:00
|
|
|
if (!response.ok) {
|
2021-02-25 17:12:48 +08:00
|
|
|
return Promise.reject('Network response error: ' + response.status);
|
2021-01-05 20:58:50 +08:00
|
|
|
}
|
|
|
|
/* TODO: use this for non-developers?
|
|
|
|
response.clone()
|
|
|
|
let data = response.text();
|
|
|
|
try {
|
|
|
|
return JSON.parse(data);
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
|
|
|
// console.log(data);
|
|
|
|
return Promise.reject(Notification.JsonParse);
|
|
|
|
return {
|
|
|
|
Result: false,
|
|
|
|
ErrorCode: 952, // Notification.JsonParse
|
|
|
|
ErrorMessage: e.message,
|
|
|
|
ErrorMessageAdditional: data
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
return response.json();
|
|
|
|
});
|
2020-09-14 18:39:15 +08:00
|
|
|
};
|
|
|
|
|
2016-04-21 01:12:51 +08:00
|
|
|
};
|