ajax_pagination 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.md CHANGED
@@ -1,3 +1,8 @@
1
+ ## v0.0.2
2
+ * Semi-transparent white rectangle changed to completely transparent rectangle. Instead, the opacity of the content behind it is lowered to achieve the same effect, but works better on backgrounds of other colours as well.
3
+ * Added rdoc comments to code
4
+ * README updated
5
+
1
6
  ## v0.0.1
2
7
 
3
8
  * initial release
data/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  Copyright © 2012 Ronald Ping Man Chan
2
2
  If you want to use this program under a different license, please contact me.
3
- All pull requests on GitHub will be assumed to assign non-exclusive rights for perpertual usage for any use whatsoever for those modifications to Ronald Ping Man Chan. If you do not have the power to assign that right, do not submit a pull request. This allows me to release the software under a more permissible license in the future, if I so wish.
3
+ All pull requests on GitHub will be assumed to assign to me non-exclusive rights for perpertual usage for any use whatsoever for those modifications to Ronald Ping Man Chan. If you do not have the power to assign that right, do not submit a pull request. This allows me to release the software under a more permissible license in the future, if I so wish.
4
4
  Ronald
5
5
 
6
6
  The software is released under LGPL. The LGPL License on this software means that this Gem can be used in any software (proprietary or otherwise), by using the gem command. However, the gem cannot be included directly in proprietary software (eg. as a plugin), without distributions of the software being under LGPL. Distribution of this Gem must be released under the LGPL as well.
data/README.md CHANGED
@@ -1,15 +1,16 @@
1
1
  # AJAX Pagination
2
- **TO BE RELEASED**
3
2
 
4
3
  Handles AJAX pagination for you, by hooking up the links you want to load content with javascript in designated page containers. Each webpage can have multiple page containers, each with a different set of pagination links. The page containers can be nested. Degrades gracefully when javascript is disabled.
5
4
 
6
5
  ## Introduction
7
- This gem can ajaxify any pagination solution. Links wrapped in containers with specific classes will be ajaxified. This means that clicking it will instead send an AJAX request for the page in javascript format. The result will replace the content in a container for the content. this gem is tested to work for will_paginate, but should work for other pagination solutions, as well as navigation level links or tabbed interfaces. The ajax call will load new content into the designated content container.
6
+ This gem can ajaxify any pagination solution. Links wrapped in containers with specific classes will be ajaxified. This means that clicking it will instead send an AJAX request for the page in javascript format. The result will replace the content in a container for the content. this gem is tested to work with will_paginate, but should work for other pagination solutions, as well as navigation level links or tabbed interfaces. The ajax call will load new content into the designated content container.
7
+
8
+ Please note, this is not a pagination solution by itself. You should use a pagination solution such as will_paginate and Kaminari, or a menu builder such as Simple-navigation or Semantic-menu, or you can roll your own. After that is implemented, you can use AJAX Pagination to ajaxify it, so that when users change pages, they do not have the reload the whole page.
8
9
 
9
10
  ## Background
10
11
  This gem depends on Rails 3.1+, jQuery and jquery-historyjs. The gem was extracted from http://github.com/xrymbos/nztrain-v2/, and further development will be tied to the needs of the application. Therefore, some dependencies are because the application uses a particular version of these other gems. If you need to use this in other versions/javascript frameworks, I would welcome any pull requests. They are not currently supported because I do not need to use this gem in those other frameworks.
11
12
 
12
- The original AJAX pagination functionality was inspired by the RailsCasts on pagination with AJAX. However, other functionality was added to the pagination, add more modular code was desired, especially when many different controllers need pagination.
13
+ The original AJAX pagination functionality was inspired by the RailsCasts on pagination with AJAX. However, other functionality was added to the pagination, and more modular code was desired, especially when many different controllers need pagination. Added functionality includes loading cues and support of multiple pagination areas on the same webpage.
13
14
 
14
15
  Because the code became more modular, it also made it suitable to turn into a Ruby Gem, so that others can create AJAX pagination without fiddling with the details.
15
16
 
@@ -28,9 +29,18 @@ Then add to your assets/javascripts/application.js,
28
29
  //= require ajax_pagination
29
30
  ```
30
31
 
32
+ AJAX Pagination depends on jquery-rails and jquery-historyjs, so if their javascript files are not already included, also include to following in your assets/javascripts/application.js file:
33
+
34
+ ```javascript
35
+ //= require jquery
36
+ //= require history
37
+ ```
38
+
31
39
  ## Getting started
32
40
 
33
- The pagination needs a name (in case you have multiple sets of AJAX pagination on the same webpage). By default, the name is "page". If you only intend to use a single set of pagination links, then leave it as "page".
41
+ The pagination needs a name (in case you have multiple sets of AJAX pagination on the same webpage). By default, the name is "page". If you only intend to use a single set of pagination links, then leave it as "page". The name is used to distinguish different sets of pagination links, and is also used as a default for other functionaility.
42
+
43
+ ### Ajaxifying the content
34
44
 
35
45
  Move the content to be paginated into a partial. If you are using the will_paginate gem (or similar), there is only one set of content to put into a partial. If you are using this to paginate between distinct views or even different controllers, you will need to move each set of content into a different partial.
36
46
 
@@ -54,7 +64,7 @@ If the partial is not named the same, pass it the new name as an option:
54
64
  <%= ajax_pagination :partial => "mypartial" %>
55
65
  ```
56
66
 
57
- This will case it to display content in the _mypartial.* view.
67
+ This will cause it to display content in the _mypartial.* view.
58
68
 
59
69
  If you are using will_paginate, and the links are wrapped in a div with class="pagination", the links will be ajaxified automatically.
60
70
 
@@ -72,9 +82,11 @@ If you are using will_paginate, you can simply put this in the partial (so that
72
82
 
73
83
  Note: It is recommended to set the pagination parameter to nil. When AJAX pagination calls the controller with a request for the partial, it appends ?pagination=NAMEOFPAGINATION. If the parameter is not set, AJAX Pagination will not respond to the AJAX call. will_paginate by default keeps any parameters in the query string. However, because this parameter is for internal use only, setting it to nil will keep the parameter from showing up in the url, making it look nicer (also better for caching).
74
84
 
75
- Now, AJAX pagination will automatically call the controller for new content when an ajaxified link is clicked.
85
+ Now, AJAX Pagination will automatically call the controller for new content when an ajaxified link is clicked.
76
86
 
77
- However, the controller needs to be told how to respond. Add a call to ajax_pagination(format) in the controller action, which will return javascript containing the partial.
87
+ ### Controller responder
88
+
89
+ However, the controller needs to be told how to respond. Add a call to <tt>ajax_pagination(format)</tt> in the controller action, which will return javascript containing the partial.
78
90
 
79
91
  ```ruby
80
92
  respond_to do |format|
@@ -91,13 +103,16 @@ ajax_pagination format, :pagination => "page", :partial => "mypartial"
91
103
 
92
104
  The pagination should now work.
93
105
 
94
- AJAX Pagination can also add a loading image and partially blanking out of the paginated content. To do this, wrap all the content you want to cover with ajax_pagination_loadzone. For example, in the partial, you might have:
106
+ ### Loading visualization
107
+
108
+ AJAX Pagination can also add a loading image and partially blanking out of the paginated content. To do this, wrap all the content you want to cover with <tt>ajax_pagination_loadzone</tt>. For example, in the partial, you might have:
95
109
 
96
110
  ```erb
97
111
  <%= will_paginate @objects, :params => { :pagination => nil } %>
98
112
  <%= ajax_pagination_loadzone do %>
99
- All content here is covered by a semi-transparent white rectangle.
100
- A loading image is displayed on top, and any links here are unclickable
113
+ While this partial is being loaded with other content,
114
+ all content here has opacity reduced, and is covered by a transparent rectangle,
115
+ a loading image is displayed on top, and any links here are unclickable
101
116
  <% end %>
102
117
  ```
103
118
 
@@ -105,20 +120,75 @@ Links outside are still clickable (such as the will_paginate links).
105
120
 
106
121
  The loading image is currently an image asset at "ajax-loader.gif", so put your loading image there. (TODO: add default loader image, and make the location changeable)
107
122
 
123
+ ### Content reloading
124
+
125
+ The back and forward buttons on your browser may not work properly yet. It will work as long as the link includes distinct query parameter with the same name as the pagination name for the set. For example, if the name of the pagination set is "page" (the default), when the browser url changes, AJAX Pagination looks for a change in the links query parameter with the same name, such as if the url changes from /path/to/controller?page=4 to /path/to/controller?page=9, then AJAX Pagination knows that the content corresponding to the pagination set needs reloading. The absence of the parameter is a distinct state, so changes such as /path/to/controller to /path/to/controller?page=0 are detected.
126
+
127
+ However, if the pagination is to different controllers, eg. url changes from /ControllerA to /ControllerB, AJAX Pagination will not reload the content, because the name of the pagination set is "page", and the url ?page=... parameter has not changed. There are some options that can be passed to ajax_pagination, through the reload option.
128
+
129
+ ```erb
130
+ <%= ajax_pagination :reload => {:query => "watching"} %>
131
+ ```
132
+
133
+ By passing reload a hash, mapping query to watching, AJAX Pagination will reload the content if the query parameter named "watching" changes. For example, if the url changes from ?watching=A to ?watching=B.
134
+
135
+ For more flexibility, a regular expression can be passed like so:
136
+
137
+ ```erb
138
+ <%= ajax_pagination :reload => {:urlregex => "page=([0-9]+)", :regexindex => 1} %>
139
+ ```
140
+
141
+ Which applies the regex expression /page=([0-9]+)/ to the url. The parameter regexindex then selects the nth matching subexpression, on which changes are detected. The subexpression indexed 0 is the complete match. The subexpression index 1 is the page number in this case. So when that changes, the page is reloaded.
142
+
143
+ For more flexibility, a number of conditions can be passed in an array. If any of them change, the content is reloaded.
144
+
145
+ ```erb
146
+ <%= ajax_pagination :reload => [{:urlregex => "page=([0-9]+)", :regexindex => 1},{:query => "watching"}] %>
147
+ ```
148
+
149
+ Instead of passing in the Array/Hash Ruby object, a string in json form is accepted:
150
+
151
+ ```erb
152
+ <%= ajax_pagination :reload => '[{"urlregex":"page=([0-9]+)","regexindex":1},{"query":"page"}]' %>
153
+ ```
154
+
155
+ ## AJAX Call
156
+ The AJAX Call is triggered by a link wrapped in any container with a certain class. The AJAX Call is to the same address, but with the ?pagination=NAME parameter added. The format requested is javascript. If the controller also returns javascript for other uses, AJAX Pagination does not necessarily prevent such uses. The ajax_pagination(format, :pagination => "page") function in the controller handles the AJAX Call when the format is javascript, and the ?pagination parameter is set to the correct string. It also returns true if the pagination parameter matches. Therefore, you can use use the javascript format when it does not match, as shown below:
157
+
158
+ ```ruby
159
+ respond_to do |format|
160
+ format.html # index.html.erb
161
+ format.js unless ajax_pagination(format)
162
+ end
163
+ ```
164
+
165
+ Note that **unless** does not need to be used, since respond_to is actually sensitive to the ordering, so an equivalent effect is achieved with:
166
+
167
+ ```ruby
168
+ respond_to do |format|
169
+ format.html # index.html.erb
170
+ ajax_pagination(format)
171
+ format.js # index.js.erb
172
+ end
173
+ ```
174
+
108
175
  ## Javascript Dependency
109
176
  As well as the included ajax_pagination.js file, this gem uses jquery.ba-bbq.js, a jquery plugin. This is included in the gem as an asset already to simplify installation. ajax_pagination.js will automatically require jquery.ba-bbq.js.
110
177
 
111
178
  However, if you are already using this (especially a different version of this), simply ensure that your file is named jquery.ba-bbq.js as well, so that it overrides the file in the gem.
112
179
 
180
+ The other javascript dependencies rely on gems: jquery-rails, and jquery-historyjs. So if they are used, AJAX Pagination should play well with other gems using the libraries.
181
+
113
182
  ## Development
114
- You are welcome to submit pull requests on GitHub at https://github.com/ronalchn/ajax_pagination, however, please be aware that submitting a pull request is assumed to grant non-exclusive, perpetual rights to the use of the software for any use whatsoever. This allows me to release the software under a more permissible license in the future if I want.
183
+ You are welcome to submit pull requests on GitHub at https://github.com/ronalchn/ajax_pagination, however, please be aware that submitting a pull request is assumed to grant to me non-exclusive, perpetual rights to the use of the software for any use whatsoever. This allows me to release the software under a more permissible license in the future if I want.
115
184
 
116
185
  If you do not submit a pull request, your modifications will not be merged into the codebase.
117
186
 
118
187
  ## License
119
188
  Copyright © 2012 Ronald Ping Man Chan
189
+
120
190
  If you want to use this program under a different license, please contact me.
121
- All pull requests to my repository will be assumed to assign non-exclusive rights for perpertual usage for any use whatsoever for those modifications to Ronald Ping Man Chan. If you do not have the power to assign that right, do not submit a pull request. This allows me to release the software under a more permissible license in the future if I want.
191
+ All pull requests to my repository will be assumed to assign to me non-exclusive rights for perpertual usage for any use whatsoever for those modifications to Ronald Ping Man Chan. If you do not have the power to assign that right, do not submit a pull request. This allows me to release the software under a more permissible license in the future if I want.
122
192
  Ronald
123
193
 
124
194
  The software is released under LGPL. The LGPL License on this software means that this Gem can be used in any software (proprietary or otherwise), by using the gem command. However, the gem cannot be included directly in proprietary software (eg. as a plugin), without distributions of the software being under LGPL. Distribution of this Gem must be released under the LGPL as well.
@@ -1,5 +1,30 @@
1
1
  module AjaxPagination
2
+ # This module is automatically added to all controllers
2
3
  module ControllerAdditions
4
+ # Registers a javascript format when params [:pagination] matches options [:pagination] ( = "page" by default).
5
+ # AJAX Pagination uses this response to render only the content which has changed. When this format is triggered,
6
+ # a partial is passed back, and sent to AJAX Pagination as a function argument in javascript.
7
+ #
8
+ # Call this method in a respond_to block, in a controller action:
9
+ #
10
+ # class CommentsController < ApplicationController
11
+ # def index
12
+ # @comments = Comment.all
13
+ # respond_to do |format|
14
+ # format.html # index.html.erb
15
+ # ajax_pagination(format)
16
+ # end
17
+ # end
18
+ # end
19
+ #
20
+ # Options:
21
+ # [:+pagination+]
22
+ # Changes the pagination name triggering this response. Triggered when params [:pagination] == options [:pagination].
23
+ # Defaults to "page"
24
+ #
25
+ # [:+partial+]
26
+ # Changes the partial that is returned by this response. Defaults to options [:pagination].
27
+ #
3
28
  def ajax_pagination(format,options = {})
4
29
  if params[:pagination] == (options[:pagination] || 'page')
5
30
  partial = options[:partial] || params[:pagination]
@@ -1,5 +1,50 @@
1
1
  module AjaxPagination
2
+ # This module is automatically added to all views, providing helper methods.
2
3
  module HelperAdditions
4
+ # Renders a partial wrapped in a div tag. When the page content displayed in the partial needs changing
5
+ # AJAX Pagination will replace content inside this div tag, with the new content returned by the controller
6
+ # (To manage that, see +ajax_pagination+ in ControllerAdditions).
7
+ #
8
+ # Call this method instead of +render+ to display the partial representing the page content:
9
+ #
10
+ # Listing Comments:
11
+ #
12
+ # <%= ajax_pagination %>
13
+ #
14
+ # <%= link_to 'New Comment', new_comment_path %>
15
+ #
16
+ # Options:
17
+ # [:+pagination+]
18
+ # Changes the pagination name, which is used for requesting new content, and to uniquely identify the
19
+ # wrapping div tag. The name passed here should be the same as the pagination name used in the controller
20
+ # respond_to block. Defaults to "page".
21
+ #
22
+ # [:+partial+]
23
+ # Changes the partial which is rendered. Defaults to +options [:pagination]+. The partial should generally
24
+ # be the same as that given in the controller respond_to block, unless you are doing something strange.
25
+ #
26
+ # [:+reload+]
27
+ # Used to detect whether the partial needs reloading, based on how the url changes. When pagination links are
28
+ # clicked, they are easily detected, and will load the new content automatically. When the browser
29
+ # back/forwards buttons are used, AJAX Pagination needs to know whether the content inside this pagination
30
+ # partial needs reloading. This is not certain, because the webpage may contain multiple pagination partials,
31
+ # and moreover, the page may have other functionality using the History.pushState feature. Defaults to nil.
32
+ #
33
+ # If passed a hash of the form :+query+ => "parametername", AJAX Pagination will parse the url, to find
34
+ # the parameter +parametername+ inside the query string (ie. /path/?parametername=value). When it changes,
35
+ # the partial is reloaded.
36
+ #
37
+ # If passed a hash of the form :+urlregex+ => "regularexpression", AJAX Pagination will apply the regular
38
+ # expression to the url. If a particular subexpression of the match changes, the partial is reloaded. The
39
+ # subexpression used defaults to the whole match. If the hash includes :+regexindex+ => N, the Nth subexpression
40
+ # is used.
41
+ #
42
+ # Different parts of the url can be watched for any changes, by passing an array of hashes. For example:
43
+ #
44
+ # <%= ajax_pagination :reload => [{:urlregex => "page=([0-9]+)", :regexindex => 1},{:query => "watching"}] %>
45
+ #
46
+ # If nil, AJAX Pagination acts as if it was passed {:query => options [:pagination]}.
47
+ #
3
48
  def ajax_pagination(options = {})
4
49
  pagination = options[:pagination] || 'page' # by default the name of the pagination is 'page'
5
50
  partial = options[:partial] || pagination # default partial rendered is the name of the pagination
@@ -15,9 +60,28 @@ module AjaxPagination
15
60
  render partial
16
61
  end
17
62
  end
63
+
64
+ # Wraps content in a div tag. AJAX Pagination uses this to display visual loading cues. When a partial
65
+ # is reloaded, it may take some time. In the meanwhile, AJAX Pagination will look in the partial amoung its
66
+ # immediate child for this tag. If this tag exists, it will cover this tag with a semi-transparent
67
+ # rectangle, and make the old partial unclickable (links and other elements are therefore disabled).
68
+ # A loading image is also displayed above the content.
69
+ #
70
+ # Use this tag in your partial, wrapped around all the content you want to disable. For example, if you are
71
+ # displaying pagination links which you do not want to disable, as well as content you wish to disable,
72
+ # you partial might contain:
73
+ #
74
+ # <%= will_paginate @objects, :params => { :pagination => nil } %>
75
+ # <%= ajax_pagination_loadzone do %>
76
+ # All content here is covered by a semi-transparent rectangle.
77
+ # A loading image is displayed on top, and any links here are unclickable
78
+ # <% end %>
79
+ #
18
80
  def ajax_pagination_loadzone()
19
81
  content_tag :div, :class => "paginated_content", :style => "position: relative" do
20
- yield
82
+ content_tag :div do # for changing the opacity
83
+ yield
84
+ end
21
85
  end
22
86
  end
23
87
  end
@@ -1,3 +1,3 @@
1
1
  module AjaxPagination
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -1,5 +1,12 @@
1
1
  //= require jquery.ba-bbq
2
2
 
3
+ /*
4
+ * AJAX Pagination: Ajaxifying your pagination links
5
+ * https://github.com/ronalchn/ajax_pagination
6
+ *
7
+ * Copyright (c) 2012 Ronald Ping Man Chan
8
+ * Distributed under the LGPL license
9
+ */
3
10
  if (History && History.enabled) {
4
11
  var ajaxPagination;
5
12
  $(document).ready(function () {
@@ -20,7 +27,8 @@ if (History && History.enabled) {
20
27
  $(img).css({'margin': margin + 'px', 'max-height': (height/2) + 'px'});
21
28
  var div = document.createElement("DIV");
22
29
  $(div).append(img);
23
- $(div).css({'position': 'absolute', 'left': 0, 'top': 0, 'width': '100%', 'height': '100%', 'background-color': 'rgba(255,255,255,0.7)', 'text-align': 'center', 'opacity': 0.9});
30
+ $(div).css({'position': 'absolute', 'left': 0, 'top': 0, 'width': '100%', 'height': '100%', 'text-align': 'center', 'opacity': 0.9});
31
+ paginated_content.children().css({'opacity': 0.4});
24
32
  paginated_content.append(div);
25
33
 
26
34
  // scroll to top of paginated_section if it is not visible
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ajax_pagination
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 27
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 1
10
- version: 0.0.1
9
+ - 2
10
+ version: 0.0.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Ronald Ping Man Chan
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2012-02-18 00:00:00 Z
18
+ date: 2012-02-19 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: rspec
@@ -138,3 +138,4 @@ specification_version: 3
138
138
  summary: Handles AJAX pagination, changing the content when certain links are followed.
139
139
  test_files: []
140
140
 
141
+ has_rdoc: