MDL-66076 forumreport_summary: Minor cleanup of report code
[moodle.git] / mod / forum / report / summary / amd / src / filters.js
CommitLineData
b29de56d
MH
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 * Module responsible for handling forum summary report filters.
18 *
19 * @module forumreport_summary/filters
20 * @package forumreport_summary
21 * @copyright 2019 Michael Hawkins <michaelh@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25import $ from 'jquery';
26import Popper from 'core/popper';
27
28export const init = (root) => {
29 root = $(root);
30
31 // Hide loading spinner and show report once page is ready.
32 // This ensures filters can be applied when sorting by columns.
33 $(document).ready(function() {
34 $('.loading-icon').hide();
35 $('#summaryreport').removeClass('hidden');
36 });
37
38 // Generic filter handlers.
39
40 // Event handler to clear filters.
41 $(root).on("click", ".filter-clear", function(event) {
42 // Clear checkboxes.
43 let selected = event.target.parentNode.parentNode.parentElement.querySelectorAll('input[type="checkbox"]:checked');
44
45 selected.forEach(function(checkbox) {
46 checkbox.checked = false;
47 });
48 });
49
50 // Called to override click event to trigger a proper generate request with filtering.
51 var generateWithFilters = (event) => {
52 var newLink = $('#filtersform').attr('action');
53
54 if (event) {
55 event.preventDefault();
56
57 let filterParams = event.target.search.substr(1);
58 newLink += '&' + filterParams;
59 }
60
61 $('#filtersform').attr('action', newLink);
62 $('#filtersform').submit();
63 };
64
65 // Override 'reset table preferences' so it generates with filters.
66 $('.resettable').on("click", "a", function(event) {
67 generateWithFilters(event);
68 });
69
70 // Override table heading sort links so they generate with filters.
71 $('thead').on("click", "a", function(event) {
72 generateWithFilters(event);
73 });
74
75 // Override pagination page links so they generate with filters.
76 $('.pagination').on("click", "a", function(event) {
77 generateWithFilters(event);
78 });
79
80 // Select all checkboxes within a filter section.
81 var selectAll = (checkboxdiv) => {
82 let targetdiv = document.getElementById(checkboxdiv);
83 let deselected = targetdiv.querySelectorAll('input[type="checkbox"]:not(:checked)');
84
85 deselected.forEach(function(checkbox) {
86 checkbox.checked = true;
87 });
88 };
89
90 // Groups filter specific handlers.
91
92 // Event to handle select all groups.
93 $('#filter-groups-popover .select-all').on('click', function() {
94 selectAll('filter-groups-popover');
95 });
96
97 // Event handler for showing groups filter popover.
98 $('#filter-groups-button').on('click', function() {
99 // Create popover.
100 var referenceElement = document.querySelector('#filter-groups-button'),
101 popperContent = document.querySelector('#filter-groups-popover');
102
103 new Popper(referenceElement, popperContent, {placement: 'bottom'});
104
105 // Show popover.
106 $('#filter-groups-popover').removeClass('hidden');
107 });
108
109 // Event handler to save groups filter.
110 $(root).on("click", "#filter-groups-popover .filter-save", function() {
111 // Close the popover.
112 $('#filter-groups-popover').addClass('hidden');
113
114 // Submit the filter values and re-generate report.
115 generateWithFilters(false);
116 });
117};