Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for turbolinks; Allow periodic checks while scrolling #15

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 48 additions & 2 deletions app/assets/javascripts/jquery.infinite-pages.js.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ Released under the MIT License
(($, window) ->
# Define the plugin class
class InfinitePages
# Internal id to tracking the elements used for multiple instances
@_ID: 0

# Internal helper to return a unique id for a new instance for the page
@_nextId: ->
@_ID += 1
@_ID

# Default settings
defaults:
Expand All @@ -35,20 +42,39 @@ Released under the MIT License
@$container = $(container)
@$table = $(container).find('table')
@$context = $(@options.context)
@instanceId = @constructor._nextId()
@invalidateAt = +new Date # timestamp before which we ignore responses
@requestAts = [] # list of timestamps for pending requests
@init()

# Setup and bind to related events
init: ->

# Debounce scroll event to improve performance
scrollDelay = 250
scrollTimeout = null
scrollHandler = (=> @check())
lastCheckAt = null
scrollHandler = =>
lastCheckAt = +new Date
@check()

# Have we waited enough time since the last check?
shouldCheck = -> +new Date > lastCheckAt + scrollDelay

@$context.scroll ->
scrollHandler() if shouldCheck() # Call check once every scrollDelay ms
if scrollTimeout
clearTimeout(scrollTimeout)
scrollTimeout = null
scrollTimeout = setTimeout(scrollHandler, 250)
scrollTimeout = setTimeout(scrollHandler, scrollDelay)

# Set a data attribute so we can find again after a turbolink page is loaded

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line exceeds maximum allowed length

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line exceeds maximum allowed length

@$container.attr('data-jquery-infinite-pages-container', @instanceId)

# Setup callbacks to handle turbolinks page loads
$(window.document)
.on("page:before-unload", => @_invalidateActiveRequests())
.on("page:change", => @_recache())

# Internal helper for logging messages
_log: (msg) ->
Expand Down Expand Up @@ -80,6 +106,7 @@ Released under the MIT License
else
@_loading()

@requestAts.push +new Date # note when this request started
$.getScript(@$container.find(@options.navSelector).attr('href'))
.done(=> @_success())
.fail(=> @_error())
Expand All @@ -91,12 +118,17 @@ Released under the MIT License
@$container.find(@options.navSelector).each(@options.loading)

_success: ->
# ignore any requests that started before we last invalidated
return if @_isInvalidatedRequest(@requestAts.shift())
@_recache()
@options.state.loading = false
@_log "New page loaded!"
if typeof @options.success is 'function'
@$container.find(@options.navSelector).each(@options.success)

_error: ->
# ignore any requests that started before we last invalidated
return if @_isInvalidatedRequest(@requestAts.shift())
@options.state.loading = false
@_log "Error loading new page :("
if typeof @options.error is 'function'
Expand All @@ -113,6 +145,20 @@ Released under the MIT License
@_log "Scroll checks resumed"
@check()

_recache: ->
# Recache the element references we use (needed when using turbolinks)
@$container = $("[data-jquery-infinite-pages-container=#{@instanceId}]")
@$table = @$container.find('table')
@$context = $(@options.context)

_invalidateActiveRequests: ->
# Invalidate any active requests (needed when using turbolinks)
@invalidateAt = +new Date

_isInvalidatedRequest: (requestAt) ->
# Check to see if a request was invalidated
requestAt < @invalidateAt

# Define the plugin
$.fn.extend infinitePages: (option, args...) ->
@each ->
Expand Down