Merge branch 'MDL-81713-main' of https://github.com/junpataleta/moodle
[moodle.git] / lib / amd / src / process_monitor.js
blob3e7c73f553ee6256df7e090117262b302c84837d
1 // This file is part of Moodle - http://moodle.org/
2 //
3 // Moodle is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, either version 3 of the License, or
6 // (at your option) any later version.
7 //
8 // Moodle is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 // GNU General Public License for more details.
13 // You should have received a copy of the GNU General Public License
14 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16 /**
17  * Process monitor includer.
18  *
19  * @module     core/process_monitor
20  * @copyright  2022 Ferran Recio <ferran@moodle.com>
21  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22  */
24 import log from 'core/log';
25 import {manager} from 'core/local/process_monitor/manager';
26 import {LoadingProcess} from 'core/local/process_monitor/loadingprocess';
27 import {ProcessQueue} from 'core/local/process_monitor/processqueue';
28 import Templates from 'core/templates';
30 let initialized = false;
32 /**
33  * Get the parent container.
34  * @private
35  * @return {HTMLelement} the process monitor container.
36  */
37 const getParentContainer = () => {
38     // The footer pop over depends on the theme.
39     return document.querySelector(`#page`) ?? document.body;
42 export const processMonitor = {
43     /**
44      * Adds a new process to the monitor.
45      * @param {Object} definition the process definition
46      * @param {String} definition.name the process name
47      * @param {Number} definition.percentage the current percentage (0 - 100)
48      * @param {String} definition.error the error message if any
49      * @param {String} definition.url possible link url if any
50      * @returns {LoadingProcess} the loading process
51      */
52     addLoadingProcess: function(definition) {
53         this.initProcessMonitor();
54         const process = new LoadingProcess(manager, definition);
55         return process;
56     },
58     /**
59      * Remove all processes form the current monitor.
60      */
61     removeAllProcesses: function() {
62         manager.getInitialStatePromise().then(() => {
63             manager.dispatch('removeAllProcesses');
64             return;
65         }).catch(() => {
66             log.error(`Cannot update process monitor.`);
67         });
68     },
70     /**
71      * Initialize the process monitor.
72      */
73     initProcessMonitor: async function() {
74         if (initialized) {
75             return;
76         }
77         initialized = true;
78         const container = getParentContainer();
79         if (document.getElementById(`#processMonitor`)) {
80             return;
81         }
82         try {
83             const {html, js} = await Templates.renderForPromise('core/local/process_monitor/monitor', {});
84             Templates.appendNodeContents(container, html, js);
85         } catch (error) {
86             log.error(`Cannot load the process monitor`);
87         }
88     },
90     /**
91      * Return the process monitor initial state promise.
92      * @returns {Promise} Promise of the initial state fully loaded
93      */
94     getInitialStatePromise: function() {
95         return manager.getInitialStatePromise();
96     },
98     /**
99      * Load the load queue monitor.
100      *
101      * @return {Promise<ProcessQueue>} when the file uploader is ready to be used.
102      */
103     createProcessQueue: async function() {
104         processMonitor.initProcessMonitor();
105         const processQueue = new ProcessQueue(manager);
106         await processMonitor.getInitialStatePromise();
107         return processQueue;
108     }