2022-12-13 10:21:01 -05:00
|
|
|
import Component from "@glimmer/component";
|
2023-12-01 10:47:54 -05:00
|
|
|
import { cached } from "@glimmer/tracking";
|
2022-05-13 13:33:38 -04:00
|
|
|
import { capitalize } from "@ember/string";
|
2022-06-17 09:01:34 -04:00
|
|
|
import { escapeExpression } from "discourse/lib/utilities";
|
2023-12-01 10:47:54 -05:00
|
|
|
import getURL from "discourse-common/lib/get-url";
|
2023-02-07 13:26:47 -05:00
|
|
|
import TextViewComponent from "./result-types/text";
|
2022-12-13 10:21:01 -05:00
|
|
|
|
|
|
|
export default class QueryRowContent extends Component {
|
|
|
|
@cached
|
|
|
|
get results() {
|
2023-02-07 13:26:47 -05:00
|
|
|
return this.args.columnComponents.map((t, idx) => {
|
2022-12-13 10:21:01 -05:00
|
|
|
const value = this.args.row[idx],
|
|
|
|
id = parseInt(value, 10);
|
|
|
|
|
|
|
|
const ctx = {
|
|
|
|
value,
|
|
|
|
id,
|
|
|
|
baseuri: getURL(""),
|
|
|
|
};
|
|
|
|
|
|
|
|
if (this.args.row[idx] === null) {
|
2023-02-07 13:26:47 -05:00
|
|
|
return {
|
|
|
|
component: TextViewComponent,
|
|
|
|
textValue: "NULL",
|
|
|
|
};
|
2022-12-13 10:21:01 -05:00
|
|
|
} else if (t.name === "text") {
|
2023-02-07 13:26:47 -05:00
|
|
|
return {
|
|
|
|
component: TextViewComponent,
|
2023-05-04 18:45:43 -04:00
|
|
|
textValue: escapeExpression(this.args.row[idx].toString()),
|
2023-02-07 13:26:47 -05:00
|
|
|
};
|
2022-12-13 10:21:01 -05:00
|
|
|
}
|
2023-02-07 13:26:47 -05:00
|
|
|
|
2022-12-13 10:21:01 -05:00
|
|
|
const lookupFunc = this.args[`lookup${capitalize(t.name)}`];
|
|
|
|
if (lookupFunc) {
|
|
|
|
ctx[t.name] = lookupFunc.call(this.args, id);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (t.name === "url") {
|
|
|
|
let [url, name] = guessUrl(value);
|
|
|
|
ctx["href"] = url;
|
|
|
|
ctx["target"] = name;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2023-02-07 13:26:47 -05:00
|
|
|
return {
|
|
|
|
component: t.component || TextViewComponent,
|
|
|
|
ctx,
|
|
|
|
};
|
2022-12-13 10:21:01 -05:00
|
|
|
} catch (e) {
|
|
|
|
return "error";
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2015-09-21 17:43:23 -04:00
|
|
|
|
2019-05-13 00:41:37 -04:00
|
|
|
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];
|
|
|
|
}
|