1- import Component from " @glimmer/component" ;
2- import { service } from " @ember/service" ;
3- import { dasherize } from " @ember/string" ;
4- import { htmlSafe } from " @ember/template" ;
5- import concatClass from " discourse/helpers/concat-class" ;
6- import icon from " discourse/helpers/d-icon" ;
1+ import curryComponent from " ember-curry-component" ;
2+ import { getOwnerWithFallback } from " discourse/lib/get-owner" ;
73import { withPluginApi } from " discourse/lib/plugin-api" ;
8- import { escapeExpression } from " discourse/lib/utilities" ;
9- import isValidUrl from " ../lib/isValidUrl" ;
4+ import CustomHeaderIcon from " ../components/custom-header-icon" ;
105
11- const beforeIcon = [" chat" , " search" , " hamburger" , " user-menu" ];
12-
13- function buildIconTemplate (iconNameOrImageUrl , title ) {
14- if (isValidUrl (iconNameOrImageUrl)) {
15- return <template >
16- <img src ={{iconNameOrImageUrl }} aria-hidden =" true" />
17- <span class =" sr-only" >{{title }} </span >
18- </template >;
19- }
20-
21- return <template >{{icon iconNameOrImageUrl label =title }} </template >;
22- }
6+ const BEFORE_ICONS = [" chat" , " search" , " hamburger" , " user-menu" ];
237
248export default {
259 name: " header-icon-links" ,
@@ -29,61 +13,17 @@ export default {
2913 const links = settings .header_links || [];
3014
3115 links .forEach ((link ) => {
32- const iconTemplate = buildIconTemplate (link .icon , link .title );
33- const className = ` header-icon-${ dasherize (link .title )} ` ;
34- const target = link .target === " blank" ? " _blank" : " " ;
35- const rel = link .target ? " noopener" : " " ;
36-
37- const numericWidth = Number (link .width );
38- const style = Number .isFinite (numericWidth)
39- ? htmlSafe (` width: ${ escapeExpression (numericWidth)} px` )
40- : undefined ;
41-
42- const iconComponent = class extends Component {
43- static shouldRender (args , context ) {
44- return context .site .mobileView
45- ? link .view === " vmo" || link .view === " vdm"
46- : link .view === " vdo" || link .view === " vdm" ;
47- }
48-
49- @service site;
50-
51- get isLastLink () {
52- const visibleLinks = links .filter ((item ) =>
53- this .site .mobileView
54- ? item .view === " vmo" || item .view === " vdm"
55- : item .view === " vdo" || item .view === " vdm"
56- );
57-
58- return link === visibleLinks .at (- 1 );
16+ api .headerIcons .add (
17+ link .title ,
18+ curryComponent (
19+ CustomHeaderIcon,
20+ { link, links },
21+ getOwnerWithFallback ()
22+ ),
23+ {
24+ before: BEFORE_ICONS ,
5925 }
60-
61- <template >
62- <li
63- class ={{concatClass
64- " custom-header-icon-link"
65- className
66- link.view
67- ( if this . isLastLink " last-custom-icon" )
68- }}
69- >
70- <a
71- class =" btn no-text icon btn-flat"
72- href ={{link.url }}
73- title ={{link.title }}
74- target ={{target }}
75- rel ={{rel }}
76- style ={{style }}
77- >
78- {{iconTemplate }}
79- </a >
80- </li >
81- </template >
82- };
83-
84- api .headerIcons .add (link .title , iconComponent, {
85- before: beforeIcon,
86- });
26+ );
8727 });
8828 } catch (error) {
8929 // eslint-disable-next-line no-console
0 commit comments