MDL-61899 tool_dataprivacy: Addition of js file and misc. updates.
[moodle.git] / admin / tool / dataprivacy / amd / src / expand_contract.js
CommitLineData
c2aad218
AG
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 * Potential user selector module.
18 *
19 * @module tool_dataprivacy/expand_contract
20 * @class page-expand-contract
21 * @package tool_dataprivacy
22 * @copyright 2018 Adrian Greeve
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26define(['jquery', 'core/url', 'core/str'], function($, url, str) {
27
28 var expandedImage = $('<img alt="" src="' + url.imageUrl('t/expanded') + '"/>');
29 var collapsedImage = $('<img alt="" src="' + url.imageUrl('t/collapsed') + '"/>');
30
31 return /** @alias module:tool_dataprivacy/expand-collapse */ {
32 /**
33 * Expand or collapse a selected node.
34 *
35 * @param {object} targetnode The node that we want to expand / collapse
36 * @param {object} thisnode The node that was clicked.
37 * @return {null}
38 */
39 expandCollapse: function(targetnode, thisnode) {
40 if (targetnode.hasClass('hide')) {
41 targetnode.removeClass('hide');
42 targetnode.addClass('visible');
43 targetnode.attr('aria-expanded', true);
44 thisnode.find(':header i.fa').removeClass('fa-plus-square');
45 thisnode.find(':header i.fa').addClass('fa-minus-square');
46 thisnode.find(':header img.icon').attr('src', expandedImage.attr('src'));
47 } else {
48 targetnode.removeClass('visible');
49 targetnode.addClass('hide');
50 targetnode.attr('aria-expanded', false);
51 thisnode.find(':header i.fa').removeClass('fa-minus-square');
52 thisnode.find(':header i.fa').addClass('fa-plus-square');
53 thisnode.find(':header img.icon').attr('src', collapsedImage.attr('src'));
54 }
55 },
56
57 /**
58 * Expand or collapse all nodes on this page.
59 *
60 * @param {string} nextstate The next state to change to.
61 * @return {null}
62 */
63 expandCollapseAll: function(nextstate) {
64 var currentstate = (nextstate == 'visible') ? 'hide' : 'visible';
65 var ariaexpandedstate = (nextstate == 'visible') ? true : false;
66 var iconclassnow = (nextstate == 'visible') ? 'fa-plus-square' : 'fa-minus-square';
67 var iconclassnext = (nextstate == 'visible') ? 'fa-minus-square' : 'fa-plus-square';
68 var imagenow = (nextstate == 'visible') ? expandedImage.attr('src') : collapsedImage.attr('src');
69 $('.' + currentstate).each(function() {
70 $(this).removeClass(currentstate);
71 $(this).addClass(nextstate);
72 $(this).attr('aria-expanded', ariaexpandedstate);
73 });
74 $('.tool_dataprivacy-expand-all').data('visibilityState', currentstate);
75
76 str.get_string(currentstate, 'tool_dataprivacy').then(function(langString) {
77 $('.tool_dataprivacy-expand-all').html(langString);
78 }).catch(Notification.exception);
79
80 $(':header i.fa').each(function() {
81 $(this).removeClass(iconclassnow);
82 $(this).addClass(iconclassnext);
83 });
84 $(':header img.icon').each(function() {
85 $(this).attr('src', imagenow);
86 });
87 }
88 };
89});