aboutsummaryrefslogtreecommitdiff
path: root/resources/assets/javascripts/chunk-loader.js
blob: dcd95cc63df3934e4ebdf17736ba3fa31e249269 (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
98
99
100
101
102
export const loadScript = function (script_name) {
    return new Promise(function (resolve, reject) {
        let script = document.createElement('script');
        script.src = `${STUDIP.ASSETS_URL}${script_name}`;
        script.onload = resolve;
        script.onerror = reject;
        document.head.appendChild(script);
    });
};

export const loadChunk = (function () {
    let mathjax_promise = null;

    return function (chunk) {
        let promise = null;
        switch (chunk) {

            case 'code-highlight':
                promise = import(
                    /* webpackChunkName: "code-highlight" */
                    './chunks/code-highlight'
                ).then(({default: hljs}) => {
                    return hljs;
                });
                break;

            case 'courseware':
                promise = Promise.all([
                    STUDIP.loadChunk('vue'),
                    import(
                        /* webpackChunkName: "courseware" */
                        './chunks/courseware'
                    ),
                ]).then(([Vue]) => Vue);
                break;

            case 'chartist':
                promise = import(
                    /* webpackChunkName: "chartist" */
                    './chunks/chartist'
                ).then(({ default: Chartist }) => Chartist);
                break;

            case 'fullcalendar':
                promise = import(
                    /* webpackChunkName: "fullcalendar" */
                    './chunks/fullcalendar'
                );
                break;

            case 'tablesorter':
                promise = import(
                    /* webpackChunkName: "tablesorter" */
                    './chunks/tablesorter'
                );
                break;

            case 'mathjax':
                if (mathjax_promise === null) {
                    mathjax_promise = STUDIP.loadScript(
                        'javascripts/mathjax/MathJax.js?config=TeX-AMS_HTML,default'
                    ).then(() => {
                        (function (origPrint) {
                            window.print = function () {
                                window.MathJax.Hub.Queue(
                                    ['Delay', window.MathJax.Callback, 700],
                                    origPrint
                                );
                            };
                        })(window.print);

                        return window.MathJax;
                    }).catch(() => {
                        console.log('Could not load mathjax')
                    });
                }
                promise = mathjax_promise;
                break;

            case 'vue':
                promise = import(
                    /* webpackChunkName: "vue.js" */
                    './chunks/vue'
                );
                break;

            case 'wysiwyg':
                promise = import(
                    /* webpackChunkName: "wysiwyg.js" */
                    './chunks/wysiwyg'
                );
                break;

            default:
                promise = Promise.reject(new Error(`Unknown chunk: ${chunk}`));
        }

        return promise.catch((error) => {
            console.error(`Could not load chunk ${chunk}`, error);
        });
    };
}());