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

109 lines
2.7 KiB
Plaintext
Raw Normal View History

2020-09-04 07:23:11 -04:00
import Handlebars from "handlebars";
2018-10-10 07:56:23 -04:00
import { categoryLinkHTML } from "discourse/helpers/category-link";
import { autoUpdatingRelativeAge } from "discourse/lib/formatter";
2021-01-27 04:38:56 -05:00
import { convertIconClass, iconHTML } from "discourse-common/lib/icon-library";
2015-08-26 00:36:39 -04:00
function icon_or_image_replacement(str, ctx) {
str = Ember.get(ctx.contexts[0], str);
2018-10-10 07:56:23 -04:00
if (Ember.isEmpty(str)) {
return "";
}
2015-08-26 00:36:39 -04:00
2018-11-15 11:12:32 -05:00
if (str.indexOf("fa-") > -1) {
const icon = iconHTML(convertIconClass(str));
return new Handlebars.SafeString(icon);
2015-08-26 00:36:39 -04:00
} else {
return new Handlebars.SafeString("<img src='" + str + "'>");
}
}
function category_badge_replacement(str, ctx) {
const category = Ember.get(ctx.contexts[0], str);
return categoryLinkHTML(category, {
2020-09-04 07:23:11 -04:00
allowUncategorized: true,
});
}
function bound_date_replacement(str, ctx) {
const value = Ember.get(ctx.contexts[0], str);
2018-10-10 07:56:23 -04:00
return new Handlebars.SafeString(
autoUpdatingRelativeAge(new Date(value), { title: true })
);
2015-08-26 00:36:39 -04:00
}
const esc = Handlebars.Utils.escapeExpression;
2015-06-30 18:12:12 -04:00
// consider moving this elsewhere
function guessUrl(t) {
let [dest, name] = [t, t];
const split = t.split(/,(.+)/);
if (split.length > 1) {
name = split[0];
dest = split[1];
}
return [dest, name];
}
const QueryRowContentComponent = Ember.Component.extend({
tagName: "tr",
rowContents: null,
didReceiveAttrs() {
const row = this.row;
const parentView = this.parentView;
const fallback = this.fallbackTemplate;
const helpers = {
"icon-or-image": icon_or_image_replacement,
"category-link": category_badge_replacement,
2020-09-04 07:23:11 -04:00
reltime: bound_date_replacement,
};
const parts = this.columnTemplates.map((t, idx) => {
const value = row[idx],
id = parseInt(value, 10);
const ctx = {
value,
id,
2020-09-04 07:23:11 -04:00
baseuri: Discourse.BaseUri === "/" ? "" : Discourse.BaseUri,
2018-10-10 07:56:23 -04:00
};
const params = {};
if (row[idx] === null) {
return "NULL";
} else if (t.name === "text") {
return esc(row[idx]);
}
const lookupFunc = parentView[`lookup${t.name.capitalize()}`];
if (lookupFunc) {
ctx[t.name] = parentView[`lookup${t.name.capitalize()}`](id);
}
if (t.name === "url") {
let [url, name] = guessUrl(value);
ctx["href"] = url;
ctx["target"] = name;
}
2020-01-12 18:43:30 -05:00
if (t.name === "category" || t.name === "badge" || t.name === "reltime") {
// only replace helpers if needed
params.helpers = helpers;
}
try {
2020-01-12 18:43:30 -05:00
return new Handlebars.SafeString((t.template || fallback)(ctx, params));
} catch (e) {
return "error";
}
});
this.set("rowContents", `<td>${parts.join("</td><td>")}</td>`.htmlSafe());
2020-09-04 07:23:11 -04:00
},
});
2015-06-30 18:12:12 -04:00
export default QueryRowContentComponent;