diff --git a/assets/javascripts/discourse/components/query-result.js.es6 b/assets/javascripts/discourse/components/query-result.js.es6 index 537f300..bf0658d 100644 --- a/assets/javascripts/discourse/components/query-result.js.es6 +++ b/assets/javascripts/discourse/components/query-result.js.es6 @@ -4,6 +4,7 @@ const Escape = Handlebars.Utils.escapeExpression; import avatarTemplate from 'discourse/lib/avatar-template'; import { categoryLinkHTML } from 'discourse/helpers/category-link'; +import Badge from 'discourse/models/badge'; var defaultFallback = function(buffer, content, defaultRender) { defaultRender(buffer, content); }; @@ -16,10 +17,14 @@ function randomIdShort() { }); } -function transformedRelTable(table) { +function transformedRelTable(table, modelClass) { const result = {}; table.forEach(function(item) { - result[item.id] = item; + if (modelClass) { + result[item.id] = modelClass.create(item); + } else { + result[item.id] = item; + } }); return result; } @@ -87,10 +92,32 @@ const QueryResultComponent = Ember.Component.extend({ transformedUserTable: function() { return transformedRelTable(this.get('content.relations.user')); }.property('content.relations.user'), + transformedBadgeTable: function() { + return transformedRelTable(this.get('content.relations.badge'), Badge); + }.property('content.relations.badge'), + transformedPostTable: function() { + return transformedRelTable(this.get('content.relations.post')); + }.property('content.relations.post'), + transformedTopicTable: function() { + return transformedRelTable(this.get('content.relations.topic')); + }.property('content.relations.topic'), - lookupUser: function(id) { + lookupUser(id) { return this.get('transformedUserTable')[id]; }, + lookupBadge(id) { + return this.get('transformedBadgeTable')[id]; + }, + lookupPost(id) { + return this.get('transformedPostTable')[id]; + }, + lookupTopic(id) { + return this.get('transformedTopicTable')[id]; + }, + + lookupCategory(id) { + return this.site.get('categoriesById')[id]; + }, downloadResult(format) { // Create a frame to submit the form in (?) diff --git a/assets/javascripts/discourse/components/query-row-content.js.es6 b/assets/javascripts/discourse/components/query-row-content.js.es6 index 7a96585..a2dca18 100644 --- a/assets/javascripts/discourse/components/query-row-content.js.es6 +++ b/assets/javascripts/discourse/components/query-row-content.js.es6 @@ -1,5 +1,29 @@ import binarySearch from 'discourse/plugins/discourse-data-explorer/discourse/lib/binary-search'; +import avatarTemplate from 'discourse/lib/avatar-template'; +function icon_or_image_replacement(str, ctx) { + str = Ember.get(ctx.contexts[0], str); + if (Ember.isEmpty(str)) { return ""; } + + if (str.indexOf('fa-') === 0) { + return new Handlebars.SafeString(""); + } else { + return new Handlebars.SafeString(""); + } +} + +function shorthandTinyAvatar(username, uploadId, ctx) { + username = Ember.get(ctx.contexts[0], username); + uploadId = Ember.get(ctx.contexts[0], uploadId); + return new Handlebars.SafeString(Discourse.Utilities.avatarImg({ + size: "tiny", + extraClasses: '', + title: username, + avatarTemplate: avatarTemplate(username, uploadId) + })); +} + +const esc = Handlebars.Utils.escapeExpression; const QueryRowContentComponent = Ember.Component.extend({ tagName: "tr", @@ -12,18 +36,29 @@ const QueryRowContentComponent = Ember.Component.extend({ const self = this; const row = this.get('row'); const relations = this.get('extra.relations'); + const parent = self.get('parent'); const parts = this.get('columnTemplates').map(function(t, idx) { - const params = {}; + const ctx = {}; + const params = {} if (t.name === "text") { - return row[idx]; + return esc(row[idx]); } else if (t.name === "user") { - params.user = self.get('parent').lookupUser(parseInt(row[idx])); + ctx.user = parent.lookupUser(parseInt(row[idx])); + if (!ctx.user) { + return esc(row[idx]); + } + } else if (t.name === "badge") { + ctx.badge = parent.lookupBadge(parseInt(row[idx])); + params.helpers = {"icon-or-image": icon_or_image_replacement}; + } else if (t.name === "post") { + ctx.post = parent.lookupPost(parseInt(row[idx])); + params.helpers = {avatar: shorthandTinyAvatar}; } else { - params.value = row[idx]; + ctx.value = row[idx]; } - return new Handlebars.SafeString(t.template(params)); + return new Handlebars.SafeString(t.template(ctx, params)); }); buffer.push("" + parts.join("") + ""); diff --git a/assets/javascripts/discourse/templates/explorer/badge.raw.hbs b/assets/javascripts/discourse/templates/explorer/badge.raw.hbs new file mode 100644 index 0000000..e0d5243 --- /dev/null +++ b/assets/javascripts/discourse/templates/explorer/badge.raw.hbs @@ -0,0 +1,2 @@ +{{! source: badge-button component }} +{{icon-or-image badge.icon}} {{badge.displayName}} diff --git a/assets/javascripts/discourse/templates/explorer/post.raw.hbs b/assets/javascripts/discourse/templates/explorer/post.raw.hbs new file mode 100644 index 0000000..6698277 --- /dev/null +++ b/assets/javascripts/discourse/templates/explorer/post.raw.hbs @@ -0,0 +1,4 @@ + diff --git a/assets/javascripts/discourse/templates/explorer/user.raw.hbs b/assets/javascripts/discourse/templates/explorer/user.raw.hbs index f9a69bf..1b24eea 100644 --- a/assets/javascripts/discourse/templates/explorer/user.raw.hbs +++ b/assets/javascripts/discourse/templates/explorer/user.raw.hbs @@ -1,5 +1 @@ -{{#if user}} - {{avatar user imageSize="tiny"}} {{user.username}} -{{else}} - User #{{value}} (deleted) -{{/if}} +{{avatar user imageSize="tiny"}} {{user.username}} diff --git a/plugin.rb b/plugin.rb index 934f543..4f273ea 100644 --- a/plugin.rb +++ b/plugin.rb @@ -41,9 +41,6 @@ after_initialize do class SmallBadgeSerializer < ApplicationSerializer attributes :id, :name, :badge_type, :description, :icon - def badge_type - object.badge_type.name - end end class SmallPostWithExcerptSerializer < ApplicationSerializer