2020-12-03 00:34:50 +01:00
|
|
|
import { createApp } from 'vue';
|
|
|
|
import { createStore } from 'vuex';
|
|
|
|
|
2020-12-03 01:35:22 +01:00
|
|
|
if (process.env.CHANNEL !== 'stable'){
|
|
|
|
console.info("Loading: UI");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-12-03 00:34:50 +01:00
|
|
|
class UI {
|
|
|
|
constructor(
|
|
|
|
interfaceId,
|
|
|
|
storeConfig,
|
|
|
|
uiConfig, // {component, parentElement?}
|
|
|
|
commsConfig,
|
|
|
|
) {
|
|
|
|
this.interfaceId = interfaceId;
|
|
|
|
this.commsConfig = commsConfig;
|
2020-12-03 01:35:22 +01:00
|
|
|
this.storeConfig = storeConfig,
|
|
|
|
this.uiConfig = uiConfig;
|
|
|
|
|
|
|
|
this.init();
|
2020-12-03 00:34:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async init() {
|
|
|
|
// initialize vuejs, but only once (check handled in initVue())
|
|
|
|
// we need to initialize this _after_ initializing comms.
|
2020-12-04 02:02:25 +01:00
|
|
|
|
2020-12-03 00:34:50 +01:00
|
|
|
this.initVue();
|
|
|
|
}
|
|
|
|
|
|
|
|
async initVue() {
|
2020-12-04 02:02:25 +01:00
|
|
|
if (this.storeConfig) {
|
|
|
|
this.vuexStore = createStore(this.storeConfig);
|
|
|
|
}
|
|
|
|
|
2020-12-03 01:35:22 +01:00
|
|
|
this.initUi();
|
2020-12-03 00:34:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async initUi() {
|
|
|
|
const random = Math.round(Math.random() * 69420);
|
2020-12-03 01:35:22 +01:00
|
|
|
// const uwid = `uw-${this.interfaceId}-root-${random}`
|
|
|
|
const uwid = 'not-so-random-id'
|
2020-12-03 00:34:50 +01:00
|
|
|
|
|
|
|
const rootDiv = document.createElement('div');
|
|
|
|
|
2020-12-04 02:02:25 +01:00
|
|
|
try {
|
|
|
|
rootDiv.setAttribute('style', `position: ${this.uiConfig.style?.position ?? 'relative'}; width: ${this.uiConfig.style?.width ?? '100%'}; height: ${this.uiConfig.style?.height ?? '100%'}; ${this.uiConfig.additionalStyle ?? ''}`);
|
|
|
|
rootDiv.setAttribute('id', uwid);
|
|
|
|
} catch (e) {
|
|
|
|
console.error("ERROR:", e)
|
|
|
|
}
|
|
|
|
|
|
|
|
console.warn('UI: init 3', this.uiConfig);
|
|
|
|
|
|
|
|
|
|
|
|
if (this.uiConfig?.parentElement) {
|
2020-12-03 00:34:50 +01:00
|
|
|
this.uiConfig.parentElement.appendChild(rootDiv);
|
|
|
|
} else {
|
|
|
|
document.body.appendChild(rootDiv);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.element = rootDiv;
|
|
|
|
|
2020-12-04 02:02:25 +01:00
|
|
|
const app = createApp(this.uiConfig.component);
|
|
|
|
if (this.vuexStore) {
|
|
|
|
app.use(this.vuexStore);
|
|
|
|
}
|
|
|
|
app.mount(`#${uwid}`);
|
2020-12-03 00:34:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Replaces ui config and re-inits the UI
|
|
|
|
* @param {*} newUiConfig
|
|
|
|
*/
|
|
|
|
replace(newUiConfig) {
|
|
|
|
this.element?.remove();
|
|
|
|
this.uiConfig = newUiConfig;
|
|
|
|
this.initUi();
|
|
|
|
}
|
|
|
|
|
|
|
|
destroy() {
|
2020-12-04 02:02:25 +01:00
|
|
|
// this.comms?.destroy();
|
2020-12-03 00:34:50 +01:00
|
|
|
this.element?.remove();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-03 01:35:22 +01:00
|
|
|
if (process.env.CHANNEL !== 'stable'){
|
|
|
|
console.info("UI.js loaded");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-12-03 01:16:57 +01:00
|
|
|
export default UI;
|