reorganized qmCSV service

added heraldry markers
This commit is contained in:
Ryan Carpenter
2016-07-29 11:40:57 +08:00
parent f83ef8d3af
commit d6cfadb37a
10 changed files with 799 additions and 672 deletions
+66 -152
View File
@@ -1,13 +1,21 @@
angular.module('quartermaester')
.controller("mapCtrl", function($scope, $q, $filter, $timeout, uiGmapGoogleMapApi, smgMapType, qmCsv) {
.controller("mapCtrl", function($scope, $filter, $timeout, uiGmapGoogleMapApi, smgMapType, qmCsv) {
var qmData = {};
$scope.state = "slider";
$scope.search = "";
$scope.slider = {
show: "episodes",
currentEpisode: 0,
currentChapter: 0
};
$scope.options = {
characterPaths: true,
houseHeraldry: false,
geographicRegions: false,
politicalAllegiances: false
};
$scope.map = {
center: {
latitude: 1.3182,
@@ -16,60 +24,67 @@ angular.module('quartermaester')
zoom: 4,
options: {
maxZoom: 5,
minZoom: 1
minZoom: 1,
disableDefaultUI: true,
zoomControl: true
},
events: {
click: mapClick
},
control: {},
smgMapType: smgMapType,
locationClick: locationClick,
locations: []
heraldryClick: heraldryClick
};
$scope.options = {
characterPaths: true,
houseHeraldry: false,
geographicRegions: false,
politicalAllegiances: false
$scope.mapModels = {
towns: [],
heraldry: [],
characters: [],
paths: [],
regions: [],
loyalties: []
};
$scope.search = "";
$scope.smgMapType = smgMapType;
$scope.episodes = [];
$scope.chapters = [];
$scope.searchResults = [];
$scope.toState = toState;
$scope.slideTo = slideTo;
$scope.panTo = panTo;
$scope.episodes = [];
$scope.chapters = [];
$scope.searchResults = [
{
icon: "glyphicon-user",
title: "Eddard Stark",
subtitle: "House Stark",
search: "Eddard Stark",
click: function(){showCharacter("eddard")}
}
];
$scope.refreshMap = refreshMap;
$scope.locationDetail = null;
// Set up promises
uiGmapGoogleMapApi.then(onMapLoad);
$q.all([
qmCsv.get('locations.csv'),
qmCsv.get('houses.csv')
]).then(createLocationMarkers);
$q.all([
qmCsv.get('books.csv'),
qmCsv.get('chapters.csv')
]).then(addChaptersToScope);
qmCsv.get('episodes.csv').then(addEpisodesToScope);
$q.all([
qmCsv.get('characters.csv'),
qmCsv.get('houses.csv')
]).then(addCharactersToScope);
$q.all([
qmCsv.get('regions.csv'),
qmCsv.get('borders.csv')
]).then(addRegionsToScope);
qmCsv.loadData().then(addToScope);
//////////////////
function addToScope(d) {
qmData = d;
// Add data from CSV files into $scope
$scope.episodes = qmData.episodes;
$scope.chapters = qmData.chapters;
$scope.characters = qmData.characters;
$scope.searchResults = qmData.searchResults;
$scope.mapModels.regions = qmData.regions;
$scope.$watch('slider', refreshMap, true);
$scope.$watch('options', refreshMap, true);
return true;
}
function refreshMap() {
// Redraw map with new slider values
$scope.mapModels.towns = $filter('qmSlider')(qmData.towns, $scope.slider);
$scope.mapModels.heraldry = $scope.options.houseHeraldry ? $filter('qmSlider')(qmData.heraldry, $scope.slider) : [];
//console.log("heraldry", $scope.mapModels.heraldry);
}
function toState(stateName) {
$scope.state = stateName;
if (stateName=="search")
@@ -79,21 +94,22 @@ angular.module('quartermaester')
}
function slideTo(input) {
var books = $scope.books.map(function(book) {return book.abbreviation;});
var sliderIndex = ($scope.slider.show=="episodes") ? "currentEpisode" : "currentChapter";
console.log("qmData", qmData);
var books = qmData.books.map(function(book) {return book.abbreviation;});
var measure = ($scope.slider.show=="episodes") ? "currentEpisode" : "currentChapter";
var sliderMax = ($scope.slider.show=="episodes") ? $scope.episodes.length-1 : $scope.chapters.length-1;
switch (input) {
case -10:
$scope.slider[sliderIndex] = 0;
$scope.slider[measure] = 0;
break;
case -1:
if ($scope.slider[sliderIndex]>0) $scope.slider[sliderIndex]--;
if ($scope.slider[measure]>0) $scope.slider[measure]--;
break;
case 1:
if ($scope.slider[sliderIndex]<sliderMax) $scope.slider[sliderIndex]++;
if ($scope.slider[measure]<sliderMax) $scope.slider[measure]++;
break;
case 10:
$scope.slider[sliderIndex] = sliderMax;
$scope.slider[measure] = sliderMax;
break;
default:
$scope.state = "slider";
@@ -107,7 +123,7 @@ angular.module('quartermaester')
$scope.slider.show = "chapters";
var reMatch = /(\w{4})\-(\d+)/.exec(input);
var bookId = books.indexOf(reMatch[1]);
$scope.slider.currentChapter = parseInt($scope.books[bookId].precedingChapters, 10) + parseInt(reMatch[2], 10);
$scope.slider.currentChapter = parseInt(qmData.books[bookId].precedingChapters, 10) + parseInt(reMatch[2], 10);
break;
}
@@ -122,114 +138,12 @@ angular.module('quartermaester')
$scope.state = "location";
}
function addEpisodesToScope(response) {
$scope.episodes = response.map(function(episode) {
var epNumber = (episode.episode.length==2) ? episode.episode : "0"+episode.episode;
return {
name: "S" + episode.season + "E" + epNumber + ": " + episode.title,
url: (episode.awoiaf=="") ? "https://en.wikipedia.org/wiki/"+episode.wikipedia : "http://awoiaf.westeros.org/index.php/"+episode.awoiaf
};
});
//console.log("e", $scope.episodes);
$scope.searchResults = $scope.searchResults.concat(response.map(function(episode) {
var epNumber = (episode.episode.length==2) ? episode.episode : "0"+episode.episode;
return {
icon: "glyphicon-facetime-video",
title: episode.title,
subtitle: "HBO episode",
search: episode.title,
abbr: "S" + episode.season + "E" + epNumber + ": " + episode.title,
click: function(){slideTo(this.abbr)}
};
}));
}
function addChaptersToScope(response) {
$scope.books = response[0];
$scope.chapters = response[1].map(function(chapter) {
return {
name: response[0][chapter.book].abbreviation + "-" + chapter.chapter + ": " + chapter.title,
url: "http://awoiaf.westeros.org/index.php/"+chapter.awoiaf
};
});
//console.log("c", $scope.chapters);
$scope.searchResults = $scope.searchResults.concat(response[1].map(function(chapter) {
return {
icon: "glyphicon-book",
title: chapter.title,
subtitle: response[0][chapter.book].title,
search: chapter.title,
abbr: response[0][chapter.book].abbreviation + "-" + chapter.chapter,
click: function(){slideTo(this.abbr)}
};
}));
}
function addCharactersToScope(responses) {
$scope.characters = responses[0].map(function(character) {
var output = {
name: character.name,
url: "http://awoiaf.westeros.org/index.php/" + character.awoiaf,
house: ""
}
if (character.house!="") {
matchingHouse = $filter('filter')(responses[1], {wikiKey: character.house})[0];
output.house = {
name: decodeURI(matchingHouse.wikiKey).replace(/_/g," "),
url: "http://awoiaf.westeros.org/index.php/" + matchingHouse.wikiKey,
img: matchingHouse.img
}
}
return output;
});
}
function addRegionsToScope(responses) {
// console.log("regions", responses[0]);
// console.log("borders", responses[1]);
var borderArrays = {};
for (var i=0; i<responses[1].length; i++) {
if (!(responses[1][i].name in borderArrays)) borderArrays[responses[1][i].name] = [];
borderArrays[responses[1][i].name].push(responses[1][i]);
}
$scope.map.regions = responses[0].map(function(region) {
return {
key: region.name,
fill: {
color: region.color,
opacity: 0.5
},
stroke: {
weight: 0
},
path: borderArrays[region.name]
};
});
}
function createLocationMarkers(responses) {
$scope.houses = responses[1];
$scope.map.locations = qmCsv.setHomes(responses[0], responses[1]);
//console.log("locations", $scope.map.locations);
$scope.searchResults = $scope.searchResults.concat($scope.map.locations.map(function(location) {
return {
icon: "glyphicon-map-marker",
key: location.key,
title: location.name,
subtitle: (location.occupants.length) ? location.occupants[0].name : "",
search: (location.occupants.length) ? location.name + " " + location.occupants[0].name : location.name,
click: function(){panTo(this.key)}
};
}));
}
function locationClick(marker, eventName, model) {
model.currentOccupant = qmCsv.getCurrentOccupant(model);
$scope.locationDetail = model;
$scope.state = "location";
}
function heraldryClick(marker, eventName, model) {
$scope.locationDetail = model;
$scope.state = "location";
}