Files
discourse-data-explorer/assets/javascripts/discourse/components/query-row-content.js.es6
T

80 lines
2.3 KiB
JavaScript
Raw Normal View History

2015-09-21 14:43:23 -07:00
import { categoryLinkHTML } from 'discourse/helpers/category-link';
import { autoUpdatingRelativeAge } from 'discourse/lib/formatter';
2016-11-09 11:58:18 -05:00
import { bufferedRender } from 'discourse-common/lib/buffered-render';
2015-09-21 14:43:23 -07:00
2015-08-25 21:36:39 -07:00
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("<i class='fa " + str + "'></i>");
} else {
return new Handlebars.SafeString("<img src='" + str + "'>");
}
}
2015-09-21 14:43:23 -07:00
function category_badge_replacement(str, ctx) {
const category = Ember.get(ctx.contexts[0], str);
return categoryLinkHTML(category, {
allowUncategorized: true,
});
}
function bound_date_replacement(str, ctx) {
const value = Ember.get(ctx.contexts[0], str);
return new Handlebars.SafeString(autoUpdatingRelativeAge(new Date(value), {title: true }));
2015-08-25 21:36:39 -07:00
}
const esc = Handlebars.Utils.escapeExpression;
2015-06-30 15:12:12 -07:00
2016-11-09 11:58:18 -05:00
const QueryRowContentComponent = Ember.Component.extend(bufferedRender({
2015-06-30 15:12:12 -07:00
tagName: "tr",
2016-11-09 11:58:18 -05:00
buildBuffer(buffer) {
2015-08-25 20:48:19 -07:00
const self = this;
2015-06-30 15:12:12 -07:00
const row = this.get('row');
const parentView = self.get('parentView');
const fallback = this.get('fallbackTemplate');
2015-09-21 14:43:23 -07:00
const helpers = {
"icon-or-image": icon_or_image_replacement,
"category-link": category_badge_replacement,
"reltime": bound_date_replacement,
};
2015-08-25 20:48:19 -07:00
const parts = this.get('columnTemplates').map(function(t, idx) {
2015-09-21 14:43:23 -07:00
const value = row[idx],
id = parseInt(value);
const ctx = {value, id, baseuri: Discourse.BaseUri === '/' ? '' : Discourse.BaseUri };
const params = {};
if (row[idx] === null) {
return "NULL";
} else if (t.name === "text") {
2015-08-25 21:36:39 -07:00
return esc(row[idx]);
2015-08-25 20:48:19 -07:00
}
const lookupFunc = parentView[`lookup${t.name.capitalize()}`];
2015-09-21 14:43:23 -07:00
if (lookupFunc) {
ctx[t.name] = parentView[`lookup${t.name.capitalize()}`](id);
2015-09-21 14:43:23 -07:00
}
if (t.name === "category" || t.name === "badge" || t.name === "reltime") {
// only replace helpers if needed
params.helpers = helpers;
}
try {
return new Handlebars.SafeString((t.template || fallback)(ctx, params));
} catch (e) {
console.error(e);
return "error";
}
2015-06-30 15:12:12 -07:00
});
2015-08-25 20:48:19 -07:00
buffer.push("<td>" + parts.join("</td><td>") + "</td>");
2015-06-30 15:12:12 -07:00
}
2016-11-09 11:58:18 -05:00
}));
2015-06-30 15:12:12 -07:00
export default QueryRowContentComponent;