Replace `Discourse.script` global with a module

This commit is contained in:
Robin Ward 2015-03-09 12:32:54 -04:00
parent fc962eb378
commit fb726cfa0c
5 changed files with 22 additions and 20 deletions

View File

@ -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; }

View File

@ -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;
}
});

View File

@ -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));
});
}

View File

@ -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; }

View File

@ -0,0 +1,5 @@
export default function loadScript(url) {
return new Ember.RSVP.Promise(function(resolve) {
$LAB.script(Discourse.getURL(url)).wait(() => resolve());
});
}