Skip to content

Commit

Permalink
Added service for birdwalk; added detail page
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark Cottman-Fields committed Nov 15, 2013
1 parent 3b46dd1 commit 0e46cc1
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 59 deletions.
4 changes: 2 additions & 2 deletions src/app/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ var app = angular.module('baw',

when('/attribution', {templateUrl: '/assets/attributions.html'}).

when('/birdWalks', {templateUrl: paths.site.files.birdWalks, controller: 'BirdWalkCtrl', title: 'Bird Walks'}).
when('/birdWalks/:birdWalkId', {templateUrl: paths.site.files.birdWalks, controller: 'BirdWalkCtrl', title: ':birdWalkId'}).
when('/birdWalks', {templateUrl: paths.site.files.birdWalk.list, controller: 'BirdWalksCtrl', title: 'Bird Walks'}).
when('/birdWalks/:birdWalkId', {templateUrl: paths.site.files.birdWalk.detail, controller: 'BirdWalkCtrl', title: ':birdWalkId'}).

// experiments
when('/experiments/:experiment',
Expand Down
8 changes: 8 additions & 0 deletions src/app/birdWalks/birdWalk.tpl.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<div class="row" data-ng-controller="BirdWalkCtrl">
{{params}}
<div class="col-sm-6 col-md-3" data-ng-repeat="(name, walkSpec) in spec.birdWalkSpec.walks">

</div>

{{spec.walks}}
</div>
76 changes: 23 additions & 53 deletions src/app/birdWalks/birdWalks.js
Original file line number Diff line number Diff line change
@@ -1,54 +1,24 @@
angular.module('bawApp.birdWalks', [])
.controller(
'BirdWalkCtrl',
['$scope', '$resource', '$routeParams','$route', '$http',
function BirdWalkCtrl($scope, $resource, $routeParams, $route, $http) {
// set up results
$scope.results = {
allowContact: true,
consented: false,
ethicsStatementViewed: false,
pageHit: (new Date()).toISOString()
};

$scope.spec = {};

$scope.imagesPath = '/assets/bird_walk/images/';

// download bird walk specification
downloadResources('/assets/bird_walk/bird_walk_spec.json', 'birdWalkSpec');

function downloadResources(primaryResource, primaryStoreProperty) {
var maxAttempts = 5;

function downloadRecursive(attemptsLeft, resource, storeProperty) {
if (attemptsLeft > 0) {
$http.get(resource + "?antiCache=" + Date.now().toString())
.success(function (data, status, headers, config) {
$scope.spec[storeProperty] = data;

console.info("Downloading resource " + resource + " succeeded.", data);

if (data.additionalResources) {
angular.forEach(data.additionalResources, function (value, key) {
downloadRecursive(maxAttempts, value, key);
});
}
})
.error(function (data, status, headers, config) {
console.error("Downloading resource " + resource + " failed. Attempt " + (maxAttempts - attemptsLeft) + " of " + maxAttempts);

downloadRecursive(attemptsLeft--, resource, storeProperty);
});
}
else {
console.error("Downloading resource " + resource + " failed after " + maxAttempts + "attempts");
}
}

downloadRecursive(maxAttempts, primaryResource, primaryStoreProperty);


}
}
]);
.controller('BirdWalksCtrl', ['$scope', '$resource', '$routeParams', '$route', '$http', 'BirdWalkService', 'conf.paths',
function BirdWalksCtrl($scope, $resource, $routeParams, $route, $http, BirdWalkService, paths) {
// set up results
$scope.results = {
allowContact: true,
consented: false,
ethicsStatementViewed: false,
pageHit: (new Date()).toISOString()
};

$scope.imagesPath = paths.site.files.birdWalk.images;

// download bird walk specification
BirdWalkService.getUrl(paths.site.files.birdWalk.spec, 'birdWalkSpec', $scope);
}])
.controller('BirdWalkCtrl', ['$scope', '$resource', '$routeParams', '$route', '$http', 'BirdWalkService', 'conf.paths',
function BirdWalkCtrl($scope, $resource, $routeParams, $route, $http, BirdWalkService, paths) {
$scope.imagesPath = paths.site.files.birdWalk.images;
BirdWalkService.getUrl(paths.site.files.birdWalk.spec, 'birdWalkSpec', $scope);
$scope.params = $routeParams;

//$scope.spec.birdWalkSpec.walks
}]);
6 changes: 3 additions & 3 deletions src/app/birdWalks/birdWalks.tpl.html
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<div class="row" data-ng-controller="BirdWalkCtrl">
<div class="row" data-ng-controller="BirdWalksCtrl">

<div class="col-sm-6 col-md-3" data-ng-repeat="(name, walkSpec) in spec.birdWalkSpec.walks">
<div class="thumbnail">
<img data-ng-src="{{$parent.imagesPath + $parent.spec.birdWalkSpec.locations[walkSpec.locationName].backgroundImageName}}"
<img data-ng-src="{{$parent.imagesPath + $parent.spec.birdWalkSpec.locations[walkSpec.locationName].smallImageName}}"
alt="{{name}}" class="img-responsive">

<div class="caption">
Expand All @@ -12,7 +12,7 @@ <h3>{{name}}
<p>{{walkSpec.description}}</p>

<p>
<a href="#" class="btn btn-default" role="button">Begin Bird Walk</a>
<a ng-href="birdWalks/{{walkSpec.id}}" class="btn btn-default" role="button">Begin Bird Walk</a>
</p>

<p class="text-muted small">
Expand Down
7 changes: 6 additions & 1 deletion src/baw.configuration.tpl.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,12 @@ angular.module('bawApp.configuration', [])
listen: 'listen/listen.tpl.html',
annotationViewer: 'annotationViewer/annotationViewer.tpl.html',
navigation: 'navigation/navigation.tpl.html',
birdWalks: 'birdWalks/birdWalks.tpl.html'
birdWalk : {
list: 'birdWalks/birdWalks.tpl.html',
detail: 'birdWalks/birdWalk.tpl.html',
spec: '/assets/bird_walk/bird_walk_spec.json',
images: '/assets/bird_walk/images/'
}
},
// routes used by angular
ngRoutes: {
Expand Down
31 changes: 31 additions & 0 deletions src/components/services/services.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,37 @@
return mediaResource;
}]);

bawss.factory('BirdWalkService', ['$rootScope', '$location', '$route', '$routeParams', '$http', function ($rootScope, $location, $route, $routeParams, $http) {

var birdWalkService = {};

var getUrl = function getUrl(downloadUrl, storeProperty, theScope) {
$http.get(downloadUrl, {
cache: true
})
.success(function (data, status, headers, config) {
console.info("Downloading resource " + downloadUrl + " succeeded.", data);
if (!theScope['spec']) {
theScope['spec'] = {};
}
theScope.spec[storeProperty] = data;

if (data.additionalResources) {
angular.forEach(data.additionalResources, function (value, key) {
getUrl(value, key);
});
}

}).error(function (data, status, headers, config) {
console.error("Downloading resource " + downloadUrl + " failed.");
});
};

birdWalkService.getUrl = getUrl;

return birdWalkService;
}]);

// breadcrumbs - from https://github.com/angular-app/angular-app/blob/master/client/src/common/services/breadcrumbs.js
bawss.factory('breadcrumbs', ['$rootScope', '$location', '$route', '$routeParams', function ($rootScope, $location, $route, $routeParams) {

Expand Down

0 comments on commit 0e46cc1

Please sign in to comment.