mirror of
https://github.com/EDCD/coriolis.git
synced 2025-12-09 06:43:24 +00:00
Handle localstorage failure more gracefully
This commit is contained in:
@@ -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,
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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;
|
||||
|
||||
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;
|
||||
};
|
||||
|
||||
}]);
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
<h1 ng-bind="ship.name"></h1>
|
||||
<div id="build">
|
||||
<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
|
||||
</button>
|
||||
<button ng-click="reloadBuild()" ng-disabled="!savedCode || code == savedCode">
|
||||
|
||||
Reference in New Issue
Block a user