/** * Copyright (c) 2014, Arthur Schiwon * Copyright (c) 2014, Raghu Nayyar * Copyright (c) 2011, Robin Appelman * This file is licensed under the Affero General Public License version 3 or later. * See the COPYING-README file. */ var $userList; var $userListBody; var filter; var UserList = { availableGroups: [], offset: 0, usersToLoad: 10, //So many users will be loaded when user scrolls down currentGid: '', /** * Initializes the user list * @param $el user list table element */ initialize: function($el) { this.$el = $el; // initially the list might already contain user entries (not fully ajaxified yet) // initialize these entries this.$el.find('.quota-user').singleSelect().on('change', this.onQuotaSelect); }, add: function (username, displayname, groups, subadmin, quota, storageLocation, lastLogin, sort) { var $tr = $userListBody.find('tr:first-child').clone(); // this removes just the `display:none` of the template row $tr.removeAttr('style'); var subAdminsEl; var subAdminSelect; var groupsSelect; /** * Avatar or placeholder */ if ($tr.find('div.avatardiv').length){ $tr.find('.avatardiv').imageplaceholder(username, displayname); $('div.avatardiv', $tr).avatar(username, 32); } /** * add username and displayname to row (in data and visible markup */ $tr.data('uid', username); $tr.data('displayname', displayname); $tr.find('td.name').text(username); $tr.find('td.displayName > span').text(displayname); // make them look like the multiselect buttons // until they get time to really get initialized groupsSelect = $('') .data('username', username) .data('user-groups', groups); if ($tr.find('td.subadmins').length > 0) { subAdminSelect = $(''); $td.find('img').hide(); $td.children('span').replaceWith($input); $input .focus() .keypress(function (event) { if (event.keyCode === 13) { if ($(this).val().length > 0) { var recoveryPasswordVal = $('input:password[id="recoveryPassword"]').val(); $.post( OC.generateUrl('/settings/users/changepassword'), {username: uid, password: $(this).val(), recoveryPassword: recoveryPasswordVal}, function (result) { if (result.status != 'success') { OC.Notification.show(t('admin', result.data.message)); } } ); $input.blur(); } else { $input.blur(); } } }) .blur(function () { $(this).replaceWith($('●●●●●●●')); $td.find('img').show(); }); }); $('input:password[id="recoveryPassword"]').keyup(function() { OC.Notification.hide(); }); $userListBody.on('click', '.displayName', function (event) { event.stopPropagation(); var $td = $(this).closest('td'); var $tr = $td.closest('tr'); var uid = UserList.getUID($td); var displayName = escapeHTML(UserList.getDisplayName($td)); var $input = $(''); $td.find('img').hide(); $td.children('span').replaceWith($input); $input .focus() .keypress(function (event) { if (event.keyCode === 13) { if ($(this).val().length > 0) { var $div = $tr.find('div.avatardiv'); if ($div.length) { $div.imageplaceholder(uid, displayName); } $.post( OC.filePath('settings', 'ajax', 'changedisplayname.php'), {username: uid, displayName: $(this).val()}, function (result) { if (result && result.status==='success' && $div.length){ $div.avatar(result.data.username, 32); } } ); $input.blur(); } else { $input.blur(); } } }) .blur(function () { var displayName = $input.val(); $tr.data('displayname', displayName); $input.replaceWith('' + escapeHTML(displayName) + ''); $td.find('img').show(); }); }); // init the quota field select box after it is shown the first time $('#app-settings').one('show', function() { $(this).find('#default_quota').singleSelect().on('change', UserList.onQuotaSelect); }); $('#newuser').submit(function (event) { event.preventDefault(); var username = $('#newusername').val(); var password = $('#newuserpassword').val(); if ($.trim(username) === '') { OC.dialogs.alert( t('settings', 'A valid username must be provided'), t('settings', 'Error creating user')); return false; } if ($.trim(password) === '') { OC.dialogs.alert( t('settings', 'A valid password must be provided'), t('settings', 'Error creating user')); return false; } var groups = $('#newusergroups').val(); $('#newuser').get(0).reset(); $.post( OC.generateUrl('/settings/users/users'), { username: username, password: password, groups: groups }, function (result) { if (result.groups) { for (var i in result.groups) { var gid = result.groups[i]; if(UserList.availableGroups.indexOf(gid) === -1) { UserList.availableGroups.push(gid); } $li = GroupList.getGroupLI(gid); userCount = GroupList.getUserCount($li); GroupList.setUserCount($li, userCount + 1); } } if (result.homeExists){ OC.Notification.hide(); OC.Notification.show(t('settings', 'Warning: Home directory for user "{user}" already exists', {user: result.username})); if (UserList.notificationTimeout){ window.clearTimeout(UserList.notificationTimeout); } UserList.notificationTimeout = window.setTimeout( function(){ OC.Notification.hide(); UserList.notificationTimeout = null; }, 10000); } if(!UserList.has(username)) { UserList.add(username, username, result.groups, null, 'default', result.storageLocation, 0, true); } $('#newusername').focus(); GroupList.incEveryoneCount(); }).fail(function(result, textStatus, errorThrown) { OC.dialogs.alert(result.responseJSON.message, t('settings', 'Error creating user')); }); }); // Option to display/hide the "Storage location" column $('#CheckboxStorageLocation').click(function() { if ($('#CheckboxStorageLocation').is(':checked')) { $("#headerStorageLocation").show(); $("#userlist td.storageLocation").show(); } else { $("#headerStorageLocation").hide(); $("#userlist td.storageLocation").hide(); } }); // Option to display/hide the "Last Login" column $('#CheckboxLastLogin').click(function() { if ($('#CheckboxLastLogin').is(':checked')) { $("#headerLastLogin").show(); $("#userlist td.lastLogin").show(); } else { $("#headerLastLogin").hide(); $("#userlist td.lastLogin").hide(); } }); // trigger loading of users on startup UserList.update(UserList.currentGid); });