From bcc062ecb8f455feeca73385b4198116d571e726 Mon Sep 17 00:00:00 2001 From: Loic Blot Date: Thu, 19 May 2016 22:43:27 +0200 Subject: [PATCH] Cleanup code by using selected Contact instead of a global variable for navigation --- js/public/app.js | 35 ++++++++++++----------------------- 1 file changed, 12 insertions(+), 23 deletions(-) diff --git a/js/public/app.js b/js/public/app.js index 28e0fb4..2b4c074 100644 --- a/js/public/app.js +++ b/js/public/app.js @@ -12,7 +12,6 @@ // Some global vars to improve performances var g_selectedConversation = null; var g_lastMsgDate = 0; -var g_curPhoneNumber = null; var g_unreadCountCurrentConv = 0; var g_unreadCountAllConv = 0; var g_unreadCountNotifStep = 12; @@ -101,15 +100,20 @@ app.controller('OcSmsController', ['$scope', '$interval', '$timeout', '$compile' // phoneNumber must exist if (contact.nav !== null) { - $scope.fetchConversation(contact.nav); + $scope.fetchConversation(contact); changeSelectedConversation($("a[mailbox-navigation='" + contact.nav + "']")); } }; - $scope.fetchConversation = function (phoneNumber) { + $scope.fetchConversation = function (contact) { + // If contact is not null, we will fetch a conversation for a new contact + if (contact != null) { + $scope.selectedContact = contact; + } $scope.messages = []; $.getJSON(OC.generateUrl('/apps/ocsms/get/conversation'), {'phoneNumber': phoneNumber}, function(jsondata, status) { var phoneNumberLabel = phoneNumber; + $scope.selectedContact.nav = phoneNumber; if (typeof jsondata['phoneNumbers'] != 'undefined') { phoneNumberList = arrayUnique(jsondata['phoneNumbers']); @@ -131,22 +135,13 @@ app.controller('OcSmsController', ['$scope', '$interval', '$timeout', '$compile' $scope.totalMessageCount = jsondata['msgCount'] !== undefined ? jsondata['msgCount'] : 0; $('#app-content').scrollTop(1E10); - - g_curPhoneNumber = phoneNumber; } ); }; $scope.refreshConversation = function() { - // if no conversation selected, then don't fetch page - if (g_curPhoneNumber == null) { - if ($('#app-content-header').is(':visible')) { - $('#app-content-header').hide(); - } - return; - } $.getJSON(OC.generateUrl('/apps/ocsms/get/conversation'), { - 'phoneNumber': g_curPhoneNumber, + 'phoneNumber': $scope.selectedContact.nav, "lastDate": g_lastMsgDate }, function(jsondata, status) { @@ -200,7 +195,7 @@ app.controller('OcSmsController', ['$scope', '$interval', '$timeout', '$compile' bufferedContacts.push(peerLabel); // Re-set conversation because we reload the element - if (idxVal == g_curPhoneNumber) { + if (idxVal == $scope.selectedContact.nav) { changeSelectedConversation($("a[mailbox-navigation='" + idxVal + "']")); } @@ -238,11 +233,11 @@ app.controller('OcSmsController', ['$scope', '$interval', '$timeout', '$compile' // Reinit main window $scope.selectedContact.label = ""; $scope.selectedContact.opt_numbers = ""; - $scope.removeContact({'nav': g_curPhoneNumber}); + $scope.removeContact($scope.selectedContact); $scope.$apply(function () { $scope.messages = []; }); - g_curPhoneNumber = null; + $scope.selectedContact.nav = ""; }); }; @@ -428,16 +423,10 @@ app.controller('OcSmsController', ['$scope', '$interval', '$timeout', '$compile' if (pnParam != null) { var urlPhoneNumber = decodeURIComponent(pnParam); if (urlPhoneNumber != null) { - $scope.fetchConversation(urlPhoneNumber); + $scope.fetchConversation(null); changeSelectedConversation($("a[mailbox-navigation='" + urlPhoneNumber + "']")); } } - // Don't show message headers if no conversation selected - else { - if ($('#app-content-header').is(':visible')) { - $('#app-content-header').hide(); - } - } }); $scope.fetchInitialSettings();