Browse files

Created a namespace of RunBrowser and pulled the init out of the js file

  • Loading branch information...
1 parent ee9fc1a commit 0e87f16ee536348ad42f81fe668ada8a32eb7a9c @johanan committed Apr 23, 2012
Showing with 28 additions and 27 deletions.
  1. +13 −1 index.html
  2. +14 −25 runBrowser.js
  3. +1 −1 runBrowser.min.js
View
14 index.html
@@ -108,7 +108,19 @@ <h2 class="visible-phone">RunBrowser!</h2>
<!--<script src='http://dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=6.1'></script>
<script src="http://maps.google.com/maps/api/js?v=3.6&amp;sensor=false"></script>-->
<script src="OpenLayers.js"></script>
-
<script src="runBrowser.min.js"></script>
+<script>
+function init(){
+ var ac = new RunBrowser.appController();
+ ac.addEvents();
+ ac.showHome();
+ //hide the top bar in mobile safari
+ setTimeout(function(){
+ window.scrollTo(0, 0);
+ }, 0);
+}
+
+window.addEventListener( 'DOMContentLoaded', init, false);
+</script>
</body>
</html>
View
39 runBrowser.js
@@ -1,4 +1,5 @@
-function appController(){
+(function(RunBrowser){
+RunBrowser.appController = function(){
this.mapView = null;
this.runPath = null;
this.watchid = null;
@@ -131,7 +132,7 @@ function appController(){
datakey = target.parentNode.getAttribute('data-key');
}
- var runp = new runPath();
+ var runp = new RunBrowser.runPath();
var old = JSON.parse(localStorage.getItem(datakey));
runp.startTime = old.startTime;
@@ -149,7 +150,7 @@ function appController(){
this.mapView.destroy();
}
- this.mapView = new mapView();
+ this.mapView = new RunBrowser.mapView();
for(var i=0; i<old.pointArray.length; i++ ){
this.mapView.addPoint(old.pointArray[i]);
@@ -171,7 +172,7 @@ function appController(){
this.showHome();
}
- this.errorHandler = function(error){
+ this.errorHandler = function(error){
this.backdrop.classList.remove('none');
this.errorModal.classList.remove('none');
}
@@ -187,8 +188,8 @@ function appController(){
this.mapView.destroy();
}
- this.mapView = new mapView();
- this.runPath = new runPath();
+ this.mapView = new RunBrowser.mapView();
+ this.runPath = new RunBrowser.runPath();
var This = this;
this.watchid = navigator.geolocation.watchPosition(function(location){This.GetLocation(location)}, function(error){This.errorHandler(error)}, {enableHighAccuracy:true, maximumAge: 5000, timeout: 12000 });
@@ -211,10 +212,9 @@ function appController(){
}
}
-}
-
+};
-function mapView(){
+RunBrowser.mapView = function(){
this.map = new OpenLayers.Map("map");
this.proj = new OpenLayers.Projection("EPSG:4326");
this.line = new OpenLayers.Geometry.LineString([]);
@@ -248,7 +248,7 @@ function mapView(){
}
-mapView.prototype.addPoint = function(location){
+RunBrowser.mapView.prototype.addPoint = function(location){
opLonLat = new OpenLayers.LonLat( location.coords.longitude, location.coords.latitude );
opLonLat.transform(this.proj, this.map.getProjectionObject());
point = new OpenLayers.Geometry.Point(opLonLat.lon, opLonLat.lat);
@@ -258,17 +258,17 @@ mapView.prototype.addPoint = function(location){
this.map.setCenter(opLonLat);
}
-mapView.prototype.destroy = function(){
+RunBrowser.mapView.prototype.destroy = function(){
this.map.destroy();
}
-
+
if (typeof(Number.prototype.toRad) === "undefined") {
Number.prototype.toRad = function() {
return this * Math.PI / 180;
}
}
- function runPath() {
+ RunBrowser.runPath = function() {
this.distance = 0,
this.startTime = 0,
this.lastUpdateTime = 0,
@@ -351,15 +351,4 @@ if (typeof(Number.prototype.toRad) === "undefined") {
return minMile.toPrecision(4);
}
}
-
-function init(){
- var ac = new appController();
- ac.addEvents();
- ac.showHome();
- //hide the top bar in mobile safari
- setTimeout(function(){
- window.scrollTo(0, 0);
- }, 0);
-}
-
-window.addEventListener( 'DOMContentLoaded', init, false);
+}( window.RunBrowser = window.RunBrowser || {} ));
View
2 runBrowser.min.js
@@ -1 +1 @@
-function init(){var a=new appController;a.addEvents();a.showHome();setTimeout(function(){window.scrollTo(0,0)},0)}function runPath(){this.distance=0,this.startTime=0,this.lastUpdateTime=0,this.pointArray=[],this.addPoint=function(a){if(a.coords.accuracy<=25&&a.timestamp-this.lastUpdateTime>=6e3||this.lastUpdateTime==0){var b=(new Date).getTime();this.pointArray.push(a);this.lastUpdateTime=b;if(this.startTime==0){this.startTime=b}if(this.pointArray.length>=2){var c=this.pointArray.length;this.distance+=this.computeDistance(this.pointArray[c-2],this.pointArray[c-1])}return a}return false},this.computeDistance=function(a,b){var c=a.coords.longitude;var d=b.coords.longitude;var e=a.coords.latitude;var f=b.coords.latitude;var g=3958.7558657440545;var h=(f-e).toRad();var i=(d-c).toRad();var e=e.toRad();var f=f.toRad();var j=Math.sin(h/2)*Math.sin(h/2)+Math.sin(i/2)*Math.sin(i/2)*Math.cos(e)*Math.cos(f);var k=2*Math.atan2(Math.sqrt(j),Math.sqrt(1-j));var l=g*k;return l},this.getDistance=function(){return this.distance.toPrecision(4)},this.getElapsed=function(){return(this.lastUpdateTime-this.startTime)/6e4},this.getElapsedFormat=function(){var a=this.lastUpdateTime-this.startTime;var b=Math.floor(a/1e3/60);a-=b*6e4;var c=Math.floor(a/1e3);c=(c<10?"0":"")+c;return b+":"+c},this.getCurrentMph=function(){return(this.pointArray[this.pointArray.length-1].coords.speed*2.23693629).toPrecision(4)},this.getMph=function(){return this.distance/(this.getElapsed()/60)},this.getMinMile=function(){var a=this.getElapsed()/this.distance;if(isNaN(a)){return 0}return a.toPrecision(4)}}function mapView(){this.map=new OpenLayers.Map("map");this.proj=new OpenLayers.Projection("EPSG:4326");this.line=new OpenLayers.Geometry.LineString([]);this.lineLayer=new OpenLayers.Layer.Vector("Line Layer");this.timeSpan=document.getElementById("time");this.distance=document.getElementById("distance");this.mph=document.getElementById("mph");this.minMile=document.getElementById("minMile");var a=new OpenLayers.Layer.OSM;this.map.addControl(new OpenLayers.Control.DrawFeature(this.lineLayer,OpenLayers.Handler.Path));var b={strokeColor:"#0000ff",strokeOpacity:.5,strokeWidth:5};var c=new OpenLayers.Feature.Vector(this.line,null,b);this.lineLayer.addFeatures([c]);this.map.addLayers([a,this.lineLayer]);this.map.zoomTo(15);this.updateTimeDistance=function(a,b){this.timeSpan.innerHTML=a;this.distance.innerHTML=b};this.updateSpeeds=function(a,b){this.mph.innerHTML=a;this.minMile.innerHTML=b}}function appController(){this.mapView=null;this.runPath=null;this.watchid=null;this.mapPage=document.getElementById("mapPage");this.startPage=document.getElementById("startPage");this.showMapButton=document.getElementById("showMap");this.stop=document.getElementById("stop");this.saveButton=document.getElementById("saveRun");this.clearButton=document.getElementById("clearRun");this.backButton=document.getElementById("back");this.table=document.getElementById("savedRuns");this.errorModal=document.getElementById("errorModal");this.clearError=document.getElementById("clearError");this.saveModal=document.getElementById("saveModal");this.backdrop=document.getElementById("backdrop");this.addEvents=function(){var a=this;this.showMapButton.addEventListener("click",function(){a.showMap()});this.stop.addEventListener("click",function(){a.stopMap()});this.saveButton.addEventListener("click",function(){a.saveRun()});this.clearButton.addEventListener("click",function(){a.clearRun()});this.backButton.addEventListener("click",function(){a.clearRun()});this.clearError.addEventListener("click",function(){a.clearRun()})};this.saveRun=function(){localStorage.setItem(this.runPath.startTime,JSON.stringify(this.runPath));if(this.mapView!=null){this.mapView.destroy()}if(this.watchid!=null){navigator.geolocation.clearWatch(this.watchid);this.watchid=null}if(this.runPath!=null){this.runPath=null}this.backdrop.classList.add("none");this.saveModal.classList.add("none");this.mapPage.classList.add("none");this.startPage.classList.remove("none");this.showHome()};this.clearRun=function(){if(this.mapView!=null){this.mapView.destroy()}if(this.watchid!=null){navigator.geolocation.clearWatch(this.watchid);this.watchid=null}if(this.runPath!=null){this.runPath=null}this.backdrop.classList.add("none");this.saveModal.classList.add("none");this.errorModal.classList.add("none");this.mapPage.classList.add("none");this.startPage.classList.remove("none")};this.showHome=function(){var a=this;this.table.classList.remove("none");for(var b=this.table.rows.length;b>1;b--){this.table.deleteRow(b-1)}for(var c in localStorage){var d=JSON.parse(localStorage.getItem(c));var e=this.table.rows.length;var f=this.table.insertRow(e);var g=f.insertCell(0);var h=document.createElement("button");h.className="btn btn-danger";h.innerHTML='<i class="icon-remove icon-white"></i>';h.setAttribute("data-key",c);h.addEventListener("click",function(b){a.removeSavedRun(b.target)});g.appendChild(h);var i=f.insertCell(1);var j=new Date(d.startTime);var k=document.createTextNode(j.toDateString());i.appendChild(k);var l=f.insertCell(2);var m=document.createTextNode(Math.round(d.distance*100)/100);l.appendChild(m);var n=f.insertCell(3);var o=document.createElement("button");o.className="btn btn-inverse";o.innerHTML='<i class="icon-chevron-right icon-white"></i>';o.setAttribute("data-key",c);o.addEventListener("click",function(b){a.loadSavedRun(b.target)});n.appendChild(o)}if(this.table.rows.length==1){this.table.classList.add("none")}};this.loadSavedRun=function(a){var b;if(a.getAttribute("data-key")){b=a.getAttribute("data-key")}else{b=a.parentNode.getAttribute("data-key")}var c=new runPath;var d=JSON.parse(localStorage.getItem(b));c.startTime=d.startTime;c.distance=d.distance;c.lastUpdateTime=d.lastUpdateTime;c.pointArray=d.pointArray;this.mapPage.classList.remove("none");this.startPage.classList.add("none");this.stop.classList.add("none");this.backButton.classList.remove("none");if(this.mapView!=null){this.mapView.destroy()}this.mapView=new mapView;for(var e=0;e<d.pointArray.length;e++){this.mapView.addPoint(d.pointArray[e])}this.mapView.updateTimeDistance(c.getElapsedFormat(),c.getDistance());this.mapView.updateSpeeds(c.getCurrentMph(),c.getMinMile())};this.removeSavedRun=function(a){var b;if(a.getAttribute("data-key")){b=a.getAttribute("data-key")}else{b=a.parentNode.getAttribute("data-key")}localStorage.removeItem(b);this.showHome()};this.errorHandler=function(a){this.backdrop.classList.remove("none");this.errorModal.classList.remove("none")};this.showMap=function(){this.mapPage.classList.remove("none");this.startPage.classList.add("none");this.stop.classList.remove("none");this.backButton.classList.add("none");if(this.mapView!=null){this.mapView.destroy()}this.mapView=new mapView;this.runPath=new runPath;var a=this;this.watchid=navigator.geolocation.watchPosition(function(b){a.GetLocation(b)},function(b){a.errorHandler(b)},{enableHighAccuracy:true,maximumAge:5e3,timeout:12e3});setTimeout(function(){window.scrollTo(0,0)},0)};this.stopMap=function(){this.backdrop.classList.remove("none");this.saveModal.classList.remove("none")};this.GetLocation=function(a){if(ptTest=this.runPath.addPoint(a)){this.mapView.addPoint(a);this.mapView.updateTimeDistance(this.runPath.getElapsedFormat(),this.runPath.getDistance());this.mapView.updateSpeeds(this.runPath.getCurrentMph(),this.runPath.getMinMile())}}}mapView.prototype.addPoint=function(a){opLonLat=new OpenLayers.LonLat(a.coords.longitude,a.coords.latitude);opLonLat.transform(this.proj,this.map.getProjectionObject());point=new OpenLayers.Geometry.Point(opLonLat.lon,opLonLat.lat);this.line.addComponent(point);this.lineLayer.redraw();this.map.setCenter(opLonLat)};mapView.prototype.destroy=function(){this.map.destroy()};if(typeof Number.prototype.toRad==="undefined"){Number.prototype.toRad=function(){return this*Math.PI/180}}window.addEventListener("DOMContentLoaded",init,false)
+(function(a){a.appController=function(){this.mapView=null;this.runPath=null;this.watchid=null;this.mapPage=document.getElementById("mapPage");this.startPage=document.getElementById("startPage");this.showMapButton=document.getElementById("showMap");this.stop=document.getElementById("stop");this.saveButton=document.getElementById("saveRun");this.clearButton=document.getElementById("clearRun");this.backButton=document.getElementById("back");this.table=document.getElementById("savedRuns");this.errorModal=document.getElementById("errorModal");this.clearError=document.getElementById("clearError");this.saveModal=document.getElementById("saveModal");this.backdrop=document.getElementById("backdrop");this.addEvents=function(){var a=this;this.showMapButton.addEventListener("click",function(){a.showMap()});this.stop.addEventListener("click",function(){a.stopMap()});this.saveButton.addEventListener("click",function(){a.saveRun()});this.clearButton.addEventListener("click",function(){a.clearRun()});this.backButton.addEventListener("click",function(){a.clearRun()});this.clearError.addEventListener("click",function(){a.clearRun()})};this.saveRun=function(){localStorage.setItem(this.runPath.startTime,JSON.stringify(this.runPath));if(this.mapView!=null){this.mapView.destroy()}if(this.watchid!=null){navigator.geolocation.clearWatch(this.watchid);this.watchid=null}if(this.runPath!=null){this.runPath=null}this.backdrop.classList.add("none");this.saveModal.classList.add("none");this.mapPage.classList.add("none");this.startPage.classList.remove("none");this.showHome()};this.clearRun=function(){if(this.mapView!=null){this.mapView.destroy()}if(this.watchid!=null){navigator.geolocation.clearWatch(this.watchid);this.watchid=null}if(this.runPath!=null){this.runPath=null}this.backdrop.classList.add("none");this.saveModal.classList.add("none");this.errorModal.classList.add("none");this.mapPage.classList.add("none");this.startPage.classList.remove("none")};this.showHome=function(){var a=this;this.table.classList.remove("none");for(var b=this.table.rows.length;b>1;b--){this.table.deleteRow(b-1)}for(var c in localStorage){var d=JSON.parse(localStorage.getItem(c));var e=this.table.rows.length;var f=this.table.insertRow(e);var g=f.insertCell(0);var h=document.createElement("button");h.className="btn btn-danger";h.innerHTML='<i class="icon-remove icon-white"></i>';h.setAttribute("data-key",c);h.addEventListener("click",function(b){a.removeSavedRun(b.target)});g.appendChild(h);var i=f.insertCell(1);var j=new Date(d.startTime);var k=document.createTextNode(j.toDateString());i.appendChild(k);var l=f.insertCell(2);var m=document.createTextNode(Math.round(d.distance*100)/100);l.appendChild(m);var n=f.insertCell(3);var o=document.createElement("button");o.className="btn btn-inverse";o.innerHTML='<i class="icon-chevron-right icon-white"></i>';o.setAttribute("data-key",c);o.addEventListener("click",function(b){a.loadSavedRun(b.target)});n.appendChild(o)}if(this.table.rows.length==1){this.table.classList.add("none")}};this.loadSavedRun=function(b){var c;if(b.getAttribute("data-key")){c=b.getAttribute("data-key")}else{c=b.parentNode.getAttribute("data-key")}var d=new a.runPath;var e=JSON.parse(localStorage.getItem(c));d.startTime=e.startTime;d.distance=e.distance;d.lastUpdateTime=e.lastUpdateTime;d.pointArray=e.pointArray;this.mapPage.classList.remove("none");this.startPage.classList.add("none");this.stop.classList.add("none");this.backButton.classList.remove("none");if(this.mapView!=null){this.mapView.destroy()}this.mapView=new a.mapView;for(var f=0;f<e.pointArray.length;f++){this.mapView.addPoint(e.pointArray[f])}this.mapView.updateTimeDistance(d.getElapsedFormat(),d.getDistance());this.mapView.updateSpeeds(d.getCurrentMph(),d.getMinMile())};this.removeSavedRun=function(a){var b;if(a.getAttribute("data-key")){b=a.getAttribute("data-key")}else{b=a.parentNode.getAttribute("data-key")}localStorage.removeItem(b);this.showHome()};this.errorHandler=function(a){this.backdrop.classList.remove("none");this.errorModal.classList.remove("none")};this.showMap=function(){this.mapPage.classList.remove("none");this.startPage.classList.add("none");this.stop.classList.remove("none");this.backButton.classList.add("none");if(this.mapView!=null){this.mapView.destroy()}this.mapView=new a.mapView;this.runPath=new a.runPath;var b=this;this.watchid=navigator.geolocation.watchPosition(function(a){b.GetLocation(a)},function(a){b.errorHandler(a)},{enableHighAccuracy:true,maximumAge:5e3,timeout:12e3});setTimeout(function(){window.scrollTo(0,0)},0)};this.stopMap=function(){this.backdrop.classList.remove("none");this.saveModal.classList.remove("none")};this.GetLocation=function(a){if(ptTest=this.runPath.addPoint(a)){this.mapView.addPoint(a);this.mapView.updateTimeDistance(this.runPath.getElapsedFormat(),this.runPath.getDistance());this.mapView.updateSpeeds(this.runPath.getCurrentMph(),this.runPath.getMinMile())}}};a.mapView=function(){this.map=new OpenLayers.Map("map");this.proj=new OpenLayers.Projection("EPSG:4326");this.line=new OpenLayers.Geometry.LineString([]);this.lineLayer=new OpenLayers.Layer.Vector("Line Layer");this.timeSpan=document.getElementById("time");this.distance=document.getElementById("distance");this.mph=document.getElementById("mph");this.minMile=document.getElementById("minMile");var a=new OpenLayers.Layer.OSM;this.map.addControl(new OpenLayers.Control.DrawFeature(this.lineLayer,OpenLayers.Handler.Path));var b={strokeColor:"#0000ff",strokeOpacity:.5,strokeWidth:5};var c=new OpenLayers.Feature.Vector(this.line,null,b);this.lineLayer.addFeatures([c]);this.map.addLayers([a,this.lineLayer]);this.map.zoomTo(15);this.updateTimeDistance=function(a,b){this.timeSpan.innerHTML=a;this.distance.innerHTML=b};this.updateSpeeds=function(a,b){this.mph.innerHTML=a;this.minMile.innerHTML=b}};a.mapView.prototype.addPoint=function(a){opLonLat=new OpenLayers.LonLat(a.coords.longitude,a.coords.latitude);opLonLat.transform(this.proj,this.map.getProjectionObject());point=new OpenLayers.Geometry.Point(opLonLat.lon,opLonLat.lat);this.line.addComponent(point);this.lineLayer.redraw();this.map.setCenter(opLonLat)};a.mapView.prototype.destroy=function(){this.map.destroy()};if(typeof Number.prototype.toRad==="undefined"){Number.prototype.toRad=function(){return this*Math.PI/180}}a.runPath=function(){this.distance=0,this.startTime=0,this.lastUpdateTime=0,this.pointArray=[],this.addPoint=function(a){if(a.coords.accuracy<=25&&a.timestamp-this.lastUpdateTime>=6e3||this.lastUpdateTime==0){var b=(new Date).getTime();this.pointArray.push(a);this.lastUpdateTime=b;if(this.startTime==0){this.startTime=b}if(this.pointArray.length>=2){var c=this.pointArray.length;this.distance+=this.computeDistance(this.pointArray[c-2],this.pointArray[c-1])}return a}return false},this.computeDistance=function(a,b){var c=a.coords.longitude;var d=b.coords.longitude;var e=a.coords.latitude;var f=b.coords.latitude;var g=3958.7558657440545;var h=(f-e).toRad();var i=(d-c).toRad();var e=e.toRad();var f=f.toRad();var j=Math.sin(h/2)*Math.sin(h/2)+Math.sin(i/2)*Math.sin(i/2)*Math.cos(e)*Math.cos(f);var k=2*Math.atan2(Math.sqrt(j),Math.sqrt(1-j));var l=g*k;return l},this.getDistance=function(){return this.distance.toPrecision(4)},this.getElapsed=function(){return(this.lastUpdateTime-this.startTime)/6e4},this.getElapsedFormat=function(){var a=this.lastUpdateTime-this.startTime;var b=Math.floor(a/1e3/60);a-=b*6e4;var c=Math.floor(a/1e3);c=(c<10?"0":"")+c;return b+":"+c},this.getCurrentMph=function(){return(this.pointArray[this.pointArray.length-1].coords.speed*2.23693629).toPrecision(4)},this.getMph=function(){return this.distance/(this.getElapsed()/60)},this.getMinMile=function(){var a=this.getElapsed()/this.distance;if(isNaN(a)){return 0}return a.toPrecision(4)}}})(window.RunBrowser=window.RunBrowser||{})

0 comments on commit 0e87f16

Please sign in to comment.