Bug 33169: Build breadcrumbs and left-hand side menu from routes definition
[koha.git] / koha-tmpl / intranet-tmpl / prog / js / vue / modules / erm.ts
1 import { createApp } from "vue";
2 import { createWebHistory, createRouter } from "vue-router";
3 import { createPinia } from "pinia";
4
5 import { library } from "@fortawesome/fontawesome-svg-core";
6 import {
7     faPlus,
8     faMinus,
9     faPencil,
10     faTrash,
11     faSpinner,
12 } from "@fortawesome/free-solid-svg-icons";
13 import { FontAwesomeIcon } from "@fortawesome/vue-fontawesome";
14 import vSelect from "vue-select";
15
16 library.add(faPlus, faMinus, faPencil, faTrash, faSpinner);
17
18 import App from "../components/ERM/Main.vue";
19
20 import { routes as routesDef } from "../routes/erm";
21
22 import { useMainStore } from "../stores/main";
23 import { useVendorStore } from "../stores/vendors";
24 import { useAVStore } from "../stores/authorised-values";
25 import { useERMStore } from "../stores/erm";
26 import { useNavigationStore } from "../stores/navigation";
27 import i18n from "../i18n";
28
29 const pinia = createPinia();
30
31 const mainStore = useMainStore(pinia);
32 const AVStore = useAVStore(pinia);
33 const navigationStore = useNavigationStore(pinia);
34 const routes = navigationStore.setRoutes(routesDef);
35
36 const router = createRouter({
37     history: createWebHistory(),
38     linkExactActiveClass: "current",
39     routes,
40 });
41
42 const app = createApp(App);
43
44 const rootComponent = app
45     .use(i18n)
46     .use(pinia)
47     .use(router)
48     .component("font-awesome-icon", FontAwesomeIcon)
49     .component("v-select", vSelect);
50
51 app.config.unwrapInjectedRef = true;
52 app.provide("vendorStore", useVendorStore(pinia));
53 app.provide("mainStore", mainStore);
54 app.provide("AVStore", AVStore);
55 app.provide("navigationStore", navigationStore);
56 const ERMStore = useERMStore(pinia);
57 app.provide("ERMStore", ERMStore);
58
59 app.mount("#erm");
60
61 const { removeMessages } = mainStore;
62 router.beforeEach((to, from) => {
63     navigationStore.$patch({ current: to.matched, params: to.params || {} });
64     removeMessages(); // This will actually flag the messages as displayed already
65 });
66 router.afterEach((to, from) => {
67     let tab_id = "agreement"; // Agreements
68
69     if (to.path.match(/\/erm\/licenses/)) {
70         tab_id = "license";
71     } else if (to.path.match(/\/erm\/eholdings\/local\/packages/)) {
72         tab_id = "package";
73     } else if (to.path.match(/\/erm\/eholdings\/local\/titles/)) {
74         tab_id = "title";
75     }
76     let node = document.getElementById(`${tab_id}_search_tab`);
77
78     if (node) {
79         node.click();
80     }
81 });