Skip to content

Commit

Permalink
feat(citizenScienceRouting): start refactor for new datasets and citi…
Browse files Browse the repository at this point in the history
zen science api on server
  • Loading branch information
peichins committed Nov 2, 2017
1 parent d0baa6c commit 0b21ca1
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 88 deletions.
21 changes: 11 additions & 10 deletions src/app/citizenScience/bristlebird/bristlebird.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class BristlebirdController {
UserProfile,
UserProfileEvents,
CitizenScienceCommon,
CsApi,
SampleLabels,
backgroundImage,
paths) {
Expand Down Expand Up @@ -99,20 +100,17 @@ class BristlebirdController {

$scope.labels = [];

//self.getSamples = CitizenScienceCommon.bindGetSamples($scope);

self.getSample = CitizenScienceCommon.bindGetSample($scope);

$scope.currentSample = {};


// the model passed to ngAudio
$scope.audioElementModel = CitizenScienceCommon.getAudioModel();

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

$scope.numSamplesViewed = SampleLabels.getNumSamplesViewed();

CitizenScienceCommon.getLabels($scope.csProject).then(function (labels) {
CsApi.getLabels($scope.csProject).then(function (labels) {
$scope.labels = labels;
});

Expand All @@ -138,7 +136,10 @@ class BristlebirdController {
};


CitizenScienceCommon.getSettings($scope.csProject).then(
/**
* Retrieve settings about this citizen science project
*/
CsApi.getSettings($scope.csProject).then(
function (settings) {
$scope.settings = settings;
if ($scope.settings.hasOwnProperty("sampleDuration")) {
Expand All @@ -159,7 +160,7 @@ class BristlebirdController {
backgroundImage.currentBackground = backgroundPath;
$scope.$broadcast("update-selected-labels", SampleLabels.getLabelsForSample($scope.samples[$scope.currentSampleNum].id));
// record that this sample has been viewed
SampleLabels.setValue($scope.currentSample.id)
SampleLabels.setValue($scope.currentSample.id);
$scope.numSamplesViewed = SampleLabels.getNumSamplesViewed();
}
});
Expand Down Expand Up @@ -196,11 +197,10 @@ angular
"bawApp.components.progress",
"bawApp.citizenScience.common",
"bawApp.citizenScience.sampleLabels",
// "bawApp.components.citizenScienceTextLabels",
// "bawApp.components.citizenScienceExamples",
"bawApp.components.citizenScienceThumbLabels",
"bawApp.components.onboarding",
"bawApp.components.background"
"bawApp.components.background",
"bawApp.citizenScience.csApiMock"
])
.controller(
"BristlebirdController",
Expand All @@ -215,6 +215,7 @@ angular
"UserProfile",
"UserProfileEvents",
"CitizenScienceCommon",
"CsApi",
"SampleLabels",
"backgroundImage",
"conf.paths",
Expand Down
117 changes: 102 additions & 15 deletions src/app/citizenScience/citizenScienceApiMock.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ var csApiMock = angular.module("bawApp.citizenScience.csApiMock", ["bawApp.citiz



sampleLabels.factory("CsApi", [
csApiMock.factory("CsApi", [
"CitizenScienceCommon",
"$http",
function CsApi(CitizenScienceCommon, $http) {
Expand Down Expand Up @@ -47,29 +47,116 @@ sampleLabels.factory("CsApi", [
return url;
};

/**
* Load and save all samples on page load
*/
$http.get(self.apiUrl(
"samples",
"ebb")).then(function (response) {
self.allSamples = response.data;
});



self.publicFunctions = {

/**
* Gets the media data for the specified sample
* @param recording_id
* @param offset
*/
getSample : function (datasetItemId) {

var url = self.apiUrl(
"samples",
"ebb",
"phil");
//TODO: error handling
return $http.get(url).then(function (response) {


// mock version returns all samples. Then we search then here to get the right one

var itemNum = response.data.findIndex(item => item.id === datasetItemId);

if (itemNum === -1) {
return {};
}

var item = response.data[itemNum];
if (itemNum > 0) {
item.previousSampleId = response.data[itemNum - 1].id;
} else {
item.previousSampleId = null;
}
if (itemNum < response.data.length-1) {
item.nextSampleId = response.data[itemNum + 1].id;
} else {
item.nextSampleId = null;
}




return item;
});
},
/**
* Gets the identifiers for the next sample
* to be used for navigation
* @param recording_id
* @param offset
*/
getNextSample : function (datasetItemId) {
var url = self.apiUrl(
"nextSample",
"ebb");
return $http.get(url).then(function (response) {
return response.data;
});

},
/**
* Gets the identifiers for the previous sample, to be used for navigation
* @param recording_id
* @param offset
*/
getPrevousSample : function (datasetItemId) {
var url = self.apiUrl(
"previousSample",
"ebb");
return $http.get(url).then(function (response) {
return response.data;
});

},

getLabels: function (project) {
var response = $http.get(self.apiUrl(
"labels",
project
));

getSamples : function () {
if ($scope.samples.length === 0) {
return response.then(function (response) {
var labels = [];
if (Array.isArray(response.data)) {
labels = response.data;
}

var url = self.functions.apiUrl(
"samples",
$scope.csProject,
UserProfile.profile.userName);
//TODO: error handling
$http.get(url).then(function (response) {
var samples = response.data;
$scope.samples = samples;
$scope.currentSampleNum = 0;
return labels;
});
},

getSettings: function (project) {
return $http.get(self.apiUrl(
"settings",
project
));
}
}

};
};

return publicFunctions;
return self.publicFunctions;



Expand Down
46 changes: 1 addition & 45 deletions src/app/citizenScience/citizenScienceCommon.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ citizenScienceCommon.factory("CitizenScienceCommon", [

self.functions = {


getAudioModel: function () {
return self.audioElementModel;
},
Expand Down Expand Up @@ -110,28 +111,6 @@ citizenScienceCommon.factory("CitizenScienceCommon", [
return JSON.stringify(labels);
},

/**
* Returns a function that retrieves the samples for the user and the project
* @param $scope
* @returns {function}
*/
bindGetSamples: function ($scope) {

UserProfile.get.then(getSamples);
return getSamples;
},

/**
* returns the sample of the given id
* @param sampleId int
* @param project string
*/
getNextSample: function (sampleId, project, ) {
var getSample = function (sampleNum) {
return $scope.samples[sampleNum];
};
return getSample;
},


/**
Expand Down Expand Up @@ -169,29 +148,6 @@ citizenScienceCommon.factory("CitizenScienceCommon", [

return showAudio;

},

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;
});
},

getSettings: function (project) {
return $http.get(self.apiUrl(
"settings",
project
));
}

};
Expand Down
29 changes: 24 additions & 5 deletions src/app/citizenScience/citizenScienceSampleLabels.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@ sampleLabels.factory("SampleLabels", [

};

/**
* stringifies the object that acts as a join between samples and labels
* Then stores that json string in local storage
*/
self.writeToStorage = function () {
var value = JSON.stringify(self.data);
localStorage.setItem(self.localStorageKey, value);

console.log("saved responses: ", value + "ok");



};


Expand Down Expand Up @@ -84,6 +84,13 @@ sampleLabels.factory("SampleLabels", [

init : self.init,

/**
* Looks up the data to see if there is a boolean value stored for a given sampleId and labelId
* and if so, returns it.
* @param sampleId
* @param labelId
* @returns {boolean}
*/
getValue : function (sampleId, labelId) {

if (self.data[sampleId] !== undefined) {
Expand Down Expand Up @@ -123,6 +130,13 @@ sampleLabels.factory("SampleLabels", [

},

/**
* returns an object that holds all the labels that have been applied to
* the given sample and their values. (if a label has been removed then it will be stored
* as false. If it has never been applied, it will not be present).
* @param sampleId
* @returns {*}
*/
getLabelsForSample : function (sampleId) {

if (typeof(self.data[sampleId]) !== "object") {
Expand All @@ -139,7 +153,12 @@ sampleLabels.factory("SampleLabels", [
* should be added to the data object as an empty object
*/
getNumSamplesViewed : function () {
return Object.keys(self.data).length;
if (self.data !== undefined) {
return Object.keys(self.data).length;
} else {
return 0;
}

}


Expand Down
Loading

0 comments on commit 0b21ca1

Please sign in to comment.