From 8aae861b573a343b94eb06c206bcd059b4ac5f46 Mon Sep 17 00:00:00 2001 From: Aurélien Bompard 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')}}"> {% endblock %} From 2a8ab77b2d78b16c8076e758376ad3e82fef07f2 Mon Sep 17 00:00:00 2001 From: Aurélien Bompard 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 @@ From 0e7559e003d2532f4fec4bbbeb3fa8ae77c59b59 Mon Sep 17 00:00:00 2001 From: Aurélien Bompard 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) +}); {% 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() }} + From 7e3eb51a00e877be10f48dfaacb8b8235d037166 Mon Sep 17 00:00:00 2001 From: Aurélien Bompard 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):