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

This PR is a consolidation of other PRs ..! #35

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@

.idea
Gemfile.lock
*.gem
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
Ch-Ch-Ch-Changes
================

#### 0.2.3 (September 6, 2019)

* Added unpulled pull requests on main repo (except turbolinks related)
* Small code tidy

#### 0.2.0 (February 12, 2015)

* Add context option to define scrolling container (thanks @pdw207)
Expand Down
37 changes: 29 additions & 8 deletions app/assets/javascripts/jquery.infinite-pages.js.coffee
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
###
jQuery Infinite Pages v0.2.0
jQuery Infinite Pages v0.2.3
https://github.com/magoosh/jquery-infinite-pages

Released under the MIT License
Expand All @@ -19,6 +19,7 @@ Released under the MIT License
debug: false # set to true to log messages to the console
navSelector: 'a[rel=next]'
buffer: 1000 # 1000px buffer by default
debounce: 250 # 250ms debounce by default
loading: null # optional callback when next-page request begins
success: null # optional callback when next-page request finishes
error: null # optional callback when next-page request fails
Expand All @@ -33,7 +34,6 @@ Released under the MIT License
constructor: (container, options) ->
@options = $.extend({}, @defaults, options)
@$container = $(container)
@$table = $(container).find('table')
@$context = $(@options.context)
@init()

Expand All @@ -43,12 +43,14 @@ Released under the MIT License
# Debounce scroll event to improve performance
scrollTimeout = null
scrollHandler = (=> @check())
debounce = @options.debounce

@$context.scroll ->
if scrollTimeout
# Use namespace to let us unbind event handler
@$context.on 'scroll.infinitePages', ->
if scrollTimeout && self.active
clearTimeout(scrollTimeout)
scrollTimeout = null
scrollTimeout = setTimeout(scrollHandler, 250)
scrollTimeout = setTimeout(scrollHandler, debounce)

# Internal helper for logging messages
_log: (msg) ->
Expand All @@ -58,7 +60,7 @@ Released under the MIT License
# load event if close enough
check: ->
nav = @$container.find(@options.navSelector)
if nav.size() == 0
if nav.length == 0
@_log "No more pages to load"
else
windowBottom = @$context.scrollTop() + @$context.height()
Expand All @@ -80,7 +82,7 @@ Released under the MIT License
else
@_loading()

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

Expand All @@ -92,6 +94,7 @@ Released under the MIT License

_success: ->
@options.state.loading = false
@jqXHR = null
@_log "New page loaded!"
if typeof @options.success is 'function'
@$container.find(@options.navSelector).each(@options.success)
Expand All @@ -112,6 +115,19 @@ Released under the MIT License
@options.state.paused = false
@_log "Scroll checks resumed"
@check()

stop: ->
@$context.off 'scroll.infinitePages'
@_log "Scroll checks stopped"

# Abort loading of the page
abort: ->
if @jqXHR
@jqXHR.abort()
@jqXHR = null
@_log "Page load aborted!"
else
@_log "There was no request to abort"

# Define the plugin
$.fn.extend infinitePages: (option, args...) ->
Expand All @@ -122,6 +138,11 @@ Released under the MIT License
if !data
$this.data 'infinitepages', (data = new InfinitePages(this, option))
if typeof option == 'string'
data[option].apply(data, args)
if option == 'destroy'
data.stop args
else if option == 'reinit'
data.init args
else
data[option].apply(data, args)

) window.jQuery, window
2 changes: 1 addition & 1 deletion lib/jquery/infinite_pages/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module JqueryInfinitePages
VERSION = "0.2.0"
VERSION = "0.2.3"
end