Sha256: 365766ef6175ec8f3d5f759709be1b9b4533de21a2ab0fc81245c87a9633e75a

Contents?: true

Size: 1.52 KB

Versions: 5

Compression:

Stored size: 1.52 KB

Contents

(() => {
  const isTurbolinks = 'Turbolinks' in window && window.Turbolinks.supported;
  const isTurbolinks5 = isTurbolinks && 'clearCache' in window.Turbolinks;

  let onPageLoadFiredOnce = false;
  const pageLoadCallbacks = [];
  const triggerOnPageLoad = () => {
    pageLoadCallbacks.forEach((callback) => {
      callback();
    });
    onPageLoadFiredOnce = true;
  };

  window.Thredded = window.Thredded || {};

  // Fires the callback on DOMContentLoaded or a Turbolinks page load.
  // If called from an async script on the first page load, and the DOMContentLoad event
  // has already fired, will execute the callback immediately.
  window.Thredded.onPageLoad = (callback) => {
    pageLoadCallbacks.push(callback);
    // With async script loading, a callback may be added after the DOMContentLoaded event has already triggered.
    // This means we will receive neither a DOMContentLoaded event, nor a turbolinks:load event on Turbolinks 5.
    if (!onPageLoadFiredOnce && window.Thredded.DOMContentLoadedFired) {
      callback();
    }
  };

  if (isTurbolinks5) {
    document.addEventListener('turbolinks:load', () => {
      triggerOnPageLoad();
    });
  } else {
    // Turbolinks Classic (with or without jQuery.Turbolinks), or no Turbolinks:
    if (!window.Thredded.DOMContentLoadedFired) {
      document.addEventListener('DOMContentLoaded', () => {
        triggerOnPageLoad();
      });
    }
    if (isTurbolinks) {
      document.addEventListener('page:load', () => {
        triggerOnPageLoad();
      })
    }
  }
})();

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
thredded-0.9.3 app/assets/javascripts/thredded/core/on_page_load.es6
thredded-0.9.2 app/assets/javascripts/thredded/core/on_page_load.es6
thredded-0.9.1 app/assets/javascripts/thredded/core/on_page_load.es6
thredded-0.8.4 app/assets/javascripts/thredded/core/on_page_load.es6
thredded-0.8.2 app/assets/javascripts/thredded/core/on_page_load.es6