From 4c01f3961839bd70c9e6a84f3069023d63a55f41 Mon Sep 17 00:00:00 2001
From: ArnaudCerisier <arnaud.cerisier@gmail.com>
Date: Mon, 29 May 2017 17:45:02 +0200
Subject: [PATCH] [fix]  50/5000 Adding Query Management to the Network Tab

---
 www/js/controllers/blockchain-controllers.js  |  2 +-
 www/plugins/es/i18n/locale-en-GB.json         | 12 ++--
 www/plugins/es/i18n/locale-en.json            | 12 ++--
 www/plugins/es/i18n/locale-es-ES.json         | 12 ++--
 www/plugins/es/i18n/locale-fr-FR.json         |  2 +
 .../js/controllers/blockchain-controllers.js  | 71 +++++++++----------
 .../es/js/services/blockchain-services.js     | 37 ++++++++++
 .../es/templates/blockchain/lookup_form.html  | 20 +++++-
 8 files changed, 112 insertions(+), 56 deletions(-)

diff --git a/www/js/controllers/blockchain-controllers.js b/www/js/controllers/blockchain-controllers.js
index e64455399..72966d049 100644
--- a/www/js/controllers/blockchain-controllers.js
+++ b/www/js/controllers/blockchain-controllers.js
@@ -92,7 +92,7 @@ function BlockLookupController($scope, $timeout, $focus, $filter, $state, $ancho
     if (!$scope.entered) {
       if (state && state.stateParams && state.stateParams.q) { // Query parameter
         $scope.search.text = state.stateParams.q;
-        if ($scope.search.text && $scope.search.text .trim().length) {
+        if ($scope.search.text && $scope.search.text.trim().length) {
           $scope.search.type='text';
         }
       }
diff --git a/www/plugins/es/i18n/locale-en-GB.json b/www/plugins/es/i18n/locale-en-GB.json
index 7281905c7..4619d1f3d 100644
--- a/www/plugins/es/i18n/locale-en-GB.json
+++ b/www/plugins/es/i18n/locale-en-GB.json
@@ -97,10 +97,6 @@
       "BAD_INVITATION_FORMAT": "<span class=\"assertive\"><i class=\"ion-close-circled\"></i> Invitation unreadable (format unknown)</span> - sent by <a href=\"#/app/wot/{{pubkey}}/{{::uid}}\">{{::name||uid||pubkey}}</a>"
     }
   },
-  "TX_SEARCH": {
-    "PERIOD": "Transactions between the {{date.startDate|formatDate}} and the {{date.endDate|formatDate}}",
-    "PUBKEY": "Public key : {{pubkey|formatPubkey}}"
-  },
   "COMMENTS": {
     "DIVIDER": "Comments",
     "DIVIDER_WITH_TOTAL": "Comments ({{total}})",
@@ -189,7 +185,13 @@
       "HEADER_MEDIAN_TIME": "Date / Time",
       "HEADER_BLOCK": "Block #",
       "HEADER_ISSUER": "Peer owner",
-      "BTN_LAST": "Last blocks"
+      "BTN_LAST": "Last blocks",
+      "DISPLAY_QUERY": "View query",
+      "HIDE_QUERY": "Hide query",
+      "TX_SEARCH_FILTER": {
+        "PERIOD": "Transactions between the {{params[1]|formatDate}} and the {{params[2]|formatDate}}",
+        "PUBKEY": "Public key : {{params[1]|formatPubkey}}"
+      }
     },
     "ERROR": {
       "SEARCH_BLOCKS_FAILED": "Error while searching blocks."
diff --git a/www/plugins/es/i18n/locale-en.json b/www/plugins/es/i18n/locale-en.json
index 7281905c7..4619d1f3d 100644
--- a/www/plugins/es/i18n/locale-en.json
+++ b/www/plugins/es/i18n/locale-en.json
@@ -97,10 +97,6 @@
       "BAD_INVITATION_FORMAT": "<span class=\"assertive\"><i class=\"ion-close-circled\"></i> Invitation unreadable (format unknown)</span> - sent by <a href=\"#/app/wot/{{pubkey}}/{{::uid}}\">{{::name||uid||pubkey}}</a>"
     }
   },
-  "TX_SEARCH": {
-    "PERIOD": "Transactions between the {{date.startDate|formatDate}} and the {{date.endDate|formatDate}}",
-    "PUBKEY": "Public key : {{pubkey|formatPubkey}}"
-  },
   "COMMENTS": {
     "DIVIDER": "Comments",
     "DIVIDER_WITH_TOTAL": "Comments ({{total}})",
@@ -189,7 +185,13 @@
       "HEADER_MEDIAN_TIME": "Date / Time",
       "HEADER_BLOCK": "Block #",
       "HEADER_ISSUER": "Peer owner",
-      "BTN_LAST": "Last blocks"
+      "BTN_LAST": "Last blocks",
+      "DISPLAY_QUERY": "View query",
+      "HIDE_QUERY": "Hide query",
+      "TX_SEARCH_FILTER": {
+        "PERIOD": "Transactions between the {{params[1]|formatDate}} and the {{params[2]|formatDate}}",
+        "PUBKEY": "Public key : {{params[1]|formatPubkey}}"
+      }
     },
     "ERROR": {
       "SEARCH_BLOCKS_FAILED": "Error while searching blocks."
diff --git a/www/plugins/es/i18n/locale-es-ES.json b/www/plugins/es/i18n/locale-es-ES.json
index fbec9752a..3dac8a11b 100644
--- a/www/plugins/es/i18n/locale-es-ES.json
+++ b/www/plugins/es/i18n/locale-es-ES.json
@@ -97,10 +97,6 @@
       "BAD_INVITATION_FORMAT": "<span class=\"assertive\"><i class=\"ion-close-circled\"></i> Invitación ilegible (formato desconocido)</span> - mandada por <a href=\"#/app/wot/{{pubkey}}/{{::uid}}\">{{::name||uid}}</a>"
     }
   },
-  "TX_SEARCH": {
-    "PERIOD": "Las transacciones entre el {{date.startDate|formatDate}} y el {{date.endDate|formatDate}}",
-    "PUBKEY": "Clave pública : {{pubkey|formatPubkey}}"
-  },
   "COMMENTS": {
     "DIVIDER": "Comentarios",
     "DIVIDER_WITH_TOTAL": "Comentarios ({{total}})",
@@ -189,7 +185,13 @@
       "HEADER_MEDIAN_TIME": "Fecha / Hora",
       "HEADER_BLOCK": "Bloque #",
       "HEADER_ISSUER": "Nodo emisor",
-      "BTN_LAST": "últimos bloques"
+      "BTN_LAST": "últimos bloques",
+      "DISPLAY_QUERY": "Ver la consulta",
+      "HIDE_QUERY": "Ocultar la solicitud",
+      "TX_SEARCH_FILTER": {
+        "PERIOD": "Las transacciones entre el {{params[1]|formatDate}} y el {{params[2]|formatDate}}",
+        "PUBKEY": "Clave pública : {{params[1]|formatPubkey}}"
+      }
     },
     "ERROR": {
       "SEARCH_BLOCKS_FAILED": "Fracaso en la búsqueda de los bloques."
diff --git a/www/plugins/es/i18n/locale-fr-FR.json b/www/plugins/es/i18n/locale-fr-FR.json
index c09d707ab..c15dcb1ec 100644
--- a/www/plugins/es/i18n/locale-fr-FR.json
+++ b/www/plugins/es/i18n/locale-fr-FR.json
@@ -186,6 +186,8 @@
       "HEADER_BLOCK": "Bloc #",
       "HEADER_ISSUER": "Noeud émetteur",
       "BTN_LAST": "Derniers blocs",
+      "DISPLAY_QUERY": "Afficher la requête",
+      "HIDE_QUERY": "Masquer la requête",
       "TX_SEARCH_FILTER": {
         "PERIOD": "Transactions effectuées entre le {{params[1]|formatDate}} et le {{params[2]|formatDate}}",
         "PUBKEY": "Clé publique : {{params[1]|formatPubkey}}"
diff --git a/www/plugins/es/js/controllers/blockchain-controllers.js b/www/plugins/es/js/controllers/blockchain-controllers.js
index dbbb4fc32..af58a9560 100644
--- a/www/plugins/es/js/controllers/blockchain-controllers.js
+++ b/www/plugins/es/js/controllers/blockchain-controllers.js
@@ -48,12 +48,13 @@ function ESBlockLookupController($scope, $state, $controller, $ionicPopover, UIU
   $scope.enableFilter = true;
 
   $scope.doSearchText = function() {
-    if (!$scope.search.text || $scope.search.text.trim().length === 0) {
+    if ((!$scope.search.text || !$scope.search.text.trim().length) &&
+      (!$scope.search.filters || !$scope.search.filters.length) ) {
       return $scope.doSearchLast();
     }
 
     $scope.search.type = 'text';
-    $scope.processSearchText($scope.search.text);
+
     $scope.doSearch();
 
     $ionicHistory.nextViewOptions({
@@ -61,7 +62,7 @@ function ESBlockLookupController($scope, $state, $controller, $ionicPopover, UIU
       disableBack: true,
       historyRoot: true
     });
-    $state.go('app.blockchain_search', {q: $scope.search.text}, {
+    $state.go('app.blockchain_search', {q: $scope.search.query}, {
       reload: false,
       inherit: true,
       notify: false});
@@ -85,41 +86,6 @@ function ESBlockLookupController($scope, $state, $controller, $ionicPopover, UIU
       notify: false});
   };
 
-  // Cancel search filter
-  $scope.itemRemove = function(item) {
-    $scope.newQuery = $scope.query.replace(item.query, '');
-    $scope.newQuery = $scope.newQuery.replace(/^ AND /, '');
-    $scope.newQuery = $scope.newQuery.replace(/ AND $/, '');
-    $scope.searchFilter.item = null;
-    $scope.search.text = $scope.newQuery;
-    $scope.doSearchText();
-  };
-
-
-  $scope.processSearchText = function(text) {
-
-
-    //exports.block.parseSearchText = function(text) {
-      var filterRegexps = {
-        PERIOD: /_exists_:transactions AND medianTime:>=([0-9]+) AND medianTime:<([0-9]+)/,
-        PUBKEY: /issuer:([a-zA-Z0-9]+)/
-      };
-
-      $scope.search.filters = _.keys(filterRegexps).reduce(function(res, filterType){
-        var matches = filterRegexps[filterType].exec(text);
-        if (matches) {
-          console.log(filterType, matches);
-          var filter = {
-            type: filterType,
-            params: matches
-          };
-          return res.concat(filter);
-        }
-        return res;
-      }, []);
-    //};
-
-  };
 
   // This method override the base class method
   $scope.doSearch = function(from) {
@@ -152,6 +118,19 @@ function ESBlockLookupController($scope, $state, $controller, $ionicPopover, UIU
     // Full text search
     else if ($scope.search.type == 'text') {
 
+      // Parse text search into filters array
+      var res = esBlockchain.block.parseSearchText($scope.search.text, $scope.search.filters);
+      $scope.search.filters = res.filters;
+      var query = $scope.search.filters.reduce(function(query, filter){
+        return query + ' AND ' + filter.text;
+      }, '');
+      if (res.text.length) {
+        query += ' AND ' + res.text;
+      }
+
+      $scope.search.query = query.substr(5);
+      $scope.search.text = res.text;
+
       request.from = from;
 
       // add sort
@@ -163,7 +142,7 @@ function ESBlockLookupController($scope, $state, $controller, $ionicPopover, UIU
       }
       request.excludeCurrent = true;
 
-      promise = esBlockchain.block.searchText($scope.currency, $scope.search.text, request);
+      promise = esBlockchain.block.searchText($scope.currency, $scope.search.query, request);
     }
 
     var time = new Date().getTime();
@@ -231,5 +210,19 @@ function ESBlockLookupController($scope, $state, $controller, $ionicPopover, UIU
       $scope.actionsPopover.hide();
     }
   };
+
+  /* -- manage click -- */
+
+
+  // Cancel search filter
+  $scope.itemRemove = function(index) {
+    $scope.search.filters.splice(index, 1);
+    $scope.doSearchText();
+  };
+
+  //Show the query
+  $scope.toggleShowQuery = function() {
+    $scope.showQuery = !$scope.showQuery;
+  };
 }
 
diff --git a/www/plugins/es/js/services/blockchain-services.js b/www/plugins/es/js/services/blockchain-services.js
index 9bd185b96..5c50027c7 100644
--- a/www/plugins/es/js/services/blockchain-services.js
+++ b/www/plugins/es/js/services/blockchain-services.js
@@ -10,6 +10,14 @@ angular.module('cesium.es.blockchain.services', ['cesium.services', 'cesium.es.h
         DEFAULT_SEARCH_SIZE: 40,
         ES_CORE_API_ENDPOINT: 'ES_CORE_API( ([a-z_][a-z0-9-_.]*))?( ([0-9.]+))?( ([0-9a-f:]+))?( ([0-9]+))'
       },
+      REGEXPS = {
+        SEARCH_FILTER: {
+          PERIOD: /_exists_:transactions[ ]+AND[ ]+medianTime:>=([0-9]+)[ ]+AND[ ]+medianTime:<([0-9]+)([ ]+AND)?/,
+          PUBKEY: /issuer:([a-zA-Z0-9]+)([ ]+AND)?/
+
+        },
+        LAST_AND: /[ ]+AND$/
+      },
       FIELDS = {
         MINIMAL: ['number', 'hash', 'medianTime', 'issuer'],
         COMMONS: ['number', 'hash', 'medianTime', 'issuer', 'currency', 'version', 'powMin', 'dividend', 'membersCount', 'identities', 'joiners', 'actives', 'leavers', 'revoked', 'excluded', 'certifications', 'transactions']
@@ -116,6 +124,35 @@ angular.module('cesium.es.blockchain.services', ['cesium.services', 'cesium.es.h
         });
     };
 
+    exports.block.parseSearchText = function(text, filters) {
+
+      var unparsedText = text;
+      filters = _.keys(REGEXPS.SEARCH_FILTER).reduce(function(res, filterType){
+        var matches = REGEXPS.SEARCH_FILTER[filterType].exec(unparsedText);
+        if (matches) {
+          var filterText = matches[0];
+
+          // update rest
+          unparsedText = unparsedText.replace(filterText, '');
+
+          filterText = filterText.replace(REGEXPS.LAST_AND, '');
+
+          var filter = {
+            type: filterType,
+            text: filterText,
+            params: matches
+          };
+          return res.concat(filter);
+        }
+        return res;
+      }, filters||[]);
+
+      return {
+        filters: filters,
+        text: unparsedText.trim()
+      };
+    };
+
     return exports;
   }
 
diff --git a/www/plugins/es/templates/blockchain/lookup_form.html b/www/plugins/es/templates/blockchain/lookup_form.html
index 47358c0af..e4ca97d4a 100644
--- a/www/plugins/es/templates/blockchain/lookup_form.html
+++ b/www/plugins/es/templates/blockchain/lookup_form.html
@@ -6,7 +6,7 @@
     <div class="gray double-padding-x padding-top item-text-wrap"
          ng-repeat="filter in search.filters" ng-if="filter">
       <span>{{'BLOCKCHAIN.LOOKUP.TX_SEARCH_FILTER.'+filter.type|translate:filter}}</span>
-      <a class="icon ion-close" ng-click="itemRemove(filter)"></a>
+      <a class="icon ion-close" ng-click="itemRemove($index)"></a>
 
     </div>
 
@@ -45,6 +45,18 @@
         <small class="gray" ng-if=":rebind:search.took && expertMode">
           - {{:rebind:'COMMON.EXECUTION_TIME'|translate: {duration: search.took} }}
         </small>
+        <small class="gray" ng-if=":rebind:expertMode && search.filters && search.filters.length">
+          - <a ng-click="toggleShowQuery()"
+            ng-if="!showQuery" >
+            Voir la requête
+            <i class="icon ion-arrow-down-b gray"></i>
+          </a>
+          <a ng-click="toggleShowQuery()"
+             ng-if="showQuery" >
+             Masquer la requête
+             <i class="icon ion-arrow-up-b gray"></i>
+          </a>
+        </small>
       </h5>
       <h5 class="gray" ng-if="search.loading" >
         <ion-spinner class="icon ion-spinner-small" icon="android"></ion-spinner>
@@ -72,6 +84,12 @@
     </div>
   </div>
 
+  <div class="item no-border no-padding" ng-if=":rebind:search.filters && search.filters.length && expertMode">
+    <small class="no-padding no-margin" ng-if="showQuery">
+      <span class="gray text-wrap dark">{{:rebind:search.query}}</span>
+    </small>
+  </div>
+
   <ion-list class="list list-blocks" ng-class="::motion.ionListClass">
 
     <ng-include src="'plugins/es/templates/blockchain/items_blocks.html'"></ng-include>
-- 
GitLab