78 lines
2.3 KiB
JavaScript
78 lines
2.3 KiB
JavaScript
const APP_TRIBE = document.querySelector('meta[name="xtribe"]')?.content || 'dev';
|
|
|
|
const LG = {
|
|
loaded: {},
|
|
get current() { return Auth.getLang() || 'en'; },
|
|
fallback: {
|
|
'app.title': '{{WEBAPPNAME}}',
|
|
'list.title': '{{WEBAPPNAME}}',
|
|
'list.loading': 'Loading...',
|
|
'list.empty': 'No items found',
|
|
'list.count': '{{n}} item(s)'
|
|
},
|
|
t: function (key, vars) {
|
|
var str = LG.loaded[key] || LG.fallback[key] || key;
|
|
if (vars) {
|
|
Object.keys(vars).forEach(function (k) {
|
|
str = str.replace('{{' + k + '}}', vars[k]);
|
|
str = str.replace('{' + k + '}', vars[k]);
|
|
});
|
|
}
|
|
return str;
|
|
},
|
|
set: async function (lg) {
|
|
if (lg === 'en') { LG.loaded = {}; Auth.setLang('en'); LG.apply(); return; }
|
|
try {
|
|
var res = await axios.get('static/lg/' + lg + '.json', { validateStatus: function () { return true; } });
|
|
if (res.status === 200) { LG.loaded = res.data; Auth.setLang(lg); LG.apply(); }
|
|
} catch (err) {}
|
|
},
|
|
apply: function () {
|
|
document.querySelectorAll('[data-i18n]').forEach(function (el) {
|
|
var val = LG.t(el.dataset.i18n);
|
|
if (val && val !== el.dataset.i18n) {
|
|
if (el.tagName === 'INPUT' || el.tagName === 'TEXTAREA') { if (el.placeholder !== undefined) el.placeholder = val; }
|
|
else el.innerHTML = val;
|
|
}
|
|
});
|
|
document.documentElement.lang = LG.current;
|
|
}
|
|
};
|
|
|
|
var App = {
|
|
items: [],
|
|
|
|
async init() {
|
|
Log.debug('App.init() starting');
|
|
Auth.initAxios();
|
|
Auth.initTheme();
|
|
await LocalDB.init('{{WEBAPPNAME}}', 'bdc');
|
|
|
|
var activeAlias = Auth.getActiveAlias();
|
|
var identity = activeAlias ? Auth.getIdentity(activeAlias) : null;
|
|
if (!activeAlias || !identity?.auth) {
|
|
var returnUrl = encodeURIComponent(window.location.pathname);
|
|
window.location.href = '/identities/?redirect=' + returnUrl;
|
|
return;
|
|
}
|
|
|
|
var lang = Auth.getLang();
|
|
await LG.set(lang);
|
|
await Menu.init();
|
|
this.bindEvents();
|
|
this.setLoading(false);
|
|
},
|
|
|
|
setLoading: function (show) {
|
|
var overlay = document.getElementById('loading-overlay');
|
|
if (!show) {
|
|
overlay.classList.add('opacity-0');
|
|
setTimeout(function () { overlay.classList.add('pointer-events-none'); }, 300);
|
|
}
|
|
},
|
|
|
|
bindEvents: function () {}
|
|
};
|
|
|
|
document.addEventListener('DOMContentLoaded', function () { App.init(); });
|