1 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
17 * Participants filter managemnet.
19 * @module core_user/participants_filter
21 * @copyright 2020 Andrew Nicols <andrew@nicols.co.uk>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 import CourseFilter from './local/participantsfilter/filtertypes/courseid';
26 import * as DynamicTable from 'core_table/dynamic';
27 import GenericFilter from './local/participantsfilter/filter';
28 import Notification from 'core/notification';
29 import Selectors from './local/participantsfilter/selectors';
30 import Templates from 'core/templates';
33 * Initialise the participants filter on the element with the given id.
35 * @param {String} participantsRegionId
37 export const init = participantsRegionId => {
38 // Keep a reference to the filterset.
39 const filterSet = document.querySelector(`#${participantsRegionId}`);
41 // Keep a reference to all of the active filters.
42 const activeFilters = {
43 courseid: new CourseFilter('courseid', filterSet),
47 * Get the filter list region.
49 * @return {HTMLElement}
51 const getFilterRegion = () => filterSet.querySelector(Selectors.filterset.regions.filterlist);
54 * Add an unselected filter row.
58 const addFilterRow = () => {
59 return Templates.renderForPromise('core_user/local/participantsfilter/filterrow', {})
60 .then(({html, js}) => {
61 const newContentNodes = Templates.appendNodeContents(getFilterRegion(), html, js);
63 return newContentNodes;
66 // Note: This is a nasty hack.
67 // We should try to find a better way of doing this.
68 // We do not have the list of types in a readily consumable format, so we take the pre-rendered one and copy
70 const typeList = filterSet.querySelector(Selectors.data.typeList);
72 filterRow.forEach(contentNode => {
73 const contentTypeList = contentNode.querySelector(Selectors.filter.fields.type);
75 if (contentTypeList) {
76 contentTypeList.innerHTML = typeList.innerHTML;
83 updateFiltersOptions();
87 .catch(Notification.exception);
91 * Get the filter data source node fro the specified filter type.
93 * @param {String} filterType
94 * @return {HTMLElement}
96 const getFilterDataSource = filterType => {
97 const filterDataNode = filterSet.querySelector(Selectors.filterset.regions.datasource);
99 return filterDataNode.querySelector(Selectors.data.fields.byName(filterType));
103 * Add a filter to the list of active filters, performing any necessary setup.
105 * @param {HTMLElement} filterRow
106 * @param {String} filterType
108 const addFilter = async(filterRow, filterType) => {
109 // Name the filter on the filter row.
110 filterRow.dataset.filterType = filterType;
112 const filterDataNode = getFilterDataSource(filterType);
114 // Instantiate the Filter class.
115 let Filter = GenericFilter;
116 if (filterDataNode.dataset.filterTypeClass) {
117 Filter = await import(filterDataNode.dataset.filterTypeClass);
119 activeFilters[filterType] = new Filter(filterType, filterSet);
121 // Disable the select.
122 const typeField = filterRow.querySelector(Selectors.filter.fields.type);
123 typeField.disabled = 'disabled';
125 // Update the list of available filter types.
126 updateFiltersOptions();
130 * Get the registered filter class for the named filter.
132 * @param {String} name
133 * @return {Object} See the Filter class.
135 const getFilterObject = name => {
136 return activeFilters[name];
140 * Remove or replace the specified filter row and associated class, ensuring that if there is only one filter row,
141 * that it is replaced instead of being removed.
143 * @param {HTMLElement} filterRow
145 const removeOrReplaceFilterRow = filterRow => {
146 const filterCount = getFilterRegion().querySelectorAll(Selectors.filter.region).length;
148 if (filterCount === 1) {
149 replaceFilterRow(filterRow);
151 removeFilterRow(filterRow);
156 * Remove the specified filter row and associated class.
158 * @param {HTMLElement} filterRow
160 const removeFilterRow = filterRow => {
161 // Remove the filter object.
162 removeFilterObject(filterRow.dataset.filterType);
164 // Remove the actual filter HTML.
167 // Refresh the table.
168 updateTableFromFilter();
170 // Update the list of available filter types.
171 updateFiltersOptions();
175 * Replace the specified filter row with a new one.
177 * @param {HTMLElement} filterRow
180 const replaceFilterRow = filterRow => {
181 // Remove the filter object.
182 removeFilterObject(filterRow.dataset.filterType);
184 return Templates.renderForPromise('core_user/local/participantsfilter/filterrow', {})
185 .then(({html, js}) => {
186 const newContentNodes = Templates.replaceNode(filterRow, html, js);
188 return newContentNodes;
191 // Note: This is a nasty hack.
192 // We should try to find a better way of doing this.
193 // We do not have the list of types in a readily consumable format, so we take the pre-rendered one and copy
195 const typeList = filterSet.querySelector(Selectors.data.typeList);
197 filterRow.forEach(contentNode => {
198 const contentTypeList = contentNode.querySelector(Selectors.filter.fields.type);
200 if (contentTypeList) {
201 contentTypeList.innerHTML = typeList.innerHTML;
208 updateFiltersOptions();
213 // Refresh the table.
214 updateTableFromFilter();
218 .catch(Notification.exception);
222 * Remove the Filter Object from the register.
224 * @param {string} filterName The name of the filter to be removed
226 const removeFilterObject = filterName => {
228 const filter = getFilterObject(filterName);
232 // Remove from the list of active filters.
233 delete activeFilters[filterName];
239 * Remove all filters.
241 const removeAllFilters = async() => {
242 const filters = getFilterRegion().querySelectorAll(Selectors.filter.region);
243 filters.forEach((filterRow) => {
244 removeOrReplaceFilterRow(filterRow);
247 // Refresh the table.
248 updateTableFromFilter();
252 * Update the list of filter types to filter out those already selected.
254 const updateFiltersOptions = () => {
255 const filters = getFilterRegion().querySelectorAll(Selectors.filter.region);
256 filters.forEach(filterRow => {
257 const options = filterRow.querySelectorAll(Selectors.filter.fields.type + ' option');
258 options.forEach(option => {
259 if (option.value === filterRow.dataset.filterType) {
260 option.classList.remove('hidden');
261 option.disabled = false;
262 } else if (activeFilters[option.value]) {
263 option.classList.add('hidden');
264 option.disabled = true;
266 option.classList.remove('hidden');
267 option.disabled = false;
274 * Update the Dynamic table based upon the current filter.
278 const updateTableFromFilter = () => {
279 // TODO The main join type does not exist yet.
282 return DynamicTable.setFilters(
283 DynamicTable.getTableFromId(filterSet.dataset.tableRegion),
285 filters: Object.values(activeFilters).map(filter => filter.filterValue),
291 // Add listeners for the main actions.
292 filterSet.querySelector(Selectors.filterset.region).addEventListener('click', e => {
293 if (e.target.closest(Selectors.filterset.actions.addRow)) {
299 if (e.target.closest(Selectors.filterset.actions.applyFilters)) {
302 updateTableFromFilter();
305 if (e.target.closest(Selectors.filterset.actions.resetFilters)) {
312 // Add the listener to remove a single filter.
313 filterSet.querySelector(Selectors.filterset.regions.filterlist).addEventListener('click', e => {
314 if (e.target.closest(Selectors.filter.actions.remove)) {
317 removeOrReplaceFilterRow(e.target.closest(Selectors.filter.region));
321 // Add listeners for the filter type selection.
322 filterSet.querySelector(Selectors.filterset.regions.filterlist).addEventListener('change', e => {
323 const typeField = e.target.closest(Selectors.filter.fields.type);
324 if (typeField && typeField.value) {
325 const filter = e.target.closest(Selectors.filter.region);
327 addFilter(filter, typeField.value);