Skip to content

Commit

Permalink
feat(citizenScience): service to store responses to local storage. Us…
Browse files Browse the repository at this point in the history
…e static json file rather than gsheets proxy server
  • Loading branch information
peichins committed Aug 31, 2017
1 parent 1de3135 commit d1f58bd
Show file tree
Hide file tree
Showing 8 changed files with 218 additions and 87 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,7 @@ es6/
vendor/
src/assets/temp
.sass-cache
*.generated*
*.generated*

#citizen science temp data for development
/public/citizen_science/*
4 changes: 2 additions & 2 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -729,10 +729,10 @@ module.exports = function (grunt) {
// from there, angular deals with the route information
//"!(\\/[^\\.\\/\\?]+\\.\\w+) /" + buildDirectory + "/ [L]"

// does not match any url startng with /build, /src, or /vendor
// does not match any url startng with /build, /src, /vendor or /public
// if matched, the root (index.html) is sent back instead.
// from there, angular deals with the route information
"!(^(\\/build|\\/src|\\/vendor)) /" + buildDirectory + "/ [L]"
"!(^(\\/build|\\/src|\\/vendor|\\/public)) /" + buildDirectory + "/ [L]"

]),

Expand Down
62 changes: 24 additions & 38 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,
SampleLabels,
backgroundImage,
paths) {

Expand Down Expand Up @@ -116,61 +117,44 @@ class BristlebirdController {
$scope.labels = labels;
});


SampleLabels.init($scope.csProject, $scope.samples, $scope.labels);

$scope.$on("label-toggle", function (e, labelNumber, value) {
self.toggleLabel(labelNumber, value);
});



/**
* applies or removes the tag-sets of the given label number
* to the current sample
* @param labelNumber
* @param value boolean if omitted will flip the current value
*/
self.toggleLabel = function (labelNumber, value) {

console.log("toggling label ", labelNumber, value);
self.toggleLabel = function (labelId, value) {
console.log("toggling label ", labelId, value);
var currentSample = $scope.samples[$scope.currentSampleNum];
if (typeof value === "boolean") {
currentSample.labels[labelNumber] = value;
} else {
currentSample.labels[labelNumber] = !currentSample.labels[labelNumber];
if (typeof value !== "boolean") {
value = !SampleLabels.getValue(currentSample.id, labelId);
}
SampleLabels.setValue(currentSample.id, labelId, value);
};

currentSample.done = true;

var tags = $scope.labels.filter(function (value, index) {
return currentSample.labels[index];
}).map(function (value) {
return value.tags;
});

console.log("new tags for sample: ", CitizenScienceCommon.labelsAsString(tags));

var url = CitizenScienceCommon.apiUrl("setLabels",
$scope.csProject,
currentSample.name,
currentSample.recordingId,
currentSample.startOffset,
CitizenScienceCommon.labelsAsString(tags));
$http.get(url).then(function (response) {
console.log(response.data);
});

};
CitizenScienceCommon.getSettings($scope.csProject).then(
function (settings) {
$scope.settings = settings;
if ($scope.settings.hasOwnProperty("sampleDuration")) {
self.sampleDuration = $scope.settings.sampleDuration;
}
}
);


/**
* Get settings from sheet
*/
$http.get(CitizenScienceCommon.apiUrl(
"settings",
$scope.csProject
)).then(function (response) {
$scope.settings = response.data;
if ($scope.settings.hasOwnProperty("sampleDuration")) {
self.sampleDuration = $scope.settings.sampleDuration;
}
});

/**
* When the currentSampleNum changes, change the current audio file / spectrogram to match it
Expand All @@ -182,7 +166,7 @@ class BristlebirdController {
self.showAudio(currentSample.recordingId, currentSample.startOffset, self.sampleDuration);
var backgroundPath = self.backgroundPaths[$scope.currentSampleNum % (self.backgroundPaths.length - 1)];
backgroundImage.currentBackground = backgroundPath;
$scope.$broadcast("update-selected-labels", $scope.samples[$scope.currentSampleNum].labels);
$scope.$broadcast("update-selected-labels", SampleLabels.getLablesForSample($scope.samples[$scope.currentSampleNum].id));
}
});

Expand All @@ -208,9 +192,9 @@ class BristlebirdController {
}
});


self.backgroundPaths = ["1.jpg", "2.jpg", "3.jpg", "4.jpg"].map(fn => paths.site.assets.backgrounds.citizenScience + fn);


}

}
Expand All @@ -219,6 +203,7 @@ angular
.module("bawApp.citizenScience.bristlebird", [
"bawApp.components.progress",
"bawApp.citizenScience.common",
"bawApp.citizenScience.sampleLabels",
// "bawApp.components.citizenScienceTextLabels",
// "bawApp.components.citizenScienceExamples",
"bawApp.components.citizenScienceThumbLabels",
Expand All @@ -238,6 +223,7 @@ angular
"UserProfile",
"UserProfileEvents",
"CitizenScienceCommon",
"SampleLabels",
"backgroundImage",
"conf.paths",
BristlebirdController
Expand Down
64 changes: 30 additions & 34 deletions src/app/citizenScience/citizenScienceCommon.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ citizenScienceCommon.factory("CitizenScienceCommon", [

var self = this;

self.useLocalData = true;

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

/**
* Default values for audio model, to be updated when UserProfile is loaded
Expand Down Expand Up @@ -53,27 +56,6 @@ citizenScienceCommon.factory("CitizenScienceCommon", [

self.mediaModel = null;

/**
* creates a labels array of boolean values in the sample object, signifying whether
* the current sample has the label's tags attached to it
* @param samples Array The samples that are being initialised
* @param labels array The list of possible labels
*/
self.initSampleLabels = function (samples, labels) {

samples.forEach(function (sample) {
sample.labels = labels.map(function (label) {
for (var i = 0; i < sample.tags.length; i++) {
//TODO: make this more efficient (don't repeatedly join label tags)
if (self.compareTags(sample.tags[i], label.tags)) {
return true;
}
}
return false;
});
});
};


/**
* Checks if a tag or array of tags is the same
Expand All @@ -93,18 +75,25 @@ citizenScienceCommon.factory("CitizenScienceCommon", [

};

self.initLabels = function (labels) {

// add the index (label number) to the label object
labels.forEach((label, index) => label.labelNumber = index);
return labels;

};

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);
return [self.sheets_api_url].concat(args).join("/");

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

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

return url;
};


Expand Down Expand Up @@ -154,16 +143,17 @@ citizenScienceCommon.factory("CitizenScienceCommon", [
bindGetSamples: function ($scope) {
var getSamples = function () {
if ($scope.samples.length === 0) {

var url = self.functions.apiUrl(
"userSamples",
"samples",
$scope.csProject,
UserProfile.profile.userName);
//TODO: error handling
$http.get(url).then(function (response) {
//console.log(response.data);
var samples = response.data;
$scope.samples = samples;
self.initSampleLabels($scope.samples, $scope.labels);
//self.initSampleLabels($scope.samples, $scope.labels);
//$scope.goToSample(0);
$scope.currentSampleNum = 0;
});
Expand Down Expand Up @@ -210,20 +200,26 @@ citizenScienceCommon.factory("CitizenScienceCommon", [
},

getLabels: function (project) {

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

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

return labels;
});
},

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

};
Expand Down
Loading

0 comments on commit d1f58bd

Please sign in to comment.