~ mege
authorAlexandre Bertails <bertails@w3.org>
Mon, 22 Aug 2011 17:38:26 -0400
changeset 15 9882f1d06588
parent 14 1292c7c6a8fb (current diff)
parent 13 20f5f73e9523 (diff)
child 17 690206ecf35d
~ mege
--- a/src/main/resources/scripts/tracker.js	Mon Aug 22 17:38:01 2011 -0400
+++ b/src/main/resources/scripts/tracker.js	Mon Aug 22 17:38:26 2011 -0400
@@ -1,67 +1,28 @@
-
-function buildMessage(data, boundary) {
-    var CRLF = "\r\n";
-    var parts = [];
-    var iter = 0;
-
-    while (iter < data.length) {
-        var part = "";
-        var type = "TEXT";
 
-        part = 'Content-Disposition: form-data; ';
-        part += 'name="' + data[iter][0] + '"' + CRLF + CRLF;
-
-        part += data[iter][1] + CRLF;
-
-        parts.push(part);
-        iter++;
-    }
-
-    var request = "--" + boundary + CRLF;
-        request+= parts.join("--" + boundary + CRLF);
-        request+= "--" + boundary + "--" + CRLF;
-
-    return request;
-}
 
 function send(user, data) {
-    var boundary = "AJAX-----------------------" + Date.now();
     var xhr = new XMLHttpRequest;
 
-    if (!("withCredentials" in xhr)) {
-      return false;
-    }    
-
     try {
     xhr.open("POST", "/user/"  + user + "/run", true);
-    xhr.setRequestHeader('X-HTML5TRACK', 'true');
     xhr.onreadystatechange = function() {
-        switch (xhr.readyState) {
-         case 4:
-            document.getElementById("log").textContent = "xhr done " + xhr.status + " " + xhr.statusText;
-            break;
-         case 0:
-            document.getElementById("log").textContent = "xhr unsent";
-            break;
-         case 1:
-            document.getElementById("log").textContent = "xhr opened";
-            break;
-         case 2:
-            document.getElementById("log").textContent = "xhr headers_received";
-            break;
-         case 3:
-            document.getElementById("log").textContent = "xhr loading";
-            break;
+        if (xhr.readyState == 4) {
+            if (xhr.status != 200) { // this needs to be 201
+              document.getElementById("log").textContent = "xhr failed? [" + xhr.status + "] " + xhr.statusText;
+            } else {
+              document.getElementById("log").textContent = "resource created at " + xhr.getResponseHeader("Location");
+            }
+        } else {
+            document.getElementById("log").textContent = "xhr readyState is " + xhr.readyState;
         }
     };
-    var contentType = "multipart/form-data; boundary=" + boundary;
-    xhr.setRequestHeader("Content-Type", contentType);
+    xhr.setRequestHeader("Content-Type", "application/json");
+    xhr.setRequestHeader("Content-Length", data.length);
 
-    var data = buildMessage([ ["user", user], ["gps_coords", data] ], boundary);
-
-    xhr.sendAsBinary(data);
+    xhr.send(data);
 
     } catch (e) {
+      document.getElementById("log").textContent = "Failed to send to server " + e;
       return false;
     }
    return true;
@@ -112,8 +73,8 @@
 
 function pushCoordinates(position) {
    coordinates[coordinates.length] =
-    [ position.timestamp, position.coords.longitude, position.coords.latitude, position.coords.altitude, position.coords.speed ];
-//    [ position.timestamp, position.coords.longitude, position.coords.latitude, position.coords.altitude ];
+//    [ position.timestamp, position.coords.longitude, position.coords.latitude, position.coords.altitude, position.coords.speed ];
+    [ position.timestamp, position.coords.longitude, position.coords.latitude, position.coords.altitude ];
 }
 
 function pause() {
@@ -126,17 +87,18 @@
 
 function getCoordinates() {
   if (coordinates.length > 0 && !running) {
-    var result = "";
+    var result = "[";
     var i = 0;
     while (i < coordinates.length) {
       var position = coordinates[i];
       var j = 1;
-      result += position[0];
+      result += "[" + position[0];
       while (j < position.length) result += "," + position[j++];      
-      result += " ";
+      result += "]";
       i++;
+      if (i < coordinates.length) result += ",";
     }
-    return result;
+    return result + "]";
   }
   return null;
 }
@@ -294,7 +256,7 @@
 }
 
 function stop() {
-  //  navigator.geolocation.clearWatch(watchId);
+  navigator.geolocation.clearWatch(watchId);
   running = false;
   clearInterval(intervalId);
   got_error = 0;
@@ -314,9 +276,7 @@
   }
   if (coords != null) {
     if (!send("teole",coords)) {      
-      document.getElementById("gps_coords").value = coords;
-      document.getElementById("xhr_failed").style.display = "inline";
-      document.getElementById("xhr_failed").disabled = false;
+      // do something!      
     }
   }
   coordinates = [];
@@ -328,6 +288,9 @@
   else
     document.visibilityState = true;
 
+  send("teole", "[4,5,6]");
+  return false;
+
   // @@ in the future, make sure this is empty
    if (storage) {
     window.localStorage.setItem("track_gps_app", "");
@@ -346,11 +309,6 @@
      document.getElementById("start").disabled = true;
    }
 
-    var xhr = new XMLHttpRequest;
-    if (!!xhr && !("withCredentials" in xhr)) {
-      document.getElementById("xhr_failed").style.display = "inline";
-      document.getElementById("xhr_failed").disabled = true;
-    }        
 }
 
 window.addEventListener("load", init, false);
--- a/src/main/resources/templates/geolocation.ssp	Mon Aug 22 17:38:01 2011 -0400
+++ b/src/main/resources/templates/geolocation.ssp	Mon Aug 22 17:38:26 2011 -0400
@@ -75,13 +75,6 @@
 <button type='button' id='stop' disabled='disabled'
   onclick='stop()'>Stop</button>
 
-<form action='http://teole.jfouffa.org/2011/07/html5track.php' method='post'>  
- <input type='hidden' name='gps_coords' id='gps_coords' value='13,-71,42,-20 '/>
- <input type='hidden' name='user' value='teole'/>
-
- <button type='submit' id='xhr_failed' style='display:none'>Send</button>
-</form>
-
 </p>
 
 </section>