nextcloud/settings/js/users/groups.js

356 lines
8.8 KiB
JavaScript
Raw Normal View History

2014-02-19 11:44:18 +04:00
/**
* Copyright (c) 2014, Raghu Nayyar <beingminimal@gmail.com>
2014-03-06 11:05:44 +04:00
* Copyright (c) 2014, Arthur Schiwon <blizzz@owncloud.com>
2014-02-19 11:44:18 +04:00
* This file is licensed under the Affero General Public License version 3 or later.
* See the COPYING-README file.
*/
var $userGroupList,
$sortGroupBy;
2014-05-09 01:38:29 +04:00
var GroupList;
GroupList = {
activeGID: '',
everyoneGID: '_everyone',
filter: '',
filterGroups: false,
2014-05-09 01:38:29 +04:00
addGroup: function (gid, usercount) {
var $li = $userGroupList.find('.isgroup:last-child').clone();
$li
.data('gid', gid)
.find('.groupname').text(gid);
GroupList.setUserCount($li, usercount);
2014-05-09 01:38:29 +04:00
$li.appendTo($userGroupList);
2014-05-09 01:38:29 +04:00
GroupList.sortGroups();
2014-04-15 02:13:23 +04:00
2014-05-09 01:38:29 +04:00
return $li;
},
2014-05-09 01:38:29 +04:00
setUserCount: function (groupLiElement, usercount) {
if ($sortGroupBy !== 1) {
// If we don't sort by group count we don't display them either
return;
}
2014-05-09 01:38:29 +04:00
var $groupLiElement = $(groupLiElement);
if (usercount === undefined || usercount === 0 || usercount < 0) {
usercount = '';
$groupLiElement.data('usercount', 0);
} else {
$groupLiElement.data('usercount', usercount);
}
2014-05-09 01:38:29 +04:00
$groupLiElement.find('.usercount').text(usercount);
},
getUserCount: function ($groupLiElement) {
return parseInt($groupLiElement.data('usercount'), 10);
},
modGroupCount: function(gid, diff) {
var $li = GroupList.getGroupLI(gid);
2014-07-09 16:30:28 +04:00
var count = GroupList.getUserCount($li) + diff;
GroupList.setUserCount($li, count);
},
incEveryoneCount: function() {
GroupList.modGroupCount(GroupList.everyoneGID, 1);
},
decEveryoneCount: function() {
GroupList.modGroupCount(GroupList.everyoneGID, -1);
},
incGroupCount: function(gid) {
GroupList.modGroupCount(gid, 1);
},
decGroupCount: function(gid) {
GroupList.modGroupCount(gid, -1);
},
2014-05-09 01:38:29 +04:00
getCurrentGID: function () {
return GroupList.activeGID;
},
2014-05-09 01:38:29 +04:00
sortGroups: function () {
2014-07-08 22:12:10 +04:00
var lis = $userGroupList.find('.isgroup').get();
2014-05-09 01:38:29 +04:00
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
2014-05-09 01:38:29 +04:00
return UserList.alphanum(
$(a).find('a span').text(),
$(b).find('a span').text()
);
});
var items = [];
2014-05-09 01:38:29 +04:00
$.each(lis, function (index, li) {
items.push(li);
2014-05-09 01:38:29 +04:00
if (items.length === 100) {
$userGroupList.append(items);
items = [];
}
});
2014-05-09 01:38:29 +04:00
if (items.length > 0) {
$userGroupList.append(items);
}
},
2014-05-09 01:38:29 +04:00
createGroup: function (groupname) {
$.post(
OC.generateUrl('/settings/users/groups'),
{
id: groupname
},
function (result) {
2014-12-08 18:35:13 +03:00
if (result.groupname) {
var addedGroup = result.groupname;
UserList.availableGroups = $.unique($.merge(UserList.availableGroups, [addedGroup]));
GroupList.addGroup(result.groupname);
2014-05-09 01:38:29 +04:00
}
2014-12-08 18:35:13 +03:00
GroupList.toggleAddGroup();
}).fail(function(result) {
OC.Notification.showTemporary(t('settings', 'Error creating group: {message}', {message: result.responseJSON.message}));
2014-12-08 18:35:13 +03:00
});
},
2014-02-19 11:44:18 +04:00
2014-05-09 01:38:29 +04:00
update: function () {
2014-04-15 02:13:23 +04:00
if (GroupList.updating) {
return;
}
GroupList.updating = true;
2014-05-09 01:38:29 +04:00
$.get(
2014-12-08 17:32:53 +03:00
OC.generateUrl('/settings/users/groups'),
{
pattern: this.filter,
filterGroups: this.filterGroups ? 1 : 0,
sortGroups: $sortGroupBy
},
2014-05-09 01:38:29 +04:00
function (result) {
var lis = [];
if (result.status === 'success') {
$.each(result.data, function (i, subset) {
$.each(subset, function (index, group) {
if (GroupList.getGroupLI(group.name).length > 0) {
GroupList.setUserCount(GroupList.getGroupLI(group.name).first(), group.usercount);
}
else {
var $li = GroupList.addGroup(group.name, group.usercount);
2014-05-09 01:38:29 +04:00
$li.addClass('appear transparent');
lis.push($li);
}
});
2014-04-15 02:13:23 +04:00
});
2014-05-09 01:38:29 +04:00
if (result.data.length > 0) {
GroupList.doSort();
2014-04-15 02:13:23 +04:00
}
2014-05-09 01:38:29 +04:00
else {
GroupList.noMoreEntries = true;
}
_.defer(function () {
$(lis).each(function () {
2014-07-09 16:30:28 +04:00
this.removeClass('transparent');
});
});
2014-05-09 01:38:29 +04:00
}
GroupList.updating = false;
2014-04-15 02:13:23 +04:00
}
2014-05-09 01:38:29 +04:00
);
2014-04-15 02:13:23 +04:00
},
2014-05-09 01:38:29 +04:00
elementBelongsToAddGroup: function (el) {
2014-04-16 21:41:09 +04:00
return !(el !== $('#newgroup-form').get(0) &&
2014-05-09 01:38:29 +04:00
$('#newgroup-form').find($(el)).length === 0);
},
2014-05-09 01:38:29 +04:00
hasAddGroupNameText: function () {
2014-04-16 21:41:09 +04:00
var name = $('#newgroupname').val();
2014-05-09 01:38:29 +04:00
return $.trim(name) !== '';
},
2014-02-19 14:40:07 +04:00
showGroup: function (gid) {
GroupList.activeGID = gid;
2014-02-19 14:40:07 +04:00
UserList.empty();
UserList.update(gid);
2014-05-09 01:38:29 +04:00
$userGroupList.find('li').removeClass('active');
if (gid !== undefined) {
2014-02-20 22:11:52 +04:00
//TODO: treat Everyone properly
2014-05-09 01:38:29 +04:00
GroupList.getGroupLI(gid).addClass('active');
2014-02-20 22:11:52 +04:00
}
2014-02-19 14:40:07 +04:00
},
2014-05-09 01:38:29 +04:00
isAddGroupButtonVisible: function () {
return $('#newgroup-init').is(":visible");
},
2014-05-09 01:38:29 +04:00
toggleAddGroup: function (event) {
if (GroupList.isAddGroupButtonVisible()) {
event.stopPropagation();
$('#newgroup-form').show();
$('#newgroup-init').hide();
$('#newgroupname').focus();
GroupList.handleAddGroupInput('');
2014-05-09 01:38:29 +04:00
}
else {
$('#newgroup-form').hide();
$('#newgroup-init').show();
$('#newgroupname').val('');
}
},
handleAddGroupInput: function (input) {
if(input.length) {
$('#newgroup-form input[type="submit"]').attr('disabled', null);
} else {
$('#newgroup-form input[type="submit"]').attr('disabled', 'disabled');
}
},
2014-05-09 01:38:29 +04:00
isGroupNameValid: function (groupname) {
if ($.trim(groupname) === '') {
OC.Notification.showTemporary(t('settings', 'Error creating group: {message}', {
message: t('settings', 'A valid group name must be provided')
}));
return false;
}
return true;
},
2014-05-09 01:38:29 +04:00
hide: function (gid) {
GroupList.getGroupLI(gid).hide();
},
2014-05-09 01:38:29 +04:00
show: function (gid) {
GroupList.getGroupLI(gid).show();
},
2014-05-09 01:38:29 +04:00
remove: function (gid) {
GroupList.getGroupLI(gid).remove();
},
2014-05-09 01:38:29 +04:00
empty: function () {
$userGroupList.find('.isgroup').filter(function(index, item){
return $(item).data('gid') !== '';
2014-05-09 01:38:29 +04:00
}).remove();
2014-04-15 02:13:23 +04:00
},
2014-05-09 01:38:29 +04:00
initDeleteHandling: function () {
//set up handler
GroupDeleteHandler = new DeleteHandler('/settings/users/groups', 'groupname',
2014-05-09 01:38:29 +04:00
GroupList.hide, GroupList.remove);
//configure undo
OC.Notification.hide();
2014-06-05 22:30:20 +04:00
var msg = escapeHTML(t('settings', 'deleted {groupName}', {groupName: '%oid'})) + '<span class="undo">' +
escapeHTML(t('settings', 'undo')) + '</span>';
GroupDeleteHandler.setNotification(OC.Notification, 'deletegroup', msg,
2014-05-09 01:38:29 +04:00
GroupList.show);
//when to mark user for delete
2014-05-09 01:38:29 +04:00
$userGroupList.on('click', '.delete', function () {
// Call function for handling delete/undo
2014-05-09 01:38:29 +04:00
GroupDeleteHandler.mark(GroupList.getElementGID(this));
});
2014-02-19 11:44:18 +04:00
//delete a marked user when leaving the page
$(window).on('beforeunload', function () {
GroupDeleteHandler.deleteEntry();
});
2014-05-09 01:38:29 +04:00
},
getGroupLI: function (gid) {
return $userGroupList.find('li.isgroup').filter(function () {
return GroupList.getElementGID(this) === gid;
});
},
getElementGID: function (element) {
return ($(element).closest('li').data('gid') || '').toString();
},
getEveryoneCount: function () {
$.ajax({
type: "GET",
dataType: "json",
url: OC.generateUrl('/settings/users/stats')
}).success(function (data) {
$('#everyonegroup').data('usercount', data.totalUsers);
$('#everyonecount').text(data.totalUsers);
});
2014-04-16 21:41:09 +04:00
}
};
2014-02-19 11:44:18 +04:00
$(document).ready( function () {
2014-05-09 01:38:29 +04:00
$userGroupList = $('#usergrouplist');
GroupList.initDeleteHandling();
$sortGroupBy = $userGroupList.data('sort-groups');
if ($sortGroupBy === 1) {
// Disabled due to performance issues, when we don't need it for sorting
GroupList.getEveryoneCount();
}
// Display or hide of Create Group List Element
$('#newgroup-form').hide();
$('#newgroup-init').on('click', function (e) {
GroupList.toggleAddGroup(e);
});
$(document).on('click keydown keyup', function(event) {
2014-04-16 21:41:09 +04:00
if(!GroupList.isAddGroupButtonVisible() &&
!GroupList.elementBelongsToAddGroup(event.target) &&
!GroupList.hasAddGroupNameText()) {
GroupList.toggleAddGroup();
}
// Escape
if(!GroupList.isAddGroupButtonVisible() && event.keyCode && event.keyCode === 27) {
GroupList.toggleAddGroup();
}
});
// Responsible for Creating Groups.
$('#newgroup-form form').submit(function (event) {
2014-02-19 11:44:18 +04:00
event.preventDefault();
if(GroupList.isGroupNameValid($('#newgroupname').val())) {
GroupList.createGroup($('#newgroupname').val());
2014-02-19 11:44:18 +04:00
}
});
// click on group name
2014-05-09 01:38:29 +04:00
$userGroupList.on('click', '.isgroup', function () {
GroupList.showGroup(GroupList.getElementGID(this));
});
$('#newgroupname').on('input', function(){
GroupList.handleAddGroupInput(this.value);
});
});