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