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