| 1 | 
           efrain | 
           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 | 
            * The bulk editor toggler button control.
  | 
        
        
            | 
            | 
           18 | 
            *
  | 
        
        
            | 
            | 
           19 | 
            * @module     core_courseformat/local/content/bulkedittoggler
  | 
        
        
            | 
            | 
           20 | 
            * @class      core_courseformat/local/content/bulkedittoggler
  | 
        
        
            | 
            | 
           21 | 
            * @copyright  2023 Ferran Recio <ferran@moodle.com>
  | 
        
        
            | 
            | 
           22 | 
            * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  | 
        
        
            | 
            | 
           23 | 
            */
  | 
        
        
            | 
            | 
           24 | 
              | 
        
        
            | 
            | 
           25 | 
           import {BaseComponent} from 'core/reactive';
  | 
        
        
            | 
            | 
           26 | 
           import {getCurrentCourseEditor} from 'core_courseformat/courseeditor';
  | 
        
        
            | 
            | 
           27 | 
           import Pending from 'core/pending';
  | 
        
        
           | 1441 | 
           ariadna | 
           28 | 
           import log from "core/log";
  | 
        
        
           | 1 | 
           efrain | 
           29 | 
              | 
        
        
            | 
            | 
           30 | 
           export default class Component extends BaseComponent {
  | 
        
        
            | 
            | 
           31 | 
              | 
        
        
            | 
            | 
           32 | 
               /**
  | 
        
        
            | 
            | 
           33 | 
                * Constructor hook.
  | 
        
        
            | 
            | 
           34 | 
                */
  | 
        
        
            | 
            | 
           35 | 
               create() {
  | 
        
        
            | 
            | 
           36 | 
                   // Optional component name for debugging.
  | 
        
        
            | 
            | 
           37 | 
                   this.name = 'bulk_editor_toogler';
  | 
        
        
            | 
            | 
           38 | 
                   // Default query selectors.
  | 
        
        
            | 
            | 
           39 | 
                   this.selectors = {
  | 
        
        
            | 
            | 
           40 | 
                       BODY: `body`,
  | 
        
        
            | 
            | 
           41 | 
                       SELECTABLE: `[data-bulkcheckbox][data-is-selectable]`,
  | 
        
        
            | 
            | 
           42 | 
                   };
  | 
        
        
            | 
            | 
           43 | 
                   // Component css classes.
  | 
        
        
            | 
            | 
           44 | 
                   this.classes = {
  | 
        
        
            | 
            | 
           45 | 
                       HIDDEN: `d-none`,
  | 
        
        
            | 
            | 
           46 | 
                       BULK: `bulkenabled`,
  | 
        
        
            | 
            | 
           47 | 
                   };
  | 
        
        
            | 
            | 
           48 | 
               }
  | 
        
        
            | 
            | 
           49 | 
              | 
        
        
            | 
            | 
           50 | 
               /**
  | 
        
        
            | 
            | 
           51 | 
                * Static method to create a component instance from the mustache template.
  | 
        
        
            | 
            | 
           52 | 
                *
  | 
        
        
            | 
            | 
           53 | 
                * @param {string} target optional altentative DOM main element CSS selector
  | 
        
        
            | 
            | 
           54 | 
                * @param {object} selectors optional css selector overrides
  | 
        
        
            | 
            | 
           55 | 
                * @return {Component}
  | 
        
        
            | 
            | 
           56 | 
                */
  | 
        
        
            | 
            | 
           57 | 
               static init(target, selectors) {
  | 
        
        
           | 1441 | 
           ariadna | 
           58 | 
                   let element = document.querySelector(target);
  | 
        
        
            | 
            | 
           59 | 
                   // TODO Remove this if condition as part of MDL-83851.
  | 
        
        
            | 
            | 
           60 | 
                   if (!element) {
  | 
        
        
            | 
            | 
           61 | 
                       log.debug('Init component with id is deprecated, use a query selector instead.');
  | 
        
        
            | 
            | 
           62 | 
                       element = document.getElementById(target);
  | 
        
        
            | 
            | 
           63 | 
                   }
  | 
        
        
           | 1 | 
           efrain | 
           64 | 
                   return new this({
  | 
        
        
           | 1441 | 
           ariadna | 
           65 | 
                       element,
  | 
        
        
           | 1 | 
           efrain | 
           66 | 
                       reactive: getCurrentCourseEditor(),
  | 
        
        
            | 
            | 
           67 | 
                       selectors
  | 
        
        
            | 
            | 
           68 | 
                   });
  | 
        
        
            | 
            | 
           69 | 
               }
  | 
        
        
            | 
            | 
           70 | 
              | 
        
        
            | 
            | 
           71 | 
               /**
  | 
        
        
            | 
            | 
           72 | 
                * Initial state ready method.
  | 
        
        
            | 
            | 
           73 | 
                */
  | 
        
        
            | 
            | 
           74 | 
               stateReady() {
  | 
        
        
            | 
            | 
           75 | 
                   // Capture completion events.
  | 
        
        
            | 
            | 
           76 | 
                   this.addEventListener(
  | 
        
        
            | 
            | 
           77 | 
                       this.element,
  | 
        
        
            | 
            | 
           78 | 
                       'click',
  | 
        
        
            | 
            | 
           79 | 
                       this._enableBulk
  | 
        
        
            | 
            | 
           80 | 
                   );
  | 
        
        
            | 
            | 
           81 | 
               }
  | 
        
        
            | 
            | 
           82 | 
              | 
        
        
            | 
            | 
           83 | 
               /**
  | 
        
        
            | 
            | 
           84 | 
                * Component watchers.
  | 
        
        
            | 
            | 
           85 | 
                *
  | 
        
        
            | 
            | 
           86 | 
                * @returns {Array} of watchers
  | 
        
        
            | 
            | 
           87 | 
                */
  | 
        
        
            | 
            | 
           88 | 
               getWatchers() {
  | 
        
        
            | 
            | 
           89 | 
                   return [
  | 
        
        
            | 
            | 
           90 | 
                       {watch: `bulk.enabled:updated`, handler: this._refreshToggler},
  | 
        
        
            | 
            | 
           91 | 
                   ];
  | 
        
        
            | 
            | 
           92 | 
               }
  | 
        
        
            | 
            | 
           93 | 
              | 
        
        
            | 
            | 
           94 | 
               /**
  | 
        
        
            | 
            | 
           95 | 
                * Update a content section using the state information.
  | 
        
        
            | 
            | 
           96 | 
                *
  | 
        
        
            | 
            | 
           97 | 
                * @param {object} param
  | 
        
        
            | 
            | 
           98 | 
                * @param {Object} param.element details the update details (state.bulk in this case).
  | 
        
        
            | 
            | 
           99 | 
                */
  | 
        
        
            | 
            | 
           100 | 
               _refreshToggler({element}) {
  | 
        
        
            | 
            | 
           101 | 
                   this.element.classList.toggle(this.classes.HIDDEN, element.enabled ?? false);
  | 
        
        
            | 
            | 
           102 | 
                   document.querySelector(this.selectors.BODY)?.classList.toggle(this.classes.BULK, element.enabled);
  | 
        
        
            | 
            | 
           103 | 
               }
  | 
        
        
            | 
            | 
           104 | 
              | 
        
        
            | 
            | 
           105 | 
               /**
  | 
        
        
            | 
            | 
           106 | 
                * Dispatch the enable bulk mutation.
  | 
        
        
            | 
            | 
           107 | 
                *
  | 
        
        
            | 
            | 
           108 | 
                * The enable bulk button is outside of the course content main div.
  | 
        
        
            | 
            | 
           109 | 
                * Because content/actions captures click events only in the course
  | 
        
        
            | 
            | 
           110 | 
                * content, this button needs to trigger the enable bulk mutation
  | 
        
        
            | 
            | 
           111 | 
                * by itself.
  | 
        
        
            | 
            | 
           112 | 
                */
  | 
        
        
            | 
            | 
           113 | 
               _enableBulk() {
  | 
        
        
            | 
            | 
           114 | 
                   const pendingToggle = new Pending(`courseformat/content:bulktoggle_on`);
  | 
        
        
            | 
            | 
           115 | 
                   this.reactive.dispatch('bulkEnable', true);
  | 
        
        
            | 
            | 
           116 | 
                   // Wait for a while and focus on the first checkbox.
  | 
        
        
            | 
            | 
           117 | 
                   setTimeout(() => {
  | 
        
        
            | 
            | 
           118 | 
                       document.querySelector(this.selectors.SELECTABLE)?.focus();
  | 
        
        
            | 
            | 
           119 | 
                       pendingToggle.resolve();
  | 
        
        
            | 
            | 
           120 | 
                   }, 150);
  | 
        
        
            | 
            | 
           121 | 
               }
  | 
        
        
            | 
            | 
           122 | 
           }
  |