From 4323f9fca12b088f110fc8fa629b427956031947 Mon Sep 17 00:00:00 2001
From: blavenie <benoit.lavenier@e-is.pro>
Date: Tue, 9 Jan 2018 09:40:10 +0100
Subject: [PATCH] [fix] Blockchain search: infinite loop when error - fix #643
 [enh] Open link in a new tab, when running in a Browser

---
 www/js/services/device-services.js            | 10 ++++++++++
 www/js/services/http-services.js              | 20 +++++++++++++++----
 www/plugins/es/i18n/locale-en-GB.json         |  4 +++-
 www/plugins/es/i18n/locale-en.json            |  4 +++-
 www/plugins/es/i18n/locale-fr-FR.json         |  1 +
 .../js/controllers/blockchain-controllers.js  |  8 +++++++-
 .../es/js/controllers/document-controllers.js | 13 +++++++++---
 .../es/js/services/document-services.js       |  2 +-
 8 files changed, 51 insertions(+), 11 deletions(-)

diff --git a/www/js/services/device-services.js b/www/js/services/device-services.js
index 5ffe61da..455ab753 100644
--- a/www/js/services/device-services.js
+++ b/www/js/services/device-services.js
@@ -1,3 +1,4 @@
+var App;
 
 angular.module('cesium.device.services', ['cesium.utils.services', 'cesium.settings.services'])
 
@@ -202,6 +203,15 @@ angular.module('cesium.device.services', ['cesium.utils.services', 'cesium.setti
         return !!navigator.userAgent.match(/iPhone | iPad | iPod/i) || ionic.Platform.isIOS();
       };
 
+      exports.isDesktop = function() {
+        try {
+          // Has NodeJs + NW ?
+          return !!process && !!App;
+        } catch (err) {
+          return false;
+        }
+      };
+
       exports.ready = function() {
         if (started) return $q.when();
         return startPromise || exports.start();
diff --git a/www/js/services/http-services.js b/www/js/services/http-services.js
index 035d86ec..48032353 100644
--- a/www/js/services/http-services.js
+++ b/www/js/services/http-services.js
@@ -323,10 +323,22 @@ angular.module('cesium.http.services', ['cesium.cache.services'])
         return;
       }
     }
-    // Note: If device is enable, this will use InAppBrowser cordova plugin (=_system)
-    $window.open(uri,
-        (options.target || (Device.enable ? '_system' : '_blank')),
-        'location=yes');
+
+    // Note: If device enable, then target=_system will use InAppBrowser cordova plugin
+    var openTarget = (options.target || (Device.enable ? '_system' : '_blank'));
+    var openOptions;
+    // If desktop, should always open in new window (no tabs)
+    if (openTarget == '_blank' && Device.isDesktop() && $window.screen && $window.screen.width && $window.screen.height) {
+      openOptions= "width={0},height={1},location=1,menubar=1,toolbar=1,resizable=1,scrollbars=1".format($window.screen.width/2, $window.screen.height/2);
+    }
+    var win = $window.open(uri,
+      openTarget,
+      openOptions);
+    if (openOptions) {
+      win.moveTo($window.screen.width/2/2, $window.screen.height/2/2);
+      win.focus();
+    }
+
   }
 
   // Get time (UTC)
diff --git a/www/plugins/es/i18n/locale-en-GB.json b/www/plugins/es/i18n/locale-en-GB.json
index 11fa1811..64a78d0d 100644
--- a/www/plugins/es/i18n/locale-en-GB.json
+++ b/www/plugins/es/i18n/locale-en-GB.json
@@ -156,7 +156,8 @@
       "TITLE": "Message",
       "SENDER": "Sent by",
       "RECIPIENT": "Sent to",
-      "NO_CONTENT": "Empty message"
+      "NO_CONTENT": "Empty message",
+      "DELETE": "Delete the message"
     },
     "CONFIRM": {
       "REMOVE": "Are you sure you want to <b>delete this message</b>?<br/><br/> This operation is irreversible.",
@@ -416,6 +417,7 @@
       "REMOVE_ALL": "Are you sure you want to <b>delete these documents</b>?"
     },
     "ERROR": {
+      "LOAD_DOCUMENTS_FAILED": "Error searching documents",
       "REMOVE_FAILED": "Error deleting the document",
       "REMOVE_ALL_FAILED": "Error deleting documents"
     }
diff --git a/www/plugins/es/i18n/locale-en.json b/www/plugins/es/i18n/locale-en.json
index 11fa1811..64a78d0d 100644
--- a/www/plugins/es/i18n/locale-en.json
+++ b/www/plugins/es/i18n/locale-en.json
@@ -156,7 +156,8 @@
       "TITLE": "Message",
       "SENDER": "Sent by",
       "RECIPIENT": "Sent to",
-      "NO_CONTENT": "Empty message"
+      "NO_CONTENT": "Empty message",
+      "DELETE": "Delete the message"
     },
     "CONFIRM": {
       "REMOVE": "Are you sure you want to <b>delete this message</b>?<br/><br/> This operation is irreversible.",
@@ -416,6 +417,7 @@
       "REMOVE_ALL": "Are you sure you want to <b>delete these documents</b>?"
     },
     "ERROR": {
+      "LOAD_DOCUMENTS_FAILED": "Error searching documents",
       "REMOVE_FAILED": "Error deleting the document",
       "REMOVE_ALL_FAILED": "Error deleting documents"
     }
diff --git a/www/plugins/es/i18n/locale-fr-FR.json b/www/plugins/es/i18n/locale-fr-FR.json
index 30022285..932ab12e 100644
--- a/www/plugins/es/i18n/locale-fr-FR.json
+++ b/www/plugins/es/i18n/locale-fr-FR.json
@@ -468,6 +468,7 @@
       "REMOVE_ALL": "Etes-vous sûr de vouloir <b>supprimer ces documents</b> ?"
     },
     "ERROR": {
+      "LOAD_DOCUMENTS_FAILED": "Erreur lors de la recherche de documents",
       "REMOVE_FAILED": "Erreur lors de la suppression du document",
       "REMOVE_ALL_FAILED": "Erreur lors de la suppression des documents"
     }
diff --git a/www/plugins/es/js/controllers/blockchain-controllers.js b/www/plugins/es/js/controllers/blockchain-controllers.js
index 4ec8ff4f..10c675d0 100644
--- a/www/plugins/es/js/controllers/blockchain-controllers.js
+++ b/www/plugins/es/js/controllers/blockchain-controllers.js
@@ -76,6 +76,8 @@ function ESBlockLookupController($scope, $controller, $ionicPopover, $location,
 
   // This method override the base class method
   $scope.doSearch = function(from) {
+    if ($scope.search.error) return;
+
     from = angular.isDefined(from) ? from : 0;
     var promise;
     var request = {};
@@ -151,8 +153,12 @@ function ESBlockLookupController($scope, $controller, $ionicPopover, $location,
         $scope.search.loading = false;
       })
       .catch(function(err) {
-        UIUtils.onError('BLOCKCHAIN.ERROR.SEARCH_BLOCKS_FAILED')(err);
+        $scope.search.error = true;
         $scope.search.loading = false;
+        UIUtils.onError('BLOCKCHAIN.ERROR.SEARCH_BLOCKS_FAILED')(err)
+          .then(function() {
+            $scope.search.error = false;
+          });
       });
   };
 
diff --git a/www/plugins/es/js/controllers/document-controllers.js b/www/plugins/es/js/controllers/document-controllers.js
index 77f3a36e..1ea1f5be 100644
--- a/www/plugins/es/js/controllers/document-controllers.js
+++ b/www/plugins/es/js/controllers/document-controllers.js
@@ -59,6 +59,8 @@ function ESDocumentLookupController($scope, $ionicPopover, $location, $timeout,
   });
 
   $scope.load = function(size, offset) {
+    if ($scope.search.error) return;
+
     var options  = {
       index: $scope.search.index,
       type: $scope.search.type,
@@ -68,10 +70,11 @@ function ESDocumentLookupController($scope, $ionicPopover, $location, $timeout,
 
     // add sort
     if ($scope.search.sort) {
-      options.sort = $scope.search.sort + ':' + (!$scope.search.asc ? "desc" : "asc");
+      options.sort = {};
+      options.sort[$scope.search.sort] = (!$scope.search.asc ? "desc" : "asc");
     }
     else { // default sort
-      options.sort = "time:desc";
+      options.sort = {time:'desc'};
     }
 
     $scope.search.loading = true;
@@ -99,9 +102,13 @@ function ESDocumentLookupController($scope, $ionicPopover, $location, $timeout,
         $scope.$broadcast('$$rebind::rebind'); // notify binder
       })
       .catch(function(err) {
-        UIUtils.onError('DOCUMENT.ERROR.LOAD_DOCUMENTS_FAILED')(err);
         $scope.search.results = [];
         $scope.search.loading = false;
+        $scope.search.error = true;
+        UIUtils.onError('DOCUMENT.ERROR.LOAD_DOCUMENTS_FAILED')(err)
+          .then(function() {
+            $scope.search.error = false;
+          });
       });
   };
 
diff --git a/www/plugins/es/js/services/document-services.js b/www/plugins/es/js/services/document-services.js
index 6985c66d..7eb8d2f1 100644
--- a/www/plugins/es/js/services/document-services.js
+++ b/www/plugins/es/js/services/document-services.js
@@ -113,7 +113,7 @@ angular.module('cesium.es.document.services', ['ngResource', 'cesium.platform',
       var request = {
         from: options.from || 0,
         size: options.size || constants.DEFAULT_LOAD_SIZE,
-        sort: options.sort || 'time:desc',
+        sort: options.sort || {time:'desc'},
         _source: options._source || fields.commons
       };
       if (options.query) {
-- 
GitLab