lib/amd/src/local/process_monitor/monitor.js

  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.
  12. //
  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/>.
  15. /**
  16. * The file upload monitor component.
  17. *
  18. * @module core/local/process_monitor/monitor
  19. * @class core/local/process_monitor/monitor
  20. * @copyright 2022 Ferran Recio <ferran@moodle.com>
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22. */
  23. import Templates from 'core/templates';
  24. import {BaseComponent} from 'core/reactive';
  25. import {manager} from 'core/local/process_monitor/manager';
  26. export default class extends BaseComponent {
  27. /**
  28. * Constructor hook.
  29. */
  30. create() {
  31. // Optional component name for debugging.
  32. this.name = 'process_monitor';
  33. // Default query selectors.
  34. this.selectors = {
  35. QUEUELIST: `[data-for="process-list"]`,
  36. CLOSE: `[data-action="hide"]`,
  37. };
  38. // Default classes to toggle on refresh.
  39. this.classes = {
  40. HIDE: `d-none`,
  41. };
  42. }
  43. /**
  44. * Static method to create a component instance form the mustache template.
  45. *
  46. * @param {string} query the DOM main element query selector
  47. * @param {object} selectors optional css selector overrides
  48. * @return {this}
  49. */
  50. static init(query, selectors) {
  51. return new this({
  52. element: document.querySelector(query),
  53. reactive: manager,
  54. selectors,
  55. });
  56. }
  57. /**
  58. * Initial state ready method.
  59. *
  60. * @param {Object} state the initial state
  61. */
  62. stateReady(state) {
  63. this._updateMonitor({state, element: state.display});
  64. this.addEventListener(this.getElement(this.selectors.CLOSE), 'click', this._closeMonitor);
  65. state.queue.forEach((element) => {
  66. this._createListItem({state, element});
  67. });
  68. }
  69. /**
  70. * Return the component watchers.
  71. *
  72. * @returns {Array} of watchers
  73. */
  74. getWatchers() {
  75. return [
  76. // State changes that require to reload some course modules.
  77. {watch: `queue:created`, handler: this._createListItem},
  78. {watch: `display:updated`, handler: this._updateMonitor},
  79. ];
  80. }
  81. /**
  82. * Create a monitor item.
  83. *
  84. * @param {object} args the watcher arguments
  85. * @param {object} args.element the item state data
  86. */
  87. async _createListItem({element}) {
  88. const {html, js} = await Templates.renderForPromise(
  89. 'core/local/process_monitor/process',
  90. {...element}
  91. );
  92. const target = this.getElement(this.selectors.QUEUELIST);
  93. Templates.appendNodeContents(target, html, js);
  94. }
  95. /**
  96. * Create a monitor item.
  97. *
  98. * @param {object} args the watcher arguments
  99. * @param {object} args.element the display state data
  100. */
  101. _updateMonitor({element}) {
  102. this.element.classList.toggle(this.classes.HIDE, element.show !== true);
  103. }
  104. /**
  105. * Close the monitor.
  106. */
  107. _closeMonitor() {
  108. this.reactive.dispatch('setShow', false);
  109. }
  110. }