EnVisageOnline/Main/Source/EnVisage/Scripts/permissions.js

121 lines
4.4 KiB
JavaScript

// Permissions table root element
var areasPermissionTable = null;
// Sets by recursion or removes checks from child permissions for the specified one.
// If parent item checked, all child items set checked too.
// If parent item unchecked all child items set unchecked too.
function processChildLinks(item) {
var permissionId = $(item).val();
var currentVal = $(item).prop("checked");
var currentItemName = $(item).attr('name');
var childItems = $(areasPermissionTable).find('input[name=' + currentItemName + '][data-parent-item=' + permissionId + ']');
if (childItems.length > 0) {
$.each(childItems, function (index, childItem) {
var childVal = $(childItem).prop("checked");
if (currentVal != childVal) {
$(childItem).click();
}
});
}
}
// Sets by recursion or removes checks from parent permissions for the specified one.
// If item checked, all parent items set checked too.
// If item unchecked, and its parent doesn't have any other checked child items, parent set unchecked too.
function processParentLinks(item) {
var currentVal = $(item).prop("checked");
var parentPermissionId = $(item).attr('data-parent-item');
if (parentPermissionId && (parentPermissionId.length > 0)) {
var currentItemName = $(item).attr('name');
var parentItems = $(areasPermissionTable).find('input[name=' + currentItemName + '][value=' + parentPermissionId + ']');
if (parentItems.length > 0) {
var parentItem = parentItems.get(0);
var parentVal = $(parentItem).prop("checked");
var processFunction = null;
if (currentItemName == 'areaswrite')
processFunction = writePermissionItemClick;
if (currentItemName == 'areasread') {
processFunction = readPermissionItemClick;
}
if (currentVal != parentVal) {
if (currentVal) {
// Items was checked. We must check all parent items
processFunction(parentItem, false, false);
}
else {
// Item was unchecked. We must uncheck all parent items, if they have no checked childs
var checkedChilds = $(areasPermissionTable).find('input[name=' + currentItemName + '][data-parent-item=' + parentPermissionId + ']')
.filter(':checked');
if (checkedChilds.length < 1) {
// Items was checked. We must check all parent items
processFunction(parentItem, false, false);
}
}
processParentLinks(parentItem);
}
}
}
}
// If item was unchecked, the corresponding write permission set unchecked too.
// Param processChildren used to avoid inproper checks of all child items for the current parent item
function readPermissionItemClick(item, fromUi, processChildren) {
$(item).removeAttr("inherited");
var readVal = $(item).prop("checked");
if (!fromUi) {
readVal = !readVal;
$(item).prop("checked", readVal);
}
var permissionId = $(item).val();
var write = $(item).parent().find("input[value='" + permissionId + "'][name='areaswrite']");
if ((write.length > 0) && !readVal) {
var writeVal = $(write).prop("checked");
if (writeVal) {
writePermissionItemClick(write, false, processChildren);
}
}
if (processChildren)
processChildLinks(item);
processParentLinks(item);
}
// If item was checked, the corresponding read permission set checked too.
// Param processChildren used to avoid inproper checks of all child items for the current parent item
function writePermissionItemClick(item, fromUi, processChildren) {
$(item).removeAttr("inherited");
var writeVal = $(item).prop("checked");
if (!fromUi) {
writeVal = !writeVal;
$(item).prop("checked", writeVal);
}
var permissionId = $(item).val();
var read = $(item).parent().find("input[value='" + permissionId + "'][name='areasread']");
if ((read.length > 0) && writeVal) {
var readVal = $(read).prop("checked");
if (!readVal) {
readPermissionItemClick(read, false, processChildren);
}
}
if (processChildren)
processChildLinks(item);
processParentLinks(item);
};