Implement export for 3rd Party sites

This commit is contained in:
Colin McLeod
2015-07-15 16:13:17 -07:00
parent 82a87cb653
commit ab1ea53ce3
11 changed files with 325 additions and 68 deletions

View File

@@ -5,7 +5,7 @@ angular.module('app').controller('ExportController', ['$scope', '$stateParams',
if ($stateParams.promise) {
$scope.export = 'Generating...';
$stateParams.promise.then(function(data) {
$scope.export = data;
$scope.export = (typeof data === 'object') ? angular.toJson(data, true) : data;
});
} else {
$scope.export = angular.toJson($stateParams.data, true);

View File

@@ -7,7 +7,7 @@ angular.module('app').controller('ImportController', ['$scope', '$stateParams',
$scope.ships = Ships;
$scope.validateJson = function() {
var importObj = null;
var importObj = null, shipData = null;
$scope.jsonValid = false;
$scope.errorMsg = null;
$scope.builds = null;
@@ -22,36 +22,68 @@ angular.module('app').controller('ImportController', ['$scope', '$stateParams',
}
if (typeof importObj != 'object') {
$scope.errorMsg = 'Must be an object!';
$scope.errorMsg = 'Must be an object or array!';
return;
}
if ((!importObj.builds || !Object.keys(importObj.builds).length)) {
$scope.errorMsg = 'No builds in data';
return;
}
for (var shipId in importObj.builds) {
var shipData = Ships[shipId];
if (shipData) {
for (var buildName in importObj.builds[shipId]) {
if (typeof importObj.builds[shipId][buildName] != 'string') {
$scope.errorMsg = shipData.properties.name + ' build "' + buildName + '" must be a string!';
return;
// Using JSON from a simple/shortform/standard export
if (importObj.builds && Object.keys(importObj.builds).length) {
for (var shipId in importObj.builds) {
shipData = Ships[shipId];
if (shipData) {
for (var buildName in importObj.builds[shipId]) {
if (typeof importObj.builds[shipId][buildName] != 'string') {
$scope.errorMsg = shipData.properties.name + ' build "' + buildName + '" must be a string!';
return;
}
try {
// Actually build the ship with the code to ensure it's valid
Serializer.toShip(new Ship(shipId, shipData.properties, shipData.slots), importObj.builds[shipId][buildName]);
} catch (e) {
$scope.errorMsg = shipData.properties.name + ' build "' + buildName + '" is not valid!';
return;
}
}
try {
// Actually build the ship with the code to ensure it's valid
Serializer.toShip(new Ship(shipId, shipData.properties, shipData.slots), importObj.builds[shipId][buildName]);
} catch (e) {
$scope.errorMsg = shipData.properties.name + ' build "' + buildName + '" is not valid!';
} else {
$scope.errorMsg = '"' + shipId + '"" is not a valid Ship Id!';
return;
}
$scope.builds = importObj.builds;
}
// Using JSON from a detailed export
} else if (importObj.length && importObj[0].references && importObj[0].references.length) {
var builds = {};
for (var i = 0, l = importObj.length; i < l; i++) {
if (typeof importObj[i].name != 'string' || typeof importObj[i].ship != 'string') {
$scope.errorMsg = 'Build [' + i + '] must have a ship and build name!';
return;
}
for (var r = 0, rl = importObj[i].references.length; r < rl; r++) {
var ref = importObj[i].references[r];
if (ref.name == 'Coriolis.io' && ref.code && ref.shipId) {
if (!builds[ref.shipId]) {
builds[ref.shipId] = {};
}
try {
// Actually build the ship with the code to ensure it's valid
shipData = Ships[ref.shipId];
Serializer.toShip(new Ship(ref.shipId, shipData.properties, shipData.slots), ref.code);
} catch (e) {
$scope.errorMsg = importObj[i].ship + ' build "' + importObj[i].name + '" is not valid!';
return;
}
builds[ref.shipId][importObj[i].name] = ref.code;
} else {
$scope.errorMsg = importObj[i].ship + ' build "' + importObj[i].name + '" has an invalid Coriolis reference!';
return;
}
}
} else {
$scope.errorMsg = '"' + shipId + '"" is not a valid Ship Id!';
return;
}
$scope.builds = importObj.builds;
$scope.builds = builds;
} else {
$scope.errorMsg = 'No builds in data';
return;
}
$scope.jsonValid = true;

View File

@@ -219,12 +219,7 @@ angular.module('app').controller('OutfitController', ['$window', '$rootScope', '
if ($scope.buildName) {
$state.go('modal.export', {
data: Serializer.toJsonBuild(
$scope.buildName,
ship,
$state.href($state.current.name, $state.params, { absolute: true }),
$scope.code || Serializer.fromShip(ship)
)
data: Serializer.toDetailedBuild($scope.buildName, ship, $scope.code || Serializer.fromShip(ship))
});
}
};

View File

@@ -1,4 +1,4 @@
angular.module('app').directive('shipyardHeader', ['lodash', '$rootScope', 'Persist', 'ShipsDB', function(_, $rootScope, Persist, ships) {
angular.module('app').directive('shipyardHeader', ['lodash', '$rootScope', '$state', 'Persist', 'Serializer', 'ShipsDB', function(_, $rootScope, $state, Persist, Serializer, ships) {
return {
restrict: 'E',
@@ -18,17 +18,6 @@ angular.module('app').directive('shipyardHeader', ['lodash', '$rootScope', 'Pers
$rootScope.discounts.ship = savedDiscounts[0];
$rootScope.discounts.components = savedDiscounts[1];
// Close menus if a navigation change event occurs
$rootScope.$on('$stateChangeStart', function() {
scope.openedMenu = null;
});
// Listen to close event to close opened menus or modals
$rootScope.$on('close', function() {
scope.openedMenu = null;
$rootScope.showAbout = false;
});
/**
* Save selected insurance option
*/
@@ -44,6 +33,13 @@ angular.module('app').directive('shipyardHeader', ['lodash', '$rootScope', 'Pers
$rootScope.$broadcast('discountChange');
};
scope.detailedExport = function(e) {
e.preventDefault();
e.stopPropagation();
scope.openedMenu = null;
$state.go('modal.export', { data: Serializer.toDetailedExport(scope.allBuilds) });
};
scope.openMenu = function(e, menu) {
e.stopPropagation();
if (menu == scope.openedMenu) {
@@ -58,16 +54,15 @@ angular.module('app').directive('shipyardHeader', ['lodash', '$rootScope', 'Pers
scope.openedMenu = menu;
};
scope.about = function(e) {
e.preventDefault();
e.stopPropagation();
// Close menus if a navigation change event occurs
$rootScope.$on('$stateChangeStart', function() {
scope.openedMenu = null;
$rootScope.showAbout = true;
};
});
$rootScope.hideAbout = function() {
$rootScope.showAbout = false;
};
// Listen to close event to close opened menus or modals
$rootScope.$on('close', function() {
scope.openedMenu = null;
});
scope.$watchCollection('allBuilds', function() {
scope.buildsList = Object.keys(scope.allBuilds).sort();

View File

@@ -1,7 +1,7 @@
/**
* Service managing seralization and deserialization of models for use in URLs and persistene.
*/
angular.module('app').service('Serializer', ['lodash', 'GroupMap', 'MountMap', function(_, GroupMap, MountMap) {
angular.module('app').service('Serializer', ['lodash', 'GroupMap', 'MountMap', 'ShipsDB', 'Ship', '$state', function(_, GroupMap, MountMap, ShipsDB, Ship, $state) {
/**
* Serializes the ships selected components for all slots to a URL friendly string.
@@ -66,18 +66,18 @@ angular.module('app').service('Serializer', ['lodash', 'GroupMap', 'MountMap', f
);
};
this.toJsonBuild = function(buildName, ship, url, code) {
this.toDetailedBuild = function(buildName, ship, code) {
var standard = ship.common,
hardpoints = ship.hardpoints,
internal = ship.internal;
var data = {
$schema: 'http://cdn.coriolis.io/schemas/ship-loadout/1-draft.json#',
$schema: 'http://cdn.coriolis.io/schemas/ship-loadout/1.json#',
name: buildName,
ship: ship.name,
references: [{
name: 'Coriolis.io',
url: url,
url: $state.href('outfit', { shipId: ship.id, code: code, bn: buildName }, { absolute: true }),
code: code,
shipId: ship.id
}],
@@ -108,6 +108,21 @@ angular.module('app').service('Serializer', ['lodash', 'GroupMap', 'MountMap', f
return data;
};
this.toDetailedExport = function(builds) {
var data = [];
for (var shipId in builds) {
for (var buildName in builds[shipId]) {
var code = builds[shipId][buildName];
var shipData = ShipsDB[shipId];
var ship = new Ship(shipId, shipData.properties, shipData.slots);
this.toShip(ship, code);
data.push(this.toDetailedBuild(buildName, ship, code));
}
}
return data;
};
this.fromComparison = function(name, builds, facets, predicate, desc) {
var shipBuilds = [];