From 6641bbded2b0743c0426e8cc09ad9d8e820cefe4 Mon Sep 17 00:00:00 2001 From: Francis Lachapelle Date: Thu, 6 Aug 2015 11:18:53 -0400 Subject: [PATCH] (js) Improve User.$filter function --- .../js/Common/User.service.js | 36 ++++++++----------- 1 file changed, 15 insertions(+), 21 deletions(-) diff --git a/UI/WebServerResources/js/Common/User.service.js b/UI/WebServerResources/js/Common/User.service.js index 476adaf40..2b470d57f 100644 --- a/UI/WebServerResources/js/Common/User.service.js +++ b/UI/WebServerResources/js/Common/User.service.js @@ -22,7 +22,9 @@ $q: $q, $log: $log, $$resource: new Resource(Settings.activeUser('folderURL'), Settings.activeUser()), - $gravatar: Gravatar + $gravatar: Gravatar, + $query: '', + $users: [] }); return User; @@ -49,11 +51,7 @@ User.$users = []; return User.$q.when(User.$users); } - if (angular.isUndefined(User.$users)) { - // First session query - User.$users = []; - } - else if (User.$query == search) { + if (User.$query == search) { // Query hasn't changed return User.$q.when(User.$users); } @@ -62,36 +60,31 @@ return User.$$resource.fetch(null, 'usersSearch', param).then(function(response) { var results, index, user, compareUids = function(data) { - return user.uid == data.uid; + return this.uid == data.uid; }; if (excludedUsers) { // Remove excluded users from response results = _.filter(response.users, function(data) { - return !_.find(excludedUsers, function(user) { - return user.uid == data.uid; - }); + return !_.find(excludedUsers, compareUids, user); }); } else { results = response.users; } - // Add new users matching the search query - angular.forEach(results, function(data) { - if (!_.find(User.$users, function(user) { - return user.uid == data.uid; - })) { - var user = new User(data), - index = _.sortedIndex(User.$users, user, '$$shortFormat'); - User.$users.splice(index, 0, user); - } - }); // Remove users that no longer match the search query for (index = User.$users.length - 1; index >= 0; index--) { user = User.$users[index]; - if (!_.find(results, compareUids)) { + if (!_.find(results, compareUids, user)) { User.$users.splice(index, 1); } } + // Add new users matching the search query + _.each(results, function(data, index) { + if (_.isUndefined(_.find(User.$users, compareUids, data))) { + var user = new User(data); + User.$users.splice(index, 0, user); + } + }); User.$log.debug(User.$users); return User.$users; }); @@ -172,6 +165,7 @@ /** * @function $isSpecial * @memberof User.prototype + * @desc Only accurate from the ACL editor. * @return true if the user is not a regular system user */ User.prototype.$isSpecial = function() {