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.
|
|
|
|
*/
|
|
|
|
|
2014-05-09 01:38:29 +04:00
|
|
|
var $userGroupList;
|
|
|
|
|
|
|
|
var GroupList;
|
|
|
|
GroupList = {
|
2014-04-17 23:40:28 +04:00
|
|
|
activeGID: '',
|
2014-07-08 22:05:51 +04:00
|
|
|
everyoneGID: '_everyone',
|
2014-04-17 23:40:28 +04:00
|
|
|
|
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-02-25 18:22:20 +04:00
|
|
|
|
2014-05-09 01:38:29 +04:00
|
|
|
$li.appendTo($userGroupList);
|
2014-02-25 18:22:20 +04:00
|
|
|
|
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-02-25 18:22:20 +04:00
|
|
|
},
|
|
|
|
|
2014-05-09 01:38:29 +04:00
|
|
|
setUserCount: function (groupLiElement, usercount) {
|
|
|
|
var $groupLiElement = $(groupLiElement);
|
2014-07-08 22:05:51 +04:00
|
|
|
if (usercount === undefined || usercount === 0 || usercount < 0) {
|
2014-04-17 00:16:55 +04:00
|
|
|
usercount = '';
|
2014-08-04 17:55:28 +04:00
|
|
|
$groupLiElement.data('usercount', 0);
|
|
|
|
} else {
|
|
|
|
$groupLiElement.data('usercount', usercount);
|
2014-04-17 00:16:55 +04:00
|
|
|
}
|
2014-05-09 01:38:29 +04:00
|
|
|
$groupLiElement.find('.usercount').text(usercount);
|
2014-04-17 00:16:55 +04:00
|
|
|
},
|
|
|
|
|
2014-07-05 03:21:18 +04:00
|
|
|
getUserCount: function ($groupLiElement) {
|
|
|
|
return parseInt($groupLiElement.data('usercount'), 10);
|
|
|
|
},
|
|
|
|
|
2014-07-08 22:05:51 +04:00
|
|
|
modEveryoneCount: function(diff) {
|
2014-07-09 16:30:28 +04:00
|
|
|
var $li = GroupList.getGroupLI(GroupList.everyoneGID);
|
|
|
|
var count = GroupList.getUserCount($li) + diff;
|
2014-07-08 22:05:51 +04:00
|
|
|
GroupList.setUserCount($li, count);
|
|
|
|
},
|
|
|
|
|
|
|
|
incEveryoneCount: function() {
|
|
|
|
GroupList.modEveryoneCount(1);
|
|
|
|
},
|
|
|
|
|
|
|
|
decEveryoneCount: function() {
|
|
|
|
GroupList.modEveryoneCount(-1);
|
|
|
|
},
|
|
|
|
|
2014-05-09 01:38:29 +04:00
|
|
|
getCurrentGID: function () {
|
2014-04-17 23:40:28 +04:00
|
|
|
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-02-25 18:22:20 +04:00
|
|
|
|
2014-05-09 01:38:29 +04:00
|
|
|
lis.sort(function (a, b) {
|
|
|
|
return UserList.alphanum(
|
|
|
|
$(a).find('a span').text(),
|
|
|
|
$(b).find('a span').text()
|
|
|
|
);
|
2014-02-25 18:22:20 +04:00
|
|
|
});
|
|
|
|
|
|
|
|
var items = [];
|
2014-05-09 01:38:29 +04:00
|
|
|
$.each(lis, function (index, li) {
|
2014-02-25 18:22:20 +04:00
|
|
|
items.push(li);
|
2014-05-09 01:38:29 +04:00
|
|
|
if (items.length === 100) {
|
|
|
|
$userGroupList.append(items);
|
2014-02-25 18:22:20 +04:00
|
|
|
items = [];
|
|
|
|
}
|
|
|
|
});
|
2014-05-09 01:38:29 +04:00
|
|
|
if (items.length > 0) {
|
|
|
|
$userGroupList.append(items);
|
2014-02-25 18:22:20 +04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2014-05-09 01:38:29 +04:00
|
|
|
createGroup: function (groupname) {
|
2014-02-25 18:22:20 +04:00
|
|
|
$.post(
|
2014-12-04 16:15:55 +03:00
|
|
|
OC.generateUrl('/settings/users/groups'),
|
2014-02-25 18:22:20 +04:00
|
|
|
{
|
2014-12-04 16:15:55 +03:00
|
|
|
id: groupname
|
2014-02-25 18:22:20 +04:00
|
|
|
},
|
|
|
|
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);
|
|
|
|
|
|
|
|
$('.groupsselect, .subadminsselect')
|
|
|
|
.append($('<option>', { value: result.groupname })
|
|
|
|
.text(result.groupname));
|
2014-05-09 01:38:29 +04:00
|
|
|
}
|
2014-12-08 18:35:13 +03:00
|
|
|
GroupList.toggleAddGroup();
|
|
|
|
}).fail(function(result, textStatus, errorThrown) {
|
|
|
|
OC.dialogs.alert(result.responseJSON.message, t('settings', 'Error creating group'));
|
|
|
|
});
|
2014-02-25 18:22:20 +04: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'),
|
2014-07-09 14:19:50 +04:00
|
|
|
{
|
|
|
|
pattern: filter.getPattern(),
|
|
|
|
filterGroups: filter.filterGroups ? 1 : 0
|
|
|
|
},
|
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-06-02 19:32:47 +04:00
|
|
|
|
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;
|
|
|
|
}
|
2014-05-09 17:28:16 +04:00
|
|
|
_.defer(function () {
|
2014-06-02 19:32:47 +04:00
|
|
|
$(lis).each(function () {
|
2014-07-09 16:30:28 +04:00
|
|
|
this.removeClass('transparent');
|
2014-06-02 19:32:47 +04:00
|
|
|
});
|
2014-05-09 17:28:16 +04:00
|
|
|
});
|
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-02-25 18:41:03 +04:00
|
|
|
},
|
|
|
|
|
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-25 19:11:20 +04:00
|
|
|
},
|
|
|
|
|
2014-02-19 14:40:07 +04:00
|
|
|
showGroup: function (gid) {
|
2014-04-17 23:40:28 +04:00
|
|
|
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 () {
|
2014-02-25 18:41:03 +04:00
|
|
|
return $('#newgroup-init').is(":visible");
|
|
|
|
},
|
|
|
|
|
2014-05-09 01:38:29 +04:00
|
|
|
toggleAddGroup: function (event) {
|
|
|
|
if (GroupList.isAddGroupButtonVisible()) {
|
2014-02-25 17:26:35 +04:00
|
|
|
event.stopPropagation();
|
|
|
|
$('#newgroup-form').show();
|
|
|
|
$('#newgroup-init').hide();
|
|
|
|
$('#newgroupname').focus();
|
2014-05-09 01:38:29 +04:00
|
|
|
}
|
|
|
|
else {
|
2014-02-25 17:26:35 +04:00
|
|
|
$('#newgroup-form').hide();
|
|
|
|
$('#newgroup-init').show();
|
|
|
|
$('#newgroupname').val('');
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2014-05-09 01:38:29 +04:00
|
|
|
isGroupNameValid: function (groupname) {
|
2014-02-25 18:22:20 +04:00
|
|
|
if ($.trim(groupname) === '') {
|
|
|
|
OC.dialogs.alert(
|
2014-04-16 21:41:09 +04:00
|
|
|
t('settings', 'A valid group name must be provided'),
|
2014-02-25 18:22:20 +04:00
|
|
|
t('settings', 'Error creating group'));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
|
2014-05-09 01:38:29 +04:00
|
|
|
hide: function (gid) {
|
|
|
|
GroupList.getGroupLI(gid).hide();
|
2014-02-26 21:04:55 +04:00
|
|
|
},
|
2014-05-09 01:38:29 +04:00
|
|
|
show: function (gid) {
|
|
|
|
GroupList.getGroupLI(gid).show();
|
2014-02-26 21:04:55 +04:00
|
|
|
},
|
2014-05-09 01:38:29 +04:00
|
|
|
remove: function (gid) {
|
|
|
|
GroupList.getGroupLI(gid).remove();
|
2014-02-26 21:04:55 +04:00
|
|
|
},
|
2014-05-09 01:38:29 +04:00
|
|
|
empty: function () {
|
2014-05-09 17:28:16 +04:00
|
|
|
$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 () {
|
2014-02-26 21:04:55 +04:00
|
|
|
//set up handler
|
2014-12-04 16:15:55 +03:00
|
|
|
GroupDeleteHandler = new DeleteHandler('/settings/users/groups', 'groupname',
|
2014-05-09 01:38:29 +04:00
|
|
|
GroupList.hide, GroupList.remove);
|
2014-02-26 21:04:55 +04:00
|
|
|
|
|
|
|
//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>';
|
2014-02-26 21:04:55 +04:00
|
|
|
GroupDeleteHandler.setNotification(OC.Notification, 'deletegroup', msg,
|
2014-05-09 01:38:29 +04:00
|
|
|
GroupList.show);
|
2014-02-26 21:04:55 +04:00
|
|
|
|
|
|
|
//when to mark user for delete
|
2014-05-09 01:38:29 +04:00
|
|
|
$userGroupList.on('click', '.delete', function () {
|
2014-02-26 21:04:55 +04:00
|
|
|
// Call function for handling delete/undo
|
2014-05-09 01:38:29 +04:00
|
|
|
GroupDeleteHandler.mark(GroupList.getElementGID(this));
|
2014-02-26 21:04:55 +04:00
|
|
|
});
|
2014-02-19 11:44:18 +04:00
|
|
|
|
2014-02-26 21:04:55 +04:00
|
|
|
//delete a marked user when leaving the page
|
|
|
|
$(window).on('beforeunload', function () {
|
2014-07-17 15:13:03 +04:00
|
|
|
GroupDeleteHandler.deleteEntry();
|
2014-02-26 21:04:55 +04:00
|
|
|
});
|
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();
|
2014-08-29 19:27:23 +04: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 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');
|
2014-02-26 21:04:55 +04:00
|
|
|
GroupList.initDeleteHandling();
|
2014-08-29 19:27:23 +04:00
|
|
|
GroupList.getEveryoneCount();
|
2014-02-25 14:09:01 +04:00
|
|
|
|
|
|
|
// Display or hide of Create Group List Element
|
|
|
|
$('#newgroup-form').hide();
|
|
|
|
$('#newgroup-init').on('click', function (e) {
|
2014-02-25 17:26:35 +04:00
|
|
|
GroupList.toggleAddGroup(e);
|
2014-02-25 14:09:01 +04:00
|
|
|
});
|
|
|
|
|
2014-02-25 18:41:03 +04:00
|
|
|
$(document).on('click keydown keyup', function(event) {
|
2014-04-16 21:41:09 +04:00
|
|
|
if(!GroupList.isAddGroupButtonVisible() &&
|
|
|
|
!GroupList.elementBelongsToAddGroup(event.target) &&
|
|
|
|
!GroupList.hasAddGroupNameText()) {
|
2014-02-25 18:41:03 +04:00
|
|
|
GroupList.toggleAddGroup();
|
|
|
|
}
|
|
|
|
// Escape
|
|
|
|
if(!GroupList.isAddGroupButtonVisible() && event.keyCode && event.keyCode === 27) {
|
|
|
|
GroupList.toggleAddGroup();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2014-02-25 14:09:01 +04:00
|
|
|
// Responsible for Creating Groups.
|
|
|
|
$('#newgroup-form form').submit(function (event) {
|
2014-02-19 11:44:18 +04:00
|
|
|
event.preventDefault();
|
2014-02-25 18:22:20 +04:00
|
|
|
if(GroupList.isGroupNameValid($('#newgroupname').val())) {
|
|
|
|
GroupList.createGroup($('#newgroupname').val());
|
2014-02-19 11:44:18 +04:00
|
|
|
}
|
|
|
|
});
|
2014-02-25 17:05:48 +04:00
|
|
|
|
|
|
|
// click on group name
|
2014-05-09 01:38:29 +04:00
|
|
|
$userGroupList.on('click', '.isgroup', function () {
|
|
|
|
GroupList.showGroup(GroupList.getElementGID(this));
|
2014-02-25 17:05:48 +04:00
|
|
|
});
|
2014-03-06 11:14:42 +04:00
|
|
|
});
|