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