Proyectos de Subversion Moodle

Rev

| 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
 * Allow the user to search for grades within the grade area.
18
 *
19
 * @module    core_grades/comboboxsearch/grade
20
 * @copyright 2023 Mathew May <mathew.solutions>
21
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22
 */
23
import search_combobox from 'core/comboboxsearch/search_combobox';
24
import * as Repository from 'core_grades/searchwidget/repository';
25
import {renderForPromise, replaceNodeContents} from 'core/templates';
26
import {debounce} from 'core/utils';
27
 
28
export default class GradeItemSearch extends search_combobox {
29
 
30
    courseID;
31
 
32
    constructor() {
33
        super();
34
 
35
        // Define our standard lookups.
36
        this.selectors = {
37
            ...this.selectors,
38
            courseid: '[data-region="courseid"]',
39
            placeholder: '.gradesearchdropdown [data-region="searchplaceholder"]',
40
        };
41
        const component = document.querySelector(this.componentSelector());
42
        this.courseID = component.querySelector(this.selectors.courseid).dataset.courseid;
43
        this.instance = this.component.querySelector(this.selectors.instance).dataset.instance;
44
 
45
        const searchValueElement = this.component.querySelector(`#${this.searchInput.dataset.inputElement}`);
46
        searchValueElement.addEventListener('change', () => {
47
            this.toggleDropdown(); // Otherwise the dropdown stays open when user choose an option using keyboard.
48
 
49
            const valueElement = this.component.querySelector(`#${this.combobox.dataset.inputElement}`);
50
            if (valueElement.value !== searchValueElement.value) {
51
                valueElement.value = searchValueElement.value;
52
                valueElement.dispatchEvent(new Event('change', {bubbles: true}));
53
            }
54
 
55
            searchValueElement.value = '';
56
        });
57
 
58
        this.$component.on('hide.bs.dropdown', () => {
59
            this.searchInput.removeAttribute('aria-activedescendant');
60
 
61
            const listbox = document.querySelector(`#${this.searchInput.getAttribute('aria-controls')}[role="listbox"]`);
62
            listbox.querySelectorAll('.active[role="option"]').forEach(option => {
63
                option.classList.remove('active');
64
            });
65
            listbox.scrollTop = 0;
66
 
67
            // Use setTimeout to make sure the following code is executed after the click event is handled.
68
            setTimeout(() => {
69
                if (this.searchInput.value !== '') {
70
                    this.searchInput.value = '';
71
                    this.searchInput.dispatchEvent(new Event('input', {bubbles: true}));
72
                }
73
            });
74
        });
75
 
76
        this.renderDefault();
77
    }
78
 
79
    static init() {
80
        return new GradeItemSearch();
81
    }
82
 
83
    /**
84
     * The overall div that contains the searching widget.
85
     *
86
     * @returns {string}
87
     */
88
    componentSelector() {
89
        return '.grade-search';
90
    }
91
 
92
    /**
93
     * The dropdown div that contains the searching widget result space.
94
     *
95
     * @returns {string}
96
     */
97
    dropdownSelector() {
98
        return '.gradesearchdropdown';
99
    }
100
 
101
    /**
102
     * Build the content then replace the node.
103
     */
104
    async renderDropdown() {
105
        const {html, js} = await renderForPromise('core/local/comboboxsearch/resultset', {
106
            instance: this.instance,
107
            results: this.getMatchedResults(),
108
            hasresults: this.getMatchedResults().length > 0,
109
            searchterm: this.getSearchTerm(),
110
        });
111
        replaceNodeContents(this.selectors.placeholder, html, js);
112
        // Remove aria-activedescendant when the available options change.
113
        this.searchInput.removeAttribute('aria-activedescendant');
114
    }
115
 
116
    /**
117
     * Build the content then replace the node by default we want our form to exist.
118
     */
119
    async renderDefault() {
120
        this.setMatchedResults(await this.filterDataset(await this.getDataset()));
121
        this.filterMatchDataset();
122
 
123
        await this.renderDropdown();
124
 
125
        this.updateNodes();
126
        this.registerInputEvents();
127
    }
128
 
129
    /**
130
     * Get the data we will be searching against in this component.
131
     *
132
     * @returns {Promise<*>}
133
     */
134
    async fetchDataset() {
135
        return await Repository.gradeitemFetch(this.courseID).then((r) => r.gradeitems);
136
    }
137
 
138
    /**
139
     * Dictate to the search component how and what we want to match upon.
140
     *
141
     * @param {Array} filterableData
142
     * @returns {Array} The users that match the given criteria.
143
     */
144
    async filterDataset(filterableData) {
145
        // Sometimes we just want to show everything.
146
        if (this.getPreppedSearchTerm() === '') {
147
            return filterableData;
148
        }
149
        return filterableData.filter((grade) => Object.keys(grade).some((key) => {
150
            if (grade[key] === "") {
151
                return false;
152
            }
153
            return grade[key].toString().toLowerCase().includes(this.getPreppedSearchTerm());
154
        }));
155
    }
156
 
157
    /**
158
     * Given we have a subset of the dataset, set the field that we matched upon to inform the end user.
159
     */
160
    filterMatchDataset() {
161
        this.setMatchedResults(
162
            this.getMatchedResults().map((grade) => {
163
                return {
164
                    id: grade.id,
165
                    name: grade.name,
166
                };
167
            })
168
        );
169
    }
170
 
171
    /**
172
     * Handle any keyboard inputs.
173
     */
174
    registerInputEvents() {
175
        // Register & handle the text input.
176
        this.searchInput.addEventListener('input', debounce(async() => {
177
            this.setSearchTerms(this.searchInput.value);
178
            // We can also require a set amount of input before search.
179
            if (this.searchInput.value === '') {
180
                // Hide the "clear" search button in the search bar.
181
                this.clearSearchButton.classList.add('d-none');
182
            } else {
183
                // Display the "clear" search button in the search bar.
184
                this.clearSearchButton.classList.remove('d-none');
185
            }
186
            // User has given something for us to filter against.
187
            await this.filterrenderpipe();
188
        }, 300));
189
    }
190
 
191
    /**
192
     * The handler for when a user interacts with the component.
193
     *
194
     * @param {MouseEvent} e The triggering event that we are working with.
195
     */
196
    async clickHandler(e) {
197
        if (e.target.closest(this.selectors.clearSearch)) {
198
            e.stopPropagation();
199
            // Clear the entered search query in the search bar.
200
            this.searchInput.value = '';
201
            this.setSearchTerms(this.searchInput.value);
202
            this.searchInput.focus();
203
            this.clearSearchButton.classList.add('d-none');
204
            // Display results.
205
            await this.filterrenderpipe();
206
        }
207
    }
208
 
209
    /**
210
     * The handler for when a user changes the value of the component (selects an option from the dropdown).
211
     *
212
     * @param {Event} e The change event.
213
     */
214
    changeHandler(e) {
215
        window.location = this.selectOneLink(e.target.value);
216
    }
217
 
218
    /**
219
     * Override the input event listener for the text input area.
220
     */
221
    registerInputHandlers() {
222
        // Register & handle the text input.
223
        this.searchInput.addEventListener('input', debounce(() => {
224
            this.setSearchTerms(this.searchInput.value);
225
            // We can also require a set amount of input before search.
226
            if (this.getSearchTerm() === '') {
227
                // Hide the "clear" search button in the search bar.
228
                this.clearSearchButton.classList.add('d-none');
229
            } else {
230
                // Display the "clear" search button in the search bar.
231
                this.clearSearchButton.classList.remove('d-none');
232
            }
233
        }, 300));
234
    }
235
 
236
    /**
237
     * Build up the view all link that is dedicated to a particular result.
238
     * We will call this function when a user interacts with the combobox to redirect them to show their results in the page.
239
     *
240
     * @param {Number} gradeID The ID of the grade item selected.
241
     */
242
    selectOneLink(gradeID) {
243
        throw new Error(`selectOneLink(${gradeID}) must be implemented in ${this.constructor.name}`);
244
    }
245
}