blocks/myoverview/amd/src/view_nav.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. * Manage the timeline view navigation for the overview block.
  17. *
  18. * @copyright 2018 Bas Brands <bas@moodle.com>
  19. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  20. */
  21. import $ from 'jquery';
  22. import * as CustomEvents from 'core/custom_interaction_events';
  23. import Notification from 'core/notification';
  24. import {setUserPreference} from 'core_user/repository';
  25. import * as View from 'block_myoverview/view';
  26. import SELECTORS from 'block_myoverview/selectors';
  27. /**
  28. * Update the user preference for the block.
  29. *
  30. * @param {String} filter The type of filter: display/sort/grouping.
  31. * @param {String} value The current preferred value.
  32. * @return {Promise}
  33. */
  34. const updatePreferences = (filter, value) => {
  35. let type = null;
  36. if (filter === 'display') {
  37. type = 'block_myoverview_user_view_preference';
  38. } else if (filter === 'sort') {
  39. type = 'block_myoverview_user_sort_preference';
  40. } else if (filter === 'customfieldvalue') {
  41. type = 'block_myoverview_user_grouping_customfieldvalue_preference';
  42. } else {
  43. type = 'block_myoverview_user_grouping_preference';
  44. }
  45. return setUserPreference(type, value)
  46. .catch(Notification.exception);
  47. };
  48. /**
  49. * Event listener for the Display filter (cards, list).
  50. *
  51. * @param {object} root The root element for the overview block
  52. */
  53. const registerSelector = root => {
  54. const Selector = root.find(SELECTORS.FILTERS);
  55. CustomEvents.define(Selector, [CustomEvents.events.activate]);
  56. Selector.on(
  57. CustomEvents.events.activate,
  58. SELECTORS.FILTER_OPTION,
  59. (e, data) => {
  60. const option = $(e.target);
  61. if (option.hasClass('active')) {
  62. // If it's already active then we don't need to do anything.
  63. return;
  64. }
  65. const filter = option.attr('data-filter');
  66. const pref = option.attr('data-pref');
  67. const customfieldvalue = option.attr('data-customfieldvalue');
  68. root.find(SELECTORS.courseView.region).attr('data-' + filter, option.attr('data-value'));
  69. updatePreferences(filter, pref);
  70. if (customfieldvalue) {
  71. root.find(SELECTORS.courseView.region).attr('data-customfieldvalue', customfieldvalue);
  72. updatePreferences('customfieldvalue', customfieldvalue);
  73. }
  74. // Reset the views.
  75. // Check if the user is currently in a searching state, if so we'll reset it.
  76. const page = document.querySelector(SELECTORS.region.selectBlock);
  77. const input = page.querySelector(SELECTORS.region.searchInput);
  78. if (input.value !== '') {
  79. const clearIcon = page.querySelector(SELECTORS.region.clearIcon);
  80. input.value = '';
  81. // Triggers the init so wont need to call it again.
  82. View.clearSearch(clearIcon, root);
  83. } else {
  84. View.init(root);
  85. }
  86. data.originalEvent.preventDefault();
  87. }
  88. );
  89. Selector.on(
  90. CustomEvents.events.activate,
  91. SELECTORS.DISPLAY_OPTION,
  92. (e, data) => {
  93. const option = $(e.target);
  94. if (option.hasClass('active')) {
  95. return;
  96. }
  97. const filter = option.attr('data-display-option');
  98. const pref = option.attr('data-pref');
  99. root.find(SELECTORS.courseView.region).attr('data-display', option.attr('data-value'));
  100. updatePreferences(filter, pref);
  101. View.reset(root);
  102. data.originalEvent.preventDefault();
  103. }
  104. );
  105. };
  106. /**
  107. * Initialise the timeline view navigation by adding event listeners to
  108. * the navigation elements.
  109. *
  110. * @param {object} root The root element for the myoverview block
  111. */
  112. export const init = root => {
  113. root = $(root);
  114. registerSelector(root);
  115. };