From 60523d8e02de7b56113d4ef5090fe253ecadccd8 Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Tue, 20 Jan 2015 16:07:13 -0500 Subject: [PATCH] Convert html.js to ES6 module format --- .../discourse/helpers/custom-html.js.es6 | 4 ++- app/assets/javascripts/discourse/lib/html.js | 28 ---------------- .../javascripts/discourse/lib/html.js.es6 | 32 +++++++++++++++++++ test/javascripts/lib/html-test.js.es6 | 11 +++---- 4 files changed, 40 insertions(+), 35 deletions(-) delete mode 100644 app/assets/javascripts/discourse/lib/html.js create mode 100644 app/assets/javascripts/discourse/lib/html.js.es6 diff --git a/app/assets/javascripts/discourse/helpers/custom-html.js.es6 b/app/assets/javascripts/discourse/helpers/custom-html.js.es6 index 42155096deb..1728963bf3e 100644 --- a/app/assets/javascripts/discourse/helpers/custom-html.js.es6 +++ b/app/assets/javascripts/discourse/helpers/custom-html.js.es6 @@ -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; diff --git a/app/assets/javascripts/discourse/lib/html.js b/app/assets/javascripts/discourse/lib/html.js deleted file mode 100644 index ccb807d6a8b..00000000000 --- a/app/assets/javascripts/discourse/lib/html.js +++ /dev/null @@ -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; - } - -}; diff --git a/app/assets/javascripts/discourse/lib/html.js.es6 b/app/assets/javascripts/discourse/lib/html.js.es6 new file mode 100644 index 00000000000..f839b5ea52b --- /dev/null +++ b/app/assets/javascripts/discourse/lib/html.js.es6 @@ -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; diff --git a/test/javascripts/lib/html-test.js.es6 b/test/javascripts/lib/html-test.js.es6 index 07ef2906a17..835b3f1e650 100644 --- a/test/javascripts/lib/html-test.js.es6 +++ b/test/javascripts/lib/html-test.js.es6 @@ -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'); });