Procházet zdrojové kódy

refactored use of localStorage

Torkel Ödegaard před 11 roky
rodič
revize
b89480a284

+ 1 - 1
src/app/components/lodash.extended.js

@@ -33,4 +33,4 @@ function () {
   });
 
   return _;
-});
+});

+ 1 - 0
src/app/components/require.config.js

@@ -8,6 +8,7 @@ require.config({
     config:                   ['../config', '../config.sample'],
     settings:                 'components/settings',
     kbn:                      'components/kbn',
+    store:                    'components/store',
 
     css:                      '../vendor/require/css',
     text:                     '../vendor/require/text',

+ 20 - 0
src/app/components/store.js

@@ -0,0 +1,20 @@
+define([], function() {
+  'use strict';
+
+  return {
+    get: function(key) {
+      return window.localStorage[key];
+    },
+    set: function(key, value) {
+      window.localStorage[key] = value;
+    },
+    getBool: function(key) {
+      return window.localStorage[key] === 'true' ? true : false;
+    },
+    delete: function(key) {
+      window.localStorage.removeItem(key);
+    }
+
+  };
+
+});

+ 3 - 2
src/app/controllers/console-ctrl.js

@@ -2,12 +2,13 @@ define([
   'angular',
   'lodash',
   'moment',
+  'store'
 ],
-function (angular, _, moment) {
+function (angular, _, moment, store) {
   'use strict';
 
   var module = angular.module('grafana.controllers');
-  var consoleEnabled = window.localStorage && window.localStorage.grafanaConsole === 'true';
+  var consoleEnabled = store.getBool('grafanaConsole');
 
   if (!consoleEnabled) {
     return;

+ 4 - 3
src/app/controllers/dashboardNavCtrl.js

@@ -3,9 +3,10 @@ define([
   'lodash',
   'moment',
   'config',
+  'store',
   'filesaver'
 ],
-function (angular, _, moment, config) {
+function (angular, _, moment, config, store) {
   'use strict';
 
   var module = angular.module('grafana.controllers');
@@ -26,12 +27,12 @@ function (angular, _, moment, config) {
     };
 
     $scope.set_default = function() {
-      window.localStorage.grafanaDashboardDefault = $location.path();
+      store.set('grafanaDashboardDefault', $location.path());
       alertSrv.set('Home Set','This page has been set as your default dashboard','success',5000);
     };
 
     $scope.purge_default = function() {
-      delete window.localStorage.grafanaDashboardDefault;
+      store.delete('grafanaDashboardDefault');
       alertSrv.set('Local Default Clear','Your default dashboard has been reset to the default','success', 5000);
     };
 

+ 8 - 10
src/app/controllers/grafanaCtrl.js

@@ -3,8 +3,9 @@ define([
   'config',
   'lodash',
   'jquery',
+  'store',
 ],
-function (angular, config, _, $) {
+function (angular, config, _, $, store) {
   "use strict";
 
   var module = angular.module('grafana.controllers');
@@ -12,26 +13,23 @@ function (angular, config, _, $) {
   module.controller('GrafanaCtrl', function($scope, alertSrv, grafanaVersion, $rootScope) {
 
     $scope.grafanaVersion = grafanaVersion[0] === '@' ? 'master' : grafanaVersion;
-    $scope.consoleEnabled = (window.localStorage && window.localStorage.grafanaConsole === 'true');
+    $scope.consoleEnabled = store.getBool('grafanaConsole');
 
-    $rootScope.profilingEnabled = (window.localStorage && window.localStorage.profilingEnabled === 'true');
+    $rootScope.profilingEnabled = store.getBool('profilingEnabled');
     $rootScope.performance = { loadStart: new Date().getTime() };
 
     $scope.init = function() {
       $scope._ = _;
-      if ($rootScope.profilingEnabled) {
-        $scope.initProfiling();
-      }
+
+      if ($rootScope.profilingEnabled) { $scope.initProfiling(); }
 
       $scope.dashAlerts = alertSrv;
-      $scope.grafana = {
-        style: 'dark'
-      };
+      $scope.grafana = { style: 'dark' };
     };
 
     $scope.toggleConsole = function() {
       $scope.consoleEnabled = !$scope.consoleEnabled;
-      window.localStorage.grafanaConsole = $scope.consoleEnabled ? 'true' : 'false';
+      store.set('grafanaConsole', $scope.consoleEnabled);
     };
 
     $rootScope.onAppEvent = function(name, callback) {

+ 4 - 8
src/app/routes/dashboard-default.js

@@ -1,8 +1,9 @@
 define([
   'angular',
-  'config'
+  'config',
+  'store'
 ],
-function (angular, config) {
+function (angular, config, store) {
   "use strict";
 
   var module = angular.module('grafana.routes');
@@ -11,12 +12,7 @@ function (angular, config) {
     $routeProvider
       .when('/', {
         redirectTo: function() {
-          if (window.localStorage && window.localStorage.grafanaDashboardDefault) {
-            return window.localStorage.grafanaDashboardDefault;
-          }
-          else {
-            return config.default_route;
-          }
+          return store.get('grafanaDashboardDefault') || config.default_route;
         }
       });
   });

+ 5 - 4
src/app/services/playlistSrv.js

@@ -1,9 +1,10 @@
 define([
   'angular',
   'lodash',
-  'kbn'
+  'kbn',
+  'store'
 ],
-function (angular, _, kbn) {
+function (angular, _, kbn, store) {
   'use strict';
 
   var module = angular.module('grafana.services');
@@ -13,14 +14,14 @@ function (angular, _, kbn) {
     var favorites = { dashboards: [] };
 
     this.init = function() {
-      var existingJson = window.localStorage["grafana-favorites"];
+      var existingJson = store.get("grafana-favorites");
       if (existingJson) {
         favorites = angular.fromJson(existingJson);
       }
     };
 
     this._save = function() {
-      window.localStorage["grafana-favorites"] = angular.toJson(favorites);
+      store.set('grafana-favorites', angular.toJson(favorites));
     };
 
     this._find = function(title) {

+ 1 - 0
src/test/test-main.js

@@ -6,6 +6,7 @@ require.config({
     mocks:                 '../test/mocks',
     config:                '../config.sample',
     kbn:                   'components/kbn',
+    store:                 'components/store',
 
     settings:              'components/settings',
     lodash:                'components/lodash.extended',