2022-06-17 09:01:34 -04:00
|
|
|
import Component from "@ember/component";
|
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";
|
2022-01-26 09:13:05 -05:00
|
|
|
import getURL from "discourse-common/lib/get-url";
|
2022-05-13 13:33:38 -04:00
|
|
|
import { capitalize } from "@ember/string";
|
2022-06-01 12:42:50 -04:00
|
|
|
import { htmlSafe } from "@ember/template";
|
2022-06-17 09:01:34 -04:00
|
|
|
import { get } from "@ember/object";
|
|
|
|
import { isEmpty } from "@ember/utils";
|
|
|
|
import { escapeExpression } from "discourse/lib/utilities";
|
2015-09-21 17:43:23 -04:00
|
|
|
|
2015-08-26 00:36:39 -04:00
|
|
|
function icon_or_image_replacement(str, ctx) {
|
2022-06-17 09:01:34 -04:00
|
|
|
str = get(ctx.contexts[0], str);
|
|
|
|
if (isEmpty(str)) {
|
2018-10-10 07:56:23 -04:00
|
|
|
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));
|
2022-06-17 09:01:34 -04:00
|
|
|
return htmlSafe(icon);
|
2015-08-26 00:36:39 -04:00
|
|
|
} else {
|
2022-06-17 09:01:34 -04:00
|
|
|
return htmlSafe("<img src='" + str + "'>");
|
2015-08-26 00:36:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-21 17:43:23 -04:00
|
|
|
function category_badge_replacement(str, ctx) {
|
2022-06-17 09:01:34 -04:00
|
|
|
const category = get(ctx.contexts[0], str);
|
2015-09-21 17:43:23 -04:00
|
|
|
return categoryLinkHTML(category, {
|
2020-09-04 07:23:11 -04:00
|
|
|
allowUncategorized: true,
|
2015-09-21 17:43:23 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function bound_date_replacement(str, ctx) {
|
2022-06-17 09:01:34 -04:00
|
|
|
const value = get(ctx.contexts[0], str);
|
|
|
|
return htmlSafe(autoUpdatingRelativeAge(new Date(value), { title: true }));
|
2015-08-26 00:36:39 -04:00
|
|
|
}
|
|
|
|
|
2019-05-13 00:41:37 -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];
|
|
|
|
}
|
|
|
|
|
2022-06-17 09:01:34 -04:00
|
|
|
const QueryRowContentComponent = Component.extend({
|
2020-01-11 08:04:14 -05:00
|
|
|
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,
|
2020-01-11 08:04:14 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
const parts = this.columnTemplates.map((t, idx) => {
|
|
|
|
const value = row[idx],
|
|
|
|
id = parseInt(value, 10);
|
|
|
|
|
|
|
|
const ctx = {
|
|
|
|
value,
|
|
|
|
id,
|
2022-01-26 09:13:05 -05:00
|
|
|
baseuri: getURL(""),
|
2018-10-10 07:56:23 -04:00
|
|
|
};
|
2020-01-11 08:04:14 -05:00
|
|
|
const params = {};
|
|
|
|
|
|
|
|
if (row[idx] === null) {
|
|
|
|
return "NULL";
|
|
|
|
} else if (t.name === "text") {
|
2022-06-17 09:01:34 -04:00
|
|
|
return escapeExpression(row[idx]);
|
2020-01-11 08:04:14 -05:00
|
|
|
}
|
|
|
|
|
2022-05-13 13:33:38 -04:00
|
|
|
const lookupFunc = parentView[`lookup${capitalize(t.name)}`];
|
2020-01-11 08:04:14 -05:00
|
|
|
if (lookupFunc) {
|
2021-02-18 06:06:22 -05:00
|
|
|
ctx[t.name] = lookupFunc.call(parentView, id);
|
2020-01-11 08:04:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
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") {
|
2020-01-11 08:04:14 -05:00
|
|
|
// only replace helpers if needed
|
|
|
|
params.helpers = helpers;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2022-06-17 09:01:34 -04:00
|
|
|
return htmlSafe((t.template || fallback)(ctx, params));
|
2020-01-11 08:04:14 -05:00
|
|
|
} catch (e) {
|
|
|
|
return "error";
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-06-01 12:42:50 -04:00
|
|
|
this.set("rowContents", htmlSafe(`<td>${parts.join("</td><td>")}</td>`));
|
2020-09-04 07:23:11 -04:00
|
|
|
},
|
2020-01-11 08:04:14 -05:00
|
|
|
});
|
2015-06-30 18:12:12 -04:00
|
|
|
|
|
|
|
export default QueryRowContentComponent;
|