admin/tool/dataprivacy/amd/src/expand_contract.js

  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. * Potential user selector module.
  17. *
  18. * @module tool_dataprivacy/expand_contract
  19. * @copyright 2018 Adrian Greeve
  20. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  21. */
  22. define(['jquery', 'core/url', 'core/str', 'core/notification'], function($, url, str, Notification) {
  23. var expandedImage = $('<img alt="" src="' + url.imageUrl('t/expanded') + '"/>');
  24. var collapsedImage = $('<img alt="" src="' + url.imageUrl('t/collapsed') + '"/>');
  25. /*
  26. * Class names to apply when expanding/collapsing nodes.
  27. */
  28. var CLASSES = {
  29. EXPAND: 'fa-caret-right',
  30. COLLAPSE: 'fa-caret-down'
  31. };
  32. return /** @alias module:tool_dataprivacy/expand-collapse */ {
  33. /**
  34. * Expand or collapse a selected node.
  35. *
  36. * @param {object} targetnode The node that we want to expand / collapse
  37. * @param {object} thisnode The node that was clicked.
  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(CLASSES.EXPAND);
  45. thisnode.find(':header i.fa').addClass(CLASSES.COLLAPSE);
  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(CLASSES.COLLAPSE);
  52. thisnode.find(':header i.fa').addClass(CLASSES.EXPAND);
  53. thisnode.find(':header img.icon').attr('src', collapsedImage.attr('src'));
  54. }
  55. },
  56. /**
  57. * Expand or collapse all nodes on this page.
  58. *
  59. * @param {string} nextstate The next state to change to.
  60. */
  61. expandCollapseAll: function(nextstate) {
  62. var currentstate = (nextstate == 'visible') ? 'hide' : 'visible';
  63. var ariaexpandedstate = (nextstate == 'visible') ? true : false;
  64. var iconclassnow = (nextstate == 'visible') ? CLASSES.EXPAND : CLASSES.COLLAPSE;
  65. var iconclassnext = (nextstate == 'visible') ? CLASSES.COLLAPSE : CLASSES.EXPAND;
  66. var imagenow = (nextstate == 'visible') ? expandedImage.attr('src') : collapsedImage.attr('src');
  67. $('.' + currentstate).each(function() {
  68. $(this).removeClass(currentstate);
  69. $(this).addClass(nextstate);
  70. $(this).attr('aria-expanded', ariaexpandedstate);
  71. });
  72. $('.tool_dataprivacy-expand-all').data('visibilityState', currentstate);
  73. str.get_string(currentstate, 'tool_dataprivacy').then(function(langString) {
  74. $('.tool_dataprivacy-expand-all').html(langString);
  75. return;
  76. }).catch(Notification.exception);
  77. $(':header i.fa').each(function() {
  78. $(this).removeClass(iconclassnow);
  79. $(this).addClass(iconclassnext);
  80. });
  81. $(':header img.icon').each(function() {
  82. $(this).attr('src', imagenow);
  83. });
  84. }
  85. };
  86. });