-
Notifications
You must be signed in to change notification settings - Fork 53
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
cesmoak
wants to merge
7
commits into
magoosh:master
Choose a base branch
from
cesmoak:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
b17e240
Add support for turbolinks
cesmoak d3c1c83
Allow periodic checks while scrolling
cesmoak 5918425
Fix bug so that check calls are limited
cesmoak ad103be
Update README.md to note what this fork changes
cesmoak 8938043
Simplify turbolinks handling
cesmoak 7ebd704
Fix typo from _nextId to _nextInstanceId
cesmoak 18a9495
Remove old broken code
cesmoak File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,12 @@ Released under the MIT License | |
(($, window) -> | ||
# Define the plugin class | ||
class InfinitePages | ||
# Internal id to tracking the elements used for multiple instances | ||
@_INSTANCE_ID: 0 | ||
|
||
# Internal helper to return a unique id for a new instance for the page | ||
@_nextInstanceId: -> | ||
@_INSTANCE_ID += 1 | ||
|
||
# Default settings | ||
defaults: | ||
|
@@ -35,20 +41,18 @@ Released under the MIT License | |
@$container = $(container) | ||
@$table = $(container).find('table') | ||
@$context = $(@options.context) | ||
@instanceId = @constructor._nextId() | ||
@init() | ||
|
||
# Setup and bind to related events | ||
init: -> | ||
@_listenForScrolling() | ||
|
||
# Debounce scroll event to improve performance | ||
scrollTimeout = null | ||
scrollHandler = (=> @check()) | ||
# Set a data attribute so we can find again after a turbolink page is loaded | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
||
@$context.scroll -> | ||
if scrollTimeout | ||
clearTimeout(scrollTimeout) | ||
scrollTimeout = null | ||
scrollTimeout = setTimeout(scrollHandler, 250) | ||
# Setup the callback to handle turbolinks page loads | ||
$(window.document).on("page:change", => @_recache()) | ||
|
||
# Internal helper for logging messages | ||
_log: (msg) -> | ||
|
@@ -80,27 +84,33 @@ Released under the MIT License | |
else | ||
@_loading() | ||
|
||
$container = @$container | ||
@requestAts.push +new Date # note when this request started | ||
$.getScript(@$container.find(@options.navSelector).attr('href')) | ||
.done(=> @_success()) | ||
.fail(=> @_error()) | ||
.done(=> @_success($container)) | ||
.fail(=> @_error($container)) | ||
|
||
_loading: -> | ||
@options.state.loading = true | ||
@_log "Loading next page..." | ||
if typeof @options.loading is 'function' | ||
@$container.find(@options.navSelector).each(@options.loading) | ||
|
||
_success: -> | ||
_success: ($container) -> | ||
# ignore any requests for elements that are no longer on the page | ||
return unless $.contains(document, $container[0]) | ||
@options.state.loading = false | ||
@_log "New page loaded!" | ||
if typeof @options.success is 'function' | ||
@$container.find(@options.navSelector).each(@options.success) | ||
$container.find(@options.navSelector).each(@options.success) | ||
|
||
_error: -> | ||
_error: ($container) -> | ||
# ignore any requests for elements that are no longer on the page | ||
return unless $.contains(document, $container[0]) | ||
@options.state.loading = false | ||
@_log "Error loading new page :(" | ||
if typeof @options.error is 'function' | ||
@$container.find(@options.navSelector).each(@options.error) | ||
$container.find(@options.navSelector).each(@options.error) | ||
|
||
# Pause firing of events on scroll | ||
pause: -> | ||
|
@@ -113,6 +123,44 @@ Released under the MIT License | |
@_log "Scroll checks resumed" | ||
@check() | ||
|
||
_recache: -> | ||
# remove the existing scroll listener | ||
@$context.off("scroll") | ||
|
||
# Recache the element references we use (needed when using turbolinks) | ||
@$container = $("[data-jquery-infinite-pages-container=#{@instanceId}]") | ||
@$table = @$container.find('table') | ||
@$context = $(@options.context) | ||
|
||
@_listenForScrolling() | ||
|
||
_listenForScrolling: -> | ||
# Debounce scroll event to improve performance | ||
scrollDelay = 250 | ||
scrollTimeout = null | ||
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, scrollDelay) | ||
|
||
_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 -> | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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