| 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 | 
            * A small dropdown to filter users within the gradebook.
  | 
        
        
            | 
            | 
           18 | 
            *
  | 
        
        
            | 
            | 
           19 | 
            * @module    core_grades/searchwidget/initials
  | 
        
        
            | 
            | 
           20 | 
            * @copyright 2022 Mathew May <mathew.solutions>
  | 
        
        
            | 
            | 
           21 | 
            * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  | 
        
        
            | 
            | 
           22 | 
            */
  | 
        
        
            | 
            | 
           23 | 
              | 
        
        
            | 
            | 
           24 | 
           import Pending from 'core/pending';
  | 
        
        
            | 
            | 
           25 | 
           import * as Url from 'core/url';
  | 
        
        
            | 
            | 
           26 | 
           import CustomEvents from "core/custom_interaction_events";
  | 
        
        
            | 
            | 
           27 | 
           import $ from 'jquery';
  | 
        
        
            | 
            | 
           28 | 
              | 
        
        
            | 
            | 
           29 | 
           /**
  | 
        
        
            | 
            | 
           30 | 
            * Whether the event listener has already been registered for this module.
  | 
        
        
            | 
            | 
           31 | 
            *
  | 
        
        
            | 
            | 
           32 | 
            * @type {boolean}
  | 
        
        
            | 
            | 
           33 | 
            */
  | 
        
        
            | 
            | 
           34 | 
           let registered = false;
  | 
        
        
            | 
            | 
           35 | 
              | 
        
        
            | 
            | 
           36 | 
           // Contain our selectors within this file until they could be of use elsewhere.
  | 
        
        
            | 
            | 
           37 | 
           const selectors = {
  | 
        
        
            | 
            | 
           38 | 
               pageListItem: 'page-item',
  | 
        
        
            | 
            | 
           39 | 
               pageClickableItem: '.page-link',
  | 
        
        
            | 
            | 
           40 | 
               activeItem: 'active',
  | 
        
        
            | 
            | 
           41 | 
               formDropdown: '.initialsdropdownform',
  | 
        
        
            | 
            | 
           42 | 
               parentDomNode: '.initials-selector',
  | 
        
        
            | 
            | 
           43 | 
               firstInitial: 'firstinitial',
  | 
        
        
            | 
            | 
           44 | 
               lastInitial: 'lastinitial',
  | 
        
        
            | 
            | 
           45 | 
               initialBars: '.initialbar', // Both first and last name use this class.
  | 
        
        
            | 
            | 
           46 | 
               targetButton: 'initialswidget',
  | 
        
        
            | 
            | 
           47 | 
               formItems: {
  | 
        
        
            | 
            | 
           48 | 
                   type: 'submit',
  | 
        
        
            | 
            | 
           49 | 
                   save: 'save',
  | 
        
        
            | 
            | 
           50 | 
                   cancel: 'cancel'
  | 
        
        
            | 
            | 
           51 | 
               }
  | 
        
        
            | 
            | 
           52 | 
           };
  | 
        
        
            | 
            | 
           53 | 
              | 
        
        
            | 
            | 
           54 | 
           /**
  | 
        
        
            | 
            | 
           55 | 
            * Our initial hook into the module which will eventually allow us to handle the dropdown initials bar form.
  | 
        
        
            | 
            | 
           56 | 
            *
  | 
        
        
            | 
            | 
           57 | 
            * @param {String} callingLink The link to redirect upon form submission.
  | 
        
        
            | 
            | 
           58 | 
            * @param {Null|Number} gpr_userid The user id to filter by.
  | 
        
        
            | 
            | 
           59 | 
            * @param {Null|String} gpr_search The search value to filter by.
  | 
        
        
            | 
            | 
           60 | 
            */
  | 
        
        
            | 
            | 
           61 | 
           export const init = (callingLink, gpr_userid = null, gpr_search = null) => {
  | 
        
        
            | 
            | 
           62 | 
               if (registered) {
  | 
        
        
            | 
            | 
           63 | 
                   return;
  | 
        
        
            | 
            | 
           64 | 
               }
  | 
        
        
            | 
            | 
           65 | 
               const pendingPromise = new Pending();
  | 
        
        
            | 
            | 
           66 | 
               registerListenerEvents(callingLink, gpr_userid, gpr_search);
  | 
        
        
            | 
            | 
           67 | 
               // BS events always bubble so, we need to listen for the event higher up the chain.
  | 
        
        
            | 
            | 
           68 | 
               $(selectors.parentDomNode).on('shown.bs.dropdown', () => {
  | 
        
        
            | 
            | 
           69 | 
                   document.querySelector(selectors.pageClickableItem).focus({preventScroll: true});
  | 
        
        
            | 
            | 
           70 | 
               });
  | 
        
        
            | 
            | 
           71 | 
               pendingPromise.resolve();
  | 
        
        
            | 
            | 
           72 | 
               registered = true;
  | 
        
        
            | 
            | 
           73 | 
           };
  | 
        
        
            | 
            | 
           74 | 
              | 
        
        
            | 
            | 
           75 | 
           /**
  | 
        
        
            | 
            | 
           76 | 
            * Register event listeners.
  | 
        
        
            | 
            | 
           77 | 
            *
  | 
        
        
            | 
            | 
           78 | 
            * @param {String} callingLink The link to redirect upon form submission.
  | 
        
        
            | 
            | 
           79 | 
            * @param {Null|Number} gpr_userid The user id to filter by.
  | 
        
        
            | 
            | 
           80 | 
            * @param {Null|String} gpr_search The search value to filter by.
  | 
        
        
            | 
            | 
           81 | 
            */
  | 
        
        
            | 
            | 
           82 | 
           const registerListenerEvents = (callingLink, gpr_userid = null, gpr_search = null) => {
  | 
        
        
            | 
            | 
           83 | 
               const events = [
  | 
        
        
            | 
            | 
           84 | 
                   'click',
  | 
        
        
            | 
            | 
           85 | 
                   CustomEvents.events.activate,
  | 
        
        
            | 
            | 
           86 | 
                   CustomEvents.events.keyboardActivate
  | 
        
        
            | 
            | 
           87 | 
               ];
  | 
        
        
            | 
            | 
           88 | 
               CustomEvents.define(document, events);
  | 
        
        
            | 
            | 
           89 | 
              | 
        
        
            | 
            | 
           90 | 
               // Register events.
  | 
        
        
            | 
            | 
           91 | 
               events.forEach((event) => {
  | 
        
        
            | 
            | 
           92 | 
                   document.addEventListener(event, (e) => {
  | 
        
        
            | 
            | 
           93 | 
                       // Always fetch the latest information when we click as state is a fickle thing.
  | 
        
        
            | 
            | 
           94 | 
                       let {firstActive, lastActive, sifirst, silast} = onClickVariables();
  | 
        
        
            | 
            | 
           95 | 
                       let itemToReset = '';
  | 
        
        
            | 
            | 
           96 | 
              | 
        
        
            | 
            | 
           97 | 
                       // Prevent the usual form behaviour.
  | 
        
        
            | 
            | 
           98 | 
                       if (e.target.closest(selectors.formDropdown)) {
  | 
        
        
            | 
            | 
           99 | 
                           e.preventDefault();
  | 
        
        
            | 
            | 
           100 | 
                       }
  | 
        
        
            | 
            | 
           101 | 
              | 
        
        
            | 
            | 
           102 | 
                       // Handle the state of active initials before form submission.
  | 
        
        
            | 
            | 
           103 | 
                       if (e.target.closest(`${selectors.formDropdown} .${selectors.pageListItem}`)) {
  | 
        
        
            | 
            | 
           104 | 
                           // Ensure the li items don't cause weird clicking emptying out the form.
  | 
        
        
            | 
            | 
           105 | 
                           if (e.target.classList.contains(selectors.pageListItem)) {
  | 
        
        
            | 
            | 
           106 | 
                               return;
  | 
        
        
            | 
            | 
           107 | 
                           }
  | 
        
        
            | 
            | 
           108 | 
              | 
        
        
            | 
            | 
           109 | 
                           const initialsBar = e.target.closest(selectors.initialBars); // Find out which initial bar we are in.
  | 
        
        
            | 
            | 
           110 | 
              | 
        
        
            | 
            | 
           111 | 
                           // We want to find the current active item in the menu area the user selected.
  | 
        
        
            | 
            | 
           112 | 
                           // We also want to fetch the raw item out of the array for instant manipulation.
  | 
        
        
            | 
            | 
           113 | 
                           if (initialsBar.classList.contains(selectors.firstInitial)) {
  | 
        
        
            | 
            | 
           114 | 
                               sifirst = e.target;
  | 
        
        
            | 
            | 
           115 | 
                               itemToReset = firstActive;
  | 
        
        
            | 
            | 
           116 | 
                           } else {
  | 
        
        
            | 
            | 
           117 | 
                               silast = e.target;
  | 
        
        
            | 
            | 
           118 | 
                               itemToReset = lastActive;
  | 
        
        
            | 
            | 
           119 | 
                           }
  | 
        
        
            | 
            | 
           120 | 
                           swapActiveItems(itemToReset, e);
  | 
        
        
            | 
            | 
           121 | 
                       }
  | 
        
        
            | 
            | 
           122 | 
              | 
        
        
            | 
            | 
           123 | 
                       // Handle form submissions.
  | 
        
        
            | 
            | 
           124 | 
                       if (e.target.closest(`${selectors.formDropdown}`) && e.target.type === selectors.formItems.type) {
  | 
        
        
            | 
            | 
           125 | 
                           if (e.target.dataset.action === selectors.formItems.save) {
  | 
        
        
            | 
            | 
           126 | 
                               // Ensure we strip out the value (All) as it messes with the PHP side of the initials bar.
  | 
        
        
            | 
            | 
           127 | 
                               // Then we will redirect the user back onto the page with new filters applied.
  | 
        
        
            | 
            | 
           128 | 
                               const params = {
  | 
        
        
            | 
            | 
           129 | 
                                   'id': e.target.closest(selectors.formDropdown).dataset.courseid,
  | 
        
        
            | 
            | 
           130 | 
                                   'gpr_search': gpr_search !== null ? gpr_search : '',
  | 
        
        
            | 
            | 
           131 | 
                                   'sifirst': sifirst.parentElement.classList.contains('initialbarall') ? '' : sifirst.value,
  | 
        
        
            | 
            | 
           132 | 
                                   'silast': silast.parentElement.classList.contains('initialbarall') ? '' : silast.value,
  | 
        
        
            | 
            | 
           133 | 
                               };
  | 
        
        
            | 
            | 
           134 | 
                               if (gpr_userid !== null) {
  | 
        
        
            | 
            | 
           135 | 
                                   params.gpr_userid = gpr_userid;
  | 
        
        
            | 
            | 
           136 | 
                               }
  | 
        
        
            | 
            | 
           137 | 
                               window.location = Url.relativeUrl(callingLink, params);
  | 
        
        
            | 
            | 
           138 | 
                           }
  | 
        
        
            | 
            | 
           139 | 
                           if (e.target.dataset.action === selectors.formItems.cancel) {
  | 
        
        
            | 
            | 
           140 | 
                               $(`.${selectors.targetButton}`).dropdown('toggle');
  | 
        
        
            | 
            | 
           141 | 
                           }
  | 
        
        
            | 
            | 
           142 | 
                       }
  | 
        
        
            | 
            | 
           143 | 
                   });
  | 
        
        
            | 
            | 
           144 | 
               });
  | 
        
        
            | 
            | 
           145 | 
           };
  | 
        
        
            | 
            | 
           146 | 
              | 
        
        
            | 
            | 
           147 | 
           /**
  | 
        
        
            | 
            | 
           148 | 
            * A small abstracted helper function which allows us to ensure we have up-to-date lists of nodes.
  | 
        
        
            | 
            | 
           149 | 
            *
  | 
        
        
            | 
            | 
           150 | 
            * @returns {{firstActive: HTMLElement, lastActive: HTMLElement, sifirst: ?String, silast: ?String}}
  | 
        
        
            | 
            | 
           151 | 
            */
  | 
        
        
            | 
            | 
           152 | 
           const onClickVariables = () => {
  | 
        
        
            | 
            | 
           153 | 
               // Ensure we have an up-to-date initials bar.
  | 
        
        
            | 
            | 
           154 | 
               const firstItems = [...document.querySelectorAll(`.${selectors.firstInitial} li`)];
  | 
        
        
            | 
            | 
           155 | 
               const lastItems = [...document.querySelectorAll(`.${selectors.lastInitial} li`)];
  | 
        
        
            | 
            | 
           156 | 
               const firstActive = firstItems.filter((item) => item.classList.contains(selectors.activeItem))[0];
  | 
        
        
            | 
            | 
           157 | 
               const lastActive = lastItems.filter((item) => item.classList.contains(selectors.activeItem))[0];
  | 
        
        
            | 
            | 
           158 | 
               // Ensure we retain both of the selections from a previous instance.
  | 
        
        
            | 
            | 
           159 | 
               let sifirst = firstActive.querySelector(selectors.pageClickableItem);
  | 
        
        
            | 
            | 
           160 | 
               let silast = lastActive.querySelector(selectors.pageClickableItem);
  | 
        
        
            | 
            | 
           161 | 
               return {firstActive, lastActive, sifirst, silast};
  | 
        
        
            | 
            | 
           162 | 
           };
  | 
        
        
            | 
            | 
           163 | 
              | 
        
        
            | 
            | 
           164 | 
           /**
  | 
        
        
            | 
            | 
           165 | 
            * Given we are provided the old li and current click event, swap around the active properties.
  | 
        
        
            | 
            | 
           166 | 
            *
  | 
        
        
            | 
            | 
           167 | 
            * @param {HTMLElement} itemToReset
  | 
        
        
            | 
            | 
           168 | 
            * @param {Event} e
  | 
        
        
            | 
            | 
           169 | 
            */
  | 
        
        
            | 
            | 
           170 | 
           const swapActiveItems = (itemToReset, e) => {
  | 
        
        
            | 
            | 
           171 | 
               itemToReset.classList.remove(selectors.activeItem);
  | 
        
        
            | 
            | 
           172 | 
               itemToReset.querySelector(selectors.pageClickableItem).ariaCurrent = false;
  | 
        
        
            | 
            | 
           173 | 
              | 
        
        
            | 
            | 
           174 | 
               // Set the select item as the current item.
  | 
        
        
            | 
            | 
           175 | 
               const itemToSetActive = e.target.parentElement;
  | 
        
        
            | 
            | 
           176 | 
               itemToSetActive.classList.add(selectors.activeItem);
  | 
        
        
            | 
            | 
           177 | 
               e.target.ariaCurrent = true;
  | 
        
        
            | 
            | 
           178 | 
           };
  |