DEV: replace registerUnbound usage with default exports (#23802)

`registerUnbound` was present for legacy reasons when using helpers in raw-hbs and has been replaced by `registerRawHelper`.

For new helpers used only in classic ember template, exporting a default function from `helpers/*.js` is recommended.

This change also means that all existing helpers will be available to import in `gjs` files.

Co-authored-by: David Taylor <david@taylorhq.com>
This commit is contained in:
Joffrey JAFFEUX 2023-10-19 15:28:25 +02:00 committed by GitHub
parent 24dc36cf91
commit bc9558550d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
45 changed files with 334 additions and 265 deletions

View File

@ -1,8 +1,10 @@
import { htmlSafe } from "@ember/template";
import { registerUnbound } from "discourse-common/lib/helpers";
import { registerRawHelper } from "discourse-common/lib/helpers";
import { renderIcon } from "discourse-common/lib/icon-library";
registerUnbound("check-icon", function (value) {
registerRawHelper("check-icon", checkIcon);
export default function checkIcon(value) {
let icon = value ? "check" : "times";
return htmlSafe(renderIcon("string", icon));
});
}

View File

@ -1,6 +1,8 @@
import { registerUnbound } from "discourse-common/lib/helpers";
import { registerRawHelper } from "discourse-common/lib/helpers";
registerUnbound("value-at-tl", function (data, params) {
registerRawHelper("value-at-tl", valueAtTl);
export default function valueAtTl(data, params = {}) {
let tl = parseInt(params.level, 10);
if (data) {
let item = data.find(function (d) {
@ -12,4 +14,4 @@ registerUnbound("value-at-tl", function (data, params) {
return 0;
}
}
});
}

View File

@ -0,0 +1,8 @@
import getUrl from "discourse-common/lib/get-url";
import { registerRawHelper } from "discourse-common/lib/helpers";
registerRawHelper("base-path", basePath);
export default function basePath() {
return getUrl("");
}

View File

@ -0,0 +1,10 @@
import deprecated from "discourse-common/lib/deprecated";
import getUrl from "discourse-common/lib/get-url";
import { registerRawHelper } from "discourse-common/lib/helpers";
registerRawHelper("base-url", baseUrl);
export default function baseUrl() {
deprecated("Use `{{base-path}}` instead of `{{base-url}}`");
return getUrl("");
}

View File

@ -1,8 +1,10 @@
import { registerUnbound } from "discourse-common/lib/helpers";
import { registerRawHelper } from "discourse-common/lib/helpers";
registerUnbound(
"component-for-collection",
(collectionIdentifier, selectKit) => {
return selectKit.modifyComponentForCollection(collectionIdentifier);
}
);
registerRawHelper("component-for-collection", componentForCollection);
export default function componentForCollection(
collectionIdentifier,
selectKit
) {
return selectKit.modifyComponentForCollection(collectionIdentifier);
}

View File

@ -1,8 +1,11 @@
import { registerUnbound } from "discourse-common/lib/helpers";
import { registerRawHelper } from "discourse-common/lib/helpers";
registerUnbound(
"component-for-row",
(collectionForIdentifier, item, selectKit) => {
return selectKit.modifyComponentForRow(collectionForIdentifier, item);
}
);
registerRawHelper("component-for-row", componentForRow);
export default function componentForRow(
collectionForIdentifier,
item,
selectKit
) {
return selectKit.modifyComponentForRow(collectionForIdentifier, item);
}

View File

@ -1,9 +1,9 @@
import { htmlSafe } from "@ember/template";
import { registerUnbound } from "discourse-common/lib/helpers";
import { registerRawHelper } from "discourse-common/lib/helpers";
import { renderIcon } from "discourse-common/lib/icon-library";
export default function icon(id, options = {}) {
return htmlSafe(renderIcon("string", id, options));
}
registerUnbound("d-icon", icon);
registerRawHelper("d-icon", icon);

View File

@ -1,13 +1,14 @@
import { htmlSafe } from "@ember/template";
import deprecated from "discourse-common/lib/deprecated";
import { registerUnbound } from "discourse-common/lib/helpers";
import { registerRawHelper } from "discourse-common/lib/helpers";
import { renderIcon } from "discourse-common/lib/icon-library";
export function iconHTML(id, params) {
return renderIcon("string", id, params);
}
registerUnbound("fa-icon", function (icon, params) {
registerRawHelper("fa-icon", faIcon);
export default function faIcon(icon, params) {
deprecated("Use `{{d-icon}}` instead of `{{fa-icon}}");
return htmlSafe(iconHTML(icon, params));
});
}

View File

@ -1,10 +1,8 @@
import deprecated from "discourse-common/lib/deprecated";
import getUrl from "discourse-common/lib/get-url";
import { registerUnbound } from "discourse-common/lib/helpers";
import { default as emberGetUrl } from "discourse-common/lib/get-url";
import { registerRawHelper } from "discourse-common/lib/helpers";
registerUnbound("get-url", (value) => getUrl(value));
registerUnbound("base-url", () => {
deprecated("Use `{{base-path}}` instead of `{{base-url}}`");
return getUrl("");
});
registerUnbound("base-path", () => getUrl(""));
registerRawHelper("get-url", getUrl);
export default function getUrl(value) {
return emberGetUrl(value);
}

View File

@ -1,6 +1,8 @@
import { htmlSafe } from "@ember/template";
import { registerUnbound } from "discourse-common/lib/helpers";
import { htmlSafe as emberHtmlSafe } from "@ember/template";
import { registerRawHelper } from "discourse-common/lib/helpers";
registerUnbound("html-safe", function (string) {
return htmlSafe(string);
});
registerRawHelper("html-safe", htmlSafe);
export default function htmlSafe(string) {
return emberHtmlSafe(string);
}

View File

@ -0,0 +1,8 @@
import { registerRawHelper } from "discourse-common/lib/helpers";
import I18n from "discourse-i18n";
registerRawHelper("i18n-yes-no", i18nYesNo);
export default function i18nYesNo(value, params) {
return I18n.t(value ? "yes_value" : "no_value", params);
}

View File

@ -1,11 +1,8 @@
import { registerUnbound } from "discourse-common/lib/helpers";
import { registerRawHelper } from "discourse-common/lib/helpers";
import I18n from "discourse-i18n";
registerRawHelper("i18n", i18n);
export default function i18n(key, params) {
return I18n.t(key, params);
}
registerUnbound("i18n", i18n);
registerUnbound("i18n-yes-no", (value, params) =>
I18n.t(value ? "yes_value" : "no_value", params)
);

View File

@ -2,6 +2,7 @@ import Helper from "@ember/component/helper";
import { get } from "@ember/object";
import { dasherize } from "@ember/string";
import { htmlSafe } from "@ember/template";
import deprecated from "discourse-common/lib/deprecated";
import RawHandlebars from "discourse-common/lib/raw-handlebars";
export function makeArray(obj) {
@ -88,6 +89,11 @@ function resolveParams(ctx, options) {
* do `export default ...` from a `helpers/*.js` file.
*/
export function registerUnbound(name, fn) {
deprecated(
`[registerUnbound ${name}] registerUnbound is deprecated. Instead, you should export a default function from 'discourse/helpers/${name}.js'. If the helper is also used in raw-hbs, you can register it using 'registerRawHelper'.`,
{ id: "discourse.register-unbound" }
);
_helpers[name] = Helper.extend({
compute: (params, args) => fn(...params, args),
});

View File

@ -0,0 +1,15 @@
import { htmlSafe } from "@ember/template";
import { autoUpdatingRelativeAge } from "discourse/lib/formatter";
import { registerRawHelper } from "discourse-common/lib/helpers";
registerRawHelper("age-with-tooltip", ageWithTooltip);
export default function ageWithTooltip(dt, params = {}) {
return htmlSafe(
autoUpdatingRelativeAge(new Date(dt), {
title: true,
addAgo: params.addAgo || false,
...(params.defaultFormat && { defaultFormat: params.defaultFormat }),
})
);
}

View File

@ -0,0 +1,86 @@
import { get } from "@ember/object";
import { htmlSafe } from "@ember/template";
import { prioritizeNameInUx } from "discourse/lib/settings";
import { formatUsername } from "discourse/lib/utilities";
import { avatarImg } from "discourse-common/lib/avatar-utils";
import { registerRawHelper } from "discourse-common/lib/helpers";
import I18n from "discourse-i18n";
let _customAvatarHelpers;
export function registerCustomAvatarHelper(fn) {
_customAvatarHelpers = _customAvatarHelpers || [];
_customAvatarHelpers.push(fn);
}
export function addExtraUserClasses(u, args) {
let extraClasses = classesForUser(u).join(" ");
if (extraClasses && extraClasses.length) {
args.extraClasses = extraClasses;
}
return args;
}
export function classesForUser(u) {
let result = [];
if (_customAvatarHelpers) {
for (let i = 0; i < _customAvatarHelpers.length; i++) {
result = result.concat(_customAvatarHelpers[i](u));
}
}
return result;
}
export function renderAvatar(user, options) {
options = options || {};
if (user) {
const name = get(user, options.namePath || "name");
const username = get(user, options.usernamePath || "username");
const avatarTemplate = get(
user,
options.avatarTemplatePath || "avatar_template"
);
if (!username || !avatarTemplate) {
return "";
}
let displayName = prioritizeNameInUx(name)
? name
: formatUsername(username);
let title = options.title;
if (!title && !options.ignoreTitle) {
// first try to get a title
title = get(user, "title");
// if there was no title provided
if (!title) {
// try to retrieve a description
const description = get(user, "description");
// if a description has been provided
if (description && description.length > 0) {
// prepend the username before the description
title = I18n.t("user.avatar.name_and_description", {
name: displayName,
description,
});
}
}
}
return avatarImg({
size: options.imageSize,
extraClasses: get(user, "extras") || options.extraClasses,
title: title || displayName,
avatarTemplate,
});
} else {
return "";
}
}
registerRawHelper("avatar", avatar);
export default function avatar(user, params) {
return htmlSafe(renderAvatar.call(this, user, params));
}

View File

@ -4,7 +4,7 @@ import { isRTL } from "discourse/lib/text-direction";
import { escapeExpression } from "discourse/lib/utilities";
import Category from "discourse/models/category";
import getURL from "discourse-common/lib/get-url";
import { helperContext, registerUnbound } from "discourse-common/lib/helpers";
import { helperContext, registerRawHelper } from "discourse-common/lib/helpers";
import { iconHTML } from "discourse-common/lib/icon-library";
import I18n from "discourse-i18n";
@ -95,7 +95,8 @@ export function categoryLinkHTML(category, options) {
return htmlSafe(categoryBadgeHTML(category, categoryOptions));
}
registerUnbound("category-link", categoryLinkHTML);
export default categoryLinkHTML;
registerRawHelper("category-link", categoryLinkHTML);
function buildTopicCount(count) {
return `<span class="topic-count" aria-label="${I18n.t(

View File

@ -1,11 +1,13 @@
import { helperContext, registerUnbound } from "discourse-common/lib/helpers";
import { helperContext, registerRawHelper } from "discourse-common/lib/helpers";
function daysSinceEpoch(dt) {
// 1000 * 60 * 60 * 24 = days since epoch
return dt.getTime() / 86400000;
}
registerUnbound("cold-age-class", function (dt, params) {
registerRawHelper("cold-age-class", coldAgeClass);
export default function coldAgeClass(dt, params = {}) {
let className = params["class"] || "age";
if (!dt) {
@ -30,6 +32,4 @@ registerUnbound("cold-age-class", function (dt, params) {
}
return className;
});
export { daysSinceEpoch };
}

View File

@ -1,5 +1,5 @@
import { htmlSafe } from "@ember/template";
import { registerUnbound } from "discourse-common/lib/helpers";
import { registerRawHelper } from "discourse-common/lib/helpers";
let usernameDecorators = [];
export function addUsernameSelectorDecorator(decorator) {
@ -20,6 +20,8 @@ export function decorateUsername(username) {
return decorations.length ? htmlSafe(decorations.join("")) : "";
}
export default registerUnbound("decorate-username-selector", (username) => {
registerRawHelper("decorate-username-selector", decorateUsernameSelector);
export default function decorateUsernameSelector(username) {
return decorateUsername(username);
});
}

View File

@ -1,7 +1,7 @@
import { htmlSafe } from "@ember/template";
import { isRTL } from "discourse/lib/text-direction";
import { escapeExpression } from "discourse/lib/utilities";
import { helperContext, registerUnbound } from "discourse-common/lib/helpers";
import { helperContext, registerRawHelper } from "discourse-common/lib/helpers";
function setDir(text) {
let content = text ? text : "";
@ -13,11 +13,13 @@ function setDir(text) {
return content;
}
export default registerUnbound("dir-span", function (str, params = {}) {
registerRawHelper("dir-span", dirSpan);
export default function dirSpan(str, params = {}) {
let isHtmlSafe = false;
if (params.htmlSafe) {
isHtmlSafe = params.htmlSafe === "true";
}
let text = isHtmlSafe ? str : escapeExpression(str);
return htmlSafe(setDir(text));
});
}

View File

@ -0,0 +1,4 @@
export default function directoryColumnIsAutomatic(args) {
// Args should include key/values { column }
return args.column.type === "automatic";
}

View File

@ -0,0 +1,4 @@
export default function directoryColumnIsUserField(args) {
// Args should include key/values { column }
return args.column.type === "user_field";
}

View File

@ -1,43 +0,0 @@
import { htmlSafe } from "@ember/template";
import { number } from "discourse/lib/formatter";
import { registerUnbound } from "discourse-common/lib/helpers";
import I18n from "discourse-i18n";
registerUnbound("directory-item-label", function (args) {
// Args should include key/values { item, column }
const count = args.item.get(args.column.name);
const translationPrefix =
args.column.type === "automatic" ? "directory." : "";
return htmlSafe(I18n.t(`${translationPrefix}${args.column.name}`, { count }));
});
registerUnbound("directory-item-value", function (args) {
// Args should include key/values { item, column }
return htmlSafe(
`<span class='directory-table__value'>${number(
args.item.get(args.column.name)
)}</span>`
);
});
registerUnbound("directory-item-user-field-value", function (args) {
// Args should include key/values { item, column }
const value =
args.item.user && args.item.user.user_fields
? args.item.user.user_fields[args.column.user_field_id]
: null;
const content = value || "-";
return htmlSafe(
`<span class='directory-table__value--user-field'>${content}</span>`
);
});
registerUnbound("directory-column-is-automatic", function (args) {
// Args should include key/values { column }
return args.column.type === "automatic";
});
registerUnbound("directory-column-is-user-field", function (args) {
// Args should include key/values { column }
return args.column.type === "user_field";
});

View File

@ -0,0 +1,10 @@
import { htmlSafe } from "@ember/template";
import I18n from "discourse-i18n";
export default function directoryItemLabel(args) {
// Args should include key/values { item, column }
const count = args.item.get(args.column.name);
const translationPrefix =
args.column.type === "automatic" ? "directory." : "";
return htmlSafe(I18n.t(`${translationPrefix}${args.column.name}`, { count }));
}

View File

@ -0,0 +1,13 @@
import { htmlSafe } from "@ember/template";
export default function directoryItemUserFieldValue(args) {
// Args should include key/values { item, column }
const value =
args.item.user && args.item.user.user_fields
? args.item.user.user_fields[args.column.user_field_id]
: null;
const content = value || "-";
return htmlSafe(
`<span class='directory-table__value--user-field'>${content}</span>`
);
}

View File

@ -0,0 +1,11 @@
import { htmlSafe } from "@ember/template";
import { number } from "discourse/lib/formatter";
export default function directoryItemValue(args) {
// Args should include key/values { item, column }
return htmlSafe(
`<span class='directory-table__value'>${number(
args.item.get(args.column.name)
)}</span>`
);
}

View File

@ -1,9 +1,8 @@
import { htmlSafe } from "@ember/template";
import { registerUnbound } from "discourse-common/lib/helpers";
import { iconHTML } from "discourse-common/lib/icon-library";
import I18n from "discourse-i18n";
export default registerUnbound("directory-table-header-title", function (args) {
export default function directoryTableHeaderTitle(args) {
// Args should include key/values { field, labelKey, icon, translated }
let html = "";
@ -16,4 +15,4 @@ export default registerUnbound("directory-table-header-title", function (args) {
? args.field
: I18n.t(labelKey + "_long", { defaultValue: I18n.t(labelKey) });
return htmlSafe(html);
});
}

View File

@ -1,7 +1,8 @@
import { htmlSafe } from "@ember/template";
import renderTag from "discourse/lib/render-tag";
import { registerUnbound } from "discourse-common/lib/helpers";
import { registerRawHelper } from "discourse-common/lib/helpers";
export default registerUnbound("discourse-tag", function (name, params) {
registerRawHelper("discourse-tag", discourseTag);
export default function discourseTag(name, params) {
return htmlSafe(renderTag(name, params));
});
}

View File

@ -1,7 +1,8 @@
import { htmlSafe } from "@ember/template";
import renderTags from "discourse/lib/render-tags";
import { registerUnbound } from "discourse-common/lib/helpers";
import { registerRawHelper } from "discourse-common/lib/helpers";
export default registerUnbound("discourse-tags", function (topic, params) {
registerRawHelper("discourse-tags", discourseTags);
export default function discourseTags(topic, params) {
return htmlSafe(renderTags(topic, params));
});
}

View File

@ -1,9 +1,10 @@
import { htmlSafe } from "@ember/template";
import { emojiUnescape } from "discourse/lib/text";
import { escapeExpression } from "discourse/lib/utilities";
import { registerUnbound } from "discourse-common/lib/helpers";
import { registerRawHelper } from "discourse-common/lib/helpers";
registerUnbound("emoji", function (code, options) {
registerRawHelper("emoji", emoji);
export default function emoji(code, options) {
const escaped = escapeExpression(`:${code}:`);
return htmlSafe(emojiUnescape(escaped, options));
});
}

View File

@ -1,5 +1,3 @@
import { registerUnbound } from "discourse-common/lib/helpers";
registerUnbound("float", function (n) {
export default function float(n) {
return parseFloat(n).toFixed(1);
});
}

View File

@ -1,12 +1,9 @@
import { htmlSafe } from "@ember/template";
import { autoUpdatingRelativeAge, durationTiny } from "discourse/lib/formatter";
import { registerUnbound } from "discourse-common/lib/helpers";
import { autoUpdatingRelativeAge } from "discourse/lib/formatter";
import { registerRawHelper } from "discourse-common/lib/helpers";
registerUnbound("format-age", function (dt) {
registerRawHelper("format-age", formatAge);
export default function formatAge(dt) {
dt = new Date(dt);
return htmlSafe(autoUpdatingRelativeAge(dt));
});
registerUnbound("format-duration", function (seconds) {
return htmlSafe(durationTiny(seconds));
});
}

View File

@ -1,12 +1,14 @@
import { htmlSafe } from "@ember/template";
import { autoUpdatingRelativeAge } from "discourse/lib/formatter";
import { registerUnbound } from "discourse-common/lib/helpers";
import { registerRawHelper } from "discourse-common/lib/helpers";
/**
Display logic for dates. It is unbound in Ember but will use jQuery to
update the dates on a regular interval.
**/
registerUnbound("format-date", function (val, params) {
registerRawHelper("format-date", formatDate);
export default function formatDate(val, params = {}) {
let leaveAgo,
format = "medium",
title = true;
@ -32,4 +34,4 @@ registerUnbound("format-date", function (val, params) {
})
);
}
});
}

View File

@ -0,0 +1,8 @@
import { htmlSafe } from "@ember/template";
import { durationTiny } from "discourse/lib/formatter";
import { registerRawHelper } from "discourse-common/lib/helpers";
registerRawHelper("format-duration", formatDuration);
export default function formatDuration(seconds) {
return htmlSafe(durationTiny(seconds));
}

View File

@ -1,4 +1,5 @@
import { formatUsername } from "discourse/lib/utilities";
import { registerUnbound } from "discourse-common/lib/helpers";
import { registerRawHelper } from "discourse-common/lib/helpers";
export default registerUnbound("format-username", formatUsername);
export default formatUsername;
registerRawHelper("format-username", formatUsername);

View File

@ -1,26 +1,12 @@
import { htmlSafe } from "@ember/template";
import {
autoUpdatingRelativeAge,
longDate,
number,
} from "discourse/lib/formatter";
import { number as numberFormatter } from "discourse/lib/formatter";
import { escapeExpression } from "discourse/lib/utilities";
import { registerUnbound } from "discourse-common/lib/helpers";
import { registerRawHelper } from "discourse-common/lib/helpers";
import I18n from "discourse-i18n";
registerUnbound("raw-date", (dt) => htmlSafe(longDate(new Date(dt))));
registerRawHelper("number", number);
registerUnbound("age-with-tooltip", (dt, params) =>
htmlSafe(
autoUpdatingRelativeAge(new Date(dt), {
title: true,
addAgo: params.addAgo || false,
...(params.defaultFormat && { defaultFormat: params.defaultFormat }),
})
)
);
registerUnbound("number", (orig, params) => {
export default function number(orig, params = {}) {
orig = Math.round(parseFloat(orig));
if (isNaN(orig)) {
orig = 0;
@ -43,7 +29,7 @@ registerUnbound("number", (orig, params) => {
let addTitle = params.noTitle ? false : true;
// Round off the thousands to one decimal place
const n = number(orig);
const n = numberFormatter(orig);
if (n.toString() !== title.toString() && addTitle) {
result += " title='" + escapeExpression(title) + "'";
}
@ -55,4 +41,4 @@ registerUnbound("number", (orig, params) => {
result += ">" + n + "</span>";
return htmlSafe(result);
});
}

View File

@ -0,0 +1,9 @@
import { htmlSafe } from "@ember/template";
import { longDate } from "discourse/lib/formatter";
import { registerRawHelper } from "discourse-common/lib/helpers";
registerRawHelper("raw-date", rawDate);
export default function rawDate(dt) {
return htmlSafe(longDate(new Date(dt)));
}

View File

@ -1,6 +1,7 @@
import { registerUnbound } from "discourse-common/lib/helpers";
import { registerRawHelper } from "discourse-common/lib/helpers";
registerUnbound("shorten-url", function (url) {
registerRawHelper("shorten-url", shortenUrl);
export default function shortenUrl(url) {
let matches = url.match(/\//g);
if (matches && matches.length === 3) {
@ -9,4 +10,4 @@ registerUnbound("shorten-url", function (url) {
url = url.replace(/^https?:\/\//, "");
url = url.replace(/^www\./, "");
return url.substring(0, 80);
});
}

View File

@ -1,16 +0,0 @@
import { getSetting as getThemeSetting } from "discourse/lib/theme-settings-store";
import { registerUnbound } from "discourse-common/lib/helpers";
import I18n from "discourse-i18n";
registerUnbound("theme-i18n", (themeId, key, params) => {
return I18n.t(`theme_translations.${themeId}.${key}`, params);
});
registerUnbound(
"theme-prefix",
(themeId, key) => `theme_translations.${themeId}.${key}`
);
registerUnbound("theme-setting", (themeId, key) => {
return getThemeSetting(themeId, key);
});

View File

@ -0,0 +1,7 @@
import { registerRawHelper } from "discourse-common/lib/helpers";
import I18n from "discourse-i18n";
registerRawHelper("theme-i18n", themeI18n);
export default function themeI18n(themeId, key, params) {
return I18n.t(`theme_translations.${themeId}.${key}`, params);
}

View File

@ -0,0 +1,6 @@
import { registerRawHelper } from "discourse-common/lib/helpers";
registerRawHelper("theme-prefix", themePrefix);
export default function themePrefix(themeId, key) {
return `theme_translations.${themeId}.${key}`;
}

View File

@ -0,0 +1,7 @@
import { getSetting as getThemeSetting } from "discourse/lib/theme-settings-store";
import { registerRawHelper } from "discourse-common/lib/helpers";
registerRawHelper("theme-setting", themeSetting);
export default function themeSetting(themeId, key) {
return getThemeSetting(themeId, key);
}

View File

@ -1,7 +1,8 @@
import { htmlSafe } from "@ember/template";
import renderTopicFeaturedLink from "discourse/lib/render-topic-featured-link";
import { registerUnbound } from "discourse-common/lib/helpers";
import { registerRawHelper } from "discourse-common/lib/helpers";
export default registerUnbound("topic-featured-link", function (topic, params) {
registerRawHelper("topic-featured-link", topicFeaturedLink);
export default function topicFeaturedLink(topic, params) {
return htmlSafe(renderTopicFeaturedLink(topic, params));
});
}

View File

@ -1,7 +1,8 @@
import { htmlSafe } from "@ember/template";
import { registerUnbound } from "discourse-common/lib/helpers";
import { registerRawHelper } from "discourse-common/lib/helpers";
registerUnbound("topic-link", (topic, args) => {
registerRawHelper("topic-link", topicLink);
export default function topicLink(topic, args = {}) {
const title = topic.get("fancyTitle");
const url = topic.linked_post_number
@ -21,4 +22,4 @@ registerUnbound("topic-link", (topic, args) => {
class='${classes.join(" ")}'
data-topic-id='${topic.id}'>${title}</a>`
);
});
}

View File

@ -1,87 +1 @@
import { get } from "@ember/object";
import { htmlSafe } from "@ember/template";
import { prioritizeNameInUx } from "discourse/lib/settings";
import { formatUsername } from "discourse/lib/utilities";
import { avatarImg } from "discourse-common/lib/avatar-utils";
import { registerUnbound } from "discourse-common/lib/helpers";
import I18n from "discourse-i18n";
let _customAvatarHelpers;
export function registerCustomAvatarHelper(fn) {
_customAvatarHelpers = _customAvatarHelpers || [];
_customAvatarHelpers.push(fn);
}
export function addExtraUserClasses(u, args) {
let extraClasses = classesForUser(u).join(" ");
if (extraClasses && extraClasses.length) {
args.extraClasses = extraClasses;
}
return args;
}
export function classesForUser(u) {
let result = [];
if (_customAvatarHelpers) {
for (let i = 0; i < _customAvatarHelpers.length; i++) {
result = result.concat(_customAvatarHelpers[i](u));
}
}
return result;
}
function renderAvatar(user, options) {
options = options || {};
if (user) {
const name = get(user, options.namePath || "name");
const username = get(user, options.usernamePath || "username");
const avatarTemplate = get(
user,
options.avatarTemplatePath || "avatar_template"
);
if (!username || !avatarTemplate) {
return "";
}
let displayName = prioritizeNameInUx(name)
? name
: formatUsername(username);
let title = options.title;
if (!title && !options.ignoreTitle) {
// first try to get a title
title = get(user, "title");
// if there was no title provided
if (!title) {
// try to retrieve a description
const description = get(user, "description");
// if a description has been provided
if (description && description.length > 0) {
// prepend the username before the description
title = I18n.t("user.avatar.name_and_description", {
name: displayName,
description,
});
}
}
}
return avatarImg({
size: options.imageSize,
extraClasses: get(user, "extras") || options.extraClasses,
title: title || displayName,
avatarTemplate,
});
} else {
return "";
}
}
registerUnbound("avatar", function (user, params) {
return htmlSafe(renderAvatar.call(this, user, params));
});
export { renderAvatar };
export * from "./avatar";

View File

@ -1,6 +1,7 @@
import { registerUnbound } from "discourse-common/lib/helpers";
import { registerRawHelper } from "discourse-common/lib/helpers";
registerUnbound("value-entered", function (value) {
registerRawHelper("value-entered", valueEntered);
export default function valueEntered(value) {
if (!value) {
return "";
} else if (value.length > 0) {
@ -8,4 +9,4 @@ registerUnbound("value-entered", function (value) {
} else {
return "";
}
});
}