Skip to content
Snippets Groups Projects
Commit d4c0e474 authored by Anton Sarukhanov's avatar Anton Sarukhanov
Browse files

'that' var scope fix; strictmode fixes

parent dcce4d05
No related branches found
No related tags found
1 merge request!1Implemented Embed-code generator interface
...@@ -2,8 +2,7 @@ ...@@ -2,8 +2,7 @@
var BusMap = { var BusMap = {
cookiePrefix: "BM_", cookiePrefix: "BM_",
zoomShowVehicles: 15, zoomShowVehicles: 15,
vehicleZoom: 16, stopZoom: 18,
stopZoom: 16,
vehicleMaxAge: 10, vehicleMaxAge: 10,
}; };
...@@ -12,12 +11,12 @@ var BusMap = { ...@@ -12,12 +11,12 @@ var BusMap = {
Updating the map (Vehicles, Routes) is also handled here. Updating the map (Vehicles, Routes) is also handled here.
*/ */
BusMap.Map = function(opts) { BusMap.Map = function(opts) {
this.opts = opts;
var stops = {}; var stops = {};
var routes = {}; var routes = {};
var modal_cache = {}; var modal_cache = {};
var that = this; var that = this.Map;
window._busmap = this; that.opts = opts;
window._busmap = this.Map;
/* Constructor - create/initialize the map */ /* Constructor - create/initialize the map */
function init() { function init() {
...@@ -27,7 +26,8 @@ BusMap.Map = function(opts) { ...@@ -27,7 +26,8 @@ BusMap.Map = function(opts) {
animate: false, animate: false,
reset: true, reset: true,
}; };
that.leaflet = L.map(that.opts.mapElement, mapOptions) that.map = that.opts.mapElement;
that.leaflet = L.map(that.map, mapOptions)
.fitBounds(that.opts.bounds) .fitBounds(that.opts.bounds)
.setMaxBounds(that.opts.bounds, boundsOptions); .setMaxBounds(that.opts.bounds, boundsOptions);
if (that.opts.center) { that.leaflet.setView(that.opts.center); } if (that.opts.center) { that.leaflet.setView(that.opts.center); }
...@@ -35,9 +35,7 @@ BusMap.Map = function(opts) { ...@@ -35,9 +35,7 @@ BusMap.Map = function(opts) {
if (that.opts.zoom) that.leaflet.setZoom(that.opts.zoom); if (that.opts.zoom) that.leaflet.setZoom(that.opts.zoom);
// Listen for hash change // Listen for hash change
window.onhashchange = function() { $(window).on('hashchange', setViewFromUrl);
setViewFromUrl();
};
// Go to view requested by URL hash (if set) // Go to view requested by URL hash (if set)
var viewOk = setViewFromUrl(); var viewOk = setViewFromUrl();
...@@ -49,9 +47,9 @@ BusMap.Map = function(opts) { ...@@ -49,9 +47,9 @@ BusMap.Map = function(opts) {
// Store view parameters for recovery later. // Store view parameters for recovery later.
that.leaflet.on('moveend', lastViewStore); that.leaflet.on('moveend', lastViewStore);
that.leaflet.on('moveend', function() { if (that.opts.hashUpdate !== false) {
setUrlFromView(); that.leaflet.on('moveend', setUrlFromView);
}); }
// Show/hide markers based on zoom. // Show/hide markers based on zoom.
...@@ -62,7 +60,7 @@ BusMap.Map = function(opts) { ...@@ -62,7 +60,7 @@ BusMap.Map = function(opts) {
var tileOptions = { var tileOptions = {
}; };
if (that.opts.tileOptions) { if (that.opts.tileOptions) {
for (o in that.opts.tileOptions) { for (var o in that.opts.tileOptions) {
tileOptions[o] = that.opts.tileOptions[o]; tileOptions[o] = that.opts.tileOptions[o];
} }
} }
...@@ -148,7 +146,7 @@ BusMap.Map = function(opts) { ...@@ -148,7 +146,7 @@ BusMap.Map = function(opts) {
that.stops[s].predictions = {}; that.stops[s].predictions = {};
} }
for (var p in data.predictions) { for (var p in data.predictions) {
pr = data.predictions[p]; var pr = data.predictions[p];
if (that.stops && pr.stop_id in that.stops) { if (that.stops && pr.stop_id in that.stops) {
// Store this prediction with the relevant stop // Store this prediction with the relevant stop
if (!(pr.route in that.stops[pr.stop_id].predictions)) { if (!(pr.route in that.stops[pr.stop_id].predictions)) {
...@@ -218,11 +216,11 @@ BusMap.Map = function(opts) { ...@@ -218,11 +216,11 @@ BusMap.Map = function(opts) {
var predictions = []; var predictions = [];
var now = new Date(); var now = new Date();
var offset_mins = now.getTimezoneOffset(); var offset_mins = now.getTimezoneOffset();
psorted = vehicles[v].predictions.sort(function(a,b){ var psorted = vehicles[v].predictions.sort(function(a,b){
return new Date(a.prediction).getTime() - new Date(b.prediction).getTime(); return new Date(a.prediction).getTime() - new Date(b.prediction).getTime();
}); });
for (p in psorted) { for (var p in psorted) {
pr = psorted[p]; var pr = psorted[p];
var p_line = "<strong>" + that.stops[pr.stop_id].title + "</strong>: "; var p_line = "<strong>" + that.stops[pr.stop_id].title + "</strong>: ";
var pdate = new Date(pr.prediction); var pdate = new Date(pr.prediction);
var diff_sec = (pdate.getTime() - now.getTime()) / 1000; var diff_sec = (pdate.getTime() - now.getTime()) / 1000;
...@@ -250,7 +248,7 @@ BusMap.Map = function(opts) { ...@@ -250,7 +248,7 @@ BusMap.Map = function(opts) {
} }
// Remove stale markes from the map // Remove stale markes from the map
for (v in that.vehicleMarkers) { for (v in that.vehicleMarkers) {
var min_updated = Date.now() - (that.vehicleMaxAge * 1000) var min_updated = Date.now() - (BusMap.vehicleMaxAge * 1000)
if (that.vehicleMarkers[v].bm_updated < min_updated) { if (that.vehicleMarkers[v].bm_updated < min_updated) {
that.leaflet.removeLayer(that.vehicleMarkers[v]); that.leaflet.removeLayer(that.vehicleMarkers[v]);
delete that.vehicleMarkers[v]; delete that.vehicleMarkers[v];
...@@ -298,11 +296,11 @@ BusMap.Map = function(opts) { ...@@ -298,11 +296,11 @@ BusMap.Map = function(opts) {
that.stopMarkersClusterGroup.addLayer(that.stopMarkers[s]); that.stopMarkersClusterGroup.addLayer(that.stopMarkers[s]);
} }
// Add predictions to the marker popup, if available // Add predictions to the marker popup, if available
if (stops[s].predictions) { if (stops[s].predictions && that.opts.predictions !== false) {
var predictions = []; var predictions = [];
var now = new Date(); var now = new Date();
var offset_mins = now.getTimezoneOffset(); var offset_mins = now.getTimezoneOffset();
for (r in stops[s].predictions) { for (var r in stops[s].predictions) {
if (!(r in that.routes)) { if (!(r in that.routes)) {
console.log("Unknown route " + r + " for stop " + stops[s].title); console.log("Unknown route " + r + " for stop " + stops[s].title);
} }
...@@ -312,7 +310,7 @@ BusMap.Map = function(opts) { ...@@ -312,7 +310,7 @@ BusMap.Map = function(opts) {
psorted = stops[s].predictions[r].sort(function(a,b){ psorted = stops[s].predictions[r].sort(function(a,b){
return new Date(a.prediction).getTime() - new Date(b.prediction).getTime(); return new Date(a.prediction).getTime() - new Date(b.prediction).getTime();
}); });
for (p in psorted) { for (var p in psorted) {
pr = psorted[p]; pr = psorted[p];
var pdate = new Date(pr.prediction); var pdate = new Date(pr.prediction);
var diff_sec = (pdate.getTime() - now.getTime()) / 1000; var diff_sec = (pdate.getTime() - now.getTime()) / 1000;
...@@ -359,7 +357,7 @@ BusMap.Map = function(opts) { ...@@ -359,7 +357,7 @@ BusMap.Map = function(opts) {
var marker = that.stopMarkers[view.substring(1)]; var marker = that.stopMarkers[view.substring(1)];
if (marker) { if (marker) {
var ll = marker.getLatLng(); var ll = marker.getLatLng();
that.leaflet.setView(ll, that.stopZoom); that.leaflet.setView(ll, BusMap.stopZoom);
} else console.log('Unknown stop in view string: ' + view.substring(1)); } else console.log('Unknown stop in view string: ' + view.substring(1));
if (view.charAt(0) == "S") { if (view.charAt(0) == "S") {
marker.openPopup(); marker.openPopup();
...@@ -394,7 +392,11 @@ BusMap.Map = function(opts) { ...@@ -394,7 +392,11 @@ BusMap.Map = function(opts) {
$(this).parent().remove(); $(this).parent().remove();
setUrlFromView(); setUrlFromView();
}); });
$("#map").after(modal); $(that.map).click(function() {
modal.remove();
setUrlFromView;
});
$(that.opts.mapElement).after(modal);
modal.show(); modal.show();
var params = { var params = {
agency: that.opts.agency, agency: that.opts.agency,
...@@ -419,11 +421,11 @@ BusMap.Map = function(opts) { ...@@ -419,11 +421,11 @@ BusMap.Map = function(opts) {
function zoomShowHide() { function zoomShowHide() {
var zoom = that.leaflet.getZoom(); var zoom = that.leaflet.getZoom();
if (that.vehicleMarkersGroup) { if (that.vehicleMarkersGroup) {
if (zoom >= that.zoomShowVehicles if (zoom >= BusMap.zoomShowVehicles
&& !(that.leaflet.hasLayer(that.vehicleMarkersGroup))) { && !(that.leaflet.hasLayer(that.vehicleMarkersGroup))) {
that.leaflet.addLayer(that.vehicleMarkersGroup); that.leaflet.addLayer(that.vehicleMarkersGroup);
if (!that.opts.embed) $('#msg-zoomForVehicles').hide(); if (!that.opts.embed) $('#msg-zoomForVehicles').hide();
} else if (zoom < that.zoomShowVehicles } else if (zoom < BusMap.zoomShowVehicles
&& that.leaflet.hasLayer(that.vehicleMarkersGroup)) { && that.leaflet.hasLayer(that.vehicleMarkersGroup)) {
that.leaflet.removeLayer(that.vehicleMarkersGroup); that.leaflet.removeLayer(that.vehicleMarkersGroup);
if (!that.opts.embed) $('#msg-zoomForVehicles').show(); if (!that.opts.embed) $('#msg-zoomForVehicles').show();
...@@ -437,6 +439,9 @@ BusMap.Map = function(opts) { ...@@ -437,6 +439,9 @@ BusMap.Map = function(opts) {
delete that['hashChangedProgrammatically']; delete that['hashChangedProgrammatically'];
return; return;
} }
if (that.opts.hashListen == false) {
return;
}
var hash = window.location.hash.substring(1); var hash = window.location.hash.substring(1);
if (hash == "") return; if (hash == "") return;
var parts = hash.split(";"); var parts = hash.split(";");
...@@ -449,6 +454,9 @@ BusMap.Map = function(opts) { ...@@ -449,6 +454,9 @@ BusMap.Map = function(opts) {
} }
function setUrlFromView() { function setUrlFromView() {
if (that.opts.hashUpdate == false) {
return;
}
var view = getViewString(); var view = getViewString();
if ($(".modal").length > 0) { if ($(".modal").length > 0) {
var modalName = $(".modal").attr('id').split('-')[1]; var modalName = $(".modal").attr('id').split('-')[1];
...@@ -462,7 +470,7 @@ BusMap.Map = function(opts) { ...@@ -462,7 +470,7 @@ BusMap.Map = function(opts) {
return; return;
} }
that.hashChangedProgrammatically = true; // avoid infinite loop! that.hashChangedProgrammatically = true; // avoid infinite loop!
window.location.href.replace(window.location.hash, "#" + view); window.location = window.location.href.replace(window.location.hash, "#" + view);
} }
init(); init();
......
BusMap.Predictions = function(opts) { BusMap.Predictions = function(opts) {
this.opts = opts;
var stops = {}; var stops = {};
var routes = {}; var routes = {};
var that = this; var that = this.Predictions;
that.opts = opts;
window._buspredictions = this.Predictions;
/* Constructor - create/initialize the map */ /* Constructor - create/initialize the map */
function init() { function init() {
...@@ -19,7 +20,9 @@ BusMap.Predictions = function(opts) { ...@@ -19,7 +20,9 @@ BusMap.Predictions = function(opts) {
} }
// Listen for hash change // Listen for hash change
window.onhashchange = setViewFromUrl; if (that.opts.hashListen !== false) {
$(window).on('hashchange', setViewFromUrl);
}
// Grab initial URL // Grab initial URL
setViewFromUrl(); setViewFromUrl();
...@@ -60,7 +63,7 @@ BusMap.Predictions = function(opts) { ...@@ -60,7 +63,7 @@ BusMap.Predictions = function(opts) {
that.stops[s].predictions = {}; that.stops[s].predictions = {};
} }
for (var p in data.predictions) { for (var p in data.predictions) {
pr = data.predictions[p]; var pr = data.predictions[p];
if (that.stops && pr.stop_id in that.stops) { if (that.stops && pr.stop_id in that.stops) {
// Store this prediction with the relevant stop // Store this prediction with the relevant stop
if (!(pr.route in that.stops[pr.stop_id].predictions)) { if (!(pr.route in that.stops[pr.stop_id].predictions)) {
...@@ -95,7 +98,6 @@ BusMap.Predictions = function(opts) { ...@@ -95,7 +98,6 @@ BusMap.Predictions = function(opts) {
function setViewFromUrl() { function setViewFromUrl() {
var hash = window.location.hash.substring(1); var hash = window.location.hash.substring(1);
console.log(hash);
if (hash == "") { if (hash == "") {
$(that.opts.predictionsElement).html("No stop selected"); $(that.opts.predictionsElement).html("No stop selected");
} else applyViewString(hash); } else applyViewString(hash);
...@@ -116,18 +118,18 @@ BusMap.Predictions = function(opts) { ...@@ -116,18 +118,18 @@ BusMap.Predictions = function(opts) {
var predictions = []; var predictions = [];
var now = new Date(); var now = new Date();
var offset_mins = now.getTimezoneOffset(); var offset_mins = now.getTimezoneOffset();
for (r in stops[s].predictions) { for (var r in stops[s].predictions) {
if (!(r in that.routes)) { if (!(r in that.routes)) {
console.log("Unknown route " + r + " for stop " + stops[s].title); console.log("Unknown route " + r + " for stop " + stops[s].title);
} }
var p_line = "<strong>" + that.routes[r].title + "</strong>: "; var p_line = "<strong>" + that.routes[r].title + "</strong>: ";
var times = []; var times = [];
// Sort by estimated time to arrival // Sort by estimated time to arrival
psorted = stops[s].predictions[r].sort(function(a,b){ var psorted = stops[s].predictions[r].sort(function(a,b){
return new Date(a.prediction).getTime() - new Date(b.prediction).getTime(); return new Date(a.prediction).getTime() - new Date(b.prediction).getTime();
}); });
for (p in psorted) { for (var p in psorted) {
pr = psorted[p]; var pr = psorted[p];
var pdate = new Date(pr.prediction); var pdate = new Date(pr.prediction);
var diff_sec = (pdate.getTime() - now.getTime()) / 1000; var diff_sec = (pdate.getTime() - now.getTime()) / 1000;
var diff_min = Math.ceil(diff_sec / 60) + offset_mins; var diff_min = Math.ceil(diff_sec / 60) + offset_mins;
...@@ -144,9 +146,9 @@ BusMap.Predictions = function(opts) { ...@@ -144,9 +146,9 @@ BusMap.Predictions = function(opts) {
predictions.push("<span class='route'" + p_line + "</span>"); predictions.push("<span class='route'" + p_line + "</span>");
} }
if (predictions.length == 0) { if (predictions.length == 0) {
predictions = ['<span class="none">No arrival predictions.</span>']; var predictions = ['<span class="none">No arrival predictions.</span>'];
} }
text = '<section class="predictions stop-predictions">' var text = '<section class="predictions stop-predictions">'
+ predictions.sort().join("<br>") + '</section>'; + predictions.sort().join("<br>") + '</section>';
$(that.opts.predictionsElement).find('p').html(text); $(that.opts.predictionsElement).find('p').html(text);
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment