aboutsummaryrefslogtreecommitdiff
path: root/resources/assets/javascripts/chunks/vue.js
blob: 28961669a53e96f10c08eff3e44fa61c9e5b444f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import { createApp as vueCreateApp, h } from 'vue';
import { createStore as vuexCreateStore } from 'vuex';
import { createPinia } from 'pinia';
import eventBus from '../lib/event-bus';
import gettext from '../lib/gettext';
import PortalVue from 'portal-vue';
import BaseComponents from '../../../vue/base-components.js';
import BaseDirectives from "../../../vue/base-directives.js";
import StudipStore from "../../../vue/store/StudipStore.js";
import { resourceModule } from '@/assets/javascripts/lib/reststate-vuex.js';
import axios from 'axios';

import CKEditor from '@ckeditor/ckeditor5-vue';

// fixes a problem with @vojtechlanka/vue-tags-input
if (window.Vue) {
    window.Vue.use = () => {};
}

const getHttpClient = () =>
    axios.create({
        baseURL: STUDIP.URLHelper.getURL(`jsonapi.php/v1`, {}, true),
        headers: {
            'Content-Type': 'application/vnd.api+json',
        },
    });

const httpClient = getHttpClient();

const createVuexStore = () => {
    const store = vuexCreateStore({});

    store.registerModule('studip', StudipStore);

    STUDIP.jsonapi_schemas.forEach((name) => {
        store.registerModule(name, resourceModule({ name, httpClient }));
    });

    return store;
}

// Setup stores
const store = createVuexStore();
const pinia = createPinia();

// Define createApp function
function createApp(options = {}, ...args) {
    const app = vueCreateApp({ store, ...options }, ...args);

    app.config.compilerOptions.whitespace = 'preserve';

    // Define our own global mixin for Vue
    app.mixin({
        methods: {
            globalEmit(...args) {
                eventBus.emit(...args);
            },
            globalOn(...args) {
                eventBus.on(...args);
            },
            globalOff(...args) {
                eventBus.off(...args);
            },
            getStudipConfig: store.getters['studip/getConfig']
        },
    });

    app.use(CKEditor);
    app.use(gettext);
    app.use(PortalVue);
    app.use(store);
    app.use(pinia);

    // Register global components and directives
    registerGlobalComponents(app);
    registerGlobalDirectives(app);

    if (options.el) {
        app.mount(options.el);
    }
    return app;
}

// Define global registration functions for components and directives
function registerGlobalComponents(app) {
    for (const [name, component] of Object.entries(BaseComponents)) {
        app.component(name, component);
    }
}

function registerGlobalDirectives(app) {
    for (const [name, directive] of Object.entries(BaseDirectives)) {
        app.directive(name, directive);
    }
}

export { createApp, h, eventBus, store, pinia, httpClient };