theme/boost/amd/src/courseindexdrawercontrols.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. * Controls for the course index drawer, such as expand-all/collapse-all sections.
  17. *
  18. * @module theme_boost/courseindexdrawercontrols
  19. * @copyright 2023 Stefan Topfstedt
  20. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  21. */
  22. import {BaseComponent} from 'core/reactive';
  23. import {getCurrentCourseEditor} from 'core_courseformat/courseeditor';
  24. export default class Component extends BaseComponent {
  25. create() {
  26. this.name = 'courseindexdrawercontrols';
  27. this.selectors = {
  28. COLLAPSEALL: `[data-action="collapseallcourseindexsections"]`,
  29. EXPANDALL: `[data-action="expandallcourseindexsections"]`,
  30. };
  31. }
  32. /**
  33. * @param {element|string} target the DOM main element or its ID
  34. * @param {object} selectors optional css selector overrides
  35. * @return {Component}
  36. */
  37. static init(target, selectors) {
  38. return new Component({
  39. element: document.getElementById(target),
  40. reactive: getCurrentCourseEditor(),
  41. selectors,
  42. });
  43. }
  44. /**
  45. * Initial state ready method.
  46. */
  47. stateReady() {
  48. // Attach the on-click event handlers to the expand-all and collapse-all buttons, if present.
  49. const expandAllBtn = this.getElement(this.selectors.EXPANDALL);
  50. if (expandAllBtn) {
  51. this.addEventListener(expandAllBtn, 'click', this._expandAllSections);
  52. }
  53. const collapseAllBtn = this.getElement(this.selectors.COLLAPSEALL);
  54. if (collapseAllBtn) {
  55. this.addEventListener(collapseAllBtn, 'click', this._collapseAllSections);
  56. }
  57. }
  58. /**
  59. * On-click event handler for the collapse-all button.
  60. * @private
  61. */
  62. _collapseAllSections() {
  63. this._toggleAllSections(true);
  64. }
  65. /**
  66. * On-click event handler for the expand-all button.
  67. * @private
  68. */
  69. _expandAllSections() {
  70. this._toggleAllSections(false);
  71. }
  72. /**
  73. * Collapses or expands all sections in the course index.
  74. * @param {boolean} expandOrCollapse set to TRUE to collapse all, and FALSE to expand all.
  75. * @private
  76. */
  77. _toggleAllSections(expandOrCollapse) {
  78. this.reactive.dispatch('allSectionsIndexCollapsed', expandOrCollapse);
  79. }
  80. }