Handle localstorage failure more gracefully

This commit is contained in:
Colin McLeod
2015-05-27 01:08:51 -07:00
parent a1939fad55
commit 9c9737f978
4 changed files with 52 additions and 12 deletions

View File

@@ -25,6 +25,7 @@ angular.module('app').controller('OutfitController', ['$rootScope','$scope', '$s
$scope.availCS = Components.forShip(ship.id); $scope.availCS = Components.forShip(ship.id);
$scope.selectedSlot = null; $scope.selectedSlot = null;
$scope.savedCode = Persist.getBuild(ship.id, $scope.buildName); $scope.savedCode = Persist.getBuild(ship.id, $scope.buildName);
$scope.canSave = Persist.isEnabled();
$scope.jrSeries = { $scope.jrSeries = {
xMin: ship.unladenMass, xMin: ship.unladenMass,

View File

@@ -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 { return {
restrict: 'E', 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]; $rootScope.insurance.current = $rootScope.insurance.opts[insIndex != -1? insIndex : 0];
// Close menus if a navigation change event occurs // 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(){ scope.updateInsurance = function(){
$window.localStorage.setItem('insurance', $rootScope.insurance.current.name); Persist.setInsurance($rootScope.insurance.current.name);
}; };
scope.openMenu = function (e, menu) { scope.openMenu = function (e, menu) {

View File

@@ -5,12 +5,22 @@ angular.module('app').service('Persist', ['$window','lodash', function ($window,
var LS_KEY_BUILDS = 'builds'; var LS_KEY_BUILDS = 'builds';
var LS_KEY_COMPARISONS = 'comparisons'; var LS_KEY_COMPARISONS = 'comparisons';
var localStorage = $window.localStorage; var localStorage = $window.localStorage;
var buildJson = localStorage.getItem(LS_KEY_BUILDS); var buildJson = null;
var comparisonJson = localStorage.getItem(LS_KEY_COMPARISONS); 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.builds = buildJson? angular.fromJson(buildJson) : {};
this.comparisons = comparisonJson? angular.fromJson(comparisonJson) : {}; this.comparisons = comparisonJson? angular.fromJson(comparisonJson) : {};
var buildCount = Object.keys(this.builds).length; var buildCount = Object.keys(this.builds).length;
this.state = { this.state = {
@@ -26,6 +36,10 @@ angular.module('app').service('Persist', ['$window','lodash', function ($window,
* @param {string} code The serialized code * @param {string} code The serialized code
*/ */
this.saveBuild = function (shipId, name, code) { this.saveBuild = function (shipId, name, code) {
if (!this.lsEnabled) {
return;
}
if (!this.builds[shipId]) { if (!this.builds[shipId]) {
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 * @param {string} name The name of the build
*/ */
this.deleteBuild = function (shipId, name) { this.deleteBuild = function (shipId, name) {
if(this.builds[shipId][name]) { if(this.lsEnabled && this.builds[shipId][name]) {
delete this.builds[shipId][name]; delete this.builds[shipId][name];
if (Object.keys(this.builds[shipId]).length === 0) { if (Object.keys(this.builds[shipId]).length === 0) {
delete this.builds[shipId]; delete this.builds[shipId];
@@ -94,6 +108,10 @@ angular.module('app').service('Persist', ['$window','lodash', function ($window,
* @param {array} facets Array of facet indices * @param {array} facets Array of facet indices
*/ */
this.saveComparison = function (name, builds, facets){ this.saveComparison = function (name, builds, facets){
if (!this.lsEnabled) {
return;
}
if (!this.comparisons[name]) { if (!this.comparisons[name]) {
this.comparisons[name] = {}; this.comparisons[name] = {};
} }
@@ -122,7 +140,7 @@ angular.module('app').service('Persist', ['$window','lodash', function ($window,
* @param {string} name Comparison name * @param {string} name Comparison name
*/ */
this.deleteComparison = function (name) { this.deleteComparison = function (name) {
if (this.comparisons[name]) { if (this.lsEnabled && this.comparisons[name]) {
delete this.comparisons[name]; delete this.comparisons[name];
localStorage.setItem(LS_KEY_COMPARISONS, angular.toJson(this.comparisons)); localStorage.setItem(LS_KEY_COMPARISONS, angular.toJson(this.comparisons));
this.state.hasComparisons = Object.keys(this.comparisons).length > 0; 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); angular.copy({}, this.comparisons);
this.state.hasBuilds = false; this.state.hasBuilds = false;
this.state.buildCount = 0; this.state.buildCount = 0;
if (this.lsEnabled) {
localStorage.removeItem(LS_KEY_BUILDS); localStorage.removeItem(LS_KEY_BUILDS);
localStorage.removeItem(LS_KEY_COMPARISONS); 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;
}; };
}]); }]);

View File

@@ -4,7 +4,7 @@
<h1 ng-bind="ship.name"></h1> <h1 ng-bind="ship.name"></h1>
<div id="build"> <div id="build">
<input ng-model="buildName" ng-change="bnChange()" placeholder="Enter Build Name" maxlength="50" /> <input ng-model="buildName" ng-change="bnChange()" placeholder="Enter Build Name" maxlength="50" />
<button ng-click="saveBuild()" ng-disabled="!buildName || savedCode && code == savedCode"> <button ng-click="saveBuild()" ng-disabled="!buildName || savedCode && code == savedCode || !canSave">
<svg class="icon lg "><use xlink:href="#floppy-disk"></use></svg> Save <svg class="icon lg "><use xlink:href="#floppy-disk"></use></svg> Save
</button> </button>
<button ng-click="reloadBuild()" ng-disabled="!savedCode || code == savedCode"> <button ng-click="reloadBuild()" ng-disabled="!savedCode || code == savedCode">