Sha256: 331526219bf20a13a9f0ee35b6f017330e1ab9d8fff1e2ce2e34c8e27fc8db28

Contents?: true

Size: 1.91 KB

Versions: 39

Compression:

Stored size: 1.91 KB

Contents

//= require thredded/core/thredded

(() => {
  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;
  };

  // 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) {
    // In Turbolinks 5.0.1, turbolinks:load may have already fired (before DOMContentLoaded).
    // If so, add our own DOMContentLoaded listener:
    // See: https://github.com/turbolinks/turbolinks/commit/69d353ea73d10ee6b25c2866fc5706879ba403e3
    if (window.Turbolinks.controller.lastRenderedLocation) {
      document.addEventListener('DOMContentLoaded', () => {
        triggerOnPageLoad();
      });
    }
    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

39 entries across 39 versions & 2 rubygems

Version Path
thredded-1.1.0 app/assets/javascripts/thredded/core/on_page_load.es6
thredded-1.0.1 app/assets/javascripts/thredded/core/on_page_load.es6
thredded-1.0.0 app/assets/javascripts/thredded/core/on_page_load.es6
thredded-0.16.16 app/assets/javascripts/thredded/core/on_page_load.es6
thredded-0.16.15 app/assets/javascripts/thredded/core/on_page_load.es6
thredded-0.16.14 app/assets/javascripts/thredded/core/on_page_load.es6
thredded-0.16.13 app/assets/javascripts/thredded/core/on_page_load.es6
thredded-0.16.12 app/assets/javascripts/thredded/core/on_page_load.es6
thredded-0.16.11 app/assets/javascripts/thredded/core/on_page_load.es6
thredded-0.16.10 app/assets/javascripts/thredded/core/on_page_load.es6
thredded-0.16.9 app/assets/javascripts/thredded/core/on_page_load.es6
thredded-0.16.8 app/assets/javascripts/thredded/core/on_page_load.es6
thredded-0.16.7 app/assets/javascripts/thredded/core/on_page_load.es6
thredded-0.16.6 app/assets/javascripts/thredded/core/on_page_load.es6
thredded-0.16.5 app/assets/javascripts/thredded/core/on_page_load.es6
thredded-0.16.4 app/assets/javascripts/thredded/core/on_page_load.es6
thredded-0.16.3 app/assets/javascripts/thredded/core/on_page_load.es6
thredded-0.16.1 app/assets/javascripts/thredded/core/on_page_load.es6
thredded-0.16.0 app/assets/javascripts/thredded/core/on_page_load.es6
thredded-0.15.5 app/assets/javascripts/thredded/core/on_page_load.es6