MDL-57690 js: fix core modal uses in amd
[moodle.git] / lib / amd / src / permissionmanager.js
CommitLineData
24935689
MM
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 * @package core
17 * @class permissionmanager
18 * @copyright 2015 Martin Mastny <mastnym@vscht.cz>
19 * @since 3.0
20 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
21 */
22
23 /**
24 * @module admin/permissionmanager
25 */
e7ecb4a9
DP
26define(['jquery', 'core/config', 'core/notification', 'core/templates', 'core/yui'],
27 function($, config, notification, templates, Y) {
24935689
MM
28
29 /**
30 * Used CSS selectors
31 * @access private
32 */
33 var SELECTORS = {
34 ADDROLE: 'a.allowlink, a.prohibitlink',
35 REMOVEROLE: 'a.preventlink, a.unprohibitlink',
36 UNPROHIBIT: 'a.unprohibitlink'
37 };
38 var rolesloadedevent = $.Event('rolesloaded');
39 var contextid;
40 var contextname;
41 var adminurl;
42 var overideableroles;
43 var panel = null;
44
45 /**
46 * Load all possible roles, which could be assigned from server
47 *
48 * @access private
49 * @method loadOverideableRoles
50 */
51 var loadOverideableRoles = function() {
52 var params = {
53 contextid: contextid,
54 getroles: 1,
55 sesskey: config.sesskey
56 };
57
4d9036d6
MJ
58 // Need to tell jQuery to expect JSON as the content type may not be correct (MDL-55041).
59 $.post(adminurl + 'roles/ajax.php', params, null, 'json')
24935689
MM
60 .done(function(data) {
61 try {
62 overideableroles = data;
63 loadOverideableRoles = function() {
64 $('body').trigger(rolesloadedevent);
65 };
66 loadOverideableRoles();
c96f55e6 67 } catch (err) {
24935689
MM
68 notification.exception(err);
69 }
70 })
71 .fail(function(jqXHR, status, error) {
72 notification.exception(error);
73 });
74 };
75
76 /**
77 * Perform the UI changes after server change
78 *
79 * @access private
80 * @method changePermissions
c96f55e6 81 * @param {JQuery} row
24935689
MM
82 * @param {int} roleid
83 * @param {string} action
84 */
85 var changePermissions = function(row, roleid, action) {
86 var params = {
87 contextid: contextid,
88 roleid: roleid,
89 sesskey: M.cfg.sesskey,
90 action: action,
91 capability: row.data('name')
92 };
4d9036d6 93 $.post(adminurl + 'roles/ajax.php', params, null, 'json')
24935689
MM
94 .done(function(data) {
95 var action = data;
96 try {
97 var templatedata = {rolename: overideableroles[roleid],
98 roleid: roleid,
99 adminurl: adminurl,
100 imageurl: M.util.image_url('t/delete', 'moodle')
101 };
102 switch (action) {
103 case 'allow':
104 templatedata.spanclass = 'allowed';
105 templatedata.linkclass = 'preventlink';
106 templatedata.action = 'prevent';
107 break;
108 case 'prohibit':
109 templatedata.spanclass = 'forbidden';
110 templatedata.linkclass = 'unprohibitlink';
111 templatedata.action = 'unprohibit';
112 break;
113 case 'prevent':
114 row.find('a[data-role-id="' + roleid + '"]').first().closest('.allowed').remove();
115 return;
116 case 'unprohibit':
117 row.find('a[data-role-id="' + roleid + '"]').first().closest('.forbidden').remove();
118 return;
119 default:
120 return;
121 }
35be5826
DP
122 templates.render('core/permissionmanager_role', templatedata)
123 .done(function(content) {
124 if (action == 'allow') {
24935689 125 $(content).insertBefore(row.find('.allowmore:first'));
35be5826 126 } else if (action == 'prohibit') {
24935689
MM
127 $(content).insertBefore(row.find('.prohibitmore:first'));
128 // Remove allowed link
129 var allowedLink = row.find('.allowedroles').first().find('a[data-role-id="' + roleid + '"]');
130 if (allowedLink) {
131 allowedLink.first().closest('.allowed').remove();
132 }
133 }
134 panel.hide();
135 })
136 .fail(notification.exception);
c96f55e6 137 } catch (err) {
24935689
MM
138 notification.exception(err);
139 }
140 })
141 .fail(function(jqXHR, status, error) {
142 notification.exception(error);
143 });
144 };
145
146 /**
147 * Prompts user for selecting a role which is permitted
148 *
149 * @access private
150 * @method handleAddRole
151 * @param {event} e
152 */
35be5826 153 var handleAddRole = function(e) {
24935689
MM
154 e.preventDefault();
155
e7ecb4a9
DP
156 // TODO: MDL-57778 Convert to core/modal.
157 Y.use('moodle-core-notification-dialogue', function() {
158 $('body').one('rolesloaded', function() {
159 var link = $(e.currentTarget);
160 var action = link.data('action');
161 var row = link.closest('tr.rolecap');
162 var confirmationDetails = {
163 cap: row.data('humanname'),
164 context: contextname
165 };
166 var message = M.util.get_string('role' + action + 'info', 'core_role', confirmationDetails);
167 if (panel === null) {
168 panel = new M.core.dialogue({
169 draggable: true,
170 modal: true,
171 closeButton: true,
172 width: '450px'
173 });
174 }
175 panel.set('headerContent', M.util.get_string('role' + action + 'header', 'core_role'));
24935689 176
e7ecb4a9 177 var i, existingrolelinks;
24935689 178
e7ecb4a9
DP
179 var roles = [];
180 switch (action) {
181 case 'allow':
182 existingrolelinks = row.find(SELECTORS.REMOVEROLE);
183 break;
184 case 'prohibit':
185 existingrolelinks = row.find(SELECTORS.UNPROHIBIT);
186 break;
187 }
188 for (i in overideableroles) {
189 var disabled = '';
190 var disable = existingrolelinks.filter("[data-role-id='" + i + "']").length;
191 if (disable) {
192 disabled = 'disabled';
193 }
194 var roledetails = {roleid: i, rolename: overideableroles[i], disabled: disabled};
195 roles.push(roledetails);
24935689 196 }
24935689 197
e7ecb4a9
DP
198 templates.render('core/permissionmanager_panelcontent', {message: message, roles: roles})
199 .done(function(content) {
200 panel.set('bodyContent', content);
201 panel.show();
202 $('div.role_buttons').delegate('input', 'click', function(e) {
203 var roleid = $(e.currentTarget).data('role-id');
204 changePermissions(row, roleid, action);
205 });
206 })
207 .fail(notification.exception);
24935689 208
e7ecb4a9 209 });
24935689
MM
210 });
211 loadOverideableRoles();
212 };
213
214 /**
215 * Prompts user when removing permission
216 *
217 * @access private
218 * @method handleRemoveRole
219 * @param {event} e
220 */
35be5826 221 var handleRemoveRole = function(e) {
24935689
MM
222 e.preventDefault();
223 $('body').one('rolesloaded', function() {
224 var link = $(e.currentTarget);
225 var action = link.data('action');
226 var roleid = link.data('role-id');
227 var row = link.closest('tr.rolecap');
228 var questionDetails = {
229 role: overideableroles[roleid],
230 cap: row.data('humanname'),
231 context: contextname
232 };
233
234 notification.confirm(M.util.get_string('confirmunassigntitle', 'core_role'),
35be5826 235 M.util.get_string('confirmrole' + action, 'core_role', questionDetails),
24935689
MM
236 M.util.get_string('confirmunassignyes', 'core_role'),
237 M.util.get_string('confirmunassignno', 'core_role'),
35be5826 238 function() {
24935689
MM
239 changePermissions(row, roleid, action);
240 }
241 );
242 });
243 loadOverideableRoles();
244 };
245
246 return /** @alias module:core/permissionmanager */ {
247 /**
248 * Initialize permissionmanager
249 * @access public
c96f55e6 250 * @param {Object} args
24935689 251 */
35be5826 252 initialize: function(args) {
24935689
MM
253 contextid = args.contextid;
254 contextname = args.contextname;
255 adminurl = args.adminurl;
256 var body = $('body');
257 body.delegate(SELECTORS.ADDROLE, 'click', handleAddRole);
258 body.delegate(SELECTORS.REMOVEROLE, 'click', handleRemoveRole);
259 }
260 };
c96f55e6 261});