1 // This file is part of Moodle - http://moodle.org/
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.
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/>.
17 * Process monitor includer.
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
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;
33 * Get the parent container.
35 * @return {HTMLelement} the process monitor container.
37 const getParentContainer = () => {
38 // The footer pop over depends on the theme.
39 return document.querySelector(`#page`) ?? document.body;
42 export const processMonitor = {
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
52 addLoadingProcess: function(definition) {
53 this.initProcessMonitor();
54 const process = new LoadingProcess(manager, definition);
59 * Remove all processes form the current monitor.
61 removeAllProcesses: function() {
62 manager.getInitialStatePromise().then(() => {
63 manager.dispatch('removeAllProcesses');
66 log.error(`Cannot update process monitor.`);
71 * Initialize the process monitor.
73 initProcessMonitor: async function() {
78 const container = getParentContainer();
79 if (document.getElementById(`#processMonitor`)) {
83 const {html, js} = await Templates.renderForPromise('core/local/process_monitor/monitor', {});
84 Templates.appendNodeContents(container, html, js);
86 log.error(`Cannot load the process monitor`);
91 * Return the process monitor initial state promise.
92 * @returns {Promise} Promise of the initial state fully loaded
94 getInitialStatePromise: function() {
95 return manager.getInitialStatePromise();
99 * Load the load queue monitor.
101 * @return {Promise<ProcessQueue>} when the file uploader is ready to be used.
103 createProcessQueue: async function() {
104 processMonitor.initProcessMonitor();
105 const processQueue = new ProcessQueue(manager);
106 await processMonitor.getInitialStatePromise();