Files
2023-04-10 12:42:23 +03:00

210 lines
6.4 KiB
JavaScript

jQuery(document).ready(function ($) {
var changes = {};
$('body').on('click', '#add-role', function (e) {
e.preventDefault();
var end_point = wl_rm.ajax_url;
var val = $('#add-role-name').val();
$.post(end_point, {'action': 'add_role', 'new_role': val, security: wl_rm.security}, function (response) {
var response = JSON.parse(response);
if (response.result == 'success') {
alert(response.message);
$('#add-role-name').val('');
laod_data();
} else {
alert(response.message);
}
}).fail(function (response) {
alert(wl_rm.error_refresh);
});
});
$('body').on('click', '#remove-role', function (e) {
e.preventDefault();
var end_point = wl_rm.ajax_url;
var val = $('#remove-role-name').val();
$.post(end_point, {'action': 'remove_role', 'remove_role': val, security: wl_rm.security}, function (response) {
var response = JSON.parse(response);
if (response.result == 'success') {
alert(response.message);
laod_data();
} else {
alert(response.message);
}
}).fail(function (response) {
alert(wl_rm.error_refresh);
});
});
$('body').on('click', '#add-capability', function (e) {
e.preventDefault();
var end_point = wl_rm.ajax_url;
var val = $('#add-capability-name').val();
$.post(end_point, {'action': 'add_capability', 'add_capability': val, security: wl_rm.security}, function (response) {
var response = JSON.parse(response);
if (response.result == 'success') {
alert(response.message);
laod_data();
} else {
alert(response.message);
}
}).fail(function (response) {
alert(wl_rm.error_refresh);
});
});
$('body').on('click', '#remove-capability', function (e) {
e.preventDefault();
var end_point = wl_rm.ajax_url;
var val = $('#remove-capability-name').val();
$.post(end_point, {'action': 'remove_capability', 'remove_capability': val, security: wl_rm.security}, function (response) {
var response = JSON.parse(response);
if (response.result == 'success') {
alert(response.message);
laod_data();
} else {
alert(response.message);
}
}).fail(function (response) {
alert(wl_rm.error_refresh);
});
});
$('body').on('click', '#submit', function (e) {
e.preventDefault();
var end_point = wl_rm.ajax_url;
var val = JSON.stringify(changes);
console.log(val);
input = {'action': 'save_capability', 'data': changes, security: wl_rm.security};
console.log(input);
$.post(end_point, input, function (response) {
var response = JSON.parse(response);
if (response.result == 'success') {
console.log(response.message);
alert(response.message);
} else {
alert(response.message);
}
}).fail(function (response) {
console.log(response);
alert(wl_rm.error_refresh);
});
});
$('body').on('change', 'input[type="checkbox"]', function (e) {
role = $(this).attr('data-role');
capability = $(this).attr('data-capability');
if (!(role in changes)) {
changes[role] = {};
}
if (capability in changes[role]) {
delete changes[role][capability];
} else {
if ($(this).is(':checked')) {
changes[role][capability] = 1;
} else {
changes[role][capability] = 0;
}
}
if (Object.keys(changes[role]).length == 0) {
delete changes[role];
}
console.log(JSON.stringify(changes));
});
function load_combo_roles() {
comboRoles = '<option value="0"> -------- </option>';
var end_point = wl_rm.ajax_url;
$.post(end_point, {'action': 'get_editable_roles', security: wl_rm.security}, function (response) {
var response = JSON.parse(response);
if (response.result == 'success') {
roles = response.data;
for (var key in roles) {
comboRoles = comboRoles + '<option value="' + key + '">' + roles[key] + '</option>';
}
$('#remove-role-name').html(comboRoles);
} else {
alert(response.message);
}
}).fail(function (response) {
alert(wl_rm.error_refresh);
});
}
function load_combo_capabilities() {
comboCapabilities = '<option value="0"> -------- </option>';
var end_point = wl_rm.ajax_url;
$.post(end_point, {'action': 'get_capabilities', security: wl_rm.security}, function (response) {
var response = JSON.parse(response);
if (response.result == 'success') {
capabilities = response.data;
for (var key in capabilities) {
comboCapabilities = comboCapabilities + '<option value="' + key + '">' + key + '</option>';
}
$('#remove-capability-name').html(comboCapabilities);
} else {
alert(response.message);
}
}).fail(function (response) {
alert(wl_rm.error_refresh);
});
}
function load_role_tabs() {
navContent = '';
tabContent = '';
var end_point = wl_rm.ajax_url;
$.post(end_point, {'action': 'get_roles', security: wl_rm.security}, function (response) {
var response = JSON.parse(response);
if (response.result == 'success') {
allCap = response.data.administrator.capabilities || {};
delete response.data.administrator;
roles = response.data;
capability = '';
i = 1;
for (var role in roles) {
hidden = (i == 1) ? "" : "hidden";
checked = (i == 1) ? 'checked = "checked"' : "";
active = (i == 1) ? "nav-tab-active" : "";
navContent += '<a href="javascript: void(0)" onclick="showTab(this, ' + i + ')" class="nav-tab ' + active + '">' + roles[role].name + '</a>';
tabContent += '<input id="tab-' + i + '" type="radio" name="roles" ' + checked + ' >' +
'<label for="tab-' + i + '">' + roles[role].name + '</label>' +
'<div class ="tab">';
for (var key in allCap) {
checked = roles[role].capabilities[key] ? ' checked="checked"' : '';
capability = '<span class="inb">' +
'<label for="' + role + '-' + key + '" >' +
'<input id="' + role + '-' + key + '" type="checkbox" name="' + role + '-' + key + '"' + checked + ' data-role="' + role + '" data-capability="' + key + '" ' + ' role="switch" >' +
key +
'</label>' +
'</span>';
tabContent += capability;
}
tabContent += '</div>';
i++;
}
$('#tab_wrapper').html(tabContent);
} else {
alert(response.message);
}
}).fail(function (response) {
alert(wl_rm.error_refresh);
});
}
function laod_data() {
load_combo_roles();
load_combo_capabilities();
load_role_tabs();
}
laod_data();
});