Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

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