Skip to Content
Menu

helpers()

Adds helper classes (and other attributes) to appropriate elements as well as other helpful functionality.

JavaScript February 8, 2021

Usage

This function runs automatically, so it is not called manually. Is this incorrect?

Additional Notes

Currently this function does the following:

  • Adds rel=”nofollow external” to outbound links
  • Adds “no-icon” class to images within a tags
  • Adds “no-icon” class to child a tags whose parent also has the class.

This function used to add classes such as “last-child”, “first-child”, “odd”, and “even” to appropriate elements, but has been removed since support for old browsers who needed them has been dropped.

Was this page helpful? Yes No


    A feedback message is required to submit this form.


    Please check that you have entered a valid email address.

    Enter your email address if you would like a response.

    Thank you for your feedback!

    Source File

    Located in /assets/js/modules/helpers.js on line 4.

    1 Hook

    Find these filters and actions in the source code below to hook into them. Use wp.hooks.doAction() and wp.hooks.addFilter() in your JavaScript file.

    Filters
    "nebulaChosenOptions"
    Need a new filter hook? Request one here.

    Actions
    This function has no action hooks available. Request one?

    JavaScript
    nebula.helpers = async function(){
        if ( typeof window.gtag !== 'function' ){
            window.gtag = function(){}; //Prevent gtag() calls from erroring if GA is off or blocked. This is supplemental to a similar check in analytics.php
        }
    
        //Remove Sass render trigger query
        if ( nebula.get('sass') && !nebula.get('persistent') ){
            window.history.replaceState({}, document.title, nebula.removeQueryParameter('sass', window.location.href));
        }
    
        //Empty caches when debugging
        if ( nebula.get('debug') || nebula.dom.html.hasClass('debug') ){
            nebula.emptyCaches(); //Clear the caches
        }
    
        nebula.dom.html.removeClass('no-js').addClass('js');
        jQuery("a[href^='http']:not([href*='" + nebula.site.domain + "'])").attr('rel', 'external noreferrer noopener'); //Add rel attributes to external links. Although search crawlers do use JavaScript, don't rely on this line to instruct them. Use standard HTML attributes whenever possible.
    
        //Add general region classes (Note: not done in location.js because it is anonymized and global)
        jQuery('body').addClass('locale-' + Intl.DateTimeFormat().resolvedOptions().locale.split('-').pop().toLowerCase());
        jQuery('body').addClass('timezone-' + Intl.DateTimeFormat().resolvedOptions().timeZone.replaceAll(/[_\/]/gi, '-').toLowerCase());
    
        //Note the level of RAM available for a "lite" or "full" experience
        if ( 'deviceMemory' in navigator ){ //Device Memory - Chrome 64+
            let deviceMemoryLevel = ( navigator.deviceMemory < 1 )? 'lite' : 'full'; //Possible values (GB of RAM): 0.25, 0.5, 1, 2, 4, 8
            nebula.dom.body.addClass('device-memory-' + deviceMemoryLevel);
        }
    
        //Skip to Content button clicks - skip to the content section
        jQuery('#skip-to-content-link').on('click', function(){
            nebula.focusOnElement(jQuery('#content-section'));
        });
    
        //Remove filetype icons from images within <a> tags and buttons. Note: these contribute to CLS because they are not animations
        jQuery('a img').closest('a').addClass('no-icon');
        jQuery('.no-icon:not(a)').find('a').addClass('no-icon');
    
        jQuery('span.nebula-code').parent('p').css('margin-bottom', '0'); //Fix for <p> tags wrapping Nebula pre spans in the WYSIWYG
    
        //Maintain tab navigability on hashchange (and when loaded with a hash). This also helps accessibility for things like skip to content links
        if ( document.location.hash ){
            nebula.focusOnElement(jQuery(document.location.hash));
        }
    
        //If the hash has been changed (activation of an in-page link)
        nebula.dom.window.on('hashchange', function(){
            let hash = window.location.hash.replace(/^#/, '');
            if ( hash ){ //If the hash is not empty (like when clicking on an href="#" link)
                nebula.focusOnElement(jQuery('#' + hash));
            }
        });
    
        //Change the Bootstrap label for custom file upload inputs on upload
        jQuery('input[type="file"].custom-file-input').on('change', function(){
            if ( jQuery(this).parents('.custom-file').find('.custom-file-label').length ){
                let fileName = jQuery(this).val().split('\\').pop(); //Get the filename without the full path
                jQuery(this).parents('.custom-file').find('.custom-file-label').text(fileName);
            }
        });
    
        //Add Bootstrap form control to WP search block
        jQuery('.wp-block-search__input').addClass('form-control');
        jQuery('.wp-block-search__button').addClass('btn btn-primary');
    
        //Deactivate potential active states when the escape key is pressed
        nebula.dom.document.on('keydown', function(e){
            if ( e.key === 'Escape' ){
                nebula.dom.document.trigger('esc'); //Trigger a simpler DOM event. Is this helpful?
                jQuery('.modal').modal('hide'); //Close modals
            }
        });
    
        //Nebula preferred default Chosen.js options
        nebula.chosenOptions = wp.hooks.applyFilters('nebulaChosenOptions', {
            disable_search_threshold: 5,
            search_contains: true,
            no_results_text: 'No results found.',
            allow_single_deselect: true,
            width: '100%'
        });
    
        nebula.dragDropUpload();
    
        //Remove this once QM allows sortable Timings table
        if ( jQuery('#qm-timing').length ){
            nebula.qmSortableHelper(); //Temporary QM helper.
        }
    };
    

    Override

    To override or disable this JavaScript function, simply redeclare it with the exact same function name. Remember: Some functionality is conditionally loaded via dynamic imports, so if your function is not overriding properly, try listening for a DOM event (described below).

    JavaScript

    For non-module import functions:

    nebula.helpers = function(){
        //Write your own code here, leave it blank, or return false.
    }


    For dynamically imported module function overrides:

    jQuery(window).on('load', function(){
        nebula.helpers = function(){
            //Write your own code here, leave it blank, or return false.
        }
    });


    Custom Nebula DOM events do also exist, so you could also try the following if the Window Load listener does not work:

    jQuery(document).on('nebula_module_loaded', function(module){
        //Note that the module variable is also available to know which module specifically was imported
        if ( typeof nebula.helpers === 'function' ){
            nebula.helpers = function(){
                //Write your own code here, leave it blank, or return false.
            }
    	}
    });