diff options
author | ringmaster <epithet@gmail.com> | 2014-05-05 22:41:54 -0400 |
---|---|---|
committer | Arthur Schiwon <blizzz@owncloud.com> | 2014-06-02 12:53:58 +0200 |
commit | 747c01161530edf47c4e727250f592652e66ba31 (patch) | |
tree | d48468422eb68343e1f8ba68cf022a52f0ac4732 /settings/js | |
parent | f324aa7b240f8b06485250d583b905fda9c25d03 (diff) | |
download | nextcloud-server-747c01161530edf47c4e727250f592652e66ba31.tar.gz nextcloud-server-747c01161530edf47c4e727250f592652e66ba31.zip |
More specific selectors, javascript cleanup.
* Assigned jQuery selector results to variables for speed.
* Moved all .attr('data') calls to .data() calls.
* Added :data(uid) selector.
* Split the multiselect checkhandlers into separate functions.
* De-duped click event on password change/img.
* Used fluent interface on jQuery object for event binding.
* Removed redundant tag names in class selectors.
* Fixed incorrect date display for last login.
* Added Userlist.has() to search for uid.
* General code convention compliance fixes.
to-do: Group dropdowns currently don't add the admin group after the search result, not sure why yet.
Diffstat (limited to 'settings/js')
-rw-r--r-- | settings/js/users/users.js | 522 |
1 files changed, 273 insertions, 249 deletions
diff --git a/settings/js/users/users.js b/settings/js/users/users.js index b9654e774ba..f40854cf637 100644 --- a/settings/js/users/users.js +++ b/settings/js/users/users.js @@ -6,6 +6,10 @@ * See the COPYING-README file. */ +var $userList; +var $userListBody; +var filter; + var UserList = { availableGroups: [], offset: 30, //The first 30 users are there. No prob, if less in total. @@ -15,29 +19,30 @@ var UserList = { currentGid: '', add: function (username, displayname, groups, subadmin, quota, storageLocation, lastLogin, sort) { - var tr = $('tbody tr').first().clone(); + var $tr = $userListBody.find('tr:first-child').clone(); var subadminsEl; var subadminSelect; var groupsSelect; - if (tr.find('div.avatardiv').length){ - $('div.avatardiv', tr).avatar(username, 32); + if ($tr.find('div.avatardiv').length){ + $tr.find('.avatardiv').imageplaceholder(username, displayname); + $('div.avatardiv', $tr).avatar(username, 32); } - tr.attr('data-uid', username); - tr.attr('data-displayName', displayname); - tr.find('td.name').text(username); - tr.find('td.displayName > span').text(displayname); + $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 = $('<select multiple="multiple" class="groupsselect multiselect button" data-placehoder="Groups" title="' + t('settings', 'Groups') + '"></select>') - .attr('data-username', username) + .data('username', username) .data('user-groups', groups); - if (tr.find('td.subadmins').length > 0) { + if ($tr.find('td.subadmins').length > 0) { subadminSelect = $('<select multiple="multiple" class="subadminsselect multiselect button" data-placehoder="subadmins" title="' + t('settings', 'Group Admin') + '">') - .attr('data-username', username) + .data('username', username) .data('user-groups', groups) .data('subadmin', subadmin); - tr.find('td.subadmins').empty(); + $tr.find('td.subadmins').empty(); } $.each(this.availableGroups, function (i, group) { groupsSelect.append($('<option value="' + escapeHTML(group) + '">' + escapeHTML(group) + '</option>')); @@ -45,48 +50,49 @@ var UserList = { subadminSelect.append($('<option value="' + escapeHTML(group) + '">' + escapeHTML(group) + '</option>')); } }); - tr.find('td.groups').empty().append(groupsSelect); - subadminsEl = tr.find('td.subadmins'); + $tr.find('td.groups').empty().append(groupsSelect); + subadminsEl = $tr.find('td.subadmins'); if (subadminsEl.length > 0) { subadminsEl.append(subadminSelect); } - if (tr.find('td.remove img').length === 0 && OC.currentUser !== username) { + if ($tr.find('td.remove img').length === 0 && OC.currentUser !== username) { var deleteImage = $('<img class="svg action">').attr({ src: OC.imagePath('core', 'actions/delete') }); var deleteLink = $('<a class="action delete">') .attr({ href: '#', 'original-title': t('settings', 'Delete')}) .append(deleteImage); - tr.find('td.remove').append(deleteLink); + $tr.find('td.remove').append(deleteLink); } else if (OC.currentUser === username) { - tr.find('td.remove a').remove(); + $tr.find('td.remove a').remove(); } - var quotaSelect = tr.find('select.quota-user'); + var $quotaSelect = $tr.find('.quota-user'); if (quota === 'default') { - quotaSelect.find('option').attr('selected', null); - quotaSelect.find('option').first().attr('selected', 'selected'); - quotaSelect.data('previous', 'default'); + $quotaSelect + .data('previous', 'default') + .find('option').attr('selected', null) + .first().attr('selected', 'selected'); } else { - if (quotaSelect.find('option[value="' + quota + '"]').length > 0) { - quotaSelect.find('option[value="' + quota + '"]').attr('selected', 'selected'); + if ($quotaSelect.find('option[value="' + quota + '"]').length > 0) { + $quotaSelect.find('option[value="' + quota + '"]').attr('selected', 'selected'); } else { - quotaSelect.append('<option value="' + escapeHTML(quota) + '" selected="selected">' + escapeHTML(quota) + '</option>'); + $quotaSelect.append('<option value="' + escapeHTML(quota) + '" selected="selected">' + escapeHTML(quota) + '</option>'); } } - tr.find('td.storageLocation').text(storageLocation); + $tr.find('td.storageLocation').text(storageLocation); if(lastLogin === 0) { lastLogin = t('settings', 'never'); } else { - lastLogin = new Date(lastLogin); + lastLogin = new Date(lastLogin * 1000); lastLogin = relative_modified_date(lastLogin.getTime() / 1000); } - tr.find('td.lastLogin').text(lastLogin); - $(tr).appendTo('tbody'); + $tr.find('td.lastLogin').text(lastLogin); + $tr.appendTo($userList); if(UserList.isEmpty === true) { //when the list was emptied, one row was left, necessary to keep //add working and the layout unbroken. We need to remove this item - tr.show(); - $('tbody tr').first().remove(); + $tr.show(); + $userListBody.find('tr:first').remove(); UserList.isEmpty = false; UserList.checkUsersToLoad(); } @@ -94,25 +100,25 @@ var UserList = { UserList.doSort(); } - quotaSelect.on('change', function () { - var uid = $(this).parent().parent().attr('data-uid'); + $quotaSelect.on('change', function () { + var uid = $(this).closest('tr').data('uid'); var quota = $(this).val(); setQuota(uid, quota, function(returnedQuota){ if (quota !== returnedQuota) { - $(quotaSelect).find(':selected').text(returnedQuota); + $($quotaSelect).find(':selected').text(returnedQuota); } }); }); // defer init so the user first sees the list appear more quickly window.setTimeout(function(){ - quotaSelect.singleSelect(); - UserList.applyMultiplySelect(groupsSelect); + $quotaSelect.singleSelect(); + UserList.applyGroupSelect(groupsSelect); if (subadminSelect) { - UserList.applyMultiplySelect(subadminSelect); + UserList.applySubadminSelect(subadminSelect); } }, 0); - return tr; + return $tr; }, // From http://my.opera.com/GreyWyvern/blog/show.dml/1671288 alphanum: function(a, b) { @@ -171,8 +177,7 @@ var UserList = { } }, doSort: function() { - var self = this; - var rows = $('tbody tr').get(); + var rows = $userListBody.find('tr').get(); rows.sort(function(a, b) { a = $(a).find('td.name').text(); @@ -188,12 +193,12 @@ var UserList = { $.each(rows, function(index, row) { items.push(row); if(items.length === 100) { - $('tbody').append(items); + $userListBody.append(items); items = []; } }); if(items.length > 0) { - $('tbody').append(items); + $userListBody.append(items); } }, checkUsersToLoad: function() { @@ -206,24 +211,27 @@ var UserList = { }, empty: function() { //one row needs to be kept, because it is cloned to add new rows - $('tbody tr:not(:first)').remove(); - var tr = $('tbody tr').first(); - tr.hide(); + $userListBody.find('tr:not(:first)').remove(); + var $tr = $userListBody.find('tr:first'); + $tr.hide(); //on an update a user may be missing when the username matches with that //of the hidden row. So change this to a random string. - tr.attr('data-uid', Math.random().toString(36).substring(2)); + $tr.data('uid', Math.random().toString(36).substring(2)); UserList.isEmpty = true; UserList.offset = 0; UserList.checkUsersToLoad(); }, hide: function(uid) { - $('tr[data-uid="' + uid + '"]').hide(); + $('tr:data(' + uid + ')').hide(); }, show: function(uid) { - $('tr[data-uid="' + uid + '"]').show(); + $('tr:data(' + uid + ')').show(); }, remove: function(uid) { - $('tr').filterAttr('data-uid', uid).remove(); + $('tr:data(' + uid + ')').remove(); + }, + has: function(uid) { + return $('tr:data(' + uid + ')').length > 0; }, initDeleteHandling: function() { //set up handler @@ -238,9 +246,9 @@ var UserList = { UserList.show); //when to mark user for delete - $('table').on('click', 'td.remove>a', function (event) { + $userListBody.on('click', '.delete', function () { // Call function for handling delete/undo - var uid = $(this).parent().parent().attr('data-uid'); + var uid = $(this).closest('tr').data('uid'); UserDeleteHandler.mark(uid); }); @@ -254,147 +262,153 @@ var UserList = { if (UserList.updating) { return; } - $('table+.loading').css('visibility', 'visible'); + $userList.siblings('.loading').css('visibility', 'visible'); UserList.updating = true; if(gid === undefined) { gid = ''; } UserList.currentGid = gid; - pattern = filter.getPattern(); - var query = $.param({ offset: UserList.offset, limit: UserList.usersToLoad, gid: gid, pattern: pattern }); - $.get(OC.generateUrl('/settings/ajax/userlist') + '?' + query, function (result) { - var loadedUsers = 0; - var trs = []; - if (result.status === 'success') { - //The offset does not mirror the amount of users available, - //because it is backend-dependent. For correct retrieval, - //always the limit(requested amount of users) needs to be added. - $.each(result.data, function (index, user) { - if($('tr[data-uid="' + user.name + '"]').length > 0) { - return true; + var pattern = filter.getPattern(); + $.get( + OC.generateUrl('/settings/ajax/userlist'), + { offset: UserList.offset, limit: UserList.usersToLoad, gid: gid, pattern: pattern }, + function (result) { + var loadedUsers = 0; + var trs = []; + if (result.status === 'success') { + //The offset does not mirror the amount of users available, + //because it is backend-dependent. For correct retrieval, + //always the limit(requested amount of users) needs to be added. + $.each(result.data, function (index, user) { + if($('tr:data(' + user.name + ')').length > 0) { + return true; + } + var $tr = UserList.add(user.name, user.displayname, user.groups, user.subadmin, user.quota, user.storageLocation, user.lastLogin, false); + $tr.addClass('appear transparent'); + trs.push($tr); + loadedUsers++; + }); + if (result.data.length > 0) { + UserList.doSort(); + $userList.siblings('.loading').css('visibility', 'hidden'); } - var tr = UserList.add(user.name, user.displayname, user.groups, user.subadmin, user.quota, user.storageLocation, user.lastLogin, false); - tr.addClass('appear transparent'); - trs.push(tr); - loadedUsers++; - }); - if (result.data.length > 0) { - UserList.doSort(); - $('table+.loading').css('visibility', 'hidden'); - } - else { - UserList.noMoreEntries = true; - $('table+.loading').remove(); - } - UserList.offset += loadedUsers; - // animate - setTimeout(function() { - for (var i = 0; i < trs.length; i++) { - trs[i].removeClass('transparent'); + else { + UserList.noMoreEntries = true; + $userList.siblings('.loading').remove(); } - }, 0); - } - UserList.updating = false; + UserList.offset += loadedUsers; + // animate + setTimeout(function() { + for (var i = 0; i < trs.length; i++) { + trs[i].removeClass('transparent'); + } + }, 0); + } + UserList.updating = false; }); }, - applyMultiplySelect: function (element) { + applyGroupSelect: function (element) { var checked = []; - var user = element.attr('data-username'); - if ($(element).hasClass('groupsselect')) { - if (element.data('userGroups')) { - checked = element.data('userGroups'); + var $element = $(element); + var user = $element.data('username'); + + if ($element.data('user-groups')) { + checked = $element.data('user-groups'); + } + var checkHandler = function (group) { + if (user === OC.currentUser && group === 'admin') { + return false; } - if (user) { - var checkHandeler = function (group) { - if (user === OC.currentUser && group === 'admin') { - return false; - } - if (!oc_isadmin && checked.length === 1 && checked[0] === group) { - return false; - } - $.post( - OC.filePath('settings', 'ajax', 'togglegroups.php'), - { - username: user, - group: group - }, - function (response) { - if(response.status === 'success') { - GroupList.update(); - if(UserList.availableGroups.indexOf(response.data.groupname) === -1 - && response.data.action === 'add') { - UserList.availableGroups.push(response.data.groupname); - } - } - if(response.data.message) { - OC.Notification.show(response.data.message); - } - } - ); - }; - } else { - checkHandeler = false; + if (!oc_isadmin && checked.length === 1 && checked[0] === group) { + return false; } - var addGroup = function (select, group) { - $('select[multiple]').each(function (index, element) { - if ($(element).find('option[value="' + group + '"]').length === 0 && select.data('msid') !== $(element).data('msid')) { - $(element).append('<option value="' + escapeHTML(group) + '">' + escapeHTML(group) + '</option>'); + $.post( + OC.filePath('settings', 'ajax', 'togglegroups.php'), + { + username: user, + group: group + }, + function (response) { + if (response.status === 'success') { + GroupList.update(); + if (UserList.availableGroups.indexOf(response.data.groupname) === -1 && + response.data.action === 'add' + ) { + UserList.availableGroups.push(response.data.groupname); + } } - }); - GroupList.addGroup(escapeHTML(group)); - }; - var label; - if (oc_isadmin) { - label = t('settings', 'add group'); - } else { - label = null; - } - element.multiSelect({ - createCallback: addGroup, - createText: label, - selectedFirst: true, - checked: checked, - oncheck: checkHandeler, - onuncheck: checkHandeler, - minWidth: 100 + if (response.data.message) { + OC.Notification.show(response.data.message); + } + } + ); + }; + var addGroup = function (select, group) { + $('select[multiple]').each(function (index, element) { + $element = $(element); + if ($element.find('option[value="' + group + '"]').length === 0 && select.data('msid') !== $element.data('msid')) { + $element.append('<option value="' + escapeHTML(group) + '">' + escapeHTML(group) + '</option>'); + } }); + GroupList.addGroup(escapeHTML(group)); + }; + var label; + if (oc_isadmin) { + label = t('settings', 'add group'); + } + else { + label = null; + } + $element.multiSelect({ + createCallback: addGroup, + createText: label, + selectedFirst: true, + checked: checked, + oncheck: checkHandler, + onuncheck: checkHandler, + minWidth: 100 + }); + }, + + applySubadminSelect: function (element) { + var checked = []; + var $element = $(element); + var user = $element.data('username'); + + if ($element.data('subadmin')) { + checked = $element.data('subadmin'); } - if ($(element).hasClass('subadminsselect')) { - if (element.data('subadmin')) { - checked = element.data('subadmin'); + var checkHandler = function (group) { + if (group === 'admin') { + return false; } - var checkHandeler = function (group) { - if (group === 'admin') { - return false; + $.post( + OC.filePath('settings', 'ajax', 'togglesubadmins.php'), + { + username: user, + group: group + }, + function () { } - $.post( - OC.filePath('settings', 'ajax', 'togglesubadmins.php'), - { - username: user, - group: group - }, - function () { - } - ); - }; + ); + }; - var addSubAdmin = function (group) { - $('select[multiple]').each(function (index, element) { - if ($(element).find('option[value="' + group + '"]').length === 0) { - $(element).append('<option value="' + escapeHTML(group) + '">' + escapeHTML(group) + '</option>'); - } - }); - }; - element.multiSelect({ - createCallback: addSubAdmin, - createText: null, - checked: checked, - oncheck: checkHandeler, - onuncheck: checkHandeler, - minWidth: 100 + var addSubAdmin = function (group) { + $('select[multiple]').each(function (index, element) { + if ($(element).find('option[value="' + group + '"]').length === 0) { + $(element).append('<option value="' + escapeHTML(group) + '">' + escapeHTML(group) + '</option>'); + } }); - } + }; + $element.multiSelect({ + createCallback: addSubAdmin, + createText: null, + checked: checked, + oncheck: checkHandler, + onuncheck: checkHandler, + minWidth: 100 + }); }, _onScroll: function(e) { @@ -420,112 +434,122 @@ function setQuota (uid, quota, ready) { } $(document).ready(function () { + $userList = $('#userlist'); + $userListBody = $userList.find('tbody'); + + // This adds a custom :data(value) jQuery selector, to select elements having data-uid="value": + jQuery.expr[':'].uid = function(elem, index, match) { + // if the requested value's set, compare it, otherwise return whether the element has the uid data element + return match[3] ? jQuery(elem).data('uid') === match[3] : !!jQuery(elem).data('uid'); + }; + UserList.initDeleteHandling(); // Implements User Search - filter = new UserManagementFilter( - $('#usersearchform input'), UserList, GroupList); + filter = new UserManagementFilter($('#usersearchform input'), UserList, GroupList); + filter.addResetButton($('.userFilterReset').first()); UserList.doSort(); - UserList.availableGroups = $('#content table').data('groups'); + UserList.availableGroups = $userList.data('groups'); UserList.scrollArea = $('#app-content'); UserList.scrollArea.scroll(function(e) {UserList._onScroll(e);}); - $('table').after($('<div class="loading" style="height: 200px; visibility: hidden;"></div>')); + $userList.after($('<div class="loading" style="height: 200px; visibility: hidden;"></div>')); - $('select[multiple]').each(function (index, element) { - UserList.applyMultiplySelect($(element)); + $('.groupsselect').each(function (index, element) { + UserList.applyGroupSelect(element); + }); + $('.subadminsselect').each(function (index, element) { + UserList.applySubadminSelect(element); }); - $('table').on('click', 'td.password>img', function (event) { + $userListBody.on('click', '.password', function (event) { event.stopPropagation(); - var img = $(this); - var uid = img.parent().parent().attr('data-uid'); - var input = $('<input type="password">'); - img.css('display', 'none'); - img.parent().children('span').replaceWith(input); - input.focus(); - input.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)); + + var $td = $(this).closest('td'); + var $tr = $td.closest('tr'); + var uid = $tr.data('uid'); + var $input = $('<input type="password">'); + $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(); + ); + $input.blur(); + } else { + $input.blur(); + } } - } - }); - input.blur(function () { - $(this).replaceWith($('<span>●●●●●●●</span>')); - img.css('display', ''); - }); + }) + .blur(function () { + $(this).replaceWith($('<span>●●●●●●●</span>')); + $td.find('img').show(); + }); }); - $('input:password[id="recoveryPassword"]').keyup(function(event) { + $('input:password[id="recoveryPassword"]').keyup(function() { OC.Notification.hide(); }); - $('table').on('click', 'td.password', function (event) { - $(this).children('img').click(); - }); - - $('table').on('click', 'td.displayName>img', function (event) { + $userListBody.on('click', '.displayName', function (event) { event.stopPropagation(); - var img = $(this); - var uid = img.parent().parent().attr('data-uid'); - var displayName = escapeHTML(img.parent().parent().attr('data-displayName')); - var input = $('<input type="text" value="' + displayName + '">'); - img.css('display', 'none'); - img.parent().children('span').replaceWith(input); - input.focus(); - input.keypress(function (event) { - if (event.keyCode === 13) { - if ($(this).val().length > 0) { - $.post( - OC.filePath('settings', 'ajax', 'changedisplayname.php'), - {username: uid, displayName: $(this).val()}, - function (result) { - if (result && result.status==='success'){ - img.parent().parent().find('div.avatardiv').avatar(result.data.username, 32); + var $td = $(this).closest('td'); + var $tr = $td.closest('tr'); + var uid = $tr.data('uid'); + var displayName = escapeHTML($tr.data('displayname')); + var $input = $('<input type="text" value="' + displayName + '">'); + $td.find('img').hide(); + $td.children('span').replaceWith($input); + $input + .focus() + .keypress(function (event) { + if (event.keyCode === 13) { + if ($(this).val().length > 0) { + $tr.find('.avatardiv').imageplaceholder(uid, displayName); + $.post( + OC.filePath('settings', 'ajax', 'changedisplayname.php'), + {username: uid, displayName: $(this).val()}, + function (result) { + if (result && result.status==='success'){ + $tr.find('.avatardiv').avatar(result.data.username, 32); + } } - } - ); - input.blur(); - } else { - input.blur(); + ); + $input.blur(); + } else { + $input.blur(); + } } - } - }); - input.blur(function () { - var input = $(this), - displayName = input.val(); - input.closest('tr').attr('data-displayName', displayName); - input.replaceWith('<span>' + escapeHTML(displayName) + '</span>'); - img.css('display', ''); - }); - }); - $('table').on('click', 'td.displayName', function (event) { - $(this).children('img').click(); + }) + .blur(function () { + var displayName = $input.val(); + $tr.data('displayname', displayName); + $input.replaceWith('<span>' + escapeHTML(displayName) + '</span>'); + $td.find('img').show(); + }); }); - $('select.quota, select.quota-user').singleSelect().on('change', function () { - var select = $(this); - var uid = $(this).parent().parent().attr('data-uid'); + $('#default_quota, .quota-user').singleSelect().on('change', function () { + var $select = $(this); + var uid = $select.closest('tr').data('uid'); var quota = $(this).val(); setQuota(uid, quota, function(returnedQuota){ if (quota !== returnedQuota) { - select.find(':selected').text(returnedQuota); + $select.find(':selected').text(returnedQuota); } }); }); @@ -546,7 +570,7 @@ $(document).ready(function () { t('settings', 'Error creating user')); return false; } - var groups = $('#newusergroups').prev().children('div').data('settings').checked; + var groups = $('#newusergroups').val(); $('#newuser').get(0).reset(); $.post( OC.filePath('settings', 'ajax', 'createuser.php'), @@ -576,7 +600,7 @@ $(document).ready(function () { UserList.notificationTimeout = null; }, 10000); } - if($('tr[data-uid="' + username + '"]').length === 0) { + if(!UserList.has(username)) { UserList.add(username, username, result.data.groups, null, 'default', result.data.storageLocation, 0, true); } } |