summaryrefslogtreecommitdiff
path: root/src/leaflet-soton.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/leaflet-soton.js')
-rw-r--r--src/leaflet-soton.js176
1 files changed, 163 insertions, 13 deletions
diff --git a/src/leaflet-soton.js b/src/leaflet-soton.js
index 238a921..9fb8998 100644
--- a/src/leaflet-soton.js
+++ b/src/leaflet-soton.js
@@ -549,9 +549,14 @@ SELECT * WHERE {\
});
},
busStop: function(properties, routeLayer) {
- return getTemplateWrapper(properties, function(content) {
- var routesList = [];
+ var closeListener;
+
+ // getTemplateWrapper will call the function immediatly
+ var content = getTemplateWrapper(properties, function(content) {
+
+ var routesInfo = {};
+
var routeMasters = routeLayer.getRouteMasters();
for (var routeMasterName in routeMasters) {
@@ -561,29 +566,168 @@ SELECT * WHERE {\
var route = routeMaster.routes[i];
if (properties.routes.indexOf(route.properties.ref) !== -1) {
- routesList.push(route);
+ routesInfo[route.properties.ref] = route;
}
}
}
- var routeListControl = L.Control.Route.createRouteList({
+ /*var routeListControl = L.Control.Route.createRouteList({
routes: routesList
}, function(routeName) {
routeLayer.resetRoutes();
routeLayer.highlightRoute(routeName);
});
- content.appendChild(routeListControl);
+ content.appendChild(routeListControl);*/
+
+ var busStop = properties.uri.slice(37);
+
+ var timesTable = document.createElement("table");
+
+ timesTable.className = "ls-bus-times-table";
+
+ content.appendChild(timesTable);
+
+ var loading = document.createElement("span");
+ loading.innerHTML = "↻";
+
+ loading.className = "ls-spinning-arrow";
+
+ timesTable.appendChild(loading);
+
+ function groupStops(stops) {
+ var result = {};
+
+ for (var i in stops) {
+ var stop = stops[i];
+
+ if (stop.name in result) {
+ result[stop.name].push(stop);
+ } else {
+ result[stop.name] = [stop];
+ }
+ }
+
+ return result;
+ }
+
+ function groupedStopsToList(groupedStops) {
+ var values = [];
+
+ for (var name in groupedStops) {
+ values.push({
+ name: name,
+ stops: groupedStops[name]
+ });
+ }
+
+ return values.sort(function(a, b) {
+ return b.stops.length - a.stops.length;
+ });
+ }
+
+ function renderTimeData(stops) {
+ console.log(stops);
+
+ var groupedStops = groupStops(stops);
+
+ for (var i in properties.routes) {
+ var rName = properties.routes[i];
+ if (!(rName in groupedStops)) {
+ groupedStops[rName] = [];
+ }
+ }
+
+ var stopsList = groupedStopsToList(groupedStops);
+
+ timesTable.innerHTML = "";
+
+ for (var i in stopsList) {
+ var routeStops = stopsList[i].stops;
+ var name = stopsList[i].name;
+
+ console.log(routeStops);
+
+ var tr = document.createElement("tr");
- var IFrame = document.createElement('iframe');
- IFrame.setAttribute('src', 'http://bus.southampton.ac.uk/bus-stop-iframe/' + properties.uri.slice(37) + ".html")
- IFrame.setAttribute('frameBorder', '0');
- IFrame.setAttribute('style', 'width: 100%; height 100%;');
+ var route = document.createElement("td");
- content.appendChild(IFrame);
+ if (!(name in routesInfo)) {
+ console.warn("skipping " + name);
+ continue;
+ }
+
+ var a = L.Control.Route.createRouteLink(routesInfo[name], (function(name) {
+ return function() {
+ routeLayer.resetRoutes();
+ routeLayer.highlightRoute(name);
+ };
+ })(routesInfo[name].properties.name));
+
+ route.appendChild(a);
+
+ tr.appendChild(route);
+
+ if (routeStops.length === 0) {
+ var none = document.createElement("td");
+ none.textContent = "No busses";
+ none.colSpan = 2;
+
+ if (i !== 0) {
+ none.className = "ls-bus-times-table-seperating-td";
+ }
+
+ tr.appendChild(none);
+
+ timesTable.appendChild(tr);
+
+ } else {
+ route.rowSpan = routeStops.length;
+
+ for (var j in routeStops) {
+ var stop = routeStops[j];
+
+ var time = document.createElement("td");
+ var dest = document.createElement("td");
+
+ if (i != 0 && j == 0) {
+ time.className = "ls-bus-times-table-seperating-td";
+ dest.className = "ls-bus-times-table-seperating-td";
+ }
+
+ time.textContent = stop.time;
+ dest.textContent = stop.dest;
+
+ tr.appendChild(time);
+ tr.appendChild(dest);
- console.log(properties);
+ timesTable.appendChild(tr);
+ tr = document.createElement("tr");
+ }
+ }
+ }
+ }
+
+ UoSLive.subscribeToBusStop(busStop, function(data) {
+
+ console.log(data);
+
+ renderTimeData(data.stops);
+ });
+
+ console.log(busStop);
+
+ closeListener = function() {
+ console.log("its been closed!");
+
+ UoSLive.unsubscribeToBusStop(busStop);
+ };
});
+
+ return {
+ content: content,
+ closeListener: closeListener
+ };
},
busRoute: function(properties) {
return getTemplateWrapper(properties, function(content) {
@@ -905,9 +1049,11 @@ SELECT * WHERE {\
stopOptions: {
onEachFeature: function(feature, layer) {
layer.on('click', function(e) {
- var content = LS.infoTemplates.busStop(feature.properties, routeLayer);
+ var template = LS.infoTemplates.busStop(feature.properties, routeLayer);
- map.showInfo(content, e.latlng);
+ map.showInfo(template.content, e.latlng, {
+ closeListener: template.closeListener
+ });
});
}
}
@@ -1343,6 +1489,10 @@ SELECT * WHERE {\
popup.setContent(content);
+ if (options.closeListener) {
+ map.once('popupclose', options.closeListener);
+ }
+
popup.openOn(map);
},
closeInfo: function() {