MDL-66373 calendar: Added datetimes to timestamps conversion web service
[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';
ce8a2d11
JP
27import CustomEvents from 'core/custom_interaction_events';
28import Selectors from 'forumreport_summary/selectors';
b29de56d
MH
29
30export const init = (root) => {
ce8a2d11 31 let jqRoot = $(root);
b29de56d
MH
32
33 // Hide loading spinner and show report once page is ready.
34 // This ensures filters can be applied when sorting by columns.
35 $(document).ready(function() {
36 $('.loading-icon').hide();
37 $('#summaryreport').removeClass('hidden');
38 });
39
40 // Generic filter handlers.
41
b29de56d
MH
42 // Called to override click event to trigger a proper generate request with filtering.
43 var generateWithFilters = (event) => {
44 var newLink = $('#filtersform').attr('action');
45
46 if (event) {
47 event.preventDefault();
48
49 let filterParams = event.target.search.substr(1);
50 newLink += '&' + filterParams;
51 }
52
53 $('#filtersform').attr('action', newLink);
54 $('#filtersform').submit();
55 };
56
57 // Override 'reset table preferences' so it generates with filters.
58 $('.resettable').on("click", "a", function(event) {
59 generateWithFilters(event);
60 });
61
62 // Override table heading sort links so they generate with filters.
63 $('thead').on("click", "a", function(event) {
64 generateWithFilters(event);
65 });
66
67 // Override pagination page links so they generate with filters.
68 $('.pagination').on("click", "a", function(event) {
69 generateWithFilters(event);
70 });
71
cc15134c
MH
72 // Submit report via filter
73 var submitWithFilter = (containerelement) => {
74 // Close the container (eg popover).
75 $(containerelement).addClass('hidden');
76
77 // Submit the filter values and re-generate report.
78 generateWithFilters(false);
79 };
80
b29de56d
MH
81 // Groups filter specific handlers.
82
cc15134c 83 // Event handler for clicking select all groups.
ce8a2d11
JP
84 jqRoot.on(CustomEvents.events.activate, Selectors.filters.group.selectall, function() {
85 let deselected = root.querySelectorAll(Selectors.filters.group.checkbox + ':not(:checked)');
86 deselected.forEach(function(checkbox) {
87 checkbox.checked = true;
88 });
89 });
90
91 // Event handler for clearing filter by clicking option.
92 jqRoot.on(CustomEvents.events.activate, Selectors.filters.group.clear, function() {
93 // Clear checkboxes.
94 let selected = root.querySelectorAll(Selectors.filters.group.checkbox + ':checked');
95 selected.forEach(function(checkbox) {
96 checkbox.checked = false;
97 });
b29de56d
MH
98 });
99
100 // Event handler for showing groups filter popover.
ce8a2d11 101 jqRoot.on(CustomEvents.events.activate, Selectors.filters.group.trigger, function() {
b29de56d 102 // Create popover.
ce8a2d11
JP
103 var referenceElement = root.querySelector(Selectors.filters.group.trigger),
104 popperContent = root.querySelector(Selectors.filters.group.popover);
b29de56d
MH
105
106 new Popper(referenceElement, popperContent, {placement: 'bottom'});
107
ce8a2d11
JP
108 // Show popover.
109 popperContent.classList.remove('hidden');
110
111 // Change to outlined button.
112 referenceElement.classList.add('btn-outline-primary');
113 referenceElement.classList.remove('btn-primary');
114
115 // Let screen readers know that it's now expanded.
116 referenceElement.setAttribute('aria-expanded', true);
b29de56d
MH
117 });
118
cc15134c 119 // Event handler to click save groups filter.
ce8a2d11 120 jqRoot.on(CustomEvents.events.activate, Selectors.filters.group.save, function() {
cc15134c
MH
121 submitWithFilter('#filter-groups-popover');
122 });
b29de56d 123};