-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathcustom-header-link.js.es6
119 lines (94 loc) · 2.75 KB
/
custom-header-link.js.es6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import { iconNode } from "discourse-common/lib/icon-library";
import { withPluginApi } from "discourse/lib/plugin-api";
import DiscourseURL from "discourse/lib/url";
import { createWidget } from "discourse/widgets/widget";
import { h } from "virtual-dom";
import buildIconHTML from "../lib/icon-builder";
createWidget("custom-header-link", {
tagName: "li.custom-header-link",
buildKey: (attrs) => `custom-header-link-${attrs.id}`,
html(attrs) {
const iconHTML = buildIconHTML(attrs.icon);
const titleHTML = attrs.url
? h( "a.custom-header-link-title", attrs.title, {href: attrs.url})
: h("span.custom-header-link-title", attrs.title);
const permissions = this.handleLinkPermissions(attrs);
const allDropdownItems = settings.dropdown_links
? JSON.parse(settings.dropdown_links)
: [];
const dropdownLinks = allDropdownItems.filter(
(d) => d.headerLinkId === attrs.id
);
if (!permissions) {
return;
}
const dropdownItems = [];
dropdownLinks.forEach((link) => {
dropdownItems.push(this.attach("custom-header-dropdown", link));
});
const hasDropdown = dropdownItems.length > 0 ? true : false;
const dropdownHTML = hasDropdown
? h("ul.custom-header-dropdown", dropdownItems)
: "";
const contents = [
iconHTML,
titleHTML,
this.caretHTML(hasDropdown),
dropdownHTML,
];
return contents;
},
buildAttributes(attrs) {
return {
title: attrs.title,
};
},
buildClasses(attrs) {
const classes = [];
if (attrs.url) {
classes.push("with-url");
}
if (attrs.hasDropdown) {
classes.push("has-dropdown");
}
return classes;
},
handleLinkPermissions(attrs) {
if (!settings.security) {
return true;
}
const permissions = JSON.parse(settings.security);
const getPermissions = permissions
.filter((p) => p.headerLinkId === attrs.id)
.map((p) => p.title);
const currentUser = withPluginApi("1.2.0", (api) => {
return api.getCurrentUser();
});
const currentUserGroups = currentUser?.groups.map((g) => g.name);
if (getPermissions?.length < 1) {
return true;
}
if (getPermissions.length < 0) {
return false;
}
if (!currentUser) {
return false;
}
if (currentUserGroups.includes(getPermissions[0])) {
return true;
}
return false;
},
caretHTML(hasDropdown) {
if (!settings.show_caret_icons || !hasDropdown) {
return;
}
return h("span.custom-header-link-caret", iconNode("caret-down"));
},
click() {
if (this.site.mobileView) {
this.sendWidgetAction("showHeaderLinks"); // in mobile view, close menu on click
}
DiscourseURL.routeTo(this.attrs.url);
},
});