aboutsummaryrefslogtreecommitdiff
path: root/resources/assets/javascripts/chunk-loader.js
diff options
context:
space:
mode:
authorPhilipp Schüttlöffel <schuettloeffel@zqs.uni-hannover.de>2024-09-24 10:53:31 +0200
committerPhilipp Schüttlöffel <schuettloeffel@zqs.uni-hannover.de>2024-09-24 10:53:31 +0200
commit4459dd7917f4d1c34f40bb68f0e991e9c3d53e4c (patch)
tree5c07151ae61276d334e88f6309c30d439a85c12e /resources/assets/javascripts/chunk-loader.js
parentda0022e5c1abbf9825ae76debaabdff7e8623bb4 (diff)
parent97a188592c679890a25c37ab78463add76a52ff7 (diff)
Merge branch 'main' into issue-3911issue-3911
Diffstat (limited to 'resources/assets/javascripts/chunk-loader.js')
-rw-r--r--resources/assets/javascripts/chunk-loader.js153
1 files changed, 79 insertions, 74 deletions
diff --git a/resources/assets/javascripts/chunk-loader.js b/resources/assets/javascripts/chunk-loader.js
index dcd95cc..f372286 100644
--- a/resources/assets/javascripts/chunk-loader.js
+++ b/resources/assets/javascripts/chunk-loader.js
@@ -8,95 +8,100 @@ export const loadScript = function (script_name) {
});
};
-export const loadChunk = (function () {
- let mathjax_promise = null;
+let mathjax_promise = null;
- return function (chunk) {
- let promise = null;
- switch (chunk) {
+/** This function dynamically loads JS features organized in chunks.
+ *
+ * @param {string} chunk The name of the chunk to load.
+ * @param {{ silent: boolean }} options Options for loading the chunk.
+ * Pass `{ silent: true }` to supress
+ * error messages.
+ * @return {Promise}
+ */
+export const loadChunk = function (chunk, { silent = false } = {}) {
+ let promise = null;
+ switch (chunk) {
+ case 'code-highlight':
+ promise = import(
+ /* webpackChunkName: "code-highlight" */
+ './chunks/code-highlight'
+ ).then(({ default: hljs }) => {
+ return hljs;
+ });
+ break;
- 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 '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 'chartist':
- promise = import(
- /* webpackChunkName: "chartist" */
- './chunks/chartist'
- ).then(({ default: Chartist }) => Chartist);
- break;
+ case 'fullcalendar':
+ promise = import(
+ /* webpackChunkName: "fullcalendar" */
+ './chunks/fullcalendar'
+ );
+ break;
- case 'fullcalendar':
- promise = import(
- /* webpackChunkName: "fullcalendar" */
- './chunks/fullcalendar'
- );
- break;
+ case 'tablesorter':
+ promise = import(
+ /* webpackChunkName: "tablesorter" */
+ './chunks/tablesorter'
+ );
+ 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(() => {
+ 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.MathJax.Hub.Queue(['Delay', window.MathJax.Callback, 700], origPrint);
};
})(window.print);
return window.MathJax;
- }).catch(() => {
- console.log('Could not load mathjax')
+ })
+ .catch(() => {
+ throw new Error('Could not load mathjax');
});
- }
- promise = mathjax_promise;
- break;
+ }
+ promise = mathjax_promise;
+ break;
- case 'vue':
- promise = import(
- /* webpackChunkName: "vue.js" */
- './chunks/vue'
- );
- break;
+ case 'vue':
+ promise = import(
+ /* webpackChunkName: "vue.js" */
+ './chunks/vue'
+ );
+ break;
- case 'wysiwyg':
- promise = import(
- /* webpackChunkName: "wysiwyg.js" */
- './chunks/wysiwyg'
- );
- break;
+ case 'wysiwyg':
+ promise = import(
+ /* webpackChunkName: "wysiwyg.js" */
+ './chunks/wysiwyg'
+ );
+ break;
- default:
- promise = Promise.reject(new Error(`Unknown chunk: ${chunk}`));
- }
+ default:
+ promise = Promise.reject(new Error(`Unknown chunk: ${chunk}`));
+ }
- return promise.catch((error) => {
+ return promise.catch((error) => {
+ if (!silent) {
console.error(`Could not load chunk ${chunk}`, error);
- });
- };
-}());
+ }
+ throw error;
+ });
+};