From b89480a284b578852a2e8925684b0550d14e6a8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Sat, 16 Aug 2014 13:13:26 +0200 Subject: [PATCH] refactored use of localStorage --- src/app/components/lodash.extended.js | 2 +- src/app/components/require.config.js | 1 + src/app/components/store.js | 20 ++++++++++++++++++++ src/app/controllers/console-ctrl.js | 5 +++-- src/app/controllers/dashboardNavCtrl.js | 7 ++++--- src/app/controllers/grafanaCtrl.js | 18 ++++++++---------- src/app/routes/dashboard-default.js | 12 ++++-------- src/app/services/playlistSrv.js | 9 +++++---- src/test/test-main.js | 1 + 9 files changed, 47 insertions(+), 28 deletions(-) create mode 100644 src/app/components/store.js diff --git a/src/app/components/lodash.extended.js b/src/app/components/lodash.extended.js index 3554eef6e51..40372239672 100644 --- a/src/app/components/lodash.extended.js +++ b/src/app/components/lodash.extended.js @@ -33,4 +33,4 @@ function () { }); return _; -}); \ No newline at end of file +}); diff --git a/src/app/components/require.config.js b/src/app/components/require.config.js index 2b23ca1f900..6a17f9635a2 100644 --- a/src/app/components/require.config.js +++ b/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', diff --git a/src/app/components/store.js b/src/app/components/store.js new file mode 100644 index 00000000000..b9006f6e37d --- /dev/null +++ b/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); + } + + }; + +}); diff --git a/src/app/controllers/console-ctrl.js b/src/app/controllers/console-ctrl.js index f5ce873bbfd..8a673d99476 100644 --- a/src/app/controllers/console-ctrl.js +++ b/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; diff --git a/src/app/controllers/dashboardNavCtrl.js b/src/app/controllers/dashboardNavCtrl.js index cbcc74ae13e..7fe6e03f78b 100644 --- a/src/app/controllers/dashboardNavCtrl.js +++ b/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); }; diff --git a/src/app/controllers/grafanaCtrl.js b/src/app/controllers/grafanaCtrl.js index 2754595896f..060c0bc0803 100644 --- a/src/app/controllers/grafanaCtrl.js +++ b/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) { diff --git a/src/app/routes/dashboard-default.js b/src/app/routes/dashboard-default.js index ad8ed722d0d..6721525b9e4 100644 --- a/src/app/routes/dashboard-default.js +++ b/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; } }); }); diff --git a/src/app/services/playlistSrv.js b/src/app/services/playlistSrv.js index c7cd453bfd3..b486e5957fc 100644 --- a/src/app/services/playlistSrv.js +++ b/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) { diff --git a/src/test/test-main.js b/src/test/test-main.js index 1cecc6379e9..9c7cfe73341 100644 --- a/src/test/test-main.js +++ b/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',