From 8aae861b573a343b94eb06c206bcd059b4ac5f46 Mon Sep 17 00:00:00 2001
From: Aurélien Bompard <aurelien@bompard.org>
Date: Jan 25 2017 15:31:55 +0000
Subject: [PATCH 1/6] Better way to generate JS from Python


---

diff --git a/hubs/models.py b/hubs/models.py
index 68197d8..dd6aaa2 100755
--- a/hubs/models.py
+++ b/hubs/models.py
@@ -266,6 +266,10 @@ class Hub(BASE):
             [w for w in self.widgets if not w.left],
             key=lambda w: w.index)
 
+    @property
+    def widgets_idx(self):
+        return [w.idx for w in self.widgets]
+
     def __json__(self, session):
         return {
             'name': self.name,
diff --git a/hubs/templates/hubs.html b/hubs/templates/hubs.html
index 9de8638..4618c0c 100644
--- a/hubs/templates/hubs.html
+++ b/hubs/templates/hubs.html
@@ -335,7 +335,7 @@ function visit_counter() {
 visit_counter()
 
 function setup_widgets(widgets) {
-  var all_widgets = [{% for widget in hub.widgets %}'{{ widget.idx }}',{% endfor %}];
+  var all_widgets = {{ hub.widgets_idx|tojson }};
   if (widgets == undefined) {
     var widgets = all_widgets;
   }
diff --git a/hubs/templates/stream.html b/hubs/templates/stream.html
index 49a3dab..c2c0c7e 100644
--- a/hubs/templates/stream.html
+++ b/hubs/templates/stream.html
@@ -97,7 +97,7 @@
   src ="{{url_for('static', filename='js/hubs.js')}}"></script>
 <script>
  function setup_widgets(widgets) {
-   var all_widgets = [{% for widget in hub.widgets %}'{{ widget.idx }}',{% endfor %}];
+   var all_widgets = {{ hub.widgets_idx|tojson }};
    if (widgets == undefined) {
      var widgets = all_widgets;
    }

From 69c116e77f2aa7822ce6d38e8e00f59f4b250a3e Mon Sep 17 00:00:00 2001
From: Aurélien Bompard <aurelien@bompard.org>
Date: Jan 25 2017 15:32:20 +0000
Subject: [PATCH 2/6] Use event delegation


---

diff --git a/hubs/templates/hubs.html b/hubs/templates/hubs.html
index 4618c0c..c893767 100644
--- a/hubs/templates/hubs.html
+++ b/hubs/templates/hubs.html
@@ -282,9 +282,8 @@ $('#save_edits_btn').click(function() {
 
 <script type="text/javascript">
 function setup_edit_btns() {
-  $(".edit_widget").unbind();
-  $(".edit_widget").click(function() {
-    console.log($(this));
+  $("body").on("click", ".edit_widget", function(e) {
+    e.preventDefault();
     var _idx = $(this).attr('data-idx');
 
     $.ajax({
@@ -348,7 +347,6 @@ function setup_widgets(widgets) {
         setTimeout(function() {
           $('#widget-' + widget + ' .panel').toggleClass('panel-visible');
         }, 100);
-        setup_edit_btns()
       },
       error: function() {
         $('#widget-' + widget).html('Got an error retrieving this widget.  Sorry :(');
@@ -356,8 +354,8 @@ function setup_widgets(widgets) {
         console.trace();
       },
     });
-    setup_edit_btns();
   });
+  setup_edit_btns();
 
   {% if edit -%}
   make_widget_sortable();

From 2d911a7528e3aa134520d03059d36bb816094ae8 Mon Sep 17 00:00:00 2001
From: Aurélien Bompard <aurelien@bompard.org>
Date: Jan 25 2017 15:36:07 +0000
Subject: [PATCH 3/6] Don't use window.onload when we have jQuery


---

diff --git a/hubs/templates/stream.html b/hubs/templates/stream.html
index c2c0c7e..478d3da 100644
--- a/hubs/templates/stream.html
+++ b/hubs/templates/stream.html
@@ -119,10 +119,6 @@
      });
    });
  }
- window.onload = function(){
-   setup_widgets();
-   setup_feeds();
- }
 
  function setup_feeds() {
    var actions = {{actions|safe}};
@@ -169,5 +165,9 @@
      });
    });
  }
+ $(function() {
+   setup_widgets();
+   setup_feeds();
+ });
 </script>
 {% endblock %}

From 2a8ab77b2d78b16c8076e758376ad3e82fef07f2 Mon Sep 17 00:00:00 2001
From: Aurélien Bompard <aurelien@bompard.org>
Date: Jan 25 2017 16:11:06 +0000
Subject: [PATCH 4/6] Don't assume what the root URL is


---

diff --git a/hubs/templates/hubs.html b/hubs/templates/hubs.html
index c893767..7a132b3 100644
--- a/hubs/templates/hubs.html
+++ b/hubs/templates/hubs.html
@@ -333,14 +333,14 @@ function visit_counter() {
 
 visit_counter()
 
-function setup_widgets(widgets) {
+function setup_widgets(base_url, widgets) {
   var all_widgets = {{ hub.widgets_idx|tojson }};
   if (widgets == undefined) {
     var widgets = all_widgets;
   }
   $.each(widgets, function(i, widget) {
     $.ajax({
-      url: widget,
+      url: base_url + widget,
       dataType: 'html',
       success: function(html) {
         $('#widget-' + widget).html(html);
@@ -363,7 +363,7 @@ function setup_widgets(widgets) {
   {%- endif %}
 }
 
-setup_widgets();
+setup_widgets({{ url_for("hub", name=hub.name) }});
 
 /* This is how to activate and remove (here after 13 sec) the favicon
 notification */
diff --git a/hubs/templates/stream.html b/hubs/templates/stream.html
index 478d3da..3fd4432 100644
--- a/hubs/templates/stream.html
+++ b/hubs/templates/stream.html
@@ -96,14 +96,14 @@
 <script type="text/javascript"
   src ="{{url_for('static', filename='js/hubs.js')}}"></script>
 <script>
- function setup_widgets(widgets) {
+ function setup_widgets(base_url, widgets) {
    var all_widgets = {{ hub.widgets_idx|tojson }};
    if (widgets == undefined) {
      var widgets = all_widgets;
    }
    $.each(widgets, function(i, widget) {
      $.ajax({
-       url: '/{{hub.name}}/' + widget,
+       url: base_url + widget,
        dataType: 'html',
        success: function(html) {
          $('#widget-' + widget).html(html);
@@ -166,7 +166,7 @@
    });
  }
  $(function() {
-   setup_widgets();
+   setup_widgets({{ url_for("hub", name=hub.name) }});
    setup_feeds();
  });
 </script>

From 0e7559e003d2532f4fec4bbbeb3fa8ae77c59b59 Mon Sep 17 00:00:00 2001
From: Aurélien Bompard <aurelien@bompard.org>
Date: Jan 25 2017 16:11:06 +0000
Subject: [PATCH 5/6] Move duplicated JS code to the utils library


---

diff --git a/hubs/static/js/utils.js b/hubs/static/js/utils.js
index 1e97046..01f2382 100644
--- a/hubs/static/js/utils.js
+++ b/hubs/static/js/utils.js
@@ -10,3 +10,25 @@ var collapse_feed_entries = function(idx) {
     $("#collapse-" + idx).addClass('hidden');
     $("#content-" + idx).addClass('hidden');
 }
+
+// Load widgets
+ function setup_widgets(base_url, widgets) {
+   console.log(base_url, widgets);
+   $.each(widgets, function(i, widget) {
+     $.ajax({
+       url: base_url + widget,
+       dataType: 'html',
+       success: function(html) {
+         $('#widget-' + widget).html(html);
+         setTimeout(function() {
+           $('#widget-' + widget + ' .panel').toggleClass('panel-visible');
+         }, 100);
+       },
+       error: function() {
+         $('#widget-' + widget).html('Got an error retrieving this widget.  Sorry :(');
+         console.log('error');
+         console.trace();
+       },
+     });
+   });
+ }
diff --git a/hubs/templates/hubs.html b/hubs/templates/hubs.html
index 7a132b3..5a1df4c 100644
--- a/hubs/templates/hubs.html
+++ b/hubs/templates/hubs.html
@@ -331,49 +331,30 @@ function visit_counter() {
   }
 }
 
-visit_counter()
-
-function setup_widgets(base_url, widgets) {
-  var all_widgets = {{ hub.widgets_idx|tojson }};
-  if (widgets == undefined) {
-    var widgets = all_widgets;
-  }
-  $.each(widgets, function(i, widget) {
-    $.ajax({
-      url: base_url + widget,
-      dataType: 'html',
-      success: function(html) {
-        $('#widget-' + widget).html(html);
-        setTimeout(function() {
-          $('#widget-' + widget + ' .panel').toggleClass('panel-visible');
-        }, 100);
-      },
-      error: function() {
-        $('#widget-' + widget).html('Got an error retrieving this widget.  Sorry :(');
-        console.log('error');
-        console.trace();
-      },
-    });
-  });
+$(function() {
+  visit_counter()
+  
+  setup_widgets(
+    {{ url_for("hub", name=hub.name) }},
+    {{ hub.widgets_idx|tojson }}
+    );
   setup_edit_btns();
-
+  
   {% if edit -%}
   make_widget_sortable();
   setup_add_btns();
   {%- endif %}
-}
 
-setup_widgets({{ url_for("hub", name=hub.name) }});
-
-/* This is how to activate and remove (here after 13 sec) the favicon
-notification */
-console.log('Show notification');
-Notificon('#33ff00');
-setTimeout(function(){console.log('Hide notification'); Notificon()}, 13000)
-setTimeout(function(){
-    console.log('Show notification'); Notificon('#eb361e')
-  }, 15000)
-setTimeout(function(){console.log('Hide notification'); Notificon()}, 18000)
+  /* This is how to activate and remove (here after 13 sec) the favicon
+  notification */
+  console.log('Show notification');
+  Notificon('#33ff00');
+  setTimeout(function(){console.log('Hide notification'); Notificon()}, 13000)
+  setTimeout(function(){
+      console.log('Show notification'); Notificon('#eb361e')
+    }, 15000)
+  setTimeout(function(){console.log('Hide notification'); Notificon()}, 18000)
+});
 
 </script>
 {% endblock %}
diff --git a/hubs/templates/stream.html b/hubs/templates/stream.html
index 3fd4432..278b076 100644
--- a/hubs/templates/stream.html
+++ b/hubs/templates/stream.html
@@ -95,30 +95,9 @@
 {{ super() }}
 <script type="text/javascript"
   src ="{{url_for('static', filename='js/hubs.js')}}"></script>
+<script type="text/javascript" src="{{
+  url_for('static', filename='js/utils.js') }}"></script>
 <script>
- function setup_widgets(base_url, widgets) {
-   var all_widgets = {{ hub.widgets_idx|tojson }};
-   if (widgets == undefined) {
-     var widgets = all_widgets;
-   }
-   $.each(widgets, function(i, widget) {
-     $.ajax({
-       url: base_url + widget,
-       dataType: 'html',
-       success: function(html) {
-         $('#widget-' + widget).html(html);
-         setTimeout(function() {
-           $('#widget-' + widget + ' .panel').toggleClass('panel-visible');
-         }, 100);
-       },
-       error: function() {
-         $('#widget-' + widget).html('Got an error retrieving this widget.  Sorry :(');
-         console.log('error');
-         console.trace();
-       },
-     });
-   });
- }
 
  function setup_feeds() {
    var actions = {{actions|safe}};
@@ -166,7 +145,10 @@
    });
  }
  $(function() {
-   setup_widgets({{ url_for("hub", name=hub.name) }});
+   setup_widgets(
+    {{ url_for("hub", name=hub.name) }},
+    {{ hub.widgets_idx|tojson }}
+    );
    setup_feeds();
  });
 </script>

From 7e3eb51a00e877be10f48dfaacb8b8235d037166 Mon Sep 17 00:00:00 2001
From: Aurélien Bompard <aurelien@bompard.org>
Date: Jan 26 2017 16:12:33 +0000
Subject: [PATCH 6/6] Add a docblock


---

diff --git a/hubs/models.py b/hubs/models.py
index dd6aaa2..add289e 100755
--- a/hubs/models.py
+++ b/hubs/models.py
@@ -268,6 +268,7 @@ class Hub(BASE):
 
     @property
     def widgets_idx(self):
+        """Returns the list of indices for this hub's widgets."""
         return [w.idx for w in self.widgets]
 
     def __json__(self, session):

