admin/tool/dataprivacy/amd/src/request_filter.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. * JS module for the data requests filter.
  17. *
  18. * @module tool_dataprivacy/request_filter
  19. * @copyright 2018 Jun Pataleta
  20. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  21. */
  22. define(['jquery', 'core/form-autocomplete', 'core/str', 'core/notification'], function($, Autocomplete, Str, Notification) {
  23. /**
  24. * Selectors.
  25. *
  26. * @access private
  27. * @type {{REQUEST_FILTERS: string}}
  28. */
  29. var SELECTORS = {
  30. REQUEST_FILTERS: '#request-filters'
  31. };
  32. /**
  33. * Init function.
  34. *
  35. * @method init
  36. * @private
  37. */
  38. var init = function() {
  39. var stringkeys = [
  40. {
  41. key: 'filter',
  42. component: 'moodle'
  43. },
  44. {
  45. key: 'nofiltersapplied',
  46. component: 'moodle'
  47. }
  48. ];
  49. Str.get_strings(stringkeys).then(function(langstrings) {
  50. var placeholder = langstrings[0];
  51. var noSelectionString = langstrings[1];
  52. return Autocomplete.enhance(SELECTORS.REQUEST_FILTERS, false, '', placeholder, false, true, noSelectionString, true);
  53. }).fail(Notification.exception);
  54. var last = $(SELECTORS.REQUEST_FILTERS).val();
  55. $(SELECTORS.REQUEST_FILTERS).on('change', function() {
  56. var current = $(this).val();
  57. // Prevent form from submitting unnecessarily, eg. on blur when no filter is selected.
  58. if (last.join(',') !== current.join(',')) {
  59. // If we're submitting without filters, set the hidden input 'filters-cleared' to 1.
  60. if (current.length === 0) {
  61. $('#filters-cleared').val(1);
  62. }
  63. $(this.form).submit();
  64. }
  65. });
  66. };
  67. return /** @alias module:core/form-autocomplete */ {
  68. /**
  69. * Initialise the unified user filter.
  70. *
  71. * @method init
  72. */
  73. init: function() {
  74. init();
  75. }
  76. };
  77. });