Skip to Content

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.

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.ga !== 'function' ){
        window.ga = function(){}; //Prevent ga() 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', 'nofollow external 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.

    //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.html.addClass('device-memory-' + deviceMemoryLevel);
    }

    //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', '0px'); //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);
        }
    });

    //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?

            //Close modals
            jQuery('.modal').modal('hide');
        }
    });

    //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.

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