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