From 74ff3b5339f43dca1dd7a83d84f30570a643cbb3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20Molakvo=C3=A6?= Date: Thu, 31 Mar 2016 05:52:35 +0200 Subject: [PATCH] First contact load fix, group load and order (fix #312) Fixed direct link to contact and invalid contact access in group (fix #318) Fixed page loading with invalid group Fix mobile merge and group contact load issue --- .../contactList/contactList_controller.js | 57 ++++++++++++------- js/filters/contactGroup_filter.js | 2 +- 2 files changed, 36 insertions(+), 23 deletions(-) diff --git a/js/components/contactList/contactList_controller.js b/js/components/contactList/contactList_controller.js index eeebab605..32eb9d5f0 100644 --- a/js/components/contactList/contactList_controller.js +++ b/js/components/contactList/contactList_controller.js @@ -67,22 +67,37 @@ angular.module('contactsApp') }); }); + // Get contacts ContactService.getAll().then(function(contacts) { $scope.$apply(function() { ctrl.contacts = contacts; - // If desktop version, load first contact (see css for min-width media query) - if (!_.isEmpty(ctrl.contacts) && $(window).width() > 768) { - ctrl.setSelectedId(_.sortBy(contacts, function(contact) { - return contact.fullName(); - })[0].uid()); + }); + }); + + // Wait for ctrl.contactList to be updated, load the first contact and kill the watch + var unbindListWatch = $scope.$watch('ctrl.contactList', function() { + if(ctrl.contactList && ctrl.contactList.length > 0) { + // Check if a specific uid is requested + if($routeParams.uid && $routeParams.gid) { + ctrl.contactList.forEach(function(contact) { + if(contact.uid() === $routeParams.uid) { + ctrl.setSelectedId($routeParams.uid); + ctrl.loading = false; + } + }); + } + // No contact previously loaded, let's load the first of the list if not in mobile mode + if(ctrl.loading && $(window).width() > 768) { + ctrl.setSelectedId(ctrl.contactList[0].uid()); } ctrl.loading = false; - }); + unbindListWatch(); + } }); $scope.$watch('ctrl.routeParams.uid', function(newValue, oldValue) { // Used for mobile view to clear the url - if(typeof oldValue != 'undefined' && typeof newValue == 'undefined') { + if(typeof oldValue != 'undefined' && typeof newValue == 'undefined' && $(window).width() <= 768) { // no contact selected ctrl.show = true; return; @@ -115,21 +130,19 @@ angular.module('contactsApp') $scope.$watch('ctrl.routeParams.gid', function() { // we might have to wait until ng-repeat filled the contactList ctrl.contactList = []; - // watch for next contactList update - var unbindWatch = $scope.$watch('ctrl.contactList', function() { - if(ctrl.contactList && ctrl.contactList.length > 0 && $(window).width() > 768) { - $route.updateParams({ - gid: $routeParams.gid, - uid: ctrl.contactList[0].uid() - }); - } else { - $route.updateParams({ - gid: $routeParams.gid, - uid: undefined - }); - } - unbindWatch(); // unbind as we only want one update - }); + // not in mobile mode + if($(window).width() > 768) { + // watch for next contactList update + var unbindWatch = $scope.$watch('ctrl.contactList', function() { + if(ctrl.contactList && ctrl.contactList.length > 0) { + $route.updateParams({ + gid: $routeParams.gid, + uid: ctrl.contactList[0].uid() + }); + } + unbindWatch(); // unbind as we only want one update + }); + } }); ctrl.createContact = function() { diff --git a/js/filters/contactGroup_filter.js b/js/filters/contactGroup_filter.js index 3ceb1ca1e..f9763369c 100644 --- a/js/filters/contactGroup_filter.js +++ b/js/filters/contactGroup_filter.js @@ -22,6 +22,6 @@ angular.module('contactsApp') } } } - return filter; + return filter.length === 0 ? contacts : filter; }; });