keanu-weblite/src/services/config.service.js

30 lines
1 KiB
JavaScript
Raw Normal View History

2021-09-25 09:29:05 +02:00
export default {
install(Vue, defaultServerFromLocation, onloaded) {
2021-09-25 09:29:05 +02:00
var config = Vue.observable(require('@/assets/config.json'));
const getRuntimeConfig = () => {
2023-10-13 09:31:45 +02:00
return fetch('./config.json?ms=' + Date.now()).then((res) => res.json()).catch(err => {
console.error("Failed to get config:", err);
return {};
});
2021-09-25 09:29:05 +02:00
}
config.promise = getRuntimeConfig().then((json) => {
2021-09-25 09:29:05 +02:00
// Reactively use all the config values
for (const key of Object.keys(json)) {
Vue.set(config, key, json[key]);
}
// If default server is not set, default to current server address
if (!json.defaultServer) {
2023-02-20 16:51:05 +01:00
Vue.set(config, "defaultServer", defaultServerFromLocation);
}
// Tell callback we are done loading runtime config
if (onloaded) {
onloaded(config);
}
return config;
2021-09-25 09:29:05 +02:00
});
Vue.prototype.$config = config;
}
}