jquery-infinite-pages 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ NjczYTQwYjA4ZjRlNGFhMjBkZGY3Mzg0YTY4ZWFiMDQyMjNjZjU4ZA==
5
+ data.tar.gz: !binary |-
6
+ MzczYWYzN2Q3MzYzYTNhMTBhYTA0ZWVkYzhmODUxNmQ3OWZjNmVlNg==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ OTljMjI0OGYyYmVkNTM1ZDNjZWYyZDI3Y2U4YjczMWRkOTAxZDM2NmYyZDdm
10
+ Yjk0YTg2YmExMzIxYTFiOTdjMzY4MDJkOWQ3OGE4OGViMGQyMTI5ZDQ5YjU4
11
+ M2Q0YjQxNmIzMjE2ZTE5YmYyYzAxMDdiMWQ2NTlmZjUzMGQxNTE=
12
+ data.tar.gz: !binary |-
13
+ MmRlYzE0ZjM5Y2M1NzNiNGYwMDQyNTQ1MTYxNjY3MWRjMzQ5YjFkOGQwZmJk
14
+ OGM2MzU3NjIzYWY5ZGVkZTcyNmNkMjFmNjk5MzY3ZDU1NjY5ZWFhMjAwNDg2
15
+ MGVjNjc4NDI4M2E3ZjAzNWQxMzI2OGI0MTM1NjgwZDlkZWYyNWI=
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License
2
+
3
+ Copyright (c) 2014 Magoosh, Inc. http://magoosh.com
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,149 @@
1
+ jQuery Infinite Pages
2
+ =====================
3
+
4
+ A light-weight jQuery plugin for adding infinite scrolling to paginated HTML views
5
+ that tastes great with [Rails](https://github.com/rails/rails) and
6
+ [Kaminari](https://github.com/amatsuda/kaminari).
7
+
8
+ This project was originally designed for Rails, so it's wrapped in a gem for easy
9
+ installation. However, the core plugin is flexible enough to use anywhere.
10
+
11
+ Usage
12
+ -----
13
+ jQuery Infinite Pages binds to an element containing a `rel="next"` pagination link and
14
+ watches for scroll events.
15
+
16
+ When the link is close to the bottom of the screen, an async request to the next page
17
+ is triggered. The server's response should then append the new page and update the
18
+ pagination link.
19
+
20
+ ```coffeescript
21
+ # Setup plugin and define optional event callbacks
22
+ $('.infinite-table').infinitePages
23
+ debug: true
24
+ buffer: 200 # load new page when within 200px of nav link
25
+ loading: ->
26
+ # jQuery callback on the nav element
27
+ $(this).text("Loading...")
28
+ success: ->
29
+ # called after successful ajax call
30
+ error: ->
31
+ # called after failed ajax call
32
+ $(this).text("Trouble! Please drink some coconut water and click again")
33
+ ```
34
+
35
+ You can also manually control the firing of load events:
36
+
37
+ ```
38
+ # Force load of the next page
39
+ $('.infinite-table').infinitePages('next')
40
+
41
+ # Pause firing of events on scroll
42
+ $('.infinite-table').infinitePages('pause')
43
+
44
+ # Resume...
45
+ $('.infinite-table').infinitePages('resume')
46
+ ```
47
+
48
+ Installation
49
+ ------------
50
+
51
+ Add this line to your application's Gemfile:
52
+
53
+ ```ruby
54
+ gem 'jquery-infinite-pages', :git => 'git://github.com/magoosh/jquery-infinite-pages.git'
55
+ ```
56
+
57
+ And then execute:
58
+
59
+ ```
60
+ bundle install
61
+ ```
62
+
63
+ Add to your `application.js`:
64
+
65
+ ```javascript
66
+ //= require jquery.infinite-pages
67
+ ```
68
+
69
+ ### Non-Rails
70
+
71
+ Just copy the `jquery.infinite-pages.js.coffee` file from `app/assets/javascripts` to
72
+ wherever you want it.
73
+
74
+ Rails/Kaminari Example
75
+ ----------------------
76
+
77
+ The following is an example of how to integrate this plugin into your Rails app
78
+ using Kaminari.
79
+
80
+ Set up pagination in `lessons_controller.rb`:
81
+
82
+ ```ruby
83
+ class LessonsController
84
+ def index
85
+ @lessons = Lesson.order('lessons.name ASC').page(params[:page])
86
+ end
87
+ end
88
+ ```
89
+
90
+ Write the template for your list of lessons in `index.html.erb`:
91
+
92
+ ```erb
93
+ <div class="infinite-table">
94
+ <table>
95
+ <tr>
96
+ <th>Lesson</th>
97
+ <th></th>
98
+ </tr>
99
+ <%= render :partial => 'lessons', :object => @lessons %>
100
+ </table>
101
+ <p class="pagination">
102
+ <%= link_to_next_page(@lessons, 'Next Page', :remote => true))%>
103
+ </p>
104
+ </div>
105
+ ```
106
+
107
+ ...and `_lessons.html.erb`:
108
+
109
+ ```erb
110
+ <% @lessons.each do |lesson| %>
111
+ <tr>
112
+ <td><%= lesson.name %> (<%= lesson.length.format %>)</td>
113
+ <td><%= link_to "watch", lesson_path(lesson) %></td>
114
+ </tr>
115
+ <% end %>
116
+ ```
117
+
118
+ Append new data to the table in `index.js.erb`:
119
+
120
+ ```javascript
121
+ // Append new data
122
+ $("<%=j render(:partial => 'lessons', :object => @lessons) %>")
123
+ .appendTo($(".infinite-table table"));
124
+
125
+ // Update pagination link
126
+ <% if answers.last_page? %>
127
+ $('.pagination').html("That's all, folks!");
128
+ <% else %>
129
+ $('.pagination')
130
+ .html("<%=j link_to_next_page(@lessons, 'Next Page', :remote => true))%>");
131
+ <% end %>
132
+ ```
133
+
134
+ At this point, the pagination link at the bottom of your table should allow you
135
+ to load the next page without refreshing. Finally, we trigger the next page load
136
+ with the `infinitePages` plugin in `lessons.js.coffee`:
137
+
138
+ ```coffee
139
+ $ ->
140
+ # Configure infinite table
141
+ $('.infinite-table').infinitePages
142
+ # debug: true
143
+ loading: ->
144
+ $(this).text('Loading next page...')
145
+ error: ->
146
+ $(this).button('There was an error, please try again')
147
+ ```
148
+
149
+ Voila! You should now have an infinitely long list of lessons.
@@ -0,0 +1,126 @@
1
+ ###
2
+ jQuery Infinite Pages v0.1.1
3
+ https://github.com/magoosh/jquery-infinite-pages
4
+
5
+ Released under the MIT License
6
+ ###
7
+
8
+ #
9
+ # Built with a class-based template for jQuery plugins in Coffeescript:
10
+ # https://gist.github.com/rjz/3610858
11
+ #
12
+
13
+ (($, window) ->
14
+ # Define the plugin class
15
+ class InfinitePages
16
+
17
+ # Default settings
18
+ defaults:
19
+ debug: false # set to true to log messages to the console
20
+ navSelector: 'a[rel=next]'
21
+ buffer: 1000 # 1000px buffer by default
22
+ loading: null # optional callback when next-page request begins
23
+ success: null # optional callback when next-page request finishes
24
+ error: null # optional callback when next-page request fails
25
+ state:
26
+ paused: false
27
+ loading: false
28
+
29
+ # Constructs the new InfinitePages object
30
+ #
31
+ # container - the element containing the infinite table and pagination links
32
+ constructor: (container, options) ->
33
+ @options = $.extend({}, @defaults, options)
34
+ @$container = $(container)
35
+ @$table = $(container).find('table')
36
+
37
+ @init()
38
+
39
+ # Setup and bind to related events
40
+ init: ->
41
+
42
+ # Debounce scroll event to improve performance
43
+ scrollTimeout = null
44
+ scrollHandler = (=> @check())
45
+
46
+ $(window).scroll ->
47
+ if scrollTimeout
48
+ clearTimeout(scrollTimeout)
49
+ scrollTimeout = null
50
+ scrollTimeout = setTimeout(scrollHandler, 250)
51
+
52
+ # Internal helper for logging messages
53
+ _log: (msg) ->
54
+ console?.log(msg) if @options.debug
55
+
56
+ # Check the distance of the nav selector from the bottom of the window and fire
57
+ # load event if close enough
58
+ check: ->
59
+ nav = @$container.find(@options.navSelector)
60
+ if nav.size() == 0
61
+ @_log "No more pages to load"
62
+ else
63
+ windowBottom = $(window).scrollTop() + $(window).height()
64
+ distance = nav.offset().top - windowBottom
65
+
66
+ if @options.state.paused
67
+ @_log "Paused"
68
+ else if @options.state.loading
69
+ @_log "Waiting..."
70
+ else if (distance > @options.buffer)
71
+ @_log "#{distance - @options.buffer}px remaining..."
72
+ else
73
+ @next() # load the next page
74
+
75
+ # Load the next page
76
+ next: ->
77
+ if @options.state.done
78
+ @_log "Loaded all pages"
79
+ else
80
+ @_loading()
81
+
82
+ $.getScript(@$container.find(@options.navSelector).attr('href'))
83
+ .done (=> @_success())
84
+ .fail (=> @_error())
85
+
86
+ _loading: ->
87
+ @options.state.loading = true
88
+ @_log "Loading next page..."
89
+ if typeof @options.loading is 'function'
90
+ @$container.find(@options.navSelector).each(@options.loading)
91
+
92
+ _success: ->
93
+ @options.state.loading = false
94
+ @_log "New page loaded!"
95
+ if typeof @options.success is 'function'
96
+ @$container.find(@options.navSelector).each(@options.success)
97
+
98
+ _error: ->
99
+ @options.state.loading = false
100
+ @_log "Error loading new page :("
101
+ if typeof @options.error is 'function'
102
+ @$container.find(@options.navSelector).each(@options.error)
103
+
104
+ # Pause firing of events on scroll
105
+ pause: ->
106
+ @options.state.paused = true
107
+ @_log "Scroll checks paused"
108
+
109
+ # Resume firing of events on scroll
110
+ resume: ->
111
+ @options.state.paused = false
112
+ @_log "Scroll checks resumed"
113
+ @check()
114
+
115
+ # Define the plugin
116
+ $.fn.extend infinitePages: (option, args...) ->
117
+ @each ->
118
+ $this = $(this)
119
+ data = $this.data('infinitepages')
120
+
121
+ if !data
122
+ $this.data 'infinitepages', (data = new InfinitePages(this, option))
123
+ if typeof option == 'string'
124
+ data[option].apply(data, args)
125
+
126
+ ) window.jQuery, window
@@ -0,0 +1,2 @@
1
+ require 'jquery/infinite_pages/version'
2
+ require 'jquery/infinite_pages/engine'
@@ -0,0 +1,6 @@
1
+ module Jquery
2
+ module InfinitePages
3
+ class Engine < ::Rails::Engine
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,3 @@
1
+ module JqueryInfinitePages
2
+ VERSION = "0.1.1"
3
+ end
metadata ADDED
@@ -0,0 +1,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jquery-infinite-pages
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Zach Millman
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-04-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: jquery-rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ! '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ! '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: railties
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '3.1'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '3.1'
41
+ description: A light-weight infinite scrolling jQuery plugin, wrapped in a gem for
42
+ Rails
43
+ email:
44
+ - zach@magoosh.com
45
+ executables: []
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - lib/jquery/infinite_pages/engine.rb
50
+ - lib/jquery/infinite_pages/version.rb
51
+ - lib/jquery-infinite-pages.rb
52
+ - app/assets/javascripts/jquery.infinite-pages.js.coffee
53
+ - LICENSE
54
+ - README.md
55
+ homepage: https://github.com/magoosh/jquery-infinite-pages
56
+ licenses:
57
+ - MIT
58
+ metadata: {}
59
+ post_install_message:
60
+ rdoc_options: []
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ! '>='
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ required_rubygems_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ! '>='
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ requirements: []
74
+ rubyforge_project:
75
+ rubygems_version: 2.1.11
76
+ signing_key:
77
+ specification_version: 4
78
+ summary: Infinite pages for jQuery + Rails
79
+ test_files: []