Adding header links post vdom upgrade


(Sam) #1

So, post-vdom’ing of the header, this old code:

<script type='text/x-handlebars' data-template-name='/connectors/header-after-home-logo/add-header-links'>
{{#unless showExtraInfo}}
    <a id="recruiter" href="http://sih.onemadogre.com/t/joining-sih/494">Join us!</a>
    <a id="about" href="http://sih.onemadogre.com/c/guild-news-announcements/about-the-guild">About SiH</a>
{{/unless}}
</script>

doesn’t work, so I’ve replaced it with:

note: this code works fine as of, like, early May? anyway my comments below can be ignored

<script type="text/discourse-plugin" version="0.4">
api.decorateWidget('home-logo:after', helper => {
    const showExtraInfo = helper.attrs.minimized;
        if(!showExtraInfo) {
            return helper.h('div#header-links', [
                helper.h('a#recruiter', {
                    href:'/t/joining-sih/494', 
                    text:'Join Us!'
                }),
                helper.h('a#about', {
                    href:'/c/guild-news-announcements/about-the-guild', 
                    text:'About SiH'
                })
            ]);
        }
});
</script>

Which almost works (some css needed to reposition div#header-links, but nbd), except that my links just refresh the page.

I realize this is less a Discourse specific question and probably more a “Yuun’s making a dumb syntax error” question, but I’d appreciate it if someone could give it a look over nonetheless. :sweat:

Thanks in advance, generous stranger. :slight_smile:

Clarification: my links don’t refresh the page, they just always go to the home page. The hover text for each link is correct, but I guess it’s being overridden by the home logo’s href?


Add HTML (Link) Next To Logo
Adding plugin-outlets using a theme
Adding "+ New Topic" button on every page
Adding links to Discourse header
How to modify the header HTML, but still remaining the default founctions
(Jeff Atwood) #2

Probably something @eviltrout can advise on tomorrow.


(Sam) #3

Ah, well this worked:

<script type="text/discourse-plugin" version="0.2">
api.decorateWidget('header:after', helper => {
    const showExtraInfo = helper.attrs.topic;
        if(!showExtraInfo) {
            return helper.h('div#header-links', [
                helper.h('a#recruiter', {
                    href:'https://sih.onemadogre.com/t/joining-sih/494', 
                    text:'Join Us!'
                }),
                helper.h('a#about', {
                    href:'https://sih.onemadogre.com/c/guild-news-announcements/about-the-guild', 
                    text:'About SiH'
                })
            ]);
        }
});
</script>

After noticing the links were working, just being overridden by the home page link, I switched to using the header widget instead of home-logo. I didn’t use header at first due to missing the bit that detected whether or not topic info was loaded, but found it on a second pass. :slight_smile:

I would still appreciate any advice on whether or not this is the best approach - this seemed like the most straightforward way to me, but yeah.

Oh, blurgh. Except positioning the links isn’t quite working now, having it right after the home logo was quite convenient. >.>


Adding "+ New Topic" button on every page
Plugin outlet (hamburger menu)
(Robin Ward) #4

This is the correct approach. You are using the plugin api and decorateWidget which is the way you should modify things.

Why is positioning the links tough? Are they not appearing in the right place in the DOM? If not maybe I can add something to make them easier to insert in the right place.


(Sam) #5

Excellent, thanks.

Re: positioning, I think it may only be relatively tough, I’ll need to get back on my site and spend a little more time fiddling with the CSS. When I was using home-logo:after it was quite easy to position the links where I wanted them (on the header, just to the right of the home logo), but now that I’m using header:after some… yeah, some fiddling will be required.

I’m sure it’s fully possible, I just got distracted after updating my last post and haven’t gotten back to it yet.

Oh, huh. My links using home-logo:after work now. So: working links, easy positioning. All is well. :slight_smile:


(probus) #6

I believe the best/correct place in the DOM for this would be inside ‘contents’, after ‘title’ and ‘panel’. So in the exact same place as ‘extra-info-wrapper’. That would make it trivial to center the links both horizontally and vertically, because the new element would be right after both floated divs in the DOM.


(Clay Heaton) #7

I’m missing the big picture here – where can I find a link to all of the possible locations for the widgets? For instance, where is home-logo defined, etc?


(Robin Ward) #8

All widgets are within the folder discourse/widgets in the javascript directory. Some files contain more than one widget if they are smaller pieces that make up a bigger component.

The home-logo is here.


(Pad Pors) #9

is it possible to put something next to the search icon using this method?

i’ve tried changing user-notifications:after, and some other widget names. and it didn’t work.


(Sam) #10

I haven’t played around with it much, but this should get you started:

<script type="text/discourse-plugin" version="0.4">
api.decorateWidget('header-icons:before', helper => {
    const showExtraInfo = helper.attrs.minimized;
        if(!showExtraInfo) {
            return helper.h('div#test-links', [
                helper.h('a#test1', {
                    href:'/t/joining-sih/494', 
                    text:'Join Us!'
                }),
            ]);
        }
});
</script>
div#test-links {
    display: inline;
    float: left;
}

(Pad Pors) #11

that worked :smiley: thanks.


(Pad Pors) #12

in right to left language, i can’t bring the links to the position next to search and they go to the left side of user avatar instead.

i mean it doesn’t make any difference if i use header-icons:before or header-icons:after.

any idea how i may fix this?


(Sam) #13

Try using position: absolute and fiddling manually with the position, maybe?

I don’t know much about what changes going to RTL, but it’s worth a shot. The float: left above was just a quick thing to show it was possible to get the links there without much trouble.

e.g.:

div#test-links {
    position: absolute;
    left: -150px;
}

Puts them in a sorta correct place on my test forum.


(Ben) #14

I use Flexbox to positioning the Links. Maybe it’s useful for someone.

.d-header .title {
  display: flex;
  align-items: center;
  justify-content: center;
  margin-top: 0;
}

#header-links {
    max-width: 50%;
    font-size: 1.2rem;
    margin-left: 30px;
    margin-top: 4px;
}


(Ben) #15

This is my code in the "Customize -> </head>" section.
The Link to an external site (not discourse site) doesn’t work.

The links should open Example Domain, but it opens https://example.com/forum/index.html

I found a thread with this bug, but it’s closed. Maybe @eviltrout can help.

<script type="text/discourse-plugin" version="0.4">
api.decorateWidget('home-logo:after', helper => {
    const showExtraInfo = helper.attrs.topic;
        if(!showExtraInfo) {
            return helper.h('div#header-links', [
                helper.h('a', {
                    href:'https://example.com/index.html', 
                    text:'Tracks'
                }),
                helper.h('a.active', {
                    href:'https://example.com/forum', 
                    text:'Forum'
                })
            ]);
        }
});
</script>

(Sam) #16

That’s odd, I just checked and as long as I include https:// any external links work fine for me, in both the home-logo:after and header-icons:before locations.


(Mykhailo Poliarush) #17

My links are showing all the time. How to make it hidden if i’m on topic page and there is text on center?

Both variants doesn’t work


(Sam) #18

Ah, that’s because the header icons widget doesn’t have a property that can be accessed that shows when topic info is being displayed (attrs.minimized for home-logo, attrs.topic for header). That’s a good point, I’d forgotten about that.

But maybe you can just use the home-logo:after decoration from the first post and reposition the links to be near the search bar anyway, right? Just fiddled around with a bit and wasn’t having much luck, but could just be failing at css. :slight_smile:


(Sam) #19

Okay, here we are. Try using header-buttons:before and helper.attrs.topic, that seems to be working okay.

Sometimes I might jump the gun a bit, so let me know if there are any issues. :slight_smile:

…not that I can necessarily do anything about them, I just like to be in the know.


(Mike) #20

Any chance of writing a new summary on this? Tried getting it together but failed… :frowning: