From 29cfa6383cc2b35b037e4971ad0ca892a69fd67f Mon Sep 17 00:00:00 2001 From: Isaac Janzen <50783505+janzenisaac@users.noreply.github.com> Date: Tue, 7 Feb 2023 12:26:47 -0600 Subject: [PATCH] DEV: Convert raw-hbs to view only components (#222) - Convert raw.hbs files to co-located view only components --- .../discourse/components/query-result.hbs | 2 +- .../discourse/components/query-result.js | 34 +++++++++++++++---- .../components/query-row-content.hbs | 8 ++++- .../discourse/components/query-row-content.js | 27 +++++++++------ .../components/result-types/badge.hbs | 9 +++++ .../components/result-types/category.hbs | 5 +++ .../components/result-types/group.hbs | 5 +++ .../components/result-types/html.hbs | 1 + .../result-types/post.hbs} | 16 ++++----- .../components/result-types/reltime.hbs | 1 + .../components/result-types/text.hbs | 1 + .../components/result-types/topic.hbs | 8 +++++ .../discourse/components/result-types/url.hbs | 1 + .../components/result-types/user.hbs | 11 ++++++ .../templates/explorer/badge.raw.hbs | 10 ------ .../templates/explorer/category.raw.hbs | 5 --- .../templates/explorer/group.raw.hbs | 5 --- .../discourse/templates/explorer/html.raw.hbs | 1 - .../templates/explorer/reltime.raw.hbs | 1 - .../discourse/templates/explorer/text.raw.hbs | 1 - .../templates/explorer/topic.raw.hbs | 8 ----- .../discourse/templates/explorer/url.raw.hbs | 1 - .../discourse/templates/explorer/user.raw.hbs | 11 ------ 23 files changed, 102 insertions(+), 70 deletions(-) create mode 100644 assets/javascripts/discourse/components/result-types/badge.hbs create mode 100644 assets/javascripts/discourse/components/result-types/category.hbs create mode 100644 assets/javascripts/discourse/components/result-types/group.hbs create mode 100644 assets/javascripts/discourse/components/result-types/html.hbs rename assets/javascripts/discourse/{templates/explorer/post.raw.hbs => components/result-types/post.hbs} (53%) create mode 100644 assets/javascripts/discourse/components/result-types/reltime.hbs create mode 100644 assets/javascripts/discourse/components/result-types/text.hbs create mode 100644 assets/javascripts/discourse/components/result-types/topic.hbs create mode 100644 assets/javascripts/discourse/components/result-types/url.hbs create mode 100644 assets/javascripts/discourse/components/result-types/user.hbs delete mode 100644 assets/javascripts/discourse/templates/explorer/badge.raw.hbs delete mode 100644 assets/javascripts/discourse/templates/explorer/category.raw.hbs delete mode 100644 assets/javascripts/discourse/templates/explorer/group.raw.hbs delete mode 100644 assets/javascripts/discourse/templates/explorer/html.raw.hbs delete mode 100644 assets/javascripts/discourse/templates/explorer/reltime.raw.hbs delete mode 100644 assets/javascripts/discourse/templates/explorer/text.raw.hbs delete mode 100644 assets/javascripts/discourse/templates/explorer/topic.raw.hbs delete mode 100644 assets/javascripts/discourse/templates/explorer/url.raw.hbs delete mode 100644 assets/javascripts/discourse/templates/explorer/user.raw.hbs diff --git a/assets/javascripts/discourse/components/query-result.hbs b/assets/javascripts/discourse/components/query-result.hbs index 80190d5..01aaec1 100644 --- a/assets/javascripts/discourse/components/query-result.hbs +++ b/assets/javascripts/discourse/components/query-result.hbs @@ -68,7 +68,7 @@ {{#each this.rows as |row|}} { - let viewName = "text"; + let type = "text"; if (this.colRender[idx]) { - viewName = this.colRender[idx]; + type = this.colRender[idx]; } - - const template = findRawTemplate(`javascripts/explorer/${viewName}`); - return { name: viewName, template }; + return { name: type, component: VIEW_COMPONENTS[type] }; }); } diff --git a/assets/javascripts/discourse/components/query-row-content.hbs b/assets/javascripts/discourse/components/query-row-content.hbs index 8c8428d..55952d1 100644 --- a/assets/javascripts/discourse/components/query-row-content.hbs +++ b/assets/javascripts/discourse/components/query-row-content.hbs @@ -1,5 +1,11 @@ {{#each this.results as |result|}} - {{result}} + + + {{/each}} \ No newline at end of file diff --git a/assets/javascripts/discourse/components/query-row-content.js b/assets/javascripts/discourse/components/query-row-content.js index 41f395b..2b66c46 100644 --- a/assets/javascripts/discourse/components/query-row-content.js +++ b/assets/javascripts/discourse/components/query-row-content.js @@ -9,7 +9,7 @@ import { get } from "@ember/object"; import { isEmpty } from "@ember/utils"; import { escapeExpression } from "discourse/lib/utilities"; import { cached } from "@glimmer/tracking"; -import { findRawTemplate } from "discourse-common/lib/raw-templates"; +import TextViewComponent from "./result-types/text"; export default class QueryRowContent extends Component { constructor() { @@ -24,7 +24,8 @@ export default class QueryRowContent extends Component { @cached get results() { - return this.args.columnTemplates.map((t, idx) => { + return this.args.columnComponents.map((t, idx) => { + const params = {}; const value = this.args.row[idx], id = parseInt(value, 10); @@ -33,13 +34,19 @@ export default class QueryRowContent extends Component { id, baseuri: getURL(""), }; - const params = {}; if (this.args.row[idx] === null) { - return "NULL"; + return { + component: TextViewComponent, + textValue: "NULL", + }; } else if (t.name === "text") { - return escapeExpression(this.args.row[idx]); + return { + component: TextViewComponent, + textValue: escapeExpression(this.args.row[idx]), + }; } + const lookupFunc = this.args[`lookup${capitalize(t.name)}`]; if (lookupFunc) { ctx[t.name] = lookupFunc.call(this.args, id); @@ -57,16 +64,16 @@ export default class QueryRowContent extends Component { } try { - return htmlSafe((t.template || this.fallbackTemplate)(ctx, params)); + return { + component: t.component || TextViewComponent, + ctx, + params, + }; } catch (e) { return "error"; } }); } - - fallbackTemplate() { - return findRawTemplate("javascripts/explorer/text"); - } } function icon_or_image_replacement(str, ctx) { diff --git a/assets/javascripts/discourse/components/result-types/badge.hbs b/assets/javascripts/discourse/components/result-types/badge.hbs new file mode 100644 index 0000000..dbbfdab --- /dev/null +++ b/assets/javascripts/discourse/components/result-types/badge.hbs @@ -0,0 +1,9 @@ + + {{icon-or-image @ctx.badge.icon}} + {{@ctx.badge.display_name}} + \ No newline at end of file diff --git a/assets/javascripts/discourse/components/result-types/category.hbs b/assets/javascripts/discourse/components/result-types/category.hbs new file mode 100644 index 0000000..ddbea9d --- /dev/null +++ b/assets/javascripts/discourse/components/result-types/category.hbs @@ -0,0 +1,5 @@ +{{#if @ctx.category}} + {{category-link @ctx.category}} +{{else}} + {{@ctx.id}} +{{/if}} \ No newline at end of file diff --git a/assets/javascripts/discourse/components/result-types/group.hbs b/assets/javascripts/discourse/components/result-types/group.hbs new file mode 100644 index 0000000..7b8765e --- /dev/null +++ b/assets/javascripts/discourse/components/result-types/group.hbs @@ -0,0 +1,5 @@ +{{#if @ctx.group}} + {{@ctx.group.name}} +{{else}} + {{@ctx.id}} +{{/if}} \ No newline at end of file diff --git a/assets/javascripts/discourse/components/result-types/html.hbs b/assets/javascripts/discourse/components/result-types/html.hbs new file mode 100644 index 0000000..ceee2a9 --- /dev/null +++ b/assets/javascripts/discourse/components/result-types/html.hbs @@ -0,0 +1 @@ +{{html-safe @textValue}} \ No newline at end of file diff --git a/assets/javascripts/discourse/templates/explorer/post.raw.hbs b/assets/javascripts/discourse/components/result-types/post.hbs similarity index 53% rename from assets/javascripts/discourse/templates/explorer/post.raw.hbs rename to assets/javascripts/discourse/components/result-types/post.hbs index 2709b00..23dcb02 100644 --- a/assets/javascripts/discourse/templates/explorer/post.raw.hbs +++ b/assets/javascripts/discourse/components/result-types/post.hbs @@ -1,14 +1,14 @@ -{{#if post}} +{{#if @ctx.post}} {{else}} - {{id}} + {{@ctx.id}} {{/if}} \ No newline at end of file diff --git a/assets/javascripts/discourse/components/result-types/reltime.hbs b/assets/javascripts/discourse/components/result-types/reltime.hbs new file mode 100644 index 0000000..5bdc919 --- /dev/null +++ b/assets/javascripts/discourse/components/result-types/reltime.hbs @@ -0,0 +1 @@ +{{reltime @textValue}} \ No newline at end of file diff --git a/assets/javascripts/discourse/components/result-types/text.hbs b/assets/javascripts/discourse/components/result-types/text.hbs new file mode 100644 index 0000000..8f466ee --- /dev/null +++ b/assets/javascripts/discourse/components/result-types/text.hbs @@ -0,0 +1 @@ +{{@textValue}} \ No newline at end of file diff --git a/assets/javascripts/discourse/components/result-types/topic.hbs b/assets/javascripts/discourse/components/result-types/topic.hbs new file mode 100644 index 0000000..c38df97 --- /dev/null +++ b/assets/javascripts/discourse/components/result-types/topic.hbs @@ -0,0 +1,8 @@ +{{#if @ctx.topic}} + + {{html-safe @ctx.topic.fancy_title}} + + ({{@ctx.topic.posts_count}}) +{{else}} + {{@ctx.id}} +{{/if}} \ No newline at end of file diff --git a/assets/javascripts/discourse/components/result-types/url.hbs b/assets/javascripts/discourse/components/result-types/url.hbs new file mode 100644 index 0000000..3d0921e --- /dev/null +++ b/assets/javascripts/discourse/components/result-types/url.hbs @@ -0,0 +1 @@ +{{@ctx.target}} \ No newline at end of file diff --git a/assets/javascripts/discourse/components/result-types/user.hbs b/assets/javascripts/discourse/components/result-types/user.hbs new file mode 100644 index 0000000..afa939b --- /dev/null +++ b/assets/javascripts/discourse/components/result-types/user.hbs @@ -0,0 +1,11 @@ +{{#if @ctx.user}} + + {{avatar @ctx.user imageSize="tiny"}} + {{@ctx.user.username}} + +{{else}} + {{@ctx.id}} +{{/if}} \ No newline at end of file diff --git a/assets/javascripts/discourse/templates/explorer/badge.raw.hbs b/assets/javascripts/discourse/templates/explorer/badge.raw.hbs deleted file mode 100644 index dc4f851..0000000 --- a/assets/javascripts/discourse/templates/explorer/badge.raw.hbs +++ /dev/null @@ -1,10 +0,0 @@ -{{! source: badge-button component }} - - {{icon-or-image badge.icon}} - {{badge.display_name}} - \ No newline at end of file diff --git a/assets/javascripts/discourse/templates/explorer/category.raw.hbs b/assets/javascripts/discourse/templates/explorer/category.raw.hbs deleted file mode 100644 index d5a4fd6..0000000 --- a/assets/javascripts/discourse/templates/explorer/category.raw.hbs +++ /dev/null @@ -1,5 +0,0 @@ -{{#if category}} - {{category-link category}} -{{else}} - {{id}} -{{/if}} \ No newline at end of file diff --git a/assets/javascripts/discourse/templates/explorer/group.raw.hbs b/assets/javascripts/discourse/templates/explorer/group.raw.hbs deleted file mode 100644 index a0205d5..0000000 --- a/assets/javascripts/discourse/templates/explorer/group.raw.hbs +++ /dev/null @@ -1,5 +0,0 @@ -{{#if group}} - {{group.name}} -{{else}} - {{id}} -{{/if}} \ No newline at end of file diff --git a/assets/javascripts/discourse/templates/explorer/html.raw.hbs b/assets/javascripts/discourse/templates/explorer/html.raw.hbs deleted file mode 100644 index ccdc900..0000000 --- a/assets/javascripts/discourse/templates/explorer/html.raw.hbs +++ /dev/null @@ -1 +0,0 @@ -{{html-safe value}} \ No newline at end of file diff --git a/assets/javascripts/discourse/templates/explorer/reltime.raw.hbs b/assets/javascripts/discourse/templates/explorer/reltime.raw.hbs deleted file mode 100644 index 86a77af..0000000 --- a/assets/javascripts/discourse/templates/explorer/reltime.raw.hbs +++ /dev/null @@ -1 +0,0 @@ -{{reltime value}} \ No newline at end of file diff --git a/assets/javascripts/discourse/templates/explorer/text.raw.hbs b/assets/javascripts/discourse/templates/explorer/text.raw.hbs deleted file mode 100644 index b26a235..0000000 --- a/assets/javascripts/discourse/templates/explorer/text.raw.hbs +++ /dev/null @@ -1 +0,0 @@ -{{value}} \ No newline at end of file diff --git a/assets/javascripts/discourse/templates/explorer/topic.raw.hbs b/assets/javascripts/discourse/templates/explorer/topic.raw.hbs deleted file mode 100644 index 6611f70..0000000 --- a/assets/javascripts/discourse/templates/explorer/topic.raw.hbs +++ /dev/null @@ -1,8 +0,0 @@ -{{#if topic}} - - {{html-safe topic.fancy_title}} - - ({{topic.posts_count}}) -{{else}} - {{id}} -{{/if}} \ No newline at end of file diff --git a/assets/javascripts/discourse/templates/explorer/url.raw.hbs b/assets/javascripts/discourse/templates/explorer/url.raw.hbs deleted file mode 100644 index 4c7ab25..0000000 --- a/assets/javascripts/discourse/templates/explorer/url.raw.hbs +++ /dev/null @@ -1 +0,0 @@ -{{target}} \ No newline at end of file diff --git a/assets/javascripts/discourse/templates/explorer/user.raw.hbs b/assets/javascripts/discourse/templates/explorer/user.raw.hbs deleted file mode 100644 index 249f0c3..0000000 --- a/assets/javascripts/discourse/templates/explorer/user.raw.hbs +++ /dev/null @@ -1,11 +0,0 @@ -{{#if user}} - - {{avatar user imageSize="tiny"}} - {{user.username}} - -{{else}} - {{id}} -{{/if}} \ No newline at end of file