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
|
import axios from 'axios';
import { mapResourceModules } from '@/assets/javascripts/lib/reststate-vuex.js';
import stockImagesModule from '../store/stock-images.js';
import * as components from '../components/stock-images/components.js';
const JSONAPI_PATH = 'jsonapi.php/v1';
export const StockImagesPlugin = {
install(Vue, options = {}) {
if (!('store' in options)) {
throw new Error('You must provide the vuex store via the options argument');
}
this.enhanceStore(options.store);
this.registerComponents(Vue);
},
enhanceStore(store) {
const httpClient = getHttpClient(window.STUDIP.URLHelper.getURL(JSONAPI_PATH, {}, true));
initializeStore(store, httpClient);
},
registerComponents(Vue) {
Object.entries(components).forEach(([name, component]) => {
const exists = Vue.component(name);
if (!exists) {
Vue.component(name, component);
}
});
},
};
function getHttpClient(baseURL) {
return axios.create({ baseURL, headers: { 'Content-Type': 'application/vnd.api+json' } });
}
function initializeStore(store, httpClient) {
const modules = mapResourceModules({ names: ['stock-images'], httpClient });
Object.entries(modules).forEach(([name, module]) => {
if (!store.hasModule(name)) {
store.registerModule(name, module);
}
});
if (!store.hasModule(['studip'])) {
store.registerModule(['studip'], { namespaced: true });
}
if (!store.hasModule(['studip', 'stockImages'])) {
store.registerModule(['studip', 'stockImages'], stockImagesModule);
store.commit('studip/stockImages/setHttpClient', httpClient);
}
}
|