-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
permissions.js
137 lines (118 loc) · 3.97 KB
/
permissions.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/**
* @copyright Copyright (C) 2005 - 2017 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
Joomla = window.Joomla || {};
(function(Joomla, document) {
'use strict';
/**
* Function to send Permissions via Ajax to Com-Config Application Controller
*/
Joomla.sendPermissions = function(event) {
var target = event.target;
// Set the icon while storing the values
var icon = document.getElementById('icon_' + this.id);
icon.removeAttribute('class');
icon.setAttribute('class', 'fa fa-spinner fa-spin');
// Get values and prepare GET-Parameter
var asset = 'not',
component = Joomla.getUrlParam('component'),
extension = Joomla.getUrlParam('extension'),
option = Joomla.getUrlParam('option'),
view = Joomla.getUrlParam('view'),
title = component,
value = this.value,
context = '';
if (document.getElementById('jform_context')) {
context = document.getElementById('jform_context').value;
context = context.split('.')[0];
}
if (option == 'com_config' && component == false && extension == false) {
asset = 'root.1';
}
else if (extension == false && view == 'component') {
asset = component;
}
else if (context) {
if (view == 'group') {
asset = context + '.fieldgroup.' + Joomla.getUrlParam('id');
}
else {
asset = context + '.field.' + Joomla.getUrlParam('id');
}
title = document.getElementById('jform_title').value;
}
else if (extension != false && view != false) {
asset = extension + '.' + view + '.' + Joomla.getUrlParam('id');
title = document.getElementById('jform_title').value;
}
else if (extension == false && view != false) {
asset = option + '.' + view + '.' + Joomla.getUrlParam('id');
title = document.getElementById('jform_title').value;
}
var id = this.id.replace('jform_rules_', ''),
lastUnderscoreIndex = id.lastIndexOf('_');
var permissionData = {
comp : asset,
action : id.substring(0, lastUnderscoreIndex),
rule : id.substring(lastUnderscoreIndex + 1),
value : value,
title : title
};
// Remove JS messages, if they exist.
Joomla.removeMessages();
// Ajax request
Joomla.request({
url: document.getElementById('permissions-sliders').getAttribute('data-ajaxuri'),
method: 'POST',
data: JSON.stringify(permissionData),
perform: true,
headers: {'Content-Type': 'application/json'},
onSuccess: function(response, xhr) {
try {
response = JSON.parse(response);
} catch(e) {
console.log(e)
}
icon.removeAttribute('class');
// Check if everything is OK
if (response.data && response.data.result === true) {
icon.setAttribute('class', 'fa fa-check');
var badgeSpan = target.parentNode.parentNode.nextElementSibling.querySelector('span');
badgeSpan.removeAttribute('class');
badgeSpan.setAttribute('class', response.data['class']);
badgeSpan.innerHTML = response.data.text;
}
// Render messages, if any. There are only message in case of errors.
if (typeof response.messages === 'object' && response.messages !== null) {
Joomla.renderMessages(response.messages);
if (response.data && response.data.result === true) {
icon.setAttribute('class', 'fa fa-check');
} else {
icon.setAttribute('class', 'fa fa-times');
}
}
},
onError: function(xhr) {
// Remove the spinning icon.
icon.removeAttribute('style');
Joomla.renderMessages(Joomla.ajaxErrorsMessages(jqXHR, textStatus, error));
icon.setAttribute('class', 'fa fa-times');
}
});
}
/**
* Function to get parameters out of the URL
*/
Joomla.getUrlParam = function(variable) {
var query = window.location.search.substring(1);
var vars = query.split('&');
for (var i = 0; i < vars.length; i++) {
var pair = vars[i].split('=');
if (pair[0] == variable) {
return pair[1];
}
}
return false;
}
})(Joomla, document);