MDL-62859 block_online_users: Rewrite visibility changer as ESM

This commit is contained in:
Andrew Nicols
2023-05-22 13:51:39 +01:00
committed by Paul Holden
parent 82d7635d52
commit dea64fa266
3 changed files with 131 additions and 137 deletions
@@ -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 <mihail@moodle.com>
* @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 <mihail@moodle.com>
* @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
File diff suppressed because one or more lines are too long
@@ -21,140 +21,134 @@
* @copyright 2018 Mihail Geshoski <[email protected]>
* @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,
);
});
};