From dea64fa2661fe79d656c2bae96e44afb473f2413 Mon Sep 17 00:00:00 2001 From: Andrew Nicols Date: Mon, 22 May 2023 16:02:37 +0800 Subject: [PATCH] MDL-62859 block_online_users: Rewrite visibility changer as ESM --- .../amd/build/change_user_visibility.min.js | 16 +- .../build/change_user_visibility.min.js.map | 2 +- .../amd/src/change_user_visibility.js | 250 +++++++++--------- 3 files changed, 131 insertions(+), 137 deletions(-) diff --git a/blocks/online_users/amd/build/change_user_visibility.min.js b/blocks/online_users/amd/build/change_user_visibility.min.js index 88459892fb9..ebb6923be12 100644 --- a/blocks/online_users/amd/build/change_user_visibility.min.js +++ b/blocks/online_users/amd/build/change_user_visibility.min.js @@ -1,11 +1,11 @@ +define("block_online_users/change_user_visibility",["exports","core/str","core/notification","core_user/repository"],(function(_exports,_str,_notification,_repository){var obj; /** - * A javascript module that handles the change of the user's visibility in the - * online users block. - * - * @module block_online_users/change_user_visibility - * @copyright 2018 Mihail Geshoski - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ -define("block_online_users/change_user_visibility",["jquery","core/str","core/notification","core_user/repository"],(function($,Str,Notification,UserRepository){var SELECTORS_CHANGE_VISIBILITY_LINK="#change-user-visibility",SELECTORS_CHANGE_VISIBILITY_ICON="#change-user-visibility .icon",oppositeAction=function(action){return"show"==action?"hide":"show"},changeVisibilityLinkAttr=function(action){getTitle(action).then((function(title){$(SELECTORS_CHANGE_VISIBILITY_LINK).attr({"data-action":action,title:title})})).catch(Notification.exception)},changeVisibilityIconAttr=function(action){var icon=$(SELECTORS_CHANGE_VISIBILITY_ICON);getTitle(action).then((function(title){$(icon).attr({title:title,"aria-label":title}),icon.is("img")?$(icon).attr({src:M.util.image_url("t/"+action),alt:title}):($(icon).addClass(getIconClass(action)),$(icon).removeClass(getIconClass(oppositeAction(action))))})).catch(Notification.exception)},getIconClass=function(action){return"show"==action?"fa-eye-slash":"fa-eye"},getTitle=function(action){return Str.get_string("online_status:"+action,"block_online_users")};return{init:function(){$(SELECTORS_CHANGE_VISIBILITY_LINK).on("click",(function(e){e.preventDefault(),function(action,userid){var value="show"==action?1:0;UserRepository.setUserPreference("block_online_users_uservisibility",value,userid).then((data=>{if(data.saved){var newAction=oppositeAction(action);changeVisibilityLinkAttr(newAction),changeVisibilityIconAttr(newAction)}})).catch(Notification.exception)}($(this).attr("data-action"),$(this).attr("data-userid"))}))}}})); + * A javascript module that handles the change of the user's visibility in the + * online users block. + * + * @module block_online_users/change_user_visibility + * @copyright 2018 Mihail Geshoski + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */Object.defineProperty(_exports,"__esModule",{value:!0}),_exports.init=void 0,_notification=(obj=_notification)&&obj.__esModule?obj:{default:obj};const SELECTORS_CHANGE_VISIBILITY_LINK="#change-user-visibility",SELECTORS_CHANGE_VISIBILITY_ICON="#change-user-visibility .icon",oppositeAction=action=>"show"==action?"hide":"show",changeVisibilityLinkAttr=action=>getTitle(action).then((title=>{const link=document.querySelector(SELECTORS_CHANGE_VISIBILITY_LINK);return link.dataset.action=action,link.title=title,link})),changeVisibilityIconAttr=action=>getTitle(action).then((title=>{const icon=document.querySelector(SELECTORS_CHANGE_VISIBILITY_ICON);return icon.setAttribute("title",title),icon.setAttribute("aria-label",title),icon.closest("img")?(icon.setAttribute("src",M.util.image_url("t/".concat(action))),icon.setAttribute("alt",title)):(icon.classList.add(getIconClass(action)),icon.classList.remove(getIconClass(oppositeAction(action)))),title})),getIconClass=action=>"show"==action?"fa-eye-slash":"fa-eye",getTitle=action=>(0,_str.get_string)("online_status:".concat(action),"block_online_users");_exports.init=()=>{document.addEventListener("click",(e=>{const link=e.target.closest(SELECTORS_CHANGE_VISIBILITY_LINK);var action,userid;link&&(e.preventDefault(),action=link.dataset.action,userid=link.dataset.userid,(0,_repository.setUserPreference)("block_online_users_uservisibility","show"==action?1:0,userid).then((data=>{if(data.saved){const newAction=oppositeAction(action);changeVisibilityLinkAttr(newAction),changeVisibilityIconAttr(newAction)}return data})).catch(_notification.default.exception))}))}})); //# sourceMappingURL=change_user_visibility.min.js.map \ No newline at end of file diff --git a/blocks/online_users/amd/build/change_user_visibility.min.js.map b/blocks/online_users/amd/build/change_user_visibility.min.js.map index 892cbeca5b8..2f58f211bef 100644 --- a/blocks/online_users/amd/build/change_user_visibility.min.js.map +++ b/blocks/online_users/amd/build/change_user_visibility.min.js.map @@ -1 +1 @@ -{"version":3,"file":"change_user_visibility.min.js","sources":["../src/change_user_visibility.js"],"sourcesContent":["// This file is part of Moodle - http://moodle.org/\n//\n// Moodle is free software: you can redistribute it and/or modify\n// it under the terms of the GNU General Public License as published by\n// the Free Software Foundation, either version 3 of the License, or\n// (at your option) any later version.\n//\n// Moodle is distributed in the hope that it will be useful,\n// but WITHOUT ANY WARRANTY; without even the implied warranty of\n// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n// GNU General Public License for more details.\n//\n// You should have received a copy of the GNU General Public License\n// along with Moodle. If not, see .\n\n/**\n * A javascript module that handles the change of the user's visibility in the\n * online users block.\n *\n * @module block_online_users/change_user_visibility\n * @copyright 2018 Mihail Geshoski \n * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n */\ndefine(['jquery', 'core/str', 'core/notification', 'core_user/repository'],\n function($, Str, Notification, UserRepository) {\n\n /**\n * Selectors.\n *\n * @access private\n * @type {Object}\n */\n var SELECTORS = {\n CHANGE_VISIBILITY_LINK: '#change-user-visibility',\n CHANGE_VISIBILITY_ICON: '#change-user-visibility .icon'\n };\n\n /**\n * Change user visibility in the online users block.\n *\n * @method changeVisibility\n * @param {String} action\n * @param {String} userid\n * @private\n */\n var changeVisibility = function(action, userid) {\n\n var value = action == \"show\" ? 1 : 0;\n\n UserRepository.setUserPreference('block_online_users_uservisibility', value, userid)\n .then(data => {\n if (data.saved) {\n var newAction = oppositeAction(action);\n changeVisibilityLinkAttr(newAction);\n changeVisibilityIconAttr(newAction);\n }\n return;\n }).catch(Notification.exception);\n };\n\n /**\n * Get the opposite action.\n *\n * @method oppositeAction\n * @param {String} action\n * @return {String}\n * @private\n */\n var oppositeAction = function(action) {\n return action == 'show' ? 'hide' : 'show';\n };\n\n /**\n * Change the attribute values of the user visibility link in the online users block.\n *\n * @method changeVisibilityLinkAttr\n * @param {String} action\n * @private\n */\n var changeVisibilityLinkAttr = function(action) {\n getTitle(action).then(function(title) {\n $(SELECTORS.CHANGE_VISIBILITY_LINK).attr({\n 'data-action': action,\n 'title': title\n });\n return;\n }).catch(Notification.exception);\n };\n\n /**\n * Change the attribute values of the user visibility icon in the online users block.\n *\n * @method changeVisibilityIconAttr\n * @param {String} action\n * @private\n */\n var changeVisibilityIconAttr = function(action) {\n var icon = $(SELECTORS.CHANGE_VISIBILITY_ICON);\n getTitle(action).then(function(title) {\n // Add the proper title to the icon.\n $(icon).attr({\n 'title': title,\n 'aria-label': title\n });\n // If the icon is an image.\n if (icon.is(\"img\")) {\n $(icon).attr({\n 'src': M.util.image_url('t/' + action),\n 'alt': title\n });\n } else {\n // Add the new icon class and remove the old one.\n $(icon).addClass(getIconClass(action));\n $(icon).removeClass(getIconClass(oppositeAction(action)));\n }\n return;\n }).catch(Notification.exception);\n };\n\n /**\n * Get the proper class for the user visibility icon in the online users block.\n *\n * @method getIconClass\n * @param {String} action\n * @return {String}\n * @private\n */\n var getIconClass = function(action) {\n return action == 'show' ? 'fa-eye-slash' : 'fa-eye';\n };\n\n /**\n * Get the title description of the user visibility link in the online users block.\n *\n * @method getTitle\n * @param {String} action\n * @return {object} jQuery promise\n * @private\n */\n var getTitle = function(action) {\n return Str.get_string('online_status:' + action, 'block_online_users');\n };\n\n return {\n // Public variables and functions.\n /**\n * Initialise change user visibility function.\n *\n * @method init\n */\n init: function() {\n $(SELECTORS.CHANGE_VISIBILITY_LINK).on('click', function(e) {\n e.preventDefault();\n var action = ($(this).attr('data-action'));\n var userid = ($(this).attr('data-userid'));\n changeVisibility(action, userid);\n });\n }\n };\n});\n"],"names":["define","$","Str","Notification","UserRepository","SELECTORS","oppositeAction","action","changeVisibilityLinkAttr","getTitle","then","title","attr","catch","exception","changeVisibilityIconAttr","icon","is","M","util","image_url","addClass","getIconClass","removeClass","get_string","init","on","e","preventDefault","userid","value","setUserPreference","data","saved","newAction","changeVisibility","this"],"mappings":";;;;;;;;AAuBAA,mDAAO,CAAC,SAAU,WAAY,oBAAqB,yBAC3C,SAASC,EAAGC,IAAKC,aAAcC,oBAQ/BC,iCACwB,0BADxBA,iCAEwB,gCAkCxBC,eAAiB,SAASC,cACT,QAAVA,OAAmB,OAAS,QAUnCC,yBAA2B,SAASD,QACpCE,SAASF,QAAQG,MAAK,SAASC,OAC3BV,EAAEI,kCAAkCO,KAAK,eACtBL,aACNI,WAGdE,MAAMV,aAAaW,YAUtBC,yBAA2B,SAASR,YAChCS,KAAOf,EAAEI,kCACbI,SAASF,QAAQG,MAAK,SAASC,OAE3BV,EAAEe,MAAMJ,KAAK,OACAD,mBACKA,QAGdK,KAAKC,GAAG,OACRhB,EAAEe,MAAMJ,KAAK,KACFM,EAAEC,KAAKC,UAAU,KAAOb,YACxBI,SAIXV,EAAEe,MAAMK,SAASC,aAAaf,SAC9BN,EAAEe,MAAMO,YAAYD,aAAahB,eAAeC,cAGrDM,MAAMV,aAAaW,YAWtBQ,aAAe,SAASf,cACP,QAAVA,OAAmB,eAAiB,UAW3CE,SAAW,SAASF,eACbL,IAAIsB,WAAW,iBAAmBjB,OAAQ,6BAG9C,CAOHkB,KAAM,WACFxB,EAAEI,kCAAkCqB,GAAG,SAAS,SAASC,GACrDA,EAAEC,iBA3GS,SAASrB,OAAQsB,YAEhCC,MAAkB,QAAVvB,OAAmB,EAAI,EAEnCH,eAAe2B,kBAAkB,oCAAqCD,MAAOD,QAC5EnB,MAAKsB,UACEA,KAAKC,MAAO,KACRC,UAAY5B,eAAeC,QAC/BC,yBAAyB0B,WACzBnB,yBAAyBmB,eAG9BrB,MAAMV,aAAaW,WAkGdqB,CAFclC,EAAEmC,MAAMxB,KAAK,eACbX,EAAEmC,MAAMxB,KAAK"} \ No newline at end of file +{"version":3,"file":"change_user_visibility.min.js","sources":["../src/change_user_visibility.js"],"sourcesContent":["// This file is part of Moodle - http://moodle.org/\n//\n// Moodle is free software: you can redistribute it and/or modify\n// it under the terms of the GNU General Public License as published by\n// the Free Software Foundation, either version 3 of the License, or\n// (at your option) any later version.\n//\n// Moodle is distributed in the hope that it will be useful,\n// but WITHOUT ANY WARRANTY; without even the implied warranty of\n// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n// GNU General Public License for more details.\n//\n// You should have received a copy of the GNU General Public License\n// along with Moodle. If not, see .\n\n/**\n * A javascript module that handles the change of the user's visibility in the\n * online users block.\n *\n * @module block_online_users/change_user_visibility\n * @copyright 2018 Mihail Geshoski \n * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n */\n\nimport {get_string as getString} from 'core/str';\nimport Notification from 'core/notification';\nimport {setUserPreference} from 'core_user/repository';\n\n/**\n * Selectors.\n *\n * @access private\n * @type {Object}\n */\nconst SELECTORS = {\n CHANGE_VISIBILITY_LINK: '#change-user-visibility',\n CHANGE_VISIBILITY_ICON: '#change-user-visibility .icon',\n};\n\n/**\n * Change user visibility in the online users block.\n *\n * @method changeVisibility\n * @param {String} action\n * @param {String} userid\n * @returns {Promise}\n * @private\n */\nconst changeVisibility = (action, userid) => setUserPreference(\n 'block_online_users_uservisibility',\n action == \"show\" ? 1 : 0,\n userid,\n)\n.then((data) => {\n if (data.saved) {\n const newAction = oppositeAction(action);\n changeVisibilityLinkAttr(newAction);\n changeVisibilityIconAttr(newAction);\n }\n return data;\n}).catch(Notification.exception);\n\n/**\n * Get the opposite action.\n *\n * @method oppositeAction\n * @param {String} action\n * @return {String}\n * @private\n */\nconst oppositeAction = (action) => action == 'show' ? 'hide' : 'show';\n\n/**\n * Change the attribute values of the user visibility link in the online users block.\n *\n * @method changeVisibilityLinkAttr\n * @param {String} action\n * @returns {Promise}\n * @private\n */\nconst changeVisibilityLinkAttr = (action) => getTitle(action)\n .then((title) => {\n const link = document.querySelector(SELECTORS.CHANGE_VISIBILITY_LINK);\n link.dataset.action = action;\n link.title = title;\n return link;\n });\n\n/**\n * Change the attribute values of the user visibility icon in the online users block.\n *\n * @method changeVisibilityIconAttr\n * @param {String} action\n * @returns {Promise}\n * @private\n */\nconst changeVisibilityIconAttr = (action) => getTitle(action)\n .then((title) => {\n const icon = document.querySelector(SELECTORS.CHANGE_VISIBILITY_ICON);\n\n // Add the proper title to the icon.\n icon.setAttribute('title', title);\n icon.setAttribute('aria-label', title);\n\n if (icon.closest(\"img\")) {\n // If the icon is an image.\n icon.setAttribute('src', M.util.image_url(`t/${action}`));\n icon.setAttribute('alt', title);\n } else {\n // Add the new icon class and remove the old one.\n icon.classList.add(getIconClass(action));\n icon.classList.remove(getIconClass(oppositeAction(action)));\n }\n return title;\n });\n\n/**\n * Get the proper class for the user visibility icon in the online users block.\n *\n * @method getIconClass\n * @param {String} action\n * @return {String}\n * @private\n */\nconst getIconClass = (action) => (action == 'show') ? 'fa-eye-slash' : 'fa-eye';\n\n/**\n * Get the title description of the user visibility link in the online users block.\n *\n * @method getTitle\n * @param {String} action\n * @return {object} jQuery promise\n * @private\n */\nconst getTitle = (action) => getString(`online_status:${action}`, 'block_online_users');\n\n/**\n * Initialise change user visibility function.\n *\n * @method init\n */\nexport const init = () => {\n document.addEventListener('click', (e) => {\n const link = e.target.closest(SELECTORS.CHANGE_VISIBILITY_LINK);\n if (!link) {\n return;\n }\n e.preventDefault();\n changeVisibility(\n link.dataset.action,\n link.dataset.userid,\n );\n });\n};\n"],"names":["SELECTORS","oppositeAction","action","changeVisibilityLinkAttr","getTitle","then","title","link","document","querySelector","dataset","changeVisibilityIconAttr","icon","setAttribute","closest","M","util","image_url","classList","add","getIconClass","remove","addEventListener","e","target","userid","preventDefault","data","saved","newAction","catch","Notification","exception"],"mappings":";;;;;;;;4JAkCMA,iCACsB,0BADtBA,iCAEsB,gCAkCtBC,eAAkBC,QAAqB,QAAVA,OAAmB,OAAS,OAUzDC,yBAA4BD,QAAWE,SAASF,QACjDG,MAAMC,cACGC,KAAOC,SAASC,cAAcT,yCACpCO,KAAKG,QAAQR,OAASA,OACtBK,KAAKD,MAAQA,MACNC,QAWTI,yBAA4BT,QAAWE,SAASF,QACjDG,MAAMC,cACGM,KAAOJ,SAASC,cAAcT,yCAGpCY,KAAKC,aAAa,QAASP,OAC3BM,KAAKC,aAAa,aAAcP,OAE5BM,KAAKE,QAAQ,QAEbF,KAAKC,aAAa,MAAOE,EAAEC,KAAKC,sBAAef,UAC/CU,KAAKC,aAAa,MAAOP,SAGzBM,KAAKM,UAAUC,IAAIC,aAAalB,SAChCU,KAAKM,UAAUG,OAAOD,aAAanB,eAAeC,WAE/CI,SAWTc,aAAgBlB,QAAsB,QAAVA,OAAoB,eAAiB,SAUjEE,SAAYF,SAAW,2CAA2BA,QAAU,oCAO9C,KAChBM,SAASc,iBAAiB,SAAUC,UAC1BhB,KAAOgB,EAAEC,OAAOV,QAAQd,kCA/Fb,IAACE,OAAQuB,OAgGrBlB,OAGLgB,EAAEG,iBAnGgBxB,OAqGdK,KAAKG,QAAQR,OArGSuB,OAsGtBlB,KAAKG,QAAQe,QAtGoB,iCACzC,oCACU,QAAVvB,OAAmB,EAAI,EACvBuB,QAEHpB,MAAMsB,UACCA,KAAKC,MAAO,OACNC,UAAY5B,eAAeC,QACjCC,yBAAyB0B,WACzBlB,yBAAyBkB,kBAEtBF,QACRG,MAAMC,sBAAaC"} \ No newline at end of file diff --git a/blocks/online_users/amd/src/change_user_visibility.js b/blocks/online_users/amd/src/change_user_visibility.js index 452c28c1df0..23de8f2ee78 100644 --- a/blocks/online_users/amd/src/change_user_visibility.js +++ b/blocks/online_users/amd/src/change_user_visibility.js @@ -21,140 +21,134 @@ * @copyright 2018 Mihail Geshoski * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -define(['jquery', 'core/str', 'core/notification', 'core_user/repository'], - function($, Str, Notification, UserRepository) { - /** - * Selectors. - * - * @access private - * @type {Object} - */ - var SELECTORS = { - CHANGE_VISIBILITY_LINK: '#change-user-visibility', - CHANGE_VISIBILITY_ICON: '#change-user-visibility .icon' - }; +import {get_string as getString} from 'core/str'; +import Notification from 'core/notification'; +import {setUserPreference} from 'core_user/repository'; - /** - * Change user visibility in the online users block. - * - * @method changeVisibility - * @param {String} action - * @param {String} userid - * @private - */ - var changeVisibility = function(action, userid) { +/** + * Selectors. + * + * @access private + * @type {Object} + */ +const SELECTORS = { + CHANGE_VISIBILITY_LINK: '#change-user-visibility', + CHANGE_VISIBILITY_ICON: '#change-user-visibility .icon', +}; - var value = action == "show" ? 1 : 0; +/** + * Change user visibility in the online users block. + * + * @method changeVisibility + * @param {String} action + * @param {String} userid + * @returns {Promise} + * @private + */ +const changeVisibility = (action, userid) => setUserPreference( + 'block_online_users_uservisibility', + action == "show" ? 1 : 0, + userid, +) +.then((data) => { + if (data.saved) { + const newAction = oppositeAction(action); + changeVisibilityLinkAttr(newAction); + changeVisibilityIconAttr(newAction); + } + return data; +}).catch(Notification.exception); - UserRepository.setUserPreference('block_online_users_uservisibility', value, userid) - .then(data => { - if (data.saved) { - var newAction = oppositeAction(action); - changeVisibilityLinkAttr(newAction); - changeVisibilityIconAttr(newAction); - } - return; - }).catch(Notification.exception); - }; +/** + * Get the opposite action. + * + * @method oppositeAction + * @param {String} action + * @return {String} + * @private + */ +const oppositeAction = (action) => action == 'show' ? 'hide' : 'show'; - /** - * Get the opposite action. - * - * @method oppositeAction - * @param {String} action - * @return {String} - * @private - */ - var oppositeAction = function(action) { - return action == 'show' ? 'hide' : 'show'; - }; +/** + * Change the attribute values of the user visibility link in the online users block. + * + * @method changeVisibilityLinkAttr + * @param {String} action + * @returns {Promise} + * @private + */ +const changeVisibilityLinkAttr = (action) => getTitle(action) + .then((title) => { + const link = document.querySelector(SELECTORS.CHANGE_VISIBILITY_LINK); + link.dataset.action = action; + link.title = title; + return link; + }); - /** - * Change the attribute values of the user visibility link in the online users block. - * - * @method changeVisibilityLinkAttr - * @param {String} action - * @private - */ - var changeVisibilityLinkAttr = function(action) { - getTitle(action).then(function(title) { - $(SELECTORS.CHANGE_VISIBILITY_LINK).attr({ - 'data-action': action, - 'title': title - }); - return; - }).catch(Notification.exception); - }; +/** + * Change the attribute values of the user visibility icon in the online users block. + * + * @method changeVisibilityIconAttr + * @param {String} action + * @returns {Promise} + * @private + */ +const changeVisibilityIconAttr = (action) => getTitle(action) + .then((title) => { + const icon = document.querySelector(SELECTORS.CHANGE_VISIBILITY_ICON); - /** - * Change the attribute values of the user visibility icon in the online users block. - * - * @method changeVisibilityIconAttr - * @param {String} action - * @private - */ - var changeVisibilityIconAttr = function(action) { - var icon = $(SELECTORS.CHANGE_VISIBILITY_ICON); - getTitle(action).then(function(title) { - // Add the proper title to the icon. - $(icon).attr({ - 'title': title, - 'aria-label': title - }); + // Add the proper title to the icon. + icon.setAttribute('title', title); + icon.setAttribute('aria-label', title); + + if (icon.closest("img")) { // If the icon is an image. - if (icon.is("img")) { - $(icon).attr({ - 'src': M.util.image_url('t/' + action), - 'alt': title - }); - } else { - // Add the new icon class and remove the old one. - $(icon).addClass(getIconClass(action)); - $(icon).removeClass(getIconClass(oppositeAction(action))); - } - return; - }).catch(Notification.exception); - }; - - /** - * Get the proper class for the user visibility icon in the online users block. - * - * @method getIconClass - * @param {String} action - * @return {String} - * @private - */ - var getIconClass = function(action) { - return action == 'show' ? 'fa-eye-slash' : 'fa-eye'; - }; - - /** - * Get the title description of the user visibility link in the online users block. - * - * @method getTitle - * @param {String} action - * @return {object} jQuery promise - * @private - */ - var getTitle = function(action) { - return Str.get_string('online_status:' + action, 'block_online_users'); - }; - - return { - // Public variables and functions. - /** - * Initialise change user visibility function. - * - * @method init - */ - init: function() { - $(SELECTORS.CHANGE_VISIBILITY_LINK).on('click', function(e) { - e.preventDefault(); - var action = ($(this).attr('data-action')); - var userid = ($(this).attr('data-userid')); - changeVisibility(action, userid); - }); + icon.setAttribute('src', M.util.image_url(`t/${action}`)); + icon.setAttribute('alt', title); + } else { + // Add the new icon class and remove the old one. + icon.classList.add(getIconClass(action)); + icon.classList.remove(getIconClass(oppositeAction(action))); } - }; -}); + return title; + }); + +/** + * Get the proper class for the user visibility icon in the online users block. + * + * @method getIconClass + * @param {String} action + * @return {String} + * @private + */ +const getIconClass = (action) => (action == 'show') ? 'fa-eye-slash' : 'fa-eye'; + +/** + * Get the title description of the user visibility link in the online users block. + * + * @method getTitle + * @param {String} action + * @return {object} jQuery promise + * @private + */ +const getTitle = (action) => getString(`online_status:${action}`, 'block_online_users'); + +/** + * Initialise change user visibility function. + * + * @method init + */ +export const init = () => { + document.addEventListener('click', (e) => { + const link = e.target.closest(SELECTORS.CHANGE_VISIBILITY_LINK); + if (!link) { + return; + } + e.preventDefault(); + changeVisibility( + link.dataset.action, + link.dataset.userid, + ); + }); +};