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

Update jquery.infinite-pages.js.coffee for turbolinks #13

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all 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
51 changes: 26 additions & 25 deletions app/assets/javascripts/jquery.infinite-pages.js.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Released under the MIT License
(($, window) ->
# Define the plugin class
class InfinitePages

# Default settings
defaults:
debug: false # set to true to log messages to the console
Expand All @@ -25,44 +25,44 @@ Released under the MIT License
state:
paused: false
loading: false

# Constructs the new InfinitePages object
#
# container - the element containing the infinite table and pagination links
constructor: (container, options) ->
@options = $.extend({}, @defaults, options)
@$container = $(container)
@container = '.'+container.className;
Copy link
Contributor

Choose a reason for hiding this comment

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

This introduces new dependencies on the container argument. With your change it look like it has to be a DOM element with a class name instead of a jQuery CSS selector

Copy link
Author

Choose a reason for hiding this comment

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

Your right, it would take more work to make it a generic selector.

@$table = $(container).find('table')

@init()

# Setup and bind to related events
init: ->

# Debounce scroll event to improve performance
scrollTimeout = null
scrollHandler = (=> @check())

$(window).scroll ->
if scrollTimeout
clearTimeout(scrollTimeout)
scrollTimeout = null
scrollTimeout = setTimeout(scrollHandler, 250)

# Internal helper for logging messages
_log: (msg) ->
console?.log(msg) if @options.debug

# Check the distance of the nav selector from the bottom of the window and fire
# load event if close enough
check: ->
nav = @$container.find(@options.navSelector)
nav = $(@container).find(@options.navSelector)
if nav.size() == 0
@_log "No more pages to load"
else
windowBottom = $(window).scrollTop() + $(window).height()
distance = nav.offset().top - windowBottom

if @options.state.paused
@_log "Paused"
else if @options.state.loading
Expand All @@ -71,56 +71,57 @@ Released under the MIT License
@_log "#{distance - @options.buffer}px remaining..."
else
@next() # load the next page

# Load the next page
next: ->
if @options.state.done
@_log "Loaded all pages"
else
@_loading()

$.getScript(@$container.find(@options.navSelector).attr('href'))
$.getScript($(@container).find(@options.navSelector).attr('href'))
.done(=> @_success())
.fail(=> @_error())

_loading: ->
@options.state.loading = true
@_log "Loading next page..."
if typeof @options.loading is 'function'
@$container.find(@options.navSelector).each(@options.loading)
$(@container).find(@options.navSelector).each(@options.loading)

_success: ->
@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: ->
@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: ->
@options.state.paused = true
@_log "Scroll checks paused"

# Resume firing of events on scroll
resume: ->
@options.state.paused = false
@_log "Scroll checks resumed"
@check()

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

@each ->
$this = $(this)
data = $this.data('infinitepages')

if !data

$this.data 'infinitepages', (data = new InfinitePages(this, option))
if typeof option == 'string'
data[option].apply(data, args)

) window.jQuery, window