[enh] Add some comments

This commit is contained in:
ljf 2019-10-20 02:01:42 +02:00
parent fae6c974d1
commit 169e592cbc

View file

@ -10,7 +10,20 @@
*
*/
/**
* Update group or permissions
*
* @model data organize in the same way than /users/groups?full&include_primary_groups
* @params.operation "add"|"remove"
* @params.type "members"|"permissions"
* @param.item Name of the user or the permission to add or remove
* @param.group Name of the group affected
*
* This function is built to be apply with params generated by the use of
* HTML dataset attributes (e.g. link in the partial inline view "label" in group_list.ms)
*
* @return void
**/
function updateGroup(model, params) {
var type = params.type;
var operation = params.operation;
@ -44,7 +57,16 @@
updateView(data);
}, 'PUT', params);
}
/**
* Update the view with the new model
*
* @model data organize in the same way than /users/groups?full&include_primary_groups
*
* @return void
**/
function updateView(model) {
// Sort in aphanumerical order to improve user experience
for (var group in model.groups) {
model.groups[group].permissions.sort();
model.groups[group].permissionsInv.sort();
@ -52,8 +74,11 @@
model.groups[group].membersInv.sort();
}
// Manual render, we don't use c.view to avoid scrollTop and other
// uneeded behaviour
var rendered = c.render('views/user/group_list.ms', model);
rendered.swap(function () {
// Add click event to get a nice "reactive" interface
jQuery(".group-update").on('click', function (e) {
updateGroup(model, jQuery(this)[0].dataset);
return false;
@ -65,6 +90,8 @@
});
});
}
app.get('#/groups', function (c) {
c.api('/users/groups?full&include_primary_groups', function(data_groups) {
c.api('/users', function(data_users) {
@ -73,6 +100,8 @@
var specific_perms = {};
var all_perms = data_permissions.permissions;
var users = Object.keys(data_users.users);
// Enrich groups data with primary group indicator and inversed items list
for (var group in data_groups.groups) {
data_groups.groups[group].primary = users.indexOf(group) !== -1;
data_groups.groups[group].permissionsInv = all_perms.filter(function(item) {
@ -82,11 +111,17 @@
return data_groups.groups[group].members.indexOf(item) === -1;
});
}
// Declare all_users and visitors has special
data_groups.groups['all_users'].special = true;
data_groups.groups['visitors'].special = true;
// Data given to the view with 2 functions to display permission in
// a better way
data = {
'groups':data_groups.groups,
'displayPermission': function (text) {
// Display a permission correctly for a human
text = text.replace('.main', '');
if (text.indexOf('.') > -1)
text = text.replace('.', ' (') + ')';