pjax_rails 0.1.8 → 0.1.9

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.
data/CHANGELOG CHANGED
@@ -1,3 +1,7 @@
1
+ *0.1.9*
2
+
3
+ * Use latest jquery-pjax JavaScript code [2011-08-29 version, see https://github.com/defunkt/jquery-pjax/commits/master]
4
+
1
5
  *0.1.8*
2
6
 
3
7
  * Expand the selector to allow data-skip-pjax to be applied to a container and have that affect all its children [Sam Stephenson]
@@ -1,6 +1,8 @@
1
- // jquery_pjax.js
1
+ // jquery.pjax.js
2
2
  // copyright chris wanstrath
3
- // https://github.com/defunkt/pjax
3
+ // https://github.com/defunkt/jquery-pjax
4
+
5
+ (function($){
4
6
 
5
7
  // When called on a link, fetches the href with ajax into the
6
8
  // container specified as the first parameter or with the data-pjax
@@ -27,6 +29,13 @@ $.fn.pjax = function( container, options ) {
27
29
  else
28
30
  options = $.isPlainObject(container) ? container : {container:container}
29
31
 
32
+ // We can't persist $objects using the history API so we must use
33
+ // a String selector. Bail if we got anything else.
34
+ if ( options.container && typeof options.container !== 'string' ) {
35
+ throw "pjax container must be a string selector!"
36
+ return false
37
+ }
38
+
30
39
  return this.live('click', function(event){
31
40
  // Middle click, cmd click, and ctrl click should open
32
41
  // links in a new tab as normal.
@@ -35,7 +44,9 @@ $.fn.pjax = function( container, options ) {
35
44
 
36
45
  var defaults = {
37
46
  url: this.href,
38
- container: $(this).attr('data-pjax')
47
+ container: $(this).attr('data-pjax'),
48
+ clickedElement: $(this),
49
+ fragment: null
39
50
  }
40
51
 
41
52
  $.pjax($.extend({}, defaults, options))
@@ -53,7 +64,7 @@ $.fn.pjax = function( container, options ) {
53
64
  //
54
65
  // Accepts these extra keys:
55
66
  //
56
- // container - Where to stick the response body.
67
+ // container - Where to stick the response body. Must be a String.
57
68
  // $(container).html(xhr.responseBody)
58
69
  // push - Whether to pushState the URL. Defaults to true (of course).
59
70
  // replace - Want to use replaceState instead? That's cool.
@@ -64,102 +75,126 @@ $.fn.pjax = function( container, options ) {
64
75
  // console.log( xhr.readyState )
65
76
  //
66
77
  // Returns whatever $.ajax returns.
67
- $.pjax = function( options ) {
78
+ var pjax = $.pjax = function( options ) {
68
79
  var $container = $(options.container),
69
80
  success = options.success || $.noop
70
81
 
71
82
  // We don't want to let anyone override our success handler.
72
83
  delete options.success
73
84
 
74
- var defaults = {
75
- timeout: 1500,
76
- push: true,
77
- replace: false,
78
- // We want the browser to maintain two separate internal caches: one for
79
- // pjax'd partial page loads and one for normal page loads. Without
80
- // adding this secret parameter, some browsers will often confuse the two.
81
- data: { _pjax: true },
82
- type: 'GET',
83
- dataType: 'html',
84
- beforeSend: function(xhr){
85
- $(document).trigger('start.pjax')
86
- xhr.setRequestHeader('X-PJAX', 'true')
87
- },
88
- error: function(){
89
- window.location = options.url
90
- },
91
- complete: function(){
92
- $(document).trigger('end.pjax')
93
- },
94
- success: function(data){
95
- // If we got no data or an entire web page, go directly
96
- // to the page and let normal error handling happen.
97
- if ( !$.trim(data) || /<html/i.test(data) )
85
+ // We can't persist $objects using the history API so we must use
86
+ // a String selector. Bail if we got anything else.
87
+ if ( typeof options.container !== 'string' )
88
+ throw "pjax container must be a string selector!"
89
+
90
+ options = $.extend(true, {}, pjax.defaults, options)
91
+
92
+ if ( $.isFunction(options.url) ) {
93
+ options.url = options.url()
94
+ }
95
+
96
+ options.context = $container
97
+
98
+ options.success = function(data){
99
+ if ( options.fragment ) {
100
+ // If they specified a fragment, look for it in the response
101
+ // and pull it out.
102
+ var $fragment = $(data).find(options.fragment)
103
+ if ( $fragment.length )
104
+ data = $fragment.children()
105
+ else
98
106
  return window.location = options.url
107
+ } else {
108
+ // If we got no data or an entire web page, go directly
109
+ // to the page and let normal error handling happen.
110
+ if ( !$.trim(data) || /<html/i.test(data) )
111
+ return window.location = options.url
112
+ }
99
113
 
100
- // Make it happen.
101
- $container.html(data)
114
+ // Make it happen.
115
+ this.html(data)
102
116
 
103
- // If there's a <title> tag in the response, use it as
104
- // the page's title.
105
- var oldTitle = document.title,
106
- title = $.trim( $container.find('title').remove().text() )
107
- if ( title ) document.title = title
117
+ // If there's a <title> tag in the response, use it as
118
+ // the page's title.
119
+ var oldTitle = document.title,
120
+ title = $.trim( this.find('title').remove().text() )
121
+ if ( title ) document.title = title
108
122
 
109
- var state = {
110
- pjax: options.container,
111
- timeout: options.timeout
112
- }
123
+ var state = {
124
+ pjax: options.container,
125
+ fragment: options.fragment,
126
+ timeout: options.timeout
127
+ }
113
128
 
114
- // We can't persist $objects using the history API so we need to store
115
- // the string selector.
116
- if ( $.isPlainObject(state.pjax) )
117
- state.pjax = state.pjax.selector
118
-
119
- // If there are extra params, save the complete URL in the state object
120
- var query = $.param(options.data)
121
- if ( query != "_pjax=true" )
122
- state.url = options.url + (/\?/.test(options.url) ? "&" : "?") + query
123
-
124
- if ( options.replace ) {
125
- window.history.replaceState(state, document.title, options.url)
126
- } else if ( options.push ) {
127
- // this extra replaceState before first push ensures good back
128
- // button behavior
129
- if ( !$.pjax.active ) {
130
- window.history.replaceState($.extend({}, state, {url:null}), oldTitle)
131
- $.pjax.active = true
132
- }
133
-
134
- window.history.pushState(state, document.title, options.url)
129
+ // If there are extra params, save the complete URL in the state object
130
+ var query = $.param(options.data)
131
+ if ( query != "_pjax=true" )
132
+ state.url = options.url + (/\?/.test(options.url) ? "&" : "?") + query
133
+
134
+ if ( options.replace ) {
135
+ window.history.replaceState(state, document.title, options.url)
136
+ } else if ( options.push ) {
137
+ // this extra replaceState before first push ensures good back
138
+ // button behavior
139
+ if ( !pjax.active ) {
140
+ window.history.replaceState($.extend({}, state, {url:null}), oldTitle)
141
+ pjax.active = true
135
142
  }
136
143
 
137
- // Google Analytics support
138
- if ( (options.replace || options.push) && window._gaq )
139
- _gaq.push(['_trackPageview'])
140
-
141
- // Invoke their success handler if they gave us one.
142
- success.apply(this, arguments)
144
+ window.history.pushState(state, document.title, options.url)
143
145
  }
144
- }
145
146
 
146
- options = $.extend(true, {}, defaults, options)
147
+ // Google Analytics support
148
+ if ( (options.replace || options.push) && window._gaq )
149
+ _gaq.push(['_trackPageview'])
147
150
 
148
- if ( $.isFunction(options.url) ) {
149
- options.url = options.url()
151
+ // If the URL has a hash in it, make sure the browser
152
+ // knows to navigate to the hash.
153
+ var hash = window.location.hash.toString()
154
+ if ( hash !== '' ) {
155
+ window.location.href = hash
156
+ }
157
+
158
+ // Invoke their success handler if they gave us one.
159
+ success.apply(this, arguments)
150
160
  }
151
161
 
152
162
  // Cancel the current request if we're already pjaxing
153
- var xhr = $.pjax.xhr
163
+ var xhr = pjax.xhr
154
164
  if ( xhr && xhr.readyState < 4) {
155
165
  xhr.onreadystatechange = $.noop
156
166
  xhr.abort()
157
167
  }
158
168
 
159
- $.pjax.xhr = $.ajax(options)
160
- $(document).trigger('pjax', $.pjax.xhr, options)
169
+ pjax.options = options
170
+ pjax.xhr = $.ajax(options)
171
+ $(document).trigger('pjax', [pjax.xhr, options])
172
+
173
+ return pjax.xhr
174
+ }
175
+
161
176
 
162
- return $.pjax.xhr
177
+ pjax.defaults = {
178
+ timeout: 650,
179
+ push: true,
180
+ replace: false,
181
+ // We want the browser to maintain two separate internal caches: one for
182
+ // pjax'd partial page loads and one for normal page loads. Without
183
+ // adding this secret parameter, some browsers will often confuse the two.
184
+ data: { _pjax: true },
185
+ type: 'GET',
186
+ dataType: 'html',
187
+ beforeSend: function(xhr){
188
+ this.trigger('start.pjax', [xhr, pjax.options])
189
+ xhr.setRequestHeader('X-PJAX', 'true')
190
+ },
191
+ error: function(xhr, textStatus, errorThrown){
192
+ if ( textStatus !== 'abort' )
193
+ window.location = pjax.options.url
194
+ },
195
+ complete: function(xhr){
196
+ this.trigger('end.pjax', [xhr, pjax.options])
197
+ }
163
198
  }
164
199
 
165
200
 
@@ -172,7 +207,7 @@ var popped = ('state' in window.history), initialURL = location.href
172
207
  //
173
208
  // You probably shouldn't use pjax on pages with other pushState
174
209
  // stuff yet.
175
- $(window).bind('popstate', function(event) {
210
+ $(window).bind('popstate', function(event){
176
211
  // Ignore inital popstate that some browsers fire on page load
177
212
  var initialPop = !popped && location.href == initialURL
178
213
  popped = true
@@ -181,11 +216,12 @@ $(window).bind('popstate', function(event) {
181
216
  var state = event.state
182
217
 
183
218
  if ( state && state.pjax ) {
184
- var $container = $(state.pjax+'')
185
- if ( $container.length )
219
+ var container = state.pjax
220
+ if ( $(container+'').length )
186
221
  $.pjax({
187
222
  url: state.url || location.href,
188
- container: $container,
223
+ fragment: state.fragment,
224
+ container: container,
189
225
  push: false,
190
226
  timeout: state.timeout
191
227
  })
@@ -197,12 +233,23 @@ $(window).bind('popstate', function(event) {
197
233
 
198
234
  // Add the state property to jQuery's event object so we can use it in
199
235
  // $(window).bind('popstate')
200
- if ( $.event.props.indexOf('state') < 0 )
236
+ if ( $.inArray('state', $.event.props) < 0 )
201
237
  $.event.props.push('state')
202
238
 
203
239
 
240
+ // Is pjax supported by this browser?
241
+ $.support.pjax =
242
+ window.history && window.history.pushState && window.history.replaceState
243
+ // pushState isn't reliable on iOS yet.
244
+ && !navigator.userAgent.match(/(iPod|iPhone|iPad|WebApps\/.+CFNetwork)/)
245
+
246
+
204
247
  // Fall back to normalcy for older browsers.
205
- if ( !window.history || !window.history.pushState ) {
206
- $.pjax = $.noop
248
+ if ( !$.support.pjax ) {
249
+ $.pjax = function( options ) {
250
+ window.location = $.isFunction(options.url) ? options.url() : options.url
251
+ }
207
252
  $.fn.pjax = function() { return this }
208
- }
253
+ }
254
+
255
+ })(jQuery);
Binary file
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'pjax_rails'
3
- s.version = '0.1.8'
3
+ s.version = '0.1.9'
4
4
  s.author = 'David Heinemeier Hansson (PJAX by Chris Wanstrath)'
5
5
  s.email = 'david@loudthinking.com'
6
6
  s.summary = 'PJAX integration for Rails 3.1+'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pjax_rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.8
4
+ version: 0.1.9
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2011-08-29 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: jquery-rails
16
- requirement: &2164347420 !ruby/object:Gem::Requirement
16
+ requirement: &2152605760 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,7 +21,7 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *2164347420
24
+ version_requirements: *2152605760
25
25
  description:
26
26
  email: david@loudthinking.com
27
27
  executables: []
@@ -36,6 +36,7 @@ files:
36
36
  - ./lib/pjax.rb
37
37
  - ./lib/pjax_rails.rb
38
38
  - ./pjax_rails-0.1.7.gem
39
+ - ./pjax_rails-0.1.8.gem
39
40
  - ./pjax_rails.gemspec
40
41
  - ./README.md
41
42
  homepage: