Skip to content

Commit

Permalink
REFACTOR: Remove Discourse.__widget_helpers
Browse files Browse the repository at this point in the history
It's now a variable in the context where the templates are created.
  • Loading branch information
eviltrout committed Aug 6, 2020
1 parent 792bd3f commit 7df57b3
Show file tree
Hide file tree
Showing 9 changed files with 14 additions and 22 deletions.
1 change: 1 addition & 0 deletions app/assets/javascripts/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,4 @@
//= require_tree ./discourse/app/services

//= require_tree ./discourse/app/widgets
//= require ./widget-runtime
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,6 @@ export function convertIconClass(icon) {
.trim();
}

// TODO: Improve how helpers are registered for vdom compliation
if (typeof Discourse !== "undefined") {
Discourse.__widget_helpers.iconNode = iconNode;
}

export function registerIconRenderer(renderer) {
_renderers.unshift(renderer);
}
Expand Down
1 change: 0 additions & 1 deletion app/assets/javascripts/discourse/app/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ const _pluginCallbacks = [];

const Discourse = Application.extend({
rootElement: "#main",
__widget_helpers: {},

customEvents: {
paste: "paste"
Expand Down
5 changes: 0 additions & 5 deletions app/assets/javascripts/discourse/app/helpers/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@ export function dateNode(dt) {
}
}

// TODO: Improve how helpers are registered for vdom compliation
if (typeof Discourse !== "undefined") {
Discourse.__widget_helpers.dateNode = dateNode;
}

export function numberNode(num, opts) {
opts = opts || {};
num = parseInt(num, 10);
Expand Down
5 changes: 0 additions & 5 deletions app/assets/javascripts/discourse/app/widgets/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,6 @@ export function avatarFor(wanted, attrs) {
);
}

// TODO: Improve how helpers are registered for vdom compliation
if (typeof Discourse !== "undefined") {
Discourse.__widget_helpers.avatar = avatarFor;
}

createWidget("select-post", {
tagName: "div.select-posts",

Expand Down
5 changes: 0 additions & 5 deletions app/assets/javascripts/discourse/app/widgets/raw-html.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,3 @@ export default class RawHtml {
}

RawHtml.prototype.type = "Widget";

// TODO: Improve how helpers are registered for vdom compliation
if (typeof Discourse !== "undefined") {
Discourse.__widget_helpers.rawHtml = RawHtml;
}
11 changes: 11 additions & 0 deletions app/assets/javascripts/widget-runtime.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// discourse-skip-module

(function(context) {
// register widget helpers for compiled `hbs`
context.__widget_helpers = {
avatar: require("discourse/widgets/post").avatarFor,
dateNode: require("discourse/helpers/node").dateNode,
iconNode: require("discourse-common/lib/icon-library").iconNode,
rawHtml: require("discourse/widgets/raw-html").default
};
})(this);
2 changes: 1 addition & 1 deletion lib/javascripts/widget-hbs-compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ function compile(template) {

Object.keys(compiler.state.helpersUsed).forEach(h => {
let id = compiler.state.helpersUsed[h];
imports += `var __h${id} = Discourse.__widget_helpers.${h}; `;
imports += `var __h${id} = __widget_helpers.${h}; `;
});

return `function(attrs, state) { ${imports}var _r = [];\n${code}\nreturn _r; }`;
Expand Down
1 change: 1 addition & 0 deletions script/test_hbs_compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
{{actions-summary-item attrs=as}}
{{attach widget="actions-summary-item" attrs=as}}
{{testing value="hello"}}
{{date "today"}}
HBS

ctx = MiniRacer::Context.new(timeout: 15000)
Expand Down

0 comments on commit 7df57b3

Please sign in to comment.