From c15ef061b5b264239394cbc1f7637290d6ee0c08 Mon Sep 17 00:00:00 2001 From: Igor Zhukov Date: Thu, 13 Nov 2014 20:12:11 +0300 Subject: [PATCH] Added global search to contacts modal Closes #539 --- app/js/controllers.js | 33 +++++++++++++++++++++++- app/partials/desktop/contacts_modal.html | 5 +++- app/partials/mobile/contacts_modal.html | 6 +++-- 3 files changed, 40 insertions(+), 4 deletions(-) diff --git a/app/js/controllers.js b/app/js/controllers.js index 214d99ff..44e410f6 100644 --- a/app/js/controllers.js +++ b/app/js/controllers.js @@ -2391,12 +2391,15 @@ angular.module('myApp.controllers', ['myApp.i18n']) }) }) - .controller('ContactsModalController', function ($scope, $modal, $modalInstance, AppUsersManager, ErrorService) { + .controller('ContactsModalController', function ($scope, $timeout, $modal, $modalInstance, MtpApiManager, AppUsersManager, ErrorService) { $scope.contacts = []; + $scope.foundUsers = []; $scope.search = {}; $scope.slice = {limit: 20, limitDelta: 20}; + var jump = 0; + resetSelected(); $scope.disabledContacts = {}; @@ -2421,7 +2424,10 @@ angular.module('myApp.controllers', ['myApp.i18n']) }; function updateContacts (query) { + var curJump = ++jump; + var doneIDs = []; AppUsersManager.getContacts(query).then(function (contactsList) { + if (curJump != jump) return; $scope.contacts = []; $scope.slice.limit = 20; @@ -2431,11 +2437,36 @@ angular.module('myApp.controllers', ['myApp.i18n']) user: AppUsersManager.getUser(userID), userPhoto: AppUsersManager.getUserPhoto(userID, 'User') } + doneIDs.push(userID); $scope.contacts.push(contact); }); $scope.contactsEmpty = query ? false : !$scope.contacts.length; $scope.$broadcast('contacts_change'); }); + + if (query && query.length >= 5) { + $timeout(function() { + if (curJump != jump) return; + MtpApiManager.invokeApi('contacts.search', {q: query, limit: 10}).then(function (result) { + AppUsersManager.saveApiUsers(result.users); + if (curJump != jump) return; + angular.forEach(result.results, function(contactFound) { + var userID = contactFound.user_id; + if (doneIDs.indexOf(userID) != -1) return; + $scope.contacts.push({ + userID: userID, + user: AppUsersManager.getUser(userID), + peerString: AppUsersManager.getUserString(userID), + found: true + }); + }); + }, function (error) { + if (error.code == 400) { + error.handled = true; + } + }); + }, 500); + } }; $scope.$watch('search.query', updateContacts); diff --git a/app/partials/desktop/contacts_modal.html b/app/partials/desktop/contacts_modal.html index 443bc0b4..a297f345 100644 --- a/app/partials/desktop/contacts_modal.html +++ b/app/partials/desktop/contacts_modal.html @@ -46,7 +46,10 @@
-
+
+ + +
diff --git a/app/partials/mobile/contacts_modal.html b/app/partials/mobile/contacts_modal.html index f9240b5a..4bd2c358 100644 --- a/app/partials/mobile/contacts_modal.html +++ b/app/partials/mobile/contacts_modal.html @@ -81,8 +81,10 @@
-
- +
+ + +