REFACTOR: User menu profile link for extensibility (#8039)

This minor refactor extracts `UserMenuLinks#profileLink` &
`UserMenuLinks#linkHtml` to allow plugins / themes to control the
profile link definition, and more importantly, how it is rendered.
This commit is contained in:
Kyle Zhao 2019-08-28 02:34:51 -04:00 committed by Sam
parent 8a5eb0bdb5
commit 698e97125e
1 changed files with 23 additions and 15 deletions

View File

@ -13,6 +13,23 @@ export function addUserMenuGlyph(glyph) {
createWidget("user-menu-links", { createWidget("user-menu-links", {
tagName: "div.menu-links-header", tagName: "div.menu-links-header",
profileLink() {
const link = {
route: "user",
model: this.currentUser,
className: "user-activity-link",
icon: "user",
rawLabel: formatUsername(this.currentUser.username)
};
if (this.currentUser.is_anonymous) {
link.label = "user.profile";
link.rawLabel = null;
}
return link;
},
bookmarksGlyph() { bookmarksGlyph() {
return { return {
label: "user.bookmarks", label: "user.bookmarks",
@ -31,6 +48,10 @@ createWidget("user-menu-links", {
}; };
}, },
linkHtml(link) {
return this.attach("link", link);
},
glyphHtml(glyph) { glyphHtml(glyph) {
return this.attach("link", $.extend(glyph, { hideLabel: true })); return this.attach("link", $.extend(glyph, { hideLabel: true }));
}, },
@ -46,6 +67,7 @@ createWidget("user-menu-links", {
isAnon; isAnon;
const path = attrs.path; const path = attrs.path;
const links = [this.profileLink()];
const glyphs = []; const glyphs = [];
if (extraGlyphs) { if (extraGlyphs) {
@ -65,20 +87,6 @@ createWidget("user-menu-links", {
glyphs.push(this.messagesGlyph()); glyphs.push(this.messagesGlyph());
} }
const profileLink = {
route: "user",
model: currentUser,
className: "user-activity-link",
icon: "user",
rawLabel: formatUsername(currentUser.username)
};
if (currentUser.is_anonymous) {
profileLink.label = "user.profile";
profileLink.rawLabel = null;
}
const links = [profileLink];
if (allowAnon) { if (allowAnon) {
if (!isAnon) { if (!isAnon) {
glyphs.push({ glyphs.push({
@ -106,7 +114,7 @@ createWidget("user-menu-links", {
}); });
return h("ul.menu-links-row", [ return h("ul.menu-links-row", [
links.map(l => h("li.user", this.attach("link", l))), links.map(l => h("li.user", this.linkHtml(l))),
h("li.glyphs", glyphs.map(l => this.glyphHtml(l))) h("li.glyphs", glyphs.map(l => this.glyphHtml(l)))
]); ]);
} }