2016-05-20 12:13:10 -04:00
|
|
|
import DiscourseURL from 'discourse/lib/url';
|
2017-04-28 14:01:57 -04:00
|
|
|
import { currentThemeKey, refreshCSS } from 'discourse/lib/theme-selector';
|
2017-04-12 10:52:52 -04:00
|
|
|
|
2015-03-09 12:32:54 -04:00
|
|
|
// Use the message bus for live reloading of components for faster development.
|
2014-05-16 14:04:34 -04:00
|
|
|
export default {
|
|
|
|
|
name: "live-development",
|
2015-03-12 12:12:23 -04:00
|
|
|
initialize(container) {
|
|
|
|
|
const messageBus = container.lookup('message-bus:main');
|
2014-05-16 14:04:34 -04:00
|
|
|
|
2018-03-15 16:17:09 +11:00
|
|
|
if (window.history && window.location.search.indexOf("?preview_theme_key=") === 0) {
|
|
|
|
|
// force preview theme key to always be carried along
|
|
|
|
|
const themeKey = window.location.search.slice(19).split('&')[0];
|
|
|
|
|
if (themeKey.match(/^[a-z0-9-]+$/i)) {
|
|
|
|
|
const patchState = function(f) {
|
|
|
|
|
const patched = window.history[f];
|
|
|
|
|
|
|
|
|
|
window.history[f] = function(stateObj, name, url) {
|
|
|
|
|
if (url.indexOf("preview_theme_key=") === -1) {
|
|
|
|
|
const joiner = url.indexOf("?") === -1 ? "?" : "&";
|
|
|
|
|
url = `${url}${joiner}preview_theme_key=${themeKey}`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return patched.call(window.history, stateObj, name, url);
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
patchState("replaceState");
|
|
|
|
|
patchState("pushState");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-16 14:04:34 -04:00
|
|
|
// Custom header changes
|
|
|
|
|
$('header.custom').each(function() {
|
2015-03-09 12:32:54 -04:00
|
|
|
const header = $(this);
|
2015-03-12 12:12:23 -04:00
|
|
|
return messageBus.subscribe("/header-change/" + $(this).data('key'), function(data) {
|
2014-05-16 14:04:34 -04:00
|
|
|
return header.html(data);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2016-05-20 12:13:10 -04:00
|
|
|
// Useful to export this for debugging purposes
|
|
|
|
|
if (Discourse.Environment === 'development' && !Ember.testing) {
|
|
|
|
|
window.DiscourseURL = DiscourseURL;
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-16 14:04:34 -04:00
|
|
|
// Observe file changes
|
2015-03-12 12:12:23 -04:00
|
|
|
messageBus.subscribe("/file-change", function(data) {
|
2017-08-16 10:59:38 -04:00
|
|
|
|
2015-11-27 11:58:46 +11:00
|
|
|
if (Handlebars.compile && !Ember.TEMPLATES.empty) {
|
|
|
|
|
// hbs notifications only happen in dev
|
|
|
|
|
Ember.TEMPLATES.empty = Handlebars.compile("<div></div>");
|
|
|
|
|
}
|
2014-05-16 14:04:34 -04:00
|
|
|
_.each(data,function(me) {
|
|
|
|
|
|
|
|
|
|
if (me === "refresh") {
|
|
|
|
|
// Refresh if necessary
|
|
|
|
|
document.location.reload(true);
|
|
|
|
|
} else {
|
2017-04-18 16:48:15 -04:00
|
|
|
let themeKey = currentThemeKey();
|
|
|
|
|
|
2014-05-16 14:04:34 -04:00
|
|
|
$('link').each(function() {
|
2017-04-18 16:48:15 -04:00
|
|
|
if (me.hasOwnProperty('theme_key') && me.new_href) {
|
|
|
|
|
let target = $(this).data('target');
|
|
|
|
|
if (me.theme_key === themeKey && target === me.target) {
|
|
|
|
|
refreshCSS(this, null, me.new_href);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (this.href.match(me.name) && (me.hash || me.new_href)) {
|
2017-04-12 10:52:52 -04:00
|
|
|
refreshCSS(this, me.hash, me.new_href);
|
2014-05-16 14:04:34 -04:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|