Skip to content

Commit

Permalink
Added user profile service and model.
Browse files Browse the repository at this point in the history
Added initial bindings for Volume property.

Architecture very messy - need to refactor.

Still buggy - work not complete.
  • Loading branch information
atruskie committed Jan 28, 2014
1 parent 617e27c commit fa374dc
Show file tree
Hide file tree
Showing 8 changed files with 206 additions and 43 deletions.
4 changes: 2 additions & 2 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ module.exports = function (grunt) {
// bit of bullshit to ensure the build fails for missing files in the concat task
// note this overrides normal logging! not cool.
// These problems (failing on missing files) are expected to be resolved in grunt 0.5
grunt.log.oldWarn = grunt.log.warn;
grunt.log.warn = grunt.warn;
//grunt.log.oldWarn = grunt.log.warn;
//grunt.log.warn = grunt.warn;

/**
* Load required Grunt tasks. These are installed based on the versions listed
Expand Down
7 changes: 5 additions & 2 deletions src/app/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,12 +164,15 @@ var app = angular.module('baw',
}])


.run(['$rootScope', '$location', '$route', '$http', 'AudioEvent', 'conf.paths',
function ($rootScope, $location, $route, $http, AudioEvent, paths) {
.run(['$rootScope', '$location', '$route', '$http', 'AudioEvent', 'conf.paths', 'UserProfile',
function ($rootScope, $location, $route, $http, AudioEvent, paths, UserProfile) {

// embed configuration for easy site-wide binding
$rootScope.paths = paths;

// user profile - update user preferences when they change
UserProfile.get($rootScope, "userProfile");

// helper function for printing scope objects
baw.exports.print = $rootScope.print = function () {
var seen = [];
Expand Down
13 changes: 12 additions & 1 deletion src/app/listen/listen.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ angular.module('bawApp.listen', ['decipher.tags', 'ui.bootstrap.typeahead'])
// set up some dummy objects for use later
$scope.jumpToHide = true;
$scope.model = {
audioElement: {},
audioElement: {
},
audioEvents: [],
media: null,
selectedAudioEvent: null,
Expand All @@ -90,6 +91,16 @@ angular.module('bawApp.listen', ['decipher.tags', 'ui.bootstrap.typeahead'])
site: null
};

// bind
$scope.$watch(function () {
return $scope.model.audioElement;
}, function(newValue) {
$scope.$root.userProfile.preferences.volume = newValue;
});
$scope.$watch("$root.userProfile.preferences.volume", function(newValue) {
$scope.model.audioElement = newValue;
});

var formatPaths = function () {
if ($scope.model.media && $scope.model.media.hasOwnProperty('id')) {
//var authToken = $scope.authTokenQuery();
Expand Down
32 changes: 20 additions & 12 deletions src/baw.configuration.tpl.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ angular.module('bawApp.configuration', ['url'])
* @param {...[string]} fragments
* @returns {*}
*/
function joinPathFragments(fragments) {
function joinPathFragments(fragments) {
fragments = Array.prototype.slice.call(arguments, 0);

if (fragments.length === 0) {
Expand All @@ -33,14 +33,14 @@ angular.module('bawApp.configuration', ['url'])
var f = fragments[i];

if ((typeof f) !== "string") {
throw "Path fragment " + f + " is not a string";
throw "joingPathFragments: Path fragment " + f + " is not a string";
}

var hasFirst = f[0] === "/";
var hasLast = (f.slice(-1))[0] === "/";

if (!hasFirst) {
f = "/" + f;
f = "/" + f;
}

if (hasLast && i !== (fragments.length - 1)) {
Expand All @@ -64,19 +64,19 @@ angular.module('bawApp.configuration', ['url'])
listShort: "/audio_recordings/{recordingId}",
show: "/audio_recordings/{recordingId}",
list: "/audio_recordings/"
},
},
audioEvent: {
list: "/audio_recordings/{recordingId}/audio_events",
show: "/audio_recordings/{recordingId}/audio_events/{audioEventId}",
csv : "/audio_events/download."
csv: "/audio_events/download."
},
tagging: {
list: "/audio_recordings/{recordingId}/audio_events/{audioEventId}/taggings",
show: "/audio_recordings/{recordingId}/audio_events/{audioEventId}/taggings/{taggingId}"
},
tag: {
list: '/tags/',
show: '/tags/{tagId}'
list: '/tags/',
show: '/tags/{tagId}'
},
media: {
show: "/audio_recordings/{recordingId}/media.{format}"
Expand All @@ -85,8 +85,9 @@ angular.module('bawApp.configuration', ['url'])
ping: "/security/sign_in",
signIn: "/my_account/sign_in"
},
users: {
settings: "I really don't know yet"
user: {
profile: "/my_account",
settings: "/my_account/prefs"
}
},
links: {
Expand All @@ -104,7 +105,7 @@ angular.module('bawApp.configuration', ['url'])
listen: 'listen/listen.tpl.html',
annotationViewer: 'annotationViewer/annotationViewer.tpl.html',
navigation: 'navigation/navigation.tpl.html',
birdWalk : {
birdWalk: {
list: 'birdWalks/birdWalks.tpl.html',
detail: 'birdWalks/birdWalk.tpl.html',
spec: 'assets/bird_walk/bird_walk_spec.json',
Expand All @@ -124,7 +125,6 @@ angular.module('bawApp.configuration', ['url'])
};



// add helper paths
function recursivePath(source, root) {
for (var key in source) {
Expand Down Expand Up @@ -159,6 +159,14 @@ angular.module('bawApp.configuration', ['url'])
unitConverter: {
precisionSeconds: 9,
precisionHertz: 6

},
defaultProfile: {
createdAt: null,
email: null,
id: null,
preferences: {
volume: 1.0
},
userName: "Unknown user"
}
});
14 changes: 13 additions & 1 deletion src/components/directives/ngAudio.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ bawds.directive('ngAudio', ['$parse', function ($parse) {
throw 'Cannot put ngAudio element on an element that is not a <audio />';
}

var expression = $parse(attributes.ngAudio),
model = expression(scope);

/* Forward binding */

function play() {
element.play();
}
Expand All @@ -28,6 +33,14 @@ bawds.directive('ngAudio', ['$parse', function ($parse) {
element.currentTime = 0;
}

scope.$watch(function () {
return model.volume;
}, function(newValue, oldValue) {
element.volume = newValue;
});

/* Reverse binding */

var propertiesToUpdate = ['duration', 'src', 'currentSrc', 'volume'];
function updateObject(src, dest) {
for (var i = 0; i < propertiesToUpdate.length; i++){
Expand All @@ -38,7 +51,6 @@ bawds.directive('ngAudio', ['$parse', function ($parse) {
function updateState(event) {
scope.$safeApply2(function () {
if (attributes.ngAudio) {
var expression = $parse(attributes.ngAudio);
var target = expression(scope);
if (!target) {
expression.assign(scope, {});
Expand Down
82 changes: 82 additions & 0 deletions src/components/models/userProfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
var baw = window.baw = window.baw || {};


baw.UserProfile = (function () {

/*function makeGetter(key) {
return function() {
return this[key];
};
}
function makeSetter(key, service) {
return function(value) {
if (value !== this[key]) {
this[key] = value;
service.updatePreferences(key, this);
}
};
}*/

function UserProfile(serviceReference, profile, defaultProfile) {

if (!(this instanceof UserProfile)) {
throw new Error("Constructor called as a function");
}

var immediateSave = false;
if (!profile) {
profile = defaultProfile;
immediateSave = true;
}

if (!defaultProfile) {
throw new Error("A default profile must be supplied");
}

// make read only properties for all profile props returned
var props = Object.keys(profile)
.reduce(function (state, current, index, array) {
state[current] = {
value: profile[current],
writeable: false,
enumerable: true,
configurable: false
};
return state;
}, {});
Object.defineProperties(this, props);

// now create persistence settings logic
var merged = angular.extend(defaultProfile.preferences, this.preferences);
for (var key in merged) {
if (!merged.hasOwnProperty(key)) {
return;
}

this.preferences[key] = merged[key];

/*
Object.defineProperty(this.preferences, "_" + key, {
value: merged[key],
writeable: true,
enumerable: false,
configurable: false
});
Object.defineProperty(this.preferences, key, {
get: makeGetter(key),
set: makeSetter(key, serviceReference),
configurable: false,
enumerable: true
});*/
}

if (immediateSave) {

}

}

return UserProfile;
})();
72 changes: 72 additions & 0 deletions src/components/services/userProfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
var bawss = bawss || angular.module("bawApp.services", ["ngResource", "bawApp.configuration"]);

bawss.factory("UserProfile", [
"$http",
"conf.paths",
"conf.constants",
function ($http, paths, constants) {
var profileUrl = paths.api.routes.user.profileAbsolute;
var preferencesUrl = paths.api.routes.user.settingsAbsolute;

var methods = {};

var throttleCount = 0,
throttleAmount = 1000;

/**
* Update the server's stored settings.
* Calls to this function are throttled.
* @param keyChanged - they key of the property that just changed
* @param object - the preferences object that will be sent back to the server
*/
methods.updatePreferences = function throttleWrapper(object) {
console.debug("updatePreferences: throttled", object);
throttleCount++;

return _.throttle(
function updatePreferences(object) {
console.debug("updatePreferences: sending to server, waited %s attempts", throttleCount);
throttleCount = 0;

$http.put(preferencesUrl).then(
function success(response) {
console.info("updatePreferences:success");
},
function error(response) {
console.error("updatePreferences:failed", response);
}
);
}, throttleAmount);
};

methods.get = function (scope, property) {
scope[property] = {preferences: null};

$http.get(profileUrl).then(
function success(response) {
console.log("User profile loaded");

scope[property] = (new baw.UserProfile(methods, response.data, constants.defaultProfile));
},
function error(response) {
console.error("User profile load failed", response);

scope[property] = (new baw.UserProfile(methods, null, constants.defaultProfile));
}
).finally(function () {
scope.$watch(function () {
return scope[property].preferences;
}, function (newValue, oldValue) {
if (newValue == oldValue) {
return;
}

methods.updatePreferences(newValue);
}, true);
});
};


return methods;
}
]);
25 changes: 0 additions & 25 deletions src/components/services/userSettings.js

This file was deleted.

0 comments on commit fa374dc

Please sign in to comment.