blocks/online_users/amd/src/change_user_visibility.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. * A javascript module that handles the change of the user's visibility in the
  17. * online users block.
  18. *
  19. * @module block_online_users/change_user_visibility
  20. * @copyright 2018 Mihail Geshoski <mihail@moodle.com>
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22. */
  23. define(['jquery', 'core/ajax', 'core/str', 'core/notification'],
  24. function($, Ajax, Str, Notification) {
  25. /**
  26. * Selectors.
  27. *
  28. * @access private
  29. * @type {Object}
  30. */
  31. var SELECTORS = {
  32. CHANGE_VISIBILITY_LINK: '#change-user-visibility',
  33. CHANGE_VISIBILITY_ICON: '#change-user-visibility .icon'
  34. };
  35. /**
  36. * Change user visibility in the online users block.
  37. *
  38. * @method changeVisibility
  39. * @param {String} action
  40. * @param {String} userid
  41. * @private
  42. */
  43. var changeVisibility = function(action, userid) {
  44. var value = action == "show" ? 1 : 0;
  45. var preferences = [{
  46. 'name': 'block_online_users_uservisibility',
  47. 'value': value,
  48. 'userid': userid
  49. }];
  50. var request = {
  51. methodname: 'core_user_set_user_preferences',
  52. args: {
  53. preferences: preferences
  54. }
  55. };
  56. Ajax.call([request])[0].then(function(data) {
  57. if (data.saved) {
  58. var newAction = oppositeAction(action);
  59. changeVisibilityLinkAttr(newAction);
  60. changeVisibilityIconAttr(newAction);
  61. }
  62. return;
  63. }).catch(Notification.exception);
  64. };
  65. /**
  66. * Get the opposite action.
  67. *
  68. * @method oppositeAction
  69. * @param {String} action
  70. * @return {String}
  71. * @private
  72. */
  73. var oppositeAction = function(action) {
  74. return action == 'show' ? 'hide' : 'show';
  75. };
  76. /**
  77. * Change the attribute values of the user visibility link in the online users block.
  78. *
  79. * @method changeVisibilityLinkAttr
  80. * @param {String} action
  81. * @private
  82. */
  83. var changeVisibilityLinkAttr = function(action) {
  84. getTitle(action).then(function(title) {
  85. $(SELECTORS.CHANGE_VISIBILITY_LINK).attr({
  86. 'data-action': action,
  87. 'title': title
  88. });
  89. return;
  90. }).catch(Notification.exception);
  91. };
  92. /**
  93. * Change the attribute values of the user visibility icon in the online users block.
  94. *
  95. * @method changeVisibilityIconAttr
  96. * @param {String} action
  97. * @private
  98. */
  99. var changeVisibilityIconAttr = function(action) {
  100. var icon = $(SELECTORS.CHANGE_VISIBILITY_ICON);
  101. getTitle(action).then(function(title) {
  102. // Add the proper title to the icon.
  103. $(icon).attr({
  104. 'title': title,
  105. 'aria-label': title
  106. });
  107. // If the icon is an image.
  108. if (icon.is("img")) {
  109. $(icon).attr({
  110. 'src': M.util.image_url('t/' + action),
  111. 'alt': title
  112. });
  113. } else {
  114. // Add the new icon class and remove the old one.
  115. $(icon).addClass(getIconClass(action));
  116. $(icon).removeClass(getIconClass(oppositeAction(action)));
  117. }
  118. return;
  119. }).catch(Notification.exception);
  120. };
  121. /**
  122. * Get the proper class for the user visibility icon in the online users block.
  123. *
  124. * @method getIconClass
  125. * @param {String} action
  126. * @return {String}
  127. * @private
  128. */
  129. var getIconClass = function(action) {
  130. return action == 'show' ? 'fa-eye-slash' : 'fa-eye';
  131. };
  132. /**
  133. * Get the title description of the user visibility link in the online users block.
  134. *
  135. * @method getTitle
  136. * @param {String} action
  137. * @return {object} jQuery promise
  138. * @private
  139. */
  140. var getTitle = function(action) {
  141. return Str.get_string('online_status:' + action, 'block_online_users');
  142. };
  143. return {
  144. // Public variables and functions.
  145. /**
  146. * Initialise change user visibility function.
  147. *
  148. * @method init
  149. */
  150. init: function() {
  151. $(SELECTORS.CHANGE_VISIBILITY_LINK).on('click', function(e) {
  152. e.preventDefault();
  153. var action = ($(this).attr('data-action'));
  154. var userid = ($(this).attr('data-userid'));
  155. changeVisibility(action, userid);
  156. });
  157. }
  158. };
  159. });