-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Expand file tree
/
Copy pathpermission-management-modal.js
More file actions
394 lines (338 loc) · 14.4 KB
/
Copy pathpermission-management-modal.js
File metadata and controls
394 lines (338 loc) · 14.4 KB
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
var abp = abp || {};
(function ($) {
abp.modals = abp.modals || {};
// The toolbars, menus and bundles are rendered on the server, so a full page load
// is the only way to reflect the new permissions on the current page. Changing the
// permissions of another user or role does not affect what this page renders.
function affectsCurrentUser(providerName, providerKey) {
var currentUser = abp.currentUser;
if (!currentUser || !currentUser.isAuthenticated) {
return false;
}
if (providerName === 'U') {
return currentUser.id === providerKey;
}
if (providerName === 'R') {
return (currentUser.roles || []).indexOf(providerKey) > -1;
}
return false;
}
abp.modals.PermissionManagement = function () {
var l = abp.localization.getResource("AbpPermissionManagement");
function checkParents($tab, $checkBox) {
var parentName = $checkBox
.closest('.custom-checkbox')
.attr('data-parent-name');
if (!parentName) {
return;
}
$tab.find('.custom-checkbox')
.filter('[data-permission-name="' + parentName + '"]')
.find('input[type="checkbox"]')
.each(function () {
var $parent = $(this);
$parent.prop('checked', true);
checkParents($tab, $parent);
});
}
function uncheckChildren($tab, $checkBox) {
var permissionName = $checkBox
.closest('.custom-checkbox')
.attr('data-permission-name');
if (!permissionName) {
return;
}
$tab.find('.custom-checkbox')
.filter('[data-parent-name="' + permissionName + '"]')
.find('input[type="checkbox"]')
.each(function () {
var $child = $(this);
$child.prop('checked', false);
uncheckChildren($tab, $child);
});
}
function handleTabCheckedCheckboxCount($tab) {
var newCount = 0;
$tab.find('input[type="checkbox"]')
.not('[name="SelectAllInThisTab"]')
.each(function () {
if ($(this).is(':checked') === true) {
newCount++;
}
});
var $tabTitle = $('#' + $tab.attr('id') + '-tab');
var title = $tabTitle
.html()
.replace('<b>', '')
.replace('</b>', '')
.replace('<small>', '')
.replace('</small>', '');
var titleSplitted = title.split(' ');
if (titleSplitted[titleSplitted.length - 1].startsWith('(')) {
titleSplitted.pop();
}
var titleWithoutCount = titleSplitted.join(' ');
var newTitle = titleWithoutCount + ' (' + newCount + ')';
if (newCount > 0) {
newTitle = '<b>' + newTitle + '</b>';
} else {
newTitle = '<small>' + newTitle + '</small>';
}
$tabTitle.html(newTitle);
}
function handleUncheck($tab) {
var $checkBox = $tab.find('input[name="SelectAllInThisTab"]');
if ($checkBox.is(':checked')) {
if (
$tab
.find('input[type="checkbox"]')
.not('[name="SelectAllInThisTab"]').length > 1
) {
$($checkBox).prop('indeterminate', true);
} else {
$checkBox.prop('checked', false);
}
} else if ($checkBox.is(':indeterminate')) {
var allUnchecked = true;
$tab.find('input[type="checkbox"]')
.not('[name="SelectAllInThisTab"]')
.each(function () {
if ($(this).is(':checked') === true) {
allUnchecked = false;
}
});
if (allUnchecked) {
$($checkBox).prop('indeterminate', false);
$checkBox.prop('checked', false);
}
}
}
function handleCheck($tab) {
var $checkBox = $tab.find('input[name="SelectAllInThisTab"]');
var allChecked = true;
$tab.find('input[type="checkbox"]')
.not('[name="SelectAllInThisTab"]')
.each(function () {
if ($(this).is(':checked') === false) {
allChecked = false;
}
});
if (allChecked) {
$($checkBox).prop('indeterminate', false);
$checkBox.prop('checked', true);
} else {
$($checkBox).prop('indeterminate', true);
}
}
function initSelectAllInThisTab() {
var tabs = $('.tab-pane');
for (var i = 0; i < tabs.length; i++) {
var $tab = $(tabs[i]);
var $checkBox = $tab.find('input[name="SelectAllInThisTab"]');
var allChecked = true;
var allUnChecked = true;
$tab.find('input[type="checkbox"]')
.not('[name="SelectAllInThisTab"]')
.each(function () {
if ($(this).is(':checked') === true) {
allUnChecked = false;
} else {
allChecked = false;
}
});
if (allChecked) {
$($checkBox).prop('checked', true);
} else if (allUnChecked) {
$($checkBox).prop('checked', false);
} else {
$($checkBox).prop('indeterminate', true);
}
}
}
function setSelectAllInAllTabs() {
var $checkBox = $('#SelectAllInAllTabs');
var anyIndeterminate = false;
var allChecked = true;
var allUnChecked = true;
$('input[name="SelectAllInThisTab"]').each(function () {
if ($(this).is(':checked') === true) {
allUnChecked = false;
} else {
allChecked = false;
}
if ($(this).is(':indeterminate') === true) {
anyIndeterminate = true;
}
});
if (anyIndeterminate) {
$($checkBox).prop('indeterminate', true);
return;
} else {
$($checkBox).prop('indeterminate', false);
}
if (allChecked) {
$($checkBox).prop('checked', true);
} else if (allUnChecked) {
$($checkBox).prop('checked', false);
} else {
$($checkBox).prop('indeterminate', true);
}
}
// "Select all" checkboxes are UI helpers, so only the granted states are compared.
function getGrantedStates($el) {
return $el.find('input[type="checkbox"]')
.not('[name="SelectAllInThisTab"], [name="SelectAllInAllTabs"]')
.map(function () {
return this.name + '=' + this.checked;
})
.get()
.join('&');
}
this.initDom = function ($el) {
var initialGrantedStates = getGrantedStates($el);
$el.on('abp-ajax-success', function () {
if (getGrantedStates($el) !== initialGrantedStates &&
affectsCurrentUser($el.find('#ProviderName').val(), $el.find('#ProviderKey').val())) {
window.location.reload();
}
});
$el.find('.tab-pane').each(function () {
var $tab = $(this);
handleTabCheckedCheckboxCount($tab);
$tab.find('input[type="checkbox"]')
.not('[name="SelectAllInThisTab"]')
.each(function () {
var $checkBox = $(this);
$checkBox.change(function () {
if ($checkBox.is(':checked')) {
checkParents($tab, $checkBox);
handleCheck($tab);
} else {
uncheckChildren($tab, $checkBox);
handleUncheck($tab);
}
setSelectAllInAllTabs();
handleTabCheckedCheckboxCount($tab);
});
});
});
$('input[name="SelectAllInThisTab"]').change(function () {
var $checkBox = $(this);
var $tab = $('#' + $checkBox.attr('data-tab-id'));
if ($checkBox.is(':checked')) {
$tab.find('input[type="checkbox"]')
.not(':disabled')
.prop('checked', true);
} else {
$tab.find('input[type="checkbox"]')
.not(':disabled')
.prop('checked', false);
}
$($checkBox).prop('indeterminate', false);
setSelectAllInAllTabs();
handleTabCheckedCheckboxCount($tab);
});
$('input[name="SelectAllInAllTabs"]').change(function () {
var $checkBox = $(this);
if ($checkBox.is(':checked')) {
$('.tab-pane input[type="checkbox"]')
.not(':disabled')
.prop('checked', true);
$('input[name="SelectAllInThisTab"]').each(function () {
var $this = $(this);
if ($this.is(':indeterminate') === true) {
$this.prop('indeterminate', false);
$this.prop('checked', true);
}
});
} else {
$('.tab-pane input[type="checkbox"]')
.not(':disabled')
.prop('checked', false);
}
$($checkBox).prop('indeterminate', false);
$el.find('.tab-pane').each(function () {
handleTabCheckedCheckboxCount($(this));
});
});
initSelectAllInThisTab();
setSelectAllInAllTabs();
var $form = $("#PermissionManagementForm");
var $submitButton = $form.find("button[type='submit']");
if ($submitButton) {
$submitButton.click(function (e) {
e.preventDefault();
if (!$form.find("input:checked").length > 0) {
abp.message.confirm(l("SaveWithoutAnyPermissionsWarningMessage"))
.then(function (confirmed) {
if (confirmed) {
$form.submit();
}
});
}
else {
$form.submit();
}
});
}
var $permissionSearchInput = $('#permission-search');
$permissionSearchInput.keyup(throttle(function () {
var searchTerm = $permissionSearchInput.val().toLowerCase();
filterTabs(searchTerm);
setFirstVisibleTabActive();
}, 300));
$('#SelectAllInAllTabs').change(function () {
// Reset search input
$permissionSearchInput.val('');
$permissionSearchInput.trigger('keyup');
});
function filterTabs(searchTerm) {
var $tabs = $('#PermissionsTabs .nav-link');
$tabs.each(function (i) {
var $tabItem = $(this);
var $tab = $tabItem.parent();
if (!searchTerm) {
$tab.show();
return;
}
var tabName = $tabItem.text().toLowerCase();
var tabContentFilters = $($tabItem.attr('href')).find('[data-filter-text]');
let includedInTabContent = false;
tabContentFilters.each(function (i) {
var permissionName = $(this).attr('data-filter-text').toLowerCase();
includedInTabContent = permissionName.includes(searchTerm);
if (includedInTabContent === true) {
return false;
}
});
if (includedInTabContent || tabName.includes(searchTerm)) {
$tab.show();
} else {
$tab.hide();
}
});
}
function setFirstVisibleTabActive() {
$('#PermissionsTabsContent .tab-pane').removeClass('active show');
var $tabs = $('#PermissionsTabs .nav-link');
var $firstVisibleTab = $tabs.filter(':visible').first();
$tabs.removeClass('active');
$firstVisibleTab.addClass('active');
var $firstVisibleTabContent = $($firstVisibleTab.attr('href'));
$firstVisibleTabContent.addClass('active show');
}
function throttle(mainFunction, delay) {
let timerFlag = null; // Variable to keep track of the timer
// Returning a throttled version
return (...args) => {
if (timerFlag === null) { // If there is no timer currently running
mainFunction(...args); // Execute the main function
timerFlag = setTimeout(() => { // Set a timer to clear the timerFlag after the specified delay
timerFlag = null; // Clear the timerFlag to allow the main function to be executed again
}, delay);
}
};
}
};
};
})(jQuery);