Custom comparisons, performance improvements

This commit is contained in:
Colin McLeod
2015-05-20 00:29:24 -07:00
parent 02fe76f43b
commit 6c1e3a7410
146 changed files with 1096 additions and 1062 deletions

View File

@@ -1,24 +1,23 @@
/**
* [description]
*/
angular.module('app').service('Persist', ['lodash', function (_) {
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);
if (buildJson) {
this.builds = angular.fromJson(localStorage.getItem(LS_KEY_BUILDS));
} else {
this.builds = {};
}
this.builds = buildJson? angular.fromJson(buildJson) : {};
this.comparisons = comparisonJson? angular.fromJson(comparisonJson) : {};
var buildCount = Object.keys(this.builds).length;
this.state = {
buildCount: buildCount,
hasBuilds: buildCount > 0
}
hasBuilds: buildCount > 0,
hasComparisons: Object.keys(this.comparisons).length > 0
};
/**
* Persist a ship build in local storage.
*
@@ -39,7 +38,7 @@ angular.module('app').service('Persist', ['lodash', function (_) {
this.builds[shipId][name] = code;
// Persist updated build collection to localStorage
localStorage.setItem(LS_KEY_BUILDS, angular.toJson(this.builds));
}
};
/**
* Get the serialized code/string for a build. Returns null if a
@@ -50,11 +49,11 @@ angular.module('app').service('Persist', ['lodash', function (_) {
* @return {string} The serialized build string.
*/
this.getBuild = function (shipId, name) {
if (this.builds[shipId]) {
if (this.builds[shipId] && this.builds[shipId][name]) {
return this.builds[shipId][name];
}
return null;
}
};
/**
* Delete a build from local storage. It will also delete the ship build collection if
@@ -66,13 +65,57 @@ angular.module('app').service('Persist', ['lodash', function (_) {
this.deleteBuild = function (shipId, name) {
if(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];
this.state.buildCount--;
this.state.hasBuilds = this.state.buildCount > 0;
}
// Persist updated build collection to localStorage
localStorage.setItem(LS_KEY_BUILDS, angular.toJson(this.builds));
// TODO: Check if build exists in comparisons
}
};
/**
* Persist a comparison in localstorage.
*
* @param {string} name The name of the comparison
* @param {array} builds Array of builds
* @param {array} facets Array of facet indices
*/
this.saveComparison = function (name, builds, facets){
if (!this.comparisons[name]) {
this.comparisons[name] = {};
}
this.comparisons[name] = {
facets: facets,
builds: _.map(builds, function (b) { return {shipId: b.id, buildName: b.buildName }; })
}
localStorage.setItem(LS_KEY_COMPARISONS, angular.toJson(this.comparisons));
this.state.hasComparisons = true;
}
/**
* [getComparison description]
* @param {string} name [description]
* @return {object} Object containing array of facets and ship id + build names
*/
this.getComparison = function (name) {
if (this.comparisons[name]) {
return this.comparisons[name];
}
return null;
}
/**
* Removes the comparison from localstorage.
* @param {string} name Comparison name
*/
this.deleteComparison = function (name) {
if (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;
}
}
@@ -83,8 +126,9 @@ angular.module('app').service('Persist', ['lodash', function (_) {
angular.copy({}, this.builds); // Empty object but keep original instance
angular.copy({}, this.comparisons);
this.state.hasBuilds = false;
this.state.buildCount = 0;
localStorage.removeItem(LS_KEY_BUILDS);
localStorage.removeItem(LS_KEY_COMPARISONS);
}
};
}]);