Commit b89480a2 by Torkel Ödegaard

refactored use of localStorage

parent 5846c710
...@@ -8,6 +8,7 @@ require.config({ ...@@ -8,6 +8,7 @@ require.config({
config: ['../config', '../config.sample'], config: ['../config', '../config.sample'],
settings: 'components/settings', settings: 'components/settings',
kbn: 'components/kbn', kbn: 'components/kbn',
store: 'components/store',
css: '../vendor/require/css', css: '../vendor/require/css',
text: '../vendor/require/text', text: '../vendor/require/text',
......
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);
}
};
});
...@@ -2,12 +2,13 @@ define([ ...@@ -2,12 +2,13 @@ define([
'angular', 'angular',
'lodash', 'lodash',
'moment', 'moment',
'store'
], ],
function (angular, _, moment) { function (angular, _, moment, store) {
'use strict'; 'use strict';
var module = angular.module('grafana.controllers'); var module = angular.module('grafana.controllers');
var consoleEnabled = window.localStorage && window.localStorage.grafanaConsole === 'true'; var consoleEnabled = store.getBool('grafanaConsole');
if (!consoleEnabled) { if (!consoleEnabled) {
return; return;
......
...@@ -3,9 +3,10 @@ define([ ...@@ -3,9 +3,10 @@ define([
'lodash', 'lodash',
'moment', 'moment',
'config', 'config',
'store',
'filesaver' 'filesaver'
], ],
function (angular, _, moment, config) { function (angular, _, moment, config, store) {
'use strict'; 'use strict';
var module = angular.module('grafana.controllers'); var module = angular.module('grafana.controllers');
...@@ -26,12 +27,12 @@ function (angular, _, moment, config) { ...@@ -26,12 +27,12 @@ function (angular, _, moment, config) {
}; };
$scope.set_default = function() { $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); alertSrv.set('Home Set','This page has been set as your default dashboard','success',5000);
}; };
$scope.purge_default = function() { $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); alertSrv.set('Local Default Clear','Your default dashboard has been reset to the default','success', 5000);
}; };
......
...@@ -3,8 +3,9 @@ define([ ...@@ -3,8 +3,9 @@ define([
'config', 'config',
'lodash', 'lodash',
'jquery', 'jquery',
'store',
], ],
function (angular, config, _, $) { function (angular, config, _, $, store) {
"use strict"; "use strict";
var module = angular.module('grafana.controllers'); var module = angular.module('grafana.controllers');
...@@ -12,26 +13,23 @@ function (angular, config, _, $) { ...@@ -12,26 +13,23 @@ function (angular, config, _, $) {
module.controller('GrafanaCtrl', function($scope, alertSrv, grafanaVersion, $rootScope) { module.controller('GrafanaCtrl', function($scope, alertSrv, grafanaVersion, $rootScope) {
$scope.grafanaVersion = grafanaVersion[0] === '@' ? 'master' : grafanaVersion; $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() }; $rootScope.performance = { loadStart: new Date().getTime() };
$scope.init = function() { $scope.init = function() {
$scope._ = _; $scope._ = _;
if ($rootScope.profilingEnabled) {
$scope.initProfiling(); if ($rootScope.profilingEnabled) { $scope.initProfiling(); }
}
$scope.dashAlerts = alertSrv; $scope.dashAlerts = alertSrv;
$scope.grafana = { $scope.grafana = { style: 'dark' };
style: 'dark'
};
}; };
$scope.toggleConsole = function() { $scope.toggleConsole = function() {
$scope.consoleEnabled = !$scope.consoleEnabled; $scope.consoleEnabled = !$scope.consoleEnabled;
window.localStorage.grafanaConsole = $scope.consoleEnabled ? 'true' : 'false'; store.set('grafanaConsole', $scope.consoleEnabled);
}; };
$rootScope.onAppEvent = function(name, callback) { $rootScope.onAppEvent = function(name, callback) {
......
define([ define([
'angular', 'angular',
'config' 'config',
'store'
], ],
function (angular, config) { function (angular, config, store) {
"use strict"; "use strict";
var module = angular.module('grafana.routes'); var module = angular.module('grafana.routes');
...@@ -11,12 +12,7 @@ function (angular, config) { ...@@ -11,12 +12,7 @@ function (angular, config) {
$routeProvider $routeProvider
.when('/', { .when('/', {
redirectTo: function() { redirectTo: function() {
if (window.localStorage && window.localStorage.grafanaDashboardDefault) { return store.get('grafanaDashboardDefault') || config.default_route;
return window.localStorage.grafanaDashboardDefault;
}
else {
return config.default_route;
}
} }
}); });
}); });
......
define([ define([
'angular', 'angular',
'lodash', 'lodash',
'kbn' 'kbn',
'store'
], ],
function (angular, _, kbn) { function (angular, _, kbn, store) {
'use strict'; 'use strict';
var module = angular.module('grafana.services'); var module = angular.module('grafana.services');
...@@ -13,14 +14,14 @@ function (angular, _, kbn) { ...@@ -13,14 +14,14 @@ function (angular, _, kbn) {
var favorites = { dashboards: [] }; var favorites = { dashboards: [] };
this.init = function() { this.init = function() {
var existingJson = window.localStorage["grafana-favorites"]; var existingJson = store.get("grafana-favorites");
if (existingJson) { if (existingJson) {
favorites = angular.fromJson(existingJson); favorites = angular.fromJson(existingJson);
} }
}; };
this._save = function() { this._save = function() {
window.localStorage["grafana-favorites"] = angular.toJson(favorites); store.set('grafana-favorites', angular.toJson(favorites));
}; };
this._find = function(title) { this._find = function(title) {
......
...@@ -6,6 +6,7 @@ require.config({ ...@@ -6,6 +6,7 @@ require.config({
mocks: '../test/mocks', mocks: '../test/mocks',
config: '../config.sample', config: '../config.sample',
kbn: 'components/kbn', kbn: 'components/kbn',
store: 'components/store',
settings: 'components/settings', settings: 'components/settings',
lodash: 'components/lodash.extended', lodash: 'components/lodash.extended',
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment