From 33b134dd1a60d2f2b4968e2472f823fa40dac2d7 Mon Sep 17 00:00:00 2001
From: Anton Sarukhanov <code@ant.sr>
Date: Tue, 26 Apr 2016 21:22:18 -0400
Subject: [PATCH] Misc fixes for embed

---
 static/js/embed.js         | 10 ++++++++++
 static/js/map.js           | 11 ++++++-----
 static/js/predictions.js   | 13 ++++++++-----
 templates/modal-embed.html |  4 ++--
 4 files changed, 26 insertions(+), 12 deletions(-)

diff --git a/static/js/embed.js b/static/js/embed.js
index 69a80a8..0096c79 100644
--- a/static/js/embed.js
+++ b/static/js/embed.js
@@ -28,6 +28,11 @@ BusMap.Embed = function(opts) {
             var opt = $('<option value="' + s.id + '">' + s.title + '</option>');
             opt.appendTo("#embed-stop");
         }
+        if ($("#embed-stop option").length > 1 && !$("#embed-stop").val()) {
+            // We have stops. Select the first one.
+            $("#embed-stop option:nth-child(2)").prop('selected', true);
+            $("#embed-stop").change(); // fire event to handle the change
+        }
     }
 
     var updateEmbedUrl = function() {
@@ -66,6 +71,11 @@ BusMap.Embed = function(opts) {
         $("#embed-mode").change(function() {
             embedVars['mode'] = $(this).val();
             updateEmbedUrl();
+            if ($(this).val() == "m") {
+                $("label[for=embed-popup]").show();
+            } else {
+                $("label[for=embed-popup]").hide();
+            }
         });
         $("#embed-stop").change(function() {
             if ($(this).val()) {
diff --git a/static/js/map.js b/static/js/map.js
index 0f682ad..ed54ce9 100644
--- a/static/js/map.js
+++ b/static/js/map.js
@@ -34,6 +34,11 @@ BusMap.Map = function(opts) {
         else {that.leaflet.fitBounds(that.opts.bounds)}
         if (that.opts.zoom) that.leaflet.setZoom(that.opts.zoom);
 
+        // Listen for hash change
+        window.onhashchange = function() {
+            setViewFromUrl();
+        };
+
         // Go to view requested by URL hash (if set)
         var viewOk = setViewFromUrl();
 
@@ -48,10 +53,6 @@ BusMap.Map = function(opts) {
             setUrlFromView();
         });
 
-        // Listen for hash change
-        window.onhashchange = function() {
-            setViewFromUrl();
-        };
 
         // Show/hide markers based on zoom.
         that.leaflet.on('zoomend', zoomShowHide);
@@ -352,7 +353,7 @@ BusMap.Map = function(opts) {
         window.location.replace("#" + view);
         return _avsLatLonZoom(view);
         function _avsStop(view) {
-            if (!that.stopMarkers) {
+            if (!that.stopMarkers || !that.stopMarkers[view.substring(1)]) {
                 return setTimeout(function() { _avsStop(view) }, 500);
             }
             var marker = that.stopMarkers[view.substring(1)];
diff --git a/static/js/predictions.js b/static/js/predictions.js
index 4830860..962ead9 100644
--- a/static/js/predictions.js
+++ b/static/js/predictions.js
@@ -19,10 +19,10 @@ BusMap.Predictions = function(opts) {
         }
 
         // Listen for hash change
-        window.onhashchange = function() {
-            console.log('foo');
-            setViewFromUrl();
-        };
+        window.onhashchange = setViewFromUrl;
+
+        // Grab initial URL
+        setViewFromUrl();
 
     };
 
@@ -95,7 +95,10 @@ BusMap.Predictions = function(opts) {
 
     function setViewFromUrl() {
         var hash = window.location.hash.substring(1);
-        applyViewString(hash);
+        console.log(hash);
+        if (hash == "") {
+            $(that.opts.predictionsElement).html("No stop selected");
+        } else applyViewString(hash);
     }
 
     /* Refresh (and/or create) UI elements for Stops */
diff --git a/templates/modal-embed.html b/templates/modal-embed.html
index 5165b13..fe051a1 100644
--- a/templates/modal-embed.html
+++ b/templates/modal-embed.html
@@ -3,7 +3,7 @@
     {% elif agency.title %}{{ agency.title }}{% endif %} Bus Map
 </h2>
 <p>
-    Use maps and/or stop predictions in your website or digital signage.
+    Use maps and stop predictions in your website or digital signage.
 </p>
 <h3>Embed Code</h3>
 <textarea id="embed-code" cols=100 rows=3  readonly=true></textarea>
@@ -57,7 +57,7 @@
         <label for="embed-responsive">
             <span>Responsive:</span>
             <input id="embed-responsive" type="checkbox" checked>
-            <small>Map will resize automatically to fit a smaller window or device.</small>
+            <small>Resize automatically to fit a smaller window or device.</small>
         </label>
     </p>
 </form>
-- 
GitLab