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