From 71579762cec66388d0646e211acc77832b3a8e17 Mon Sep 17 00:00:00 2001
From: blavenie <benoit.lavenier@e-is.pro>
Date: Thu, 18 Aug 2016 18:45:20 +0200
Subject: [PATCH] - ES Settings : Force left menu to refresh, when ES plugin
 enable/disable - Settings : move plugins divider to ES plugin settings
 extends.

---
 config.xml                                    |  4 +-
 package.json                                  |  2 +-
 www/i18n/locale-en.json                       |  2 +-
 www/i18n/locale-fr-FR.json                    |  2 +-
 www/js/config.js                              |  4 +-
 www/js/services/wallet-services.js            | 64 +++++++++++--------
 www/plugins/es/i18n/locale-en.json            |  6 +-
 .../es/js/controllers/common-controllers.js   | 14 ++--
 .../es/js/controllers/settings-controllers.js | 20 ++++--
 www/plugins/es/templates/menu_extend.html     |  4 +-
 .../templates/settings/settings_extend.html   |  2 +
 www/templates/menu.html                       |  2 +-
 www/templates/settings/settings.html          |  2 -
 13 files changed, 77 insertions(+), 51 deletions(-)

diff --git a/config.xml b/config.xml
index 11c90352..a78377a3 100644
--- a/config.xml
+++ b/config.xml
@@ -3,8 +3,8 @@
   xmlns="http://www.w3.org/ns/widgets"
   xmlns:cdv="http://cordova.apache.org/ns/1.0"
   id="fr.duniter.cesium"
-  version="0.1.27"
-  android-versionCode="14"
+  version="0.1.28"
+  android-versionCode="15"
   >
   <name>Cesium</name>
   <description>
diff --git a/package.json b/package.json
index 5bdb2528..e9a56de3 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
 {
   "name": "cesium",
-  "version": "0.1.27",
+  "version": "0.1.28",
   "description": "Unhosted webapp client for Duniter network",
   "dependencies": {
     "delete-empty": "^0.1.3",
diff --git a/www/i18n/locale-en.json b/www/i18n/locale-en.json
index 0eae9815..d1c23bdc 100644
--- a/www/i18n/locale-en.json
+++ b/www/i18n/locale-en.json
@@ -69,7 +69,7 @@
     "BTN_MARKET": "Market place",
     "BTN_CURRENCIES": "Explore currencies",
     "BTN_CURRENCY": "Explore currency",
-    "BTN_ABOUT": "About",
+    "BTN_ABOUT": "about",
     "BTN_ACCOUNT": "My Account",
     "REPORT_ISSUE": "Report an issue"
   },
diff --git a/www/i18n/locale-fr-FR.json b/www/i18n/locale-fr-FR.json
index 063d5c69..110ff1b7 100644
--- a/www/i18n/locale-fr-FR.json
+++ b/www/i18n/locale-fr-FR.json
@@ -69,7 +69,7 @@
     "BTN_MARKET": "Offres/demandes",
     "BTN_CURRENCIES": "Explorer les monnaies",
     "BTN_CURRENCY": "Explorer la monnaie",
-    "BTN_ABOUT": "A propos",
+    "BTN_ABOUT": "à propos",
     "BTN_ACCOUNT": "Mon compte",
     "REPORT_ISSUE": "anomalie"
   },
diff --git a/www/js/config.js b/www/js/config.js
index 3bc00880..62912cf4 100644
--- a/www/js/config.js
+++ b/www/js/config.js
@@ -13,8 +13,8 @@ angular.module("cesium.config", [])
 	"NEW_ISSUE_LINK": "https://github.com/duniter/cesium/issues/new?labels=bug",
 	"TIMEOUT": 4000,
 	"DEBUG": false,
-	"VERSION": "0.1.27",
-	"BUILD_DATE": "2016-08-18T14:32:54.704Z"
+	"VERSION": "0.1.28",
+	"BUILD_DATE": "2016-08-18T16:43:00.994Z"
 })
 
 ;
\ No newline at end of file
diff --git a/www/js/services/wallet-services.js b/www/js/services/wallet-services.js
index ca568746..3f2859be 100644
--- a/www/js/services/wallet-services.js
+++ b/www/js/services/wallet-services.js
@@ -8,6 +8,9 @@ angular.module('cesium.wallet.services', ['ngResource', 'ngApi', 'cesium.bma.ser
   Wallet = function(id) {
 
     var
+    events = {
+      SETTINGS: 'wallet-settings-changed'
+    },
 
     defaultSettings = {
       useRelative: true,
@@ -201,41 +204,51 @@ angular.module('cesium.wallet.services', ['ngResource', 'ngApi', 'cesium.bma.ser
       return isLogin() && data.pubkey === pubkey;
     },
 
-    store = function() {
+    store = function(options) {
       if (data.settings.useLocalStorage) {
-        localStorage.setObject('CESIUM_SETTINGS', data.settings);
 
-        if (isLogin() && data.settings.rememberMe) {
-          var dataToStore = {
-            keypair: data.keypair,
-            pubkey: data.pubkey
-          };
+        if (!options || options.settings) {
+          localStorage.setObject('CESIUM_SETTINGS', data.settings);
+          // Send event when settings changed
+          $rootScope.$broadcast(events.SETTINGS);
+        }
 
-          if (data.tx && data.tx.pendings && data.tx.pendings.length>0) {
-            var pendings = data.tx.pendings.reduce(function(res, tx){
-              return tx.time ? res.concat({
-                amount: tx.amount,
-                time: tx.time,
-                hash: tx.hash
-              }) : res;
-            }, []);
-            if (pendings.length) {
-              dataToStore.tx = {
-                pendings: pendings
-              };
+        if (!options || options.data) {
+          if (isLogin() && data.settings.rememberMe) {
+            var dataToStore = {
+              keypair: data.keypair,
+              pubkey: data.pubkey
+            };
+
+            if (data.tx && data.tx.pendings && data.tx.pendings.length>0) {
+              var pendings = data.tx.pendings.reduce(function(res, tx){
+                return tx.time ? res.concat({
+                  amount: tx.amount,
+                  time: tx.time,
+                  hash: tx.hash
+                }) : res;
+              }, []);
+              if (pendings.length) {
+                dataToStore.tx = {
+                  pendings: pendings
+                };
+              }
             }
-          }
 
-          localStorage.setObject('CESIUM_DATA', dataToStore);
-        }
-        else {
-          localStorage.setObject('CESIUM_DATA', null);
+            localStorage.setObject('CESIUM_DATA', dataToStore);
+          }
+          else {
+            localStorage.setObject('CESIUM_DATA', null);
+          }
         }
       }
       else {
         localStorage.setObject('CESIUM_SETTINGS', null);
         localStorage.setObject('CESIUM_DATA', null);
+        // Send event when settings changed
+        $rootScope.$broadcast(events.SETTINGS);
       }
+
     },
 
     restore = function() {
@@ -1059,7 +1072,8 @@ angular.module('cesium.wallet.services', ['ngResource', 'ngApi', 'cesium.bma.ser
       toJson: toJson,
       fromJson: fromJson,
       defaultSettings: defaultSettings,
-      api: api
+      api: api,
+      events: events
     };
   };
 
diff --git a/www/plugins/es/i18n/locale-en.json b/www/plugins/es/i18n/locale-en.json
index 7ca7936e..0cda4e4c 100644
--- a/www/plugins/es/i18n/locale-en.json
+++ b/www/plugins/es/i18n/locale-en.json
@@ -143,11 +143,11 @@
       "PROFILE_SAVED": "Profile saved"
     }
   },
-  "SETTINGS": {
+  "ES_SETTINGS": {
     "PLUGIN_NAME": "Registry and market place",
     "ENABLE_TOGGLE": "Enable extension ?",
-    "ES_NODE": "Data node address",
-    "ES_NODE_HELP": "server.domain.com:port",
+    "NODE": "Data node address",
+    "NODE_HELP": "server.domain.com:port",
     "POPUP_NODE": {
       "TITLE" : "Data Node",
       "HELP" : "Set the address of the node to use:"
diff --git a/www/plugins/es/js/controllers/common-controllers.js b/www/plugins/es/js/controllers/common-controllers.js
index c017bb44..ea8c965f 100644
--- a/www/plugins/es/js/controllers/common-controllers.js
+++ b/www/plugins/es/js/controllers/common-controllers.js
@@ -1,4 +1,4 @@
-angular.module('cesium.es.common.controllers', ['cesium.es.services'])
+angular.module('cesium.es.common.controllers', ['ngResource', 'cesium.es.services'])
 
   // Configure menu items
   .config(function(PluginServiceProvider, APP_CONFIG) {
@@ -36,13 +36,19 @@ angular.module('cesium.es.common.controllers', ['cesium.es.services'])
 /**
  * Control menu extension
  */
-function ESMenuExtendController($scope, PluginService, Wallet) {
+function ESMenuExtendController($scope, $rootScope, PluginService, Wallet, APP_CONFIG) {
   'ngInject';
   $scope.extensionPoint = PluginService.extensions.points.current.get();
 
-  $scope.es = Wallet.data.settings.plugins && Wallet.data.settings.plugins.es ? Wallet.data.settings.plugins.es : {
-    enable: false
+  $scope.refreshEnable = function() {
+    $scope.enable = Wallet.data && Wallet.data.settings.plugins && Wallet.data.settings.plugins.es ?
+        Wallet.data.settings.plugins.es.enable :
+        !!APP_CONFIG.DUNITER_NODE_ES
   };
+
+  $rootScope.$on(Wallet.events.SETTINGS, $scope.refreshEnable);
+
+  $scope.refreshEnable();
 }
 
 function ESPicturesEditController($scope, $ionicModal, Wallet, esMarket, UIUtils, $state, CryptoUtils, $q, $ionicPopup, Device, $timeout, ModalUtils) {
diff --git a/www/plugins/es/js/controllers/settings-controllers.js b/www/plugins/es/js/controllers/settings-controllers.js
index 462b3a0a..954188b8 100644
--- a/www/plugins/es/js/controllers/settings-controllers.js
+++ b/www/plugins/es/js/controllers/settings-controllers.js
@@ -38,7 +38,8 @@ angular.module('cesium.es.settings.controllers', ['cesium.es.services'])
 /*
  * Settings extend controller
  */
-function ESExtendSettingsController ($scope, Wallet, PluginService, APP_CONFIG) {
+function ESExtendSettingsController ($scope, $rootScope, Wallet, PluginService, APP_CONFIG) {
+  'ngInject';
 
   $scope.extensionPoint = PluginService.extensions.points.current.get();
   $scope.enable = false;
@@ -59,20 +60,27 @@ function ESExtendSettingsController ($scope, Wallet, PluginService, APP_CONFIG)
 /*
  * Settings extend controller
  */
-function ESPluginSettingsController ($scope, $q,  $translate, $ionicPopup, $ionicHistory, UIUtils, APP_CONFIG, esHttp, esMarket,
+function ESPluginSettingsController ($scope, $rootScope, $q,  $translate, $ionicPopup, $ionicHistory, UIUtils, APP_CONFIG, esHttp, esMarket,
   esRegistry, esUser, Wallet) {
+  'ngInject';
 
   $scope.formData = {};
+  $scope.loading = true;
 
   $scope.$on('$ionicView.enter', function(e, $state) {
-    if (!$scope.formData.node && APP_CONFIG.DUNITER_NODE_ES) {
+    if (!$scope.formData.node) {
       if (Wallet.data.settings && Wallet.data.settings.plugins && Wallet.data.settings.plugins.es) {
         angular.merge($scope.formData, Wallet.data.settings.plugins.es);
       }
       else {
         $scope.formData.enable = !!APP_CONFIG.DUNITER_NODE_ES;
+
+      }
+      if (!$scope.formData.node) {
+        $scope.formData.node = APP_CONFIG.DUNITER_NODE_ES;
       }
     }
+    $scope.loading = false;
   });
 
   $scope.setSettingsForm = function(settingsForm) {
@@ -155,11 +163,9 @@ function ESPluginSettingsController ($scope, $q,  $translate, $ionicPopup, $ioni
 
   $scope.onSettingsChanged = function() {
     if ($scope.loading) {
-      $timeout(function() {
-        $scope.onSettingsChanged();
-      }, 200);
       return;
     }
+
     $scope.loading = true;
 
     if (!Wallet.data.settings.plugins) {
@@ -176,7 +182,7 @@ function ESPluginSettingsController ($scope, $q,  $translate, $ionicPopup, $ioni
     esHttp.setEnable($scope.formData.enable);
     esUser.refreshListeners();
 
-    Wallet.store();
+    Wallet.store({settings: true, data: false});
 
     // Clean cache
     $ionicHistory.clearCache();
diff --git a/www/plugins/es/templates/menu_extend.html b/www/plugins/es/templates/menu_extend.html
index d78b1453..5968b51e 100644
--- a/www/plugins/es/templates/menu_extend.html
+++ b/www/plugins/es/templates/menu_extend.html
@@ -1,6 +1,6 @@
 
 <!-- Main section -->
-<ng-if ng-if="es.enable && extensionPoint === 'menu-main'">
+<ng-if ng-if="enable && extensionPoint === 'menu-main'">
 
   <!-- professionals registry-->
   <ion-item menu-close class="item item-icon-left" active-link="active"
@@ -18,7 +18,7 @@
 </ng-if>
 
 <!-- User section -->
-<ng-if ng-if="es.enable && extensionPoint === 'menu-user'">
+<ng-if ng-if="enable && extensionPoint === 'menu-user'">
   <!-- user profile -->
   <ion-item menu-close class="item item-icon-left" active-link="active"
             ng-if="isLogin()"
diff --git a/www/plugins/es/templates/settings/settings_extend.html b/www/plugins/es/templates/settings/settings_extend.html
index 38cae82b..0d9d277f 100644
--- a/www/plugins/es/templates/settings/settings_extend.html
+++ b/www/plugins/es/templates/settings/settings_extend.html
@@ -1,4 +1,6 @@
 
+<span class="item item-divider" translate>SETTINGS.PLUGINS_SETTINGS</span>
+
 <div class="item ink" ui-sref="app.es_settings">
   <div class="input-label ng-binding" translate>ES_SETTINGS.PLUGIN_NAME</div>
 
diff --git a/www/templates/menu.html b/www/templates/menu.html
index b1b85741..2624b789 100644
--- a/www/templates/menu.html
+++ b/www/templates/menu.html
@@ -125,7 +125,7 @@
         <span translate="COMMON.APP_VERSION" translate-values="{version: config.VERSION}"></span>
         |
         <!-- about -->
-        <a href="#" ng-click="showAboutModal()" translate>HOME.BTN_ABOUT</a>
+        <a ng-click="showAboutModal()" translate>HOME.BTN_ABOUT</a>
       </h1>
     </ion-footer-bar>
   </ion-side-menu>
diff --git a/www/templates/settings/settings.html b/www/templates/settings/settings.html
index 8747d982..a26847a4 100644
--- a/www/templates/settings/settings.html
+++ b/www/templates/settings/settings.html
@@ -84,8 +84,6 @@
     <!-- Allow extension here -->
     <cs-extension-point name="network"></cs-extension-point>
 
-    <span class="item item-divider" translate>SETTINGS.PLUGINS_SETTINGS</span>
-
     <!-- Allow extension here -->
     <cs-extension-point name="plugins"></cs-extension-point>
 
-- 
GitLab