+ new HTTP POST from Utils
authorplehegar
Fri, 26 Aug 2011 01:13:58 -0400
changeset 106 9bf0e346e795
parent 105 6399a43908a8
child 107 f69944b2a39b
+ new HTTP POST from Utils
+ Handling workout types
src/main/resources/scripts/tracker.js
src/main/resources/templates/geolocation.ssp
--- a/src/main/resources/scripts/tracker.js	Fri Aug 26 01:11:43 2011 -0400
+++ b/src/main/resources/scripts/tracker.js	Fri Aug 26 01:13:58 2011 -0400
@@ -17,13 +17,16 @@
     if (n === null) {
       Util.log("Creating dummy user");
       n = {
-        name : "dummy@example.com",
+      	name  : "dummy@example.com",
+      	passwd: "foobar",
+        email : "dummy@example.com",
         weight : 73,
         height : 180,
         prefs : {
-            weight : Util.getPropertyString(Const.weight, Const.weight.kg), 
-            height : Util.getPropertyString(Const.height, Const.height.cm),
-            distance : Util.getPropertyString(Const.distance, Const.distance.km)
+            weight   : "kg", 
+            height   : "cm",
+            distance : "km",
+            workout  : "http://dbpedia.org/resource/Running"
         }
       };
     } else {
@@ -80,7 +83,7 @@
  locations : 0,
 
  // this will store all the geolocation data and pause
- currentActivity : new Activity("http://dbpedia.org/resource/Running")
+ currentActivity : new Activity(user.prefs.workout)
 };
 
 // add a geolocation position into the current activity
@@ -129,7 +132,7 @@
 
 
 // when watchPosition is successful at returning a GPS coordinate
-app.handleSuccess = function (position) {
+app.geoSuccess = function (position) {
   if (app.inActivity) {
    app.logGPS(position, "GPS: ");
 
@@ -164,7 +167,7 @@
 }
 
 // when watchPosition is not successful at returning a GPS coordinate
-app.handleError = function (error) {
+app.geoError = function (error) {
   var code = "unknown";
   switch(error.code) {
     case error.TIMEOUT:
@@ -234,27 +237,30 @@
   document.getElementById("start").textContent = "Start";
   document.getElementById("stop").disabled = true;
   if (app.currentActivity.events.length > 2) {
-  	if (!Util.postNewResourceAsJSON(user.getPostRunURI(),app.currentActivity)) {      
-    	if (!Util.store.set("track_gps_app", app.currentActivity)) {
+  	var success = function (xhr) {
+  		if (xhr.status == 201) {
+  			Util.log("Resource created at " + xhr.getResponseHeader("Location"));
+  		}
+  	}
+  	var error = function(obj) {
+  		if (!Util.store.set("track_gps_app", app.currentActivity)) {
       		Util.log("[ERROR] Can't store the workout");
     	}
-    }
+  	}
+  	Util.postAsJSON(user.getPostRunURI(),app.currentActivity, success, error);
   } else {
   	Util.log("[ERROR] Your workout doesn't contain enough events");
   }
-  app.currentActivity = new Activity("http://dbpedia.org/resource/Running");
+  app.currentActivity = new Activity(user.prefs.workout);
 }
 
 app.init = function () { 
    
-  if (!!document.visibilityState)
+  if (typeof(document.visibilityState) !== "undefined")
     document.addEventListener("visibilitychange", app.repaint, false);
   else
     document.visibilityState = true;
   
-  // send("teole", "{ activity:\"http://dbpedia.org/resource/Running\", events: [ {t: 1314049555597, c: [-71.09068298339844,42.361671924591064,66] },{t: 1314049559622,s: \"pause\"}]}");
-  // return false;
-
   // @@ in the future, make sure this is empty
   if (!Util.store.enabled) {
     Util.log("[ERROR] no local storage");
@@ -262,7 +268,7 @@
   app.setPrefs();
 
   if (!!navigator.geolocation) {
-    app.watchId = navigator.geolocation.watchPosition(app.handleSuccess, app.handleError, {enableHighAccuracy:true, maximumAge:0, timeout:1000});
+    app.watchId = navigator.geolocation.watchPosition(app.geoSuccess, app.geoError, {enableHighAccuracy:true, maximumAge:0, timeout:1000});
   } else {
     Util.log("[ERROR] no geolocation support");
     document.getElementById("gps_text").textContent = "not supported";
@@ -282,7 +288,6 @@
   } while ((node=nextElementSibling(node)) != null);
   app.setPrefs();
   app.repaint();
-  user.save();
 }
 
 app.changeWeightUnit = function () {
@@ -294,7 +299,6 @@
   } while ((node=nextElementSibling(node)) != null);
   app.setPrefs();
   app.repaint();
-  user.save();
 }
 
 app.changeHeightUnit = function () {
@@ -306,7 +310,16 @@
   } while ((node=nextElementSibling(node)) != null);
   app.setPrefs();
   app.repaint();
-  user.save();
+}
+
+app.changeWorkout = function () {
+  var node = firstElementChild(document.getElementById("select_workout"));
+  do {
+    if (node.selected) {
+       user.prefs.workout = node.getAttribute("value");
+       app.currentActivity = new Activity(user.prefs.workout);
+    }
+  } while ((node=nextElementSibling(node)) != null);
 }
 
 app.setPrefs = function () {
@@ -345,6 +358,11 @@
     if (node.getAttribute("value")===user.prefs.height) node.selected = true;
   } while ((node=nextElementSibling(node)) != null);
   document.getElementById("height_input").value = user.height;
+
+  node = firstElementChild(document.getElementById("select_workout"));
+  do {
+    if (node.getAttribute("value")===user.prefs.workout) node.selected = true;
+  } while ((node=nextElementSibling(node)) != null);
 }
 
 app.save = function () {
--- a/src/main/resources/templates/geolocation.ssp	Fri Aug 26 01:11:43 2011 -0400
+++ b/src/main/resources/templates/geolocation.ssp	Fri Aug 26 01:13:58 2011 -0400
@@ -9,7 +9,7 @@
  <title>HTML5 Track</title>
  <script src='/scripts/json2.js'>
  </script>
- <script src="http://www.w3.org/2008/12/et-min.js"></script>
+ <script src="/scripts/et-min.js"></script>
  <script src='/scripts/Utils.js'>
  </script>
  <script src='/scripts/tracker.js'>
@@ -107,20 +107,33 @@
 <tr>
 <th><label for='weight_input'>Weight:</label></th>
 <td>
-<input id='weight_input' type="number" size='5' style='display: inline'/> <span class='weight_unit'>kg</span>
+<input id='weight_input' type="number" size='5' style='display: inline'/>
+</td>
+<td>
+<span class='weight_unit'>kg</span>
 </td>
 </tr>
 <tr>
 <th><label for='height_input'>Height:</label></th>
 <td>
-<input id='height_input' type="number" size='5' /> <span class='height_unit'>cm</span>
+<input id='height_input' type="number" size='5' />
+</td>
+<td>
+<span class='height_unit'>cm</span>
 </td>
 </tr>
-</tbody>
-</table>
+<tr>
+<th><label for='select_workout'>Workout:</label></th>
+<td>
+<select id='select_workout' onchange='app.changeWorkout()'>
 
-<table>
-<tbody>
+<option value='http://dbpedia.org/resource/Running'>Running</option>
+<option value='http://dbpedia.org/resource/Cycling'>Cycling</option>
+<option value='http://dbpedia.org/resource/Walking'>Walking</option>
+<option value='http://www.w3.org/2011/08/Geeking'>Geeking</option>
+</select>
+</td>
+</tr>
 <tr>
 <th><label for='select_distance'>Distance unit:</label></th>
 <td>