Skip to content

[NODEBB] Help for my custom CSS

Solved Customisation
  • Hello mark šŸ™‚

    I wanted for test purposes, to put the welcome message in the Brand menu in the header widget.

    I commented in the footer widget the corresponding code then I added it in my brand header widget:

    <span id="thisuser" class="getUsername">, &nbsp;<a href="/me"><span class="username">DownPW</span></a></span>
    

    I commented out the old CSS to add the new:

    /* Welcome Message Username on Header */
    span#thisuser {
         margin-top: 0.7rem;
         margin-left: 2px;
         margin-right:-2px;
         font-size: .875rem;
         line-height: 1.4rem;
         font-weight: 500;
    }
    

    So far everything is fine, the result is correct :

    4c4eedf9-e511-4e3c-acb8-0049035729a6-image.png

    But as soon as I select links in the left sidebar (home, recent, etc ā€¦), the message is added and displayed as many times as I select a link .

    theme swatch menu.gif

    Did I miss something or do you have an idea to solve this bug?

    Thanks my friend šŸ˜‰

  • @DownPW sorry for the delay. That is being caused by ajax which is adding another element each time data is being called. Iā€™d need to see the entire code block where you are adding this to confirm, but from the video you provided, thatā€™s typically caused by the above.

    You only need that specific function to run once at page load - itā€™s running each time data is being requested which explains the additions.

  • itā€™s just the brand HTML header widget, just add this code to him

    <span id="thisuser" class="getUsername">, Ā <a href="/me"><span class="username">DownPW</span></a></span>]
    

    What do you want ? The HTML header widget on welcome message JS code ?

    EDIT : All code in PM

  • @DownPW thanks. Can you try a theory for me and change that code so it looks like the below

    <span id="thisuserbrand" class="getUsername">, Ā <a href="/me"><span class="username">DownPW</span></a></span>
    

    I expect this to return nothing, which will prove my theory around the Ajax calls.

  • Nope @phenomlab

    Same things, same problem

  • @DownPW then the problem isnā€™t the existing function. That ID I provided doesnā€™t exist anywhere and isnā€™t tied to any elements so should never trigger.

  • oups donā€™t know why I have used this code, replace by :

    <div class="getUsername">, <a href="/me"><span class="username"></span></a></div>
    

    But same problem

  • @DownPW did you remove the original line from the footer before placing it in the brand header?

  • remove no, but commented like this

    <!-- <span id="thisuser" class="getUsername">, Ā <a href="/me"><span class="username">DownPW</span></a></span> -->
    

    EDIT: test with remove = same

  • @DownPW can you send me the footer function your are using. Iā€™m 100% certain this is being caused by the ajax call.

  • @DownPW can you change the top line of this function from

    $(window).on('action:ajaxify.end', function (data) {
    

    To

    $(document).ready() {
    

    Save and let me know if this changes anything. The theory here is that the Ajax call in the top line causes the prepend to be executed multiple times

  • the code doesnā€™t work with this

  • @DownPW it should work once at least on site reload, no? Or does it crash?

  • no, doesnā€™t work and break theme and swatch applet

    image.png

  • @DownPW that shouldnā€™t cause the error you are seeing there. It looks like that is from the previous function.

  • the previous function is just fancybox and nothing more :

    // Fancybox Media Reader
    if (top.location.pathname !== '/login') {
        $(window).on('action:posts.loaded', function(data) {
            console.log("Polling DOM for lazyLoaded images to apply Fancybox");
            $(document).ready(function() {
                $('a').not('.forum-logo').not(".avatar").not(".emoji").not(".bmac-noanimate").each(function() {
                    $('a[href*=".jpg"], a[href*=".jpeg"], a[href*=".png"], a[href*=".gif"], a[href*=".webp"]').addClass("noanimate");
                });
            });
        });
    }
    
    if (top.location.pathname !== '/login') {
        $(document).ready(function() {
            $(window).on('action:ajaxify.end', function(data) {
                this.$('a').not('.forum-logo').not(".avatar").not(".emoji").not(".bmac-noanimate").each(function() {
                    $('a[href*=".jpg"], a[href*=".jpeg"], a[href*=".png"], a[href*=".gif"], a[href*=".webp"]').addClass("noanimate");
                    data.preventDefault()
                    // Strip out the images contained inside blockquotes as this looks nasty :)
                    $('blockquote img').remove();
                });
                Fancybox.bind(
                    'a[href*=".jpg"], a[href*=".jpeg"], a[href*=".png"], a[href*=".gif"], a[href*=".webp"]', {
                        groupAll: true,
                    }
                );
            });
        });
    }
    
    // Chat fancybox - fires when chat module loaded and AJAX calls new chat
    $(document).ready(function() {
        $(window).on('action:chat.loaded', function(data) {
                this.$('img').not('.forum-logo').not(".avatar").not(".emoji").not(".bmac-noanimate").each(function() {
                    var newHref = $(this).attr("src");
                    $(this).wrap("<a class='fancybox' href='" + newHref + "'/>");
                    $('a[href*=".jpg"], a[href*=".jpeg"], a[href*=".png"], a[href*=".gif"], a[href*=".webp"]').addClass("noanimate");
                    data.preventDefault();
                    // Strip out the images contained inside blockquotes as this looks nasty :)
                    $('blockquote img').remove();
                });
                Fancybox.bind(
                    'a[href*=".jpg"], a[href*=".jpeg"], a[href*=".png"], a[href*=".gif"], a[href*=".webp"]', {
                        groupAll: true,
                    }
                );
        });
    });
    

    the error above just appear wit hthe change of $(document).ready() {, no error with $(window).on('action:ajaxify.end', function (data) {

  • @DownPW very odd. Iā€™ll have a look at this directly on your dev site tomorrow morning to see why this is.

    EDIT - this is now fixed. I looked at the code, and I was right that it should work, and only execute once šŸ™‚ - not sure what happened there.

    067250ba-7af1-479f-a1d0-74cb4bfd83e2-image.png

    EDIT2 - I absolutely love this design, and am stealing it šŸ™‚ Such a clever way of depicting colour in the theme, and Iā€™m going to make it dynamic also so that the colours change if the theme css does.

    1487e619-4cf5-4650-b7fb-0bff0893c870-image.png

  • @phenomlab said in [NODEBB] Help for my custom CSS:

    EDIT - this is now fixed. I looked at the code, and I was right that it should work, and only execute once - not sure what happened there.

    āš ļø hmmm not, almost fixed @phenomlab !
    Itā€™s ok for duplicate ajax problem but not fixed for the the footer image : Itā€™s appear one time and if you go to another section (like recent or unread for example), the image on footer donā€™t appear
    Same in incognito mode.

    9d8d6189-8d40-423e-94c8-9d7543d3fe2c-image.png

    @phenomlab said in [NODEBB] Help for my custom CSS:

    EDIT2 - I absolutely love this design, and am stealing it Such a clever way of depicting colour in the theme, and Iā€™m going to make it dynamic also so that the colours change if the theme css does.

    Thank you !
    ā€“> I already told you, my code is your code šŸ˜‰

    Thinking about it, I found this idea elsewhere but I donā€™t really know where šŸ™‚
    I just added a rotation effect on it and adapted it to the dropdown of the brand menu widget.


    You talk about making it dynamic but I donā€™t really see what you mean since the theme colors represented do not change.
    Unless you want to automate it. Genre detects the 2 main color types of the theme according to 2 values/declarations present in the css file.

  • @DownPW said in [NODEBB] Help for my custom CSS:

    Itā€™s ok for duplicate ajax problem but not fixed for the the footer image : Itā€™s appear one time and if you go to another section (like recent or unread for example), the image on footer donā€™t appear

    I kind of expected that because the footer image relies on an ajax reload to determine the time, then work out which image to display based on that. The real problem here is that the prepend is being called on each request, and because itā€™s an prepend, you are seeing multiple copies.

    One way to fix this is to delete the element and recreate it, but that is horribly inefficient šŸ˜•

    Iā€™ve modified the function to include a check to see if the containing div is empty or not - if it is, we add the icon and message - if itā€™s already there, we skip it

        // Test to see if the DIV containing the icon and message is empty. If it is, insert icon and message
        if ($('#busername').length === 0) {
            $('.getUsername').prepend("<div id='busername'><i id='thisicon' class='" + theicon + "'></i></div>" + themessage);
        } else {
            // nothing to do here :)
        }
    

    (Note that this has been added to the existing function)

    And some minor css which allows us to float the new div so that it displays inline

    // Inline display fix for Welcome DIV
    div#busername {
        display: inline;
    }
    
  • @DownPW said in [NODEBB] Help for my custom CSS:

    You talk about making it dynamic but I donā€™t really see what you mean since the theme colors represented do not change.
    Unless you want to automate it. Genre detects the 2 main color types of the theme according to 2 values/declarations present in the css file.

    Simple enough -you just reference the variables, but admittedly, youā€™d need to refactor some code to make this work transiently, so itā€™s probably not a very good idea. I suppose it really depends on how often you decide to revamp your themes - in which case, it would make sense.

    I just donā€™t like static variables šŸ™‚


Did this solution help you?
Did you find the suggested solution useful? Why not buy me a coffee? It's a nice gesture, and a great way to show your appreciation šŸ’—

Related Topics