MDL-66792 tool_dataprivacy: correct fa classes in privacy registry.
[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
aa5e1501
PH
31 /*
32 * Class names to apply when expanding/collapsing nodes.
33 */
34 var CLASSES = {
35 EXPAND: 'fa-caret-right',
36 COLLAPSE: 'fa-caret-down'
37 };
38
c2aad218
AG
39 return /** @alias module:tool_dataprivacy/expand-collapse */ {
40 /**
41 * Expand or collapse a selected node.
42 *
43 * @param {object} targetnode The node that we want to expand / collapse
44 * @param {object} thisnode The node that was clicked.
c2aad218
AG
45 */
46 expandCollapse: function(targetnode, thisnode) {
47 if (targetnode.hasClass('hide')) {
48 targetnode.removeClass('hide');
49 targetnode.addClass('visible');
50 targetnode.attr('aria-expanded', true);
aa5e1501
PH
51 thisnode.find(':header i.fa').removeClass(CLASSES.EXPAND);
52 thisnode.find(':header i.fa').addClass(CLASSES.COLLAPSE);
c2aad218
AG
53 thisnode.find(':header img.icon').attr('src', expandedImage.attr('src'));
54 } else {
55 targetnode.removeClass('visible');
56 targetnode.addClass('hide');
57 targetnode.attr('aria-expanded', false);
aa5e1501
PH
58 thisnode.find(':header i.fa').removeClass(CLASSES.COLLAPSE);
59 thisnode.find(':header i.fa').addClass(CLASSES.EXPAND);
c2aad218
AG
60 thisnode.find(':header img.icon').attr('src', collapsedImage.attr('src'));
61 }
62 },
63
64 /**
65 * Expand or collapse all nodes on this page.
66 *
67 * @param {string} nextstate The next state to change to.
c2aad218
AG
68 */
69 expandCollapseAll: function(nextstate) {
70 var currentstate = (nextstate == 'visible') ? 'hide' : 'visible';
71 var ariaexpandedstate = (nextstate == 'visible') ? true : false;
aa5e1501
PH
72 var iconclassnow = (nextstate == 'visible') ? CLASSES.EXPAND : CLASSES.COLLAPSE;
73 var iconclassnext = (nextstate == 'visible') ? CLASSES.COLLAPSE : CLASSES.EXPAND;
c2aad218
AG
74 var imagenow = (nextstate == 'visible') ? expandedImage.attr('src') : collapsedImage.attr('src');
75 $('.' + currentstate).each(function() {
76 $(this).removeClass(currentstate);
77 $(this).addClass(nextstate);
78 $(this).attr('aria-expanded', ariaexpandedstate);
79 });
80 $('.tool_dataprivacy-expand-all').data('visibilityState', currentstate);
81
82 str.get_string(currentstate, 'tool_dataprivacy').then(function(langString) {
83 $('.tool_dataprivacy-expand-all').html(langString);
dd26f1be 84 return;
c2aad218
AG
85 }).catch(Notification.exception);
86
87 $(':header i.fa').each(function() {
88 $(this).removeClass(iconclassnow);
89 $(this).addClass(iconclassnext);
90 });
91 $(':header img.icon').each(function() {
92 $(this).attr('src', imagenow);
93 });
94 }
95 };
bb4030ff 96});