Sha256: 1d065e0a269dc473d9ad64b71998c905861475e2f498eb7a8223fa3e9aaf8073

Contents?: true

Size: 1.11 KB

Versions: 4

Compression:

Stored size: 1.11 KB

Contents

(() => {
  const application = Stimulus.Application.start()

  application.register("infinite-scroll", class extends Stimulus.Controller {
    static get targets() {
      return ["link"]
    }

    connect() {
      // Disable scroll event listeners
      $(window).off('scroll.infiniteScroll')
      $('#overlay section').off('scroll.infiniteScroll')

      let $link = $(this.linkTarget)

      // If the link has an actual href
      if ($link.attr('href').length > 0) {
        // If the window scrolls
        $(window).on('scroll.infiniteScroll', this.loadNextPage($link))

        // If the overlay scrolls
        $('#overlay section').on('scroll.infiniteScroll', function() {
          $(window).trigger('scroll.infiniteScroll')
        })

        $(window).trigger('scroll.infiniteScroll')
      }
    }

    loadNextPage(link) {
      if ($(window).scrollTop() > link.offset().top - $(window).height() - 500) {
        $(window).off('scroll.infiniteScroll')
        $('#overlay section').off('scroll.infiniteScroll')
        $.rails.disableElement(link)
        $.getScript(link.attr('href'))
      }
    }
    
  })
})()

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
spina-1.0.3 app/assets/javascripts/spina/admin/controllers/infinite_scroll_controller.js
spina-1.0.2 app/assets/javascripts/spina/admin/controllers/infinite_scroll_controller.js
spina-1.0.1 app/assets/javascripts/spina/admin/controllers/infinite_scroll_controller.js
spina-1.0.0 app/assets/javascripts/spina/admin/controllers/infinite_scroll_controller.js