Sha256: 548f5d12f6125f35ed3e431923af083a88777813482a09edb846380281c783f9

Contents?: true

Size: 1.74 KB

Versions: 41

Compression:

Stored size: 1.74 KB

Contents

; (function () {
  // Tell the browser not to handle scrolling when restoring via the history or
  // when reloading
  if ('scrollRestoration' in history) {
    history.scrollRestoration = 'manual'
  }

  var SCROLL_POSITION = 'scroll-position'
  var PAGE_INVALIDATED = 'page-invalidated'

  // Persist the scroll position on refresh
  addEventListener('beforeunload', function () {
    sessionStorage.setItem(SCROLL_POSITION, JSON.stringify(scrollData()))
  });

  // Invalidate the page when the next page is different from the current page
  // Persist scroll information across pages
  document.addEventListener('turbolinks:before-visit', function (event) {
    if (event.data.url !== location.href) {
      sessionStorage.setItem(PAGE_INVALIDATED, 'true')
    }
    sessionStorage.setItem(SCROLL_POSITION, JSON.stringify(scrollData()))
  })

  // When a page is fully loaded:
  // 1. Get the persisted scroll position
  // 2. If the locations match and the load did not originate from a page
  // invalidation,
  // 3. scroll to the persisted position if there, or to the top otherwise
  // 4. Remove the persisted information
  addEventListener('turbolinks:load', function (event) {
    var scrollPosition = JSON.parse(sessionStorage.getItem(SCROLL_POSITION))

    if (shouldScroll(scrollPosition)) {
      scrollTo(scrollPosition.scrollX, scrollPosition.scrollY)
    } else {
      scrollTo(0, 0)
    }
    sessionStorage.removeItem(PAGE_INVALIDATED)
  });

  function shouldScroll(scrollPosition) {
    return (scrollPosition
      && scrollPosition.location === location.href
      && !JSON.parse(sessionStorage.getItem(PAGE_INVALIDATED)))
  }

  function scrollData() {
    return {
      scrollX: scrollX,
      scrollY: scrollY,
      location: location.href
    }
  }
})()

Version data entries

41 entries across 41 versions & 2 rubygems

Version Path
spree_frontend-4.1.15 app/assets/javascripts/spree/frontend/turbolinks_scroll_fix.js
spree_frontend-4.2.7 app/assets/javascripts/spree/frontend/turbolinks_scroll_fix.js
spree_frontend-4.3.3 app/assets/javascripts/spree/frontend/turbolinks_scroll_fix.js
spree_frontend-4.2.6 app/assets/javascripts/spree/frontend/turbolinks_scroll_fix.js
spree_frontend-4.3.2 app/assets/javascripts/spree/frontend/turbolinks_scroll_fix.js
spree_frontend-4.4.0 app/assets/javascripts/spree/frontend/turbolinks_scroll_fix.js
spree_frontend-4.3.1 app/assets/javascripts/spree/frontend/turbolinks_scroll_fix.js
spree_frontend-4.3.0 app/assets/javascripts/spree/frontend/turbolinks_scroll_fix.js
spree_frontend-4.3.0.rc3 app/assets/javascripts/spree/frontend/turbolinks_scroll_fix.js
spree_frontend-4.3.0.rc2 app/assets/javascripts/spree/frontend/turbolinks_scroll_fix.js
spree_frontend-4.3.0.rc1 app/assets/javascripts/spree/frontend/turbolinks_scroll_fix.js
harpiya_frontend-4.3.0.alpha app/assets/javascripts/harpiya/frontend/turbolinks_scroll_fix.js
spree_frontend-4.2.5 app/assets/javascripts/spree/frontend/turbolinks_scroll_fix.js
spree_frontend-4.1.14 app/assets/javascripts/spree/frontend/turbolinks_scroll_fix.js
spree_frontend-4.2.4 app/assets/javascripts/spree/frontend/turbolinks_scroll_fix.js
spree_frontend-4.2.3.1 app/assets/javascripts/spree/frontend/turbolinks_scroll_fix.js
spree_frontend-4.1.13.1 app/assets/javascripts/spree/frontend/turbolinks_scroll_fix.js
spree_frontend-4.2.3 app/assets/javascripts/spree/frontend/turbolinks_scroll_fix.js
spree_frontend-4.2.2 app/assets/javascripts/spree/frontend/turbolinks_scroll_fix.js
spree_frontend-4.2.1 app/assets/javascripts/spree/frontend/turbolinks_scroll_fix.js