Skip to content

Commit

Permalink
Support usage of vue mounting helpers in a context without existing p…
Browse files Browse the repository at this point in the history
…inia/vue. This fixes charts initialization.
  • Loading branch information
dannon committed Jun 21, 2024
1 parent 4239fe1 commit 4f00347
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions client/src/utils/mountVueComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import BootstrapVue from "bootstrap-vue";
import { iconPlugin, localizationPlugin, vueRxShortcutPlugin } from "components/plugins";
import { getActivePinia, PiniaVuePlugin } from "pinia";
import { createPinia, getActivePinia, PiniaVuePlugin } from "pinia";
import Vue from "vue";

// Load Pinia
Expand All @@ -22,17 +22,28 @@ Vue.use(vueRxShortcutPlugin);
// font-awesome svg icon registration/loading
Vue.use(iconPlugin);

function getOrCreatePinia() {
// We sometimes use this utility mounting function in a context where there
// is no existing vue application or pinia store (e.g. individual charts
// displayed in an iframe).
// To support both use cases, we will a new pinia store and attach it to the
// vue application that is created for the component.
let pinia = getActivePinia();
if (!pinia) {
pinia = createPinia();
}
return pinia;
}

export const mountVueComponent = (ComponentDefinition) => {
const component = Vue.extend(ComponentDefinition);
const pinia = getActivePinia();
return (propsData, el) => new component({ propsData, el, pinia });
return (propsData, el) => new component({ propsData, el, pinia: getOrCreatePinia() });
};

export const replaceChildrenWithComponent = (el, ComponentDefinition, propsData = {}) => {
const container = document.createElement("div");
el.replaceChildren(container);
const component = Vue.extend(ComponentDefinition);
const pinia = getActivePinia();
const mountFn = (propsData, el) => new component({ propsData, el, pinia });
const mountFn = (propsData, el) => new component({ propsData, el, pinia: getOrCreatePinia() });
return mountFn(propsData, container);
};

0 comments on commit 4f00347

Please sign in to comment.