From fb726cfa0cd26d28443ed9db73c4ec9f7394fa6d Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Mon, 9 Mar 2015 12:32:54 -0400 Subject: [PATCH] Replace `Discourse.script` global with a module --- app/assets/javascripts/discourse.js | 4 ---- .../initializers/live-development.js.es6 | 23 +++++++++---------- .../discourse/lib/highlight-syntax.js.es6 | 6 ++--- .../javascripts/discourse/lib/lightbox.js.es6 | 4 +++- .../discourse/lib/load-script.js.es6 | 5 ++++ 5 files changed, 22 insertions(+), 20 deletions(-) create mode 100644 app/assets/javascripts/discourse/lib/load-script.js.es6 diff --git a/app/assets/javascripts/discourse.js b/app/assets/javascripts/discourse.js index 37a49d8d9ca..45dbef9147f 100644 --- a/app/assets/javascripts/discourse.js +++ b/app/assets/javascripts/discourse.js @@ -5,10 +5,6 @@ window.Discourse = Ember.Application.createWithMixins(Discourse.Ajax, { rootElement: '#main', _docTitle: document.title, - script: function(url) { - return $LAB.script(this.getURL(url)); - }, - getURL: function(url) { if (!url) { return url; } diff --git a/app/assets/javascripts/discourse/initializers/live-development.js.es6 b/app/assets/javascripts/discourse/initializers/live-development.js.es6 index fbfd5b7918c..d3a75461e8d 100644 --- a/app/assets/javascripts/discourse/initializers/live-development.js.es6 +++ b/app/assets/javascripts/discourse/initializers/live-development.js.es6 @@ -1,13 +1,13 @@ -/** - Use the message bus for live reloading of components for faster development. -**/ +import loadScript from 'discourse/lib/load-script'; + +// Use the message bus for live reloading of components for faster development. export default { name: "live-development", - initialize: function() { + initialize() { // subscribe to any site customizations that are loaded $('link.custom-css').each(function() { - var split = this.href.split("/"), + const split = this.href.split("/"), id = split[split.length - 1].split(".css")[0], self = this; @@ -15,7 +15,7 @@ export default { if (!$(self).data('orig')) { $(self).data('orig', self.href); } - var orig = $(self).data('orig'); + const orig = $(self).data('orig'); self.href = orig.replace(/v=.*/, "v=" + data); }); @@ -23,7 +23,7 @@ export default { // Custom header changes $('header.custom').each(function() { - var header = $(this); + const header = $(this); return Discourse.MessageBus.subscribe("/header-change/" + $(this).data('key'), function(data) { return header.html(data); }); @@ -40,10 +40,9 @@ export default { } else if (me.name.substr(-10) === "hbs") { // Reload handlebars - var js = me.name.replace(".hbs", "").replace("app/assets/javascripts", "/assets"); - Discourse.script(js + "?hash=" + me.hash).wait(function() { - var templateName; - templateName = js.replace(".js", "").replace("/assets/", ""); + const js = me.name.replace(".hbs", "").replace("app/assets/javascripts", "/assets"); + loadScript(js + "?hash=" + me.hash).then(function() { + const templateName = js.replace(".js", "").replace("/assets/", ""); return _.each(Ember.View.views, function(view) { if (view.get('templateName') === templateName) { view.set('templateName', 'empty'); @@ -63,7 +62,7 @@ export default { if (!$(this).data('orig')) { $(this).data('orig', this.href); } - var orig = $(this).data('orig'); + const orig = $(this).data('orig'); this.href = orig + (orig.indexOf('?') >= 0 ? "&hash=" : "?hash=") + me.hash; } }); diff --git a/app/assets/javascripts/discourse/lib/highlight-syntax.js.es6 b/app/assets/javascripts/discourse/lib/highlight-syntax.js.es6 index 6308a252659..e1224dee3bb 100644 --- a/app/assets/javascripts/discourse/lib/highlight-syntax.js.es6 +++ b/app/assets/javascripts/discourse/lib/highlight-syntax.js.es6 @@ -1,10 +1,10 @@ /*global hljs:true */ +import loadScript from 'discourse/lib/load-script'; + export default function highlightSyntax($elem) { const selector = Discourse.SiteSettings.autohighlight_all_code ? 'pre code' : 'pre code[class]'; $(selector, $elem).each(function(i, e) { - return Discourse.script("/javascripts/highlight.pack.js").wait(function() { - return hljs.highlightBlock(e); - }); + loadScript("/javascripts/highlight.pack.js").then(() => hljs.highlightBlock(e)); }); } diff --git a/app/assets/javascripts/discourse/lib/lightbox.js.es6 b/app/assets/javascripts/discourse/lib/lightbox.js.es6 index a705ddf07ee..e45c1f56aa1 100644 --- a/app/assets/javascripts/discourse/lib/lightbox.js.es6 +++ b/app/assets/javascripts/discourse/lib/lightbox.js.es6 @@ -1,6 +1,8 @@ +import loadScript from 'discourse/lib/load-script'; + export default function($elem) { $("a.lightbox", $elem).each(function(i, e) { - Discourse.script("/javascripts/jquery.magnific-popup-min.js").wait(function() { + loadScript("/javascripts/jquery.magnific-popup-min.js").then(function() { var $e = $(e); // do not lightbox spoiled images if ($e.parents(".spoiler").length > 0 || $e.parents(".spoiled").length > 0) { return; } diff --git a/app/assets/javascripts/discourse/lib/load-script.js.es6 b/app/assets/javascripts/discourse/lib/load-script.js.es6 new file mode 100644 index 00000000000..dc3971eeab1 --- /dev/null +++ b/app/assets/javascripts/discourse/lib/load-script.js.es6 @@ -0,0 +1,5 @@ +export default function loadScript(url) { + return new Ember.RSVP.Promise(function(resolve) { + $LAB.script(Discourse.getURL(url)).wait(() => resolve()); + }); +}