nextcloud/settings/js/users/groups.js

304 lines
7.4 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.
*/
2014-05-09 01:38:29 +04:00
var $userGroupList;
var GroupList;
GroupList = {
activeGID: '',
everyoneGID: '_everyone',
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) {
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);
},
modEveryoneCount: function(diff) {
2014-07-09 16:30:28 +04:00
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);
},
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) {
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.filePath('settings', 'ajax', 'creategroup.php'),
{
2014-05-09 01:38:29 +04:00
groupname: groupname
},
function (result) {
if (result.status !== 'success') {
OC.dialogs.alert(result.data.message,
t('settings', 'Error creating group'));
2014-05-09 01:38:29 +04:00
}
else {
if (result.data.groupname) {
2014-05-09 01:38:29 +04:00
var addedGroup = result.data.groupname;
UserList.availableGroups = $.unique($.merge(UserList.availableGroups, [addedGroup]));
GroupList.addGroup(result.data.groupname);
2014-05-09 01:38:29 +04:00
$('.groupsselect, .subadminsselect')
.append($('<option>', { value: result.data.groupname })
.text(result.data.groupname));
}
GroupList.toggleAddGroup();
}
}
2014-04-16 21:41:09 +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(
OC.generateUrl('/settings/ajax/grouplist'),
{
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-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();
2014-05-09 01:38:29 +04:00
}
else {
$('#newgroup-form').hide();
$('#newgroup-init').show();
$('#newgroupname').val('');
}
},
2014-05-09 01:38:29 +04:00
isGroupNameValid: function (groupname) {
if ($.trim(groupname) === '') {
OC.dialogs.alert(
2014-04-16 21:41:09 +04:00
t('settings', 'A valid group name must be provided'),
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-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('removegroup.php', '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/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');
GroupList.initDeleteHandling();
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));
});
});