Convert html.js to ES6 module format

This commit is contained in:
Robin Ward 2015-01-20 16:07:13 -05:00
parent 2a6657bf53
commit 60523d8e02
4 changed files with 40 additions and 35 deletions

View File

@ -1,5 +1,7 @@
import { getCustomHTML } from 'discourse/lib/html';
Handlebars.registerHelper('custom-html', function(name, contextString, options) {
var html = Discourse.HTML.getCustomHTML(name);
var html = getCustomHTML(name);
if (html) { return html; }
var container = (options || contextString).data.view.container;

View File

@ -1,28 +0,0 @@
var customizations = {};
Discourse.HTML = {
/**
Return a custom fragment of HTML by key. It can be registered via a plugin
using `setCustomHTML(key, html)`. This is used by a handlebars helper to find
the HTML content it wants. It will also check the `PreloadStore` for any server
side preloaded HTML.
**/
getCustomHTML: function(key) {
var c = customizations[key];
if (c) {
return new Handlebars.SafeString(c);
}
var html = PreloadStore.get("customHTML");
if (html && html[key] && html[key].length) {
return new Handlebars.SafeString(html[key]);
}
},
// Set a fragment of HTML by key. It can then be looked up with `getCustomHTML(key)`.
setCustomHTML: function(key, html) {
customizations[key] = html;
}
};

View File

@ -0,0 +1,32 @@
var _customizations = {};
/**
Return a custom fragment of HTML by key. It can be registered via a plugin
using `setCustomHTML(key, html)`. This is used by a handlebars helper to find
the HTML content it wants. It will also check the `PreloadStore` for any server
side preloaded HTML.
**/
export function getCustomHTML(key) {
var c = _customizations[key];
if (c) {
return new Handlebars.SafeString(c);
}
var html = PreloadStore.get("customHTML");
if (html && html[key] && html[key].length) {
return new Handlebars.SafeString(html[key]);
}
}
// Set a fragment of HTML by key. It can then be looked up with `getCustomHTML(key)`.
export function setCustomHTML(key, html) {
_customizations[key] = html;
}
var HTML = {
getCustomHTML: getCustomHTML,
setCustomHTML: setCustomHTML
};
Discourse.HTML = HTML;
export default HTML;

View File

@ -1,14 +1,13 @@
module("Discourse.HTML");
var html = Discourse.HTML;
import { getCustomHTML, setCustomHTML } from 'discourse/lib/html';
test("customHTML", function() {
blank(html.getCustomHTML('evil'), "there is no custom HTML for a key by default");
blank(getCustomHTML('evil'), "there is no custom HTML for a key by default");
html.setCustomHTML('evil', 'trout');
equal(html.getCustomHTML('evil'), 'trout', 'it retrieves the custom html');
setCustomHTML('evil', 'trout');
equal(getCustomHTML('evil'), 'trout', 'it retrieves the custom html');
PreloadStore.store('customHTML', {cookie: 'monster'});
equal(html.getCustomHTML('cookie'), 'monster', 'it returns HTML fragments from the PreloadStore');
equal(getCustomHTML('cookie'), 'monster', 'it returns HTML fragments from the PreloadStore');
});