GUI -- User Prefs written as a service; persistent topo settings updated a bit; still WIP.

Change-Id: I6945dd9eb4b325a8f1637c44e2c4b271126b2bc4
diff --git a/web/gui/src/main/webapp/app/view/topo/topo.js b/web/gui/src/main/webapp/app/view/topo/topo.js
index 5843b56..402ad86 100644
--- a/web/gui/src/main/webapp/app/view/topo/topo.js
+++ b/web/gui/src/main/webapp/app/view/topo/topo.js
@@ -29,7 +29,7 @@
     ];
 
     // references to injected services etc.
-    var $log, $cookies, fs, ks, zs, gs, ms, sus, flash, wss,
+    var $log, $cookies, fs, ks, zs, gs, ms, sus, flash, wss, ps,
         tes, tfs, tps, tis, tss, tls, tts, tos, fltr, ttbs;
 
     // DOM elements
@@ -100,7 +100,7 @@
     function toggleInstances(x) {
         if (x === 'keyev') {
             tis.toggle();
-            updateCookieState('insts', tis.isVisible());
+            updatePrefsState('insts', tis.isVisible());
         } else if (x) {
             tis.show();
         } else {
@@ -112,7 +112,7 @@
     function toggleMap(x) {
         var on = (x === 'keyev') ? !sus.visible(mapG) : !!x;
         sus.visible(mapG, on);
-        updateCookieState('bg', on);
+        updatePrefsState('bg', on);
     }
 
     //  TODO: need wrapper functions for state changes needed in cookies
@@ -248,22 +248,9 @@
             .attr('opacity', b ? 1 : 0);
     }
 
-    // --- Config from Cookies -------------------------------------------
+    // --- User Preferemces ----------------------------------------------
 
-    // TODO: write a general purpose cookie service, rather than custom here
-
-    // NOTE: in Angular 1.3.5, $cookies is just a simple object, and
-    //       cookie values are just strings. From the 1.3.5 docs:
-    //
-    //       "Only a simple Object is exposed and by adding or removing
-    //        properties to/from this object, new cookies are created/deleted
-    //        at the end of current $eval. The object's properties can only
-    //        be strings."
-    //
-    //       We may want to upgrade the version of Angular sometime soon
-    //        since later version support objects as cookie values.
-
-    var defaultCookieState = {
+    var defaultPrefsState = {
         bg: 1,
         insts: 1,
         summary: 1,
@@ -271,54 +258,27 @@
         hosts: 0
     };
 
-    var cookieState = {};
+    var prefsState = {};
 
-    function writeCookieState() {
-        var bits = [],
-            str;
-        angular.forEach(cookieState, function (value, key) {
-            bits.push(key + ':' + value);
-        });
-        str = bits.join(',');
-
-        // The angular way of doing this...
-        // $cookies.topo_state = str;
-        //  ...but it appears that this gets delayed, and doesn't 'stick' ??
-
-        // FORCE cookie to be set by writing directly to document.cookie...
-        document.cookie = 'topo_state=' + encodeURIComponent(str);
-        $log.debug('<<>> Wrote cookie:', str);
+    function topoDefPrefs() {
+        return angular.extend({}, defaultPrefsState);
     }
 
-    function readCookieState() {
-        var cook = $cookies.topo_state || '',
-            bits;
-
-        if (!cook) {
-            cookieState = angular.extend({}, defaultCookieState);
-            writeCookieState(); // seed the pot
-
-        } else {
-            bits = cook.split(',');
-            bits.forEach(function (value) {
-                var x = value.split(':');
-                cookieState[x[0]] = Number(x[1]);
-            });
-        }
+    function updatePrefsState(what, b) {
+        prefsState[what] = b ? 1 : 0;
+        ps.setPrefs('topo_prefs', prefsState);
     }
 
-    function updateCookieState(what, b) {
-        cookieState[what] = b ? 1 : 0;
-        writeCookieState();
-    }
 
-    function restoreConfigFromCookies() {
-        readCookieState();
-        $log.debug('Cookie State:', cookieState);
+    function restoreConfigFromPrefs() {
+        // NOTE: toolbar will have set this for us..
+        prefsState = ps.getPrefs('topo_prefs');
 
-        toggleInstances(cookieState.insts);
-        tps.toggleSummary(cookieState.summary);
-        tps.toggleDetails(cookieState.detail);
+        $log.debug('TOPO---- Prefs State:', prefsState);
+
+        toggleInstances(prefsState.insts);
+        tps.toggleSummary(prefsState.summary);
+        tps.toggleDetails(prefsState.detail);
     }
 
 
@@ -328,15 +288,15 @@
         .controller('OvTopoCtrl', ['$scope', '$log', '$location', '$timeout',
             '$cookies', 'FnService', 'MastService', 'KeyService', 'ZoomService',
             'GlyphService', 'MapService', 'SvgUtilService', 'FlashService',
-            'WebSocketService',
+            'WebSocketService', 'PrefsService',
             'TopoEventService', 'TopoForceService', 'TopoPanelService',
             'TopoInstService', 'TopoSelectService', 'TopoLinkService',
             'TopoTrafficService', 'TopoObliqueService', 'TopoFilterService',
             'TopoToolbarService',
 
         function ($scope, _$log_, $loc, $timeout, _$cookies_, _fs_, mast, _ks_,
-                  _zs_, _gs_, _ms_, _sus_, _flash_, _wss_, _tes_, _tfs_, _tps_,
-                  _tis_, _tss_, _tls_, _tts_, _tos_, _fltr_, _ttbs_) {
+                  _zs_, _gs_, _ms_, _sus_, _flash_, _wss_, _ps_, _tes_, _tfs_,
+                  _tps_, _tis_, _tss_, _tls_, _tts_, _tos_, _fltr_, _ttbs_) {
             var self = this,
                 projection,
                 dim,
@@ -359,6 +319,7 @@
             sus = _sus_;
             flash = _flash_;
             wss = _wss_;
+            ps = _ps_;
             tes = _tes_;
             tfs = _tfs_;
             // TODO: consider funnelling actions through TopoForceService...
@@ -403,7 +364,7 @@
                 function (proj) {
                     projection = proj;
                     $log.debug('** We installed the projection: ', proj);
-                    toggleMap(cookieState.bg);
+                    toggleMap(prefsState.bg);
                 }
             );
 
@@ -414,7 +375,7 @@
             tes.start();
 
             // temporary solution for persisting user settings
-            restoreConfigFromCookies();
+            restoreConfigFromPrefs();
 
             $log.log('OvTopoCtrl has been created');
         }]);