Skip to content

Commit

Permalink
feat(citizenScience): responses page for viewing locally saved responses
Browse files Browse the repository at this point in the history
  • Loading branch information
peichins committed Aug 10, 2018
1 parent 79257c8 commit fa3135b
Show file tree
Hide file tree
Showing 9 changed files with 242 additions and 74 deletions.
7 changes: 7 additions & 0 deletions src/app/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,13 @@ angular.module("baw",
fullWidth: true,
reloadOnUrl: false
}).
when(convertRouteParams(paths.site.ngRoutes.citizenScience.responses), {
templateUrl: "citizenScience/responses/responses.tpl.html",
controller: "ResponsesController",
title: "Citizen Science Responses",
fullWidth: true,
reloadOnUrl: false
}).
when("/citsci/ipswich", {
templateUrl: "citizenScience/ipswich/about.tpl.html",
controller: "IpswichAboutController",
Expand Down
9 changes: 6 additions & 3 deletions src/app/citizenScience/bristlebird/bristlebird.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class BristlebirdController {
$location,
CitizenScienceCommon,
CsSamples,
CsLabels,
SampleLabels,
backgroundImage,
paths) {
Expand Down Expand Up @@ -102,7 +103,7 @@ class BristlebirdController {

this.showAudio = CitizenScienceCommon.bindShowAudio($scope);

CsSamples.getLabels($scope.csProject).then(function (labels) {
CsLabels.getLabels($scope.csProject).then(function (labels) {
$scope.labels = labels;
});

Expand All @@ -111,7 +112,7 @@ class BristlebirdController {
/**
* Retrieve settings about this citizen science project
*/
CsSamples.getSettings($scope.csProject).then(
CsLabels.getSettings($scope.csProject).then(
function (settings) {
$scope.settings = settings;
if ($scope.settings.hasOwnProperty("sampleDuration")) {
Expand Down Expand Up @@ -175,7 +176,8 @@ angular
"bawApp.components.citizenScienceThumbLabels",
"bawApp.components.onboarding",
"bawApp.components.background",
"bawApp.citizenScience.csSamples"
"bawApp.citizenScience.csSamples",
"bawApp.citizenScience.csLabels"
])
.controller(
"BristlebirdController",
Expand All @@ -185,6 +187,7 @@ angular
"$location",
"CitizenScienceCommon",
"CsSamples",
"CsLabels",
"SampleLabels",
"backgroundImage",
"conf.paths",
Expand Down
1 change: 1 addition & 0 deletions src/app/citizenScience/citizenScience.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ angular
.module("bawApp.citizenScience", [
"bawApp.citizenScience.bristlebird",
"bawApp.citizenScience.ipswich",
"bawApp.citizenScience.responses"
])
.controller(
"CitizenScienceController",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ csSamples.factory("CsSamples", [
function CsSamples(CitizenScienceCommon, $http, DatasetItem) {

var self = this;
self.useLocalData = true;
self.sheets_api_url = "http://" + window.location.hostname + ":8081";
self.local_api_url = "/public/citizen_science";

// the dataset id for this citizen science project
// todo: integrate with settings for cs project
Expand Down Expand Up @@ -91,35 +88,6 @@ csSamples.factory("CsSamples", [

self.setCurrentItem();


/**
* Constructs a url for the request by concatenating the arguments, joined by "/"
* and appending to the relevant baseURL. Allows experimenting with different sources
* for the data without changing everything
* @returns {string|*}
*/
self.apiUrl = function () {
// convert to array
var base_url, url;
if (self.useLocalData) {
base_url = self.local_api_url;
} else {
base_url = self.sheets_api_url;
}
var args = Array.prototype.slice.call(arguments);

url = [base_url].concat(args).join("/");

if (self.useLocalData) {
url = url + ".json";
}

return url;
};




/**
* Adds a new page of items to the list of pages
* @param thenSelectNewItem boolean; if true will update the current item to be the first item
Expand All @@ -144,10 +112,10 @@ csSamples.factory("CsSamples", [
if (thenSelectNewItem) {
self.currentIndex.page = nextPageNum - 1;
self.currentIndex.item = 0;
self.setCurrentItem();
}
self.setCurrentItem();
});

});

};

Expand Down Expand Up @@ -196,38 +164,6 @@ csSamples.factory("CsSamples", [
return Boolean(nextIndex);
},

/**
* Gets all labels associated with the specified citizen science project
* @param project string
*/
getLabels: function (project) {
var response = $http.get(self.apiUrl(
"labels",
project
));

return response.then(function (response) {
var labels = [];
if (Array.isArray(response.data)) {
labels = response.data;
}

return labels;
});
},

/**
* Gets all settings associated with the specified citizen science project
* @param project string
* @returns {HttpPromise}
*/
getSettings: function (project) {
return $http.get(self.apiUrl(
"settings",
project
));
},

currentItem: function () {
return self.currentItem;
}
Expand All @@ -237,5 +173,3 @@ csSamples.factory("CsSamples", [
return self.publicFunctions;

}]);


85 changes: 85 additions & 0 deletions src/app/citizenScience/labels/citizenScienceLabels.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
var csLabels = angular.module("bawApp.citizenScience.csLabels", ["bawApp.citizenScience.common"]);


/**
* Manages the data for labels that will be applied to cs samples
*/
csLabels.factory("CsLabels", [
"CitizenScienceCommon",
"$http",
function CsLabels(CitizenScienceCommon, $http) {

var self = this;
self.useLocalData = true;
self.sheets_api_url = "http://" + window.location.hostname + ":8081";
self.local_api_url = "/public/citizen_science";


/**
* Constructs a url for the request by concatenating the arguments, joined by "/"
* and appending to the relevant baseURL. Allows experimenting with different sources
* for the data without changing everything
* @returns {string|*}
*/
self.apiUrl = function () {
// convert to array
var base_url, url;
if (self.useLocalData) {
base_url = self.local_api_url;
} else {
base_url = self.sheets_api_url;
}
var args = Array.prototype.slice.call(arguments);

url = [base_url].concat(args).join("/");

if (self.useLocalData) {
url = url + ".json";
}

return url;
};


self.publicFunctions = {


/**
* Gets all labels associated with the specified citizen science project
* @param project string
*/
getLabels: function (project) {
var response = $http.get(self.apiUrl(
"labels",
project
));

return response.then(function (response) {
var labels = [];
if (Array.isArray(response.data)) {
labels = response.data;
}

return labels;
});
},

/**
* Gets all settings associated with the specified citizen science project
* @param project string
* @returns {HttpPromise}
*/
getSettings: function (project) {
return $http.get(self.apiUrl(
"settings",
project
));
}

};

return self.publicFunctions;

}]);


Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var sampleLabels = angular.module("bawApp.citizenScience.sampleLabels", ["bawApp.citizenScience.common"]);
var sampleLabels = angular.module("bawApp.citizenScience.sampleLabels",
["bawApp.citizenScience.common", "baw"]);

/**
*
Expand All @@ -20,10 +21,13 @@ var sampleLabels = angular.module("bawApp.citizenScience.sampleLabels", ["bawApp
sampleLabels.factory("SampleLabels", [
"CitizenScienceCommon",
"$http",
function SampleLabels(CitizenScienceCommon, $http) {
"DatasetItem",
function SampleLabels(CitizenScienceCommon, $http, DatasetItem) {

var self = this;

self.datasetId = 3;

/**
* checks the local storage for sampleLabels
*/
Expand Down Expand Up @@ -192,11 +196,71 @@ sampleLabels.factory("SampleLabels", [
clearLabels : function () {
self.data = {};
self.writeToStorage();
}
},

/**
* Combines the SampleLabels data with the samples and labels
* to return the full report of which labels have been applied to which samples
* @return {{}|*}
*/
getData : function (labels) {

var d = self.data;

var keys = Object.keys(d);

if (keys.length === 0) {
return d;
}

var currentKey = -1;

var addItemDetails = function (response) {

var datasetItemId = keys[currentKey];

d[datasetItemId] = {
"sample" : response.data.data[0],
"labels" : JSON.parse(JSON.stringify(d[datasetItemId]))
};

for (var labelId in d[datasetItemId].labels) {

if (d[datasetItemId].labels.hasOwnProperty(labelId)) {

d[datasetItemId].labels[labelId] = {
"label": labels.find(l => true),
"response": JSON.parse(JSON.stringify(d[datasetItemId].labels[labelId]))
};

}

}

requestNextItem();


};

var requestFailed = function (response) {
requestNextItem();
};

var requestNextItem = function () {
currentKey++;
// recurse to do the next dataset item
if (currentKey < keys.length) {
DatasetItem.datasetItem(self.datasetId, keys[currentKey]).then(addItemDetails, requestFailed);
}
};

// request the first dataset item and add it to the returned object.
// when that is finished it will do the next one.
requestNextItem();


return d;
}

};

Expand Down
Loading

0 comments on commit fa3135b

Please sign in to comment.