From 17cb38281bbfa776fe39af3ec9cd9594a7476cf9 Mon Sep 17 00:00:00 2001
From: Markus Willman <mpewil@utu.fi>
Date: Wed, 28 Feb 2018 19:39:05 +0200
Subject: [PATCH] fix twitter widget link disabling, weather is still not
 working

---
 templates/info.html          | 36 +++++++++++++++++-------------------
 templates/internal/base.html |  7 +++++--
 2 files changed, 22 insertions(+), 21 deletions(-)

diff --git a/templates/info.html b/templates/info.html
index 939fdef..2de84f1 100644
--- a/templates/info.html
+++ b/templates/info.html
@@ -8,7 +8,6 @@
 {% block content %}
 
 <div class="container">
-
     <div class="row">
         <div class="col-12">
             <!-- Forecast7.com -->
@@ -33,10 +32,8 @@
             <div class="card info-card box-shadow">
                 <div id="twitter">
                     <a class="twitter-timeline" data-height="600" data-dnt="true" data-theme="light" href="https://twitter.com/Turkukaupunki?ref_src=twsrc%5Etfw">Tweets by Turkukaupunki</a>
-                    <!--<script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>-->
                     <script>
                         window.twttr = (function (d, s, id) {
-                            console.log("window.twttr");
                             var js, fjs = d.getElementsByTagName(s)[0],
                                 t = window.twttr || {};
                             if (d.getElementById(id)) return;
@@ -49,7 +46,6 @@
                             t.ready = function (f) {
                                 t._e.push(f);
                             };
-                            console.log("window.twttr finished.")
                             return t;
                         }(document, "script", "twitter-wjs"));
                     </script>
@@ -73,23 +69,25 @@
 {% endblock content %}
 
 {% block scripts %}
-{{- super() }}
+    {{- super() }}
 
-<script>
+    <script>
+        $(document).ready(function () {
+            console.log("Document ready.");
 
-    $(document).ready(function () {
-        console.log("Document ready.");
+            $('#visitTurku').disableExternalLinks();
+            $('#twitter').disableExternalLinks(); // in case the widget fails to load disable the link in the container as a placeholder
+            //$('#forecast7').disableExternalLinks(); // not working, iframes need to die out :)
 
-        $('#visitTurku').disableExternalLinks(); //Working
-        $('#forecast7').disableExternalLinks(); //Not working
-        $('#twitter').disableExternalLinks(); //Not working
-        
-        twttr.ready(function (twttr) {
-            console.log("twttr.ready");
-            //twttr.events.bind('loaded', function () { console.log("twttr loaded."); });
-            twttr.events.bind('loaded', function () { $('#twitter').disableExternalLinks(); });
+            twttr.ready(function (twttr) {
+                twttr.events.bind('loaded', function (e) {
+                    e.widgets.forEach(function (w) {
+                        // the twitter widget is loaded in an iframe, contents() is needed because of that
+                        $('#' + w.id).contents().disableExternalLinks();
+                    });
+                });
+            });
         });
-    });
-</script>
+    </script>
 
-{% endblock scripts %}
\ No newline at end of file
+{% endblock scripts %}
diff --git a/templates/internal/base.html b/templates/internal/base.html
index f8e4134..e44323f 100644
--- a/templates/internal/base.html
+++ b/templates/internal/base.html
@@ -156,7 +156,8 @@
 				if ({{ (not g.is_kiosk)|tojson|safe }} && !force)
 					return this;
 
-				this.on('click', 'a[href^="http"]:not([href*="' + location.hostname + '"])', function (e) {
+				var target = this;
+				var handler = function (e) {
 					e.preventDefault();
 					$link = $(this);
 					$modal = $('#externalLinkModal');
@@ -168,7 +169,9 @@
 						$(this).removeData('bs.modal');
 						$modal.find('.externalLinkURI').empty();
 					}).modal();
-				});
+				};
+
+				target.on('click', 'a[href^="http"]:not([href*="' + location.hostname + '"])', handler);
 				return this;
 			};
 
-- 
GitLab