/** * Copyright (c) 2014, Raghu Nayyar * Copyright (c) 2014, Arthur Schiwon * This file is licensed under the Affero General Public License version 3 or later. * See the COPYING-README file. */ var $userGroupList, $sortGroupBy; var GroupList; GroupList = { activeGID: '', everyoneGID: '_everyone', addGroup: function (gid, usercount) { var $li = $userGroupList.find('.isgroup:last-child').clone(); $li .data('gid', gid) .find('.groupname').text(gid); GroupList.setUserCount($li, usercount); $li.appendTo($userGroupList); GroupList.sortGroups(); return $li; }, setUserCount: function (groupLiElement, usercount) { if ($sortGroupBy !== 1) { // If we don't sort by group count we dont display them either return; } var $groupLiElement = $(groupLiElement); if (usercount === undefined || usercount === 0 || usercount < 0) { usercount = ''; $groupLiElement.data('usercount', 0); } else { $groupLiElement.data('usercount', usercount); } $groupLiElement.find('.usercount').text(usercount); }, getUserCount: function ($groupLiElement) { return parseInt($groupLiElement.data('usercount'), 10); }, modEveryoneCount: function(diff) { var $li = GroupList.getGroupLI(GroupList.everyoneGID); var count = GroupList.getUserCount($li) + diff; GroupList.setUserCount($li, count); }, incEveryoneCount: function() { GroupList.modEveryoneCount(1); }, decEveryoneCount: function() { GroupList.modEveryoneCount(-1); }, getCurrentGID: function () { return GroupList.activeGID; }, sortGroups: function () { var lis = $userGroupList.find('.isgroup').get(); lis.sort(function (a, b) { // "Everyone" always at the top if ($(a).data('gid') === '_everyone') { return -1; } else if ($(b).data('gid') === '_everyone') { return 1; } // "admin" always as second if ($(a).data('gid') === 'admin') { return -1; } else if ($(b).data('gid') === 'admin') { return 1; } if ($sortGroupBy === 1) { // Sort by user count first var $usersGroupA = $(a).data('usercount'), $usersGroupB = $(b).data('usercount'); if ($usersGroupA > 0 && $usersGroupA > $usersGroupB) { return -1; } if ($usersGroupB > 0 && $usersGroupB > $usersGroupA) { return 1; } } // Fallback or sort by group name return UserList.alphanum( $(a).find('a span').text(), $(b).find('a span').text() ); }); var items = []; $.each(lis, function (index, li) { items.push(li); if (items.length === 100) { $userGroupList.append(items); items = []; } }); if (items.length > 0) { $userGroupList.append(items); } }, createGroup: function (groupname) { $.post( OC.generateUrl('/settings/users/groups'), { id: groupname }, function (result) { if (result.groupname) { var addedGroup = result.groupname; UserList.availableGroups = $.unique($.merge(UserList.availableGroups, [addedGroup])); GroupList.addGroup(result.groupname); $('.groupsselect, .subadminsselect') .append($('