From 9c9737f9785c4f0c7a167851843070224fdc59ea Mon Sep 17 00:00:00 2001 From: Colin McLeod Date: Wed, 27 May 2015 01:08:51 -0700 Subject: [PATCH] Handle localstorage failure more gracefully --- app/js/controllers/controller-outfit.js | 1 + app/js/directives/directive-header.js | 8 ++-- app/js/service-persist.js | 53 +++++++++++++++++++++---- app/views/page-outfit.html | 2 +- 4 files changed, 52 insertions(+), 12 deletions(-) diff --git a/app/js/controllers/controller-outfit.js b/app/js/controllers/controller-outfit.js index e26af50c..d0e480ee 100755 --- a/app/js/controllers/controller-outfit.js +++ b/app/js/controllers/controller-outfit.js @@ -25,6 +25,7 @@ angular.module('app').controller('OutfitController', ['$rootScope','$scope', '$s $scope.availCS = Components.forShip(ship.id); $scope.selectedSlot = null; $scope.savedCode = Persist.getBuild(ship.id, $scope.buildName); + $scope.canSave = Persist.isEnabled(); $scope.jrSeries = { xMin: ship.unladenMass, diff --git a/app/js/directives/directive-header.js b/app/js/directives/directive-header.js index 9fbc752d..e4cd4a6d 100755 --- a/app/js/directives/directive-header.js +++ b/app/js/directives/directive-header.js @@ -1,4 +1,4 @@ -angular.module('app').directive('shipyardHeader', ['lodash','$window','$rootScope', 'Persist', 'ShipsDB', function (_, $window, $rootScope, Persist, ships) { +angular.module('app').directive('shipyardHeader', ['lodash', '$rootScope', 'Persist', 'ShipsDB', function (_, $rootScope, Persist, ships) { return { restrict: 'E', @@ -20,7 +20,7 @@ angular.module('app').directive('shipyardHeader', ['lodash','$window','$rootScop ] }; - var insIndex = _.findIndex($rootScope.insurance.opts, 'name', $window.localStorage.getItem('insurance')); + var insIndex = _.findIndex($rootScope.insurance.opts, 'name', Persist.getInsurance()); $rootScope.insurance.current = $rootScope.insurance.opts[insIndex != -1? insIndex : 0]; // Close menus if a navigation change event occurs @@ -35,10 +35,10 @@ angular.module('app').directive('shipyardHeader', ['lodash','$window','$rootScop }); /** - * Save selected insurance option in localstorage + * Save selected insurance option */ scope.updateInsurance = function(){ - $window.localStorage.setItem('insurance', $rootScope.insurance.current.name); + Persist.setInsurance($rootScope.insurance.current.name); }; scope.openMenu = function (e, menu) { diff --git a/app/js/service-persist.js b/app/js/service-persist.js index 822372b5..62a3bc9d 100755 --- a/app/js/service-persist.js +++ b/app/js/service-persist.js @@ -5,12 +5,22 @@ angular.module('app').service('Persist', ['$window','lodash', function ($window, var LS_KEY_BUILDS = 'builds'; var LS_KEY_COMPARISONS = 'comparisons'; var localStorage = $window.localStorage; - var buildJson = localStorage.getItem(LS_KEY_BUILDS); - var comparisonJson = localStorage.getItem(LS_KEY_COMPARISONS); + var buildJson = null; + var comparisonJson = null; + + // Safe check to determine if localStorage is enabled + try { + localStorage.setItem('s', 1); + localStorage.removeItem('s'); + buildJson = localStorage.getItem(LS_KEY_BUILDS); + comparisonJson = localStorage.getItem(LS_KEY_COMPARISONS); + this.lsEnabled = true; + } catch(e) { + this.lsEnabled = false; + } this.builds = buildJson? angular.fromJson(buildJson) : {}; this.comparisons = comparisonJson? angular.fromJson(comparisonJson) : {}; - var buildCount = Object.keys(this.builds).length; this.state = { @@ -26,6 +36,10 @@ angular.module('app').service('Persist', ['$window','lodash', function ($window, * @param {string} code The serialized code */ this.saveBuild = function (shipId, name, code) { + if (!this.lsEnabled) { + return; + } + if (!this.builds[shipId]) { this.builds[shipId] = {}; } @@ -63,7 +77,7 @@ angular.module('app').service('Persist', ['$window','lodash', function ($window, * @param {string} name The name of the build */ this.deleteBuild = function (shipId, name) { - if(this.builds[shipId][name]) { + if(this.lsEnabled && this.builds[shipId][name]) { delete this.builds[shipId][name]; if (Object.keys(this.builds[shipId]).length === 0) { delete this.builds[shipId]; @@ -94,6 +108,10 @@ angular.module('app').service('Persist', ['$window','lodash', function ($window, * @param {array} facets Array of facet indices */ this.saveComparison = function (name, builds, facets){ + if (!this.lsEnabled) { + return; + } + if (!this.comparisons[name]) { this.comparisons[name] = {}; } @@ -122,7 +140,7 @@ angular.module('app').service('Persist', ['$window','lodash', function ($window, * @param {string} name Comparison name */ this.deleteComparison = function (name) { - if (this.comparisons[name]) { + if (this.lsEnabled && this.comparisons[name]) { delete this.comparisons[name]; localStorage.setItem(LS_KEY_COMPARISONS, angular.toJson(this.comparisons)); this.state.hasComparisons = Object.keys(this.comparisons).length > 0; @@ -137,8 +155,29 @@ angular.module('app').service('Persist', ['$window','lodash', function ($window, angular.copy({}, this.comparisons); this.state.hasBuilds = false; this.state.buildCount = 0; - localStorage.removeItem(LS_KEY_BUILDS); - localStorage.removeItem(LS_KEY_COMPARISONS); + + if (this.lsEnabled) { + localStorage.removeItem(LS_KEY_BUILDS); + localStorage.removeItem(LS_KEY_COMPARISONS); + } + + }; + + this.getInsurance = function () { + if (this.lsEnabled) { + return localStorage.getItem('insurance'); + } + return null; + }; + + this.setInsurance = function (name) { + if (this.lsEnabled) { + return localStorage.setItem('insurance', name); + } + }; + + this.isEnabled = function() { + return this.lsEnabled; }; }]); diff --git a/app/views/page-outfit.html b/app/views/page-outfit.html index f417b20e..97980737 100755 --- a/app/views/page-outfit.html +++ b/app/views/page-outfit.html @@ -4,7 +4,7 @@

-