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