pjax_rails 0.3.3 → 0.3.4

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.
@@ -4,28 +4,36 @@
4
4
 
5
5
  (function($){
6
6
 
7
- // When called on a link, fetches the href with ajax into the
8
- // container specified as the first parameter or with the data-pjax
9
- // attribute on the link itself.
7
+ // When called on a container with a selector, fetches the href with
8
+ // ajax into the container or with the data-pjax attribute on the link
9
+ // itself.
10
10
  //
11
11
  // Tries to make sure the back button and ctrl+click work the way
12
12
  // you'd expect.
13
13
  //
14
+ // Exported as $.fn.pjax
15
+ //
14
16
  // Accepts a jQuery ajax options object that may include these
15
17
  // pjax specific options:
16
18
  //
19
+ //
17
20
  // container - Where to stick the response body. Usually a String selector.
18
21
  // $(container).html(xhr.responseBody)
22
+ // (default: current jquery context)
19
23
  // push - Whether to pushState the URL. Defaults to true (of course).
20
24
  // replace - Want to use replaceState instead? That's cool.
21
25
  //
22
- // For convenience the first parameter can be either the container or
26
+ // For convenience the second parameter can be either the container or
23
27
  // the options object.
24
28
  //
25
29
  // Returns the jQuery object
26
- function fnPjax(container, options) {
27
- return this.live('click.pjax', function(event){
28
- handleClick(event, container, options)
30
+ function fnPjax(selector, container, options) {
31
+ var context = this
32
+ return this.on('click.pjax', selector, function(event) {
33
+ var opts = $.extend({}, optionsFor(container, options))
34
+ if (!opts.container)
35
+ opts.container = $(this).attr('data-pjax') || context
36
+ handleClick(event, opts)
29
37
  })
30
38
  }
31
39
 
@@ -38,9 +46,9 @@ function fnPjax(container, options) {
38
46
  //
39
47
  // Examples
40
48
  //
41
- // $('a').live('click', $.pjax.click)
49
+ // $(document).on('click', 'a', $.pjax.click)
42
50
  // // is the same as
43
- // $('a').pjax()
51
+ // $(document).pjax('a')
44
52
  //
45
53
  // $(document).on('click', 'a', function(event) {
46
54
  // var container = $(this).closest('[data-pjax-container]')
@@ -58,7 +66,7 @@ function handleClick(event, container, options) {
58
66
 
59
67
  // Middle click, cmd click, and ctrl click should open
60
68
  // links in a new tab as normal.
61
- if ( event.which > 1 || event.metaKey || event.ctrlKey )
69
+ if ( event.which > 1 || event.metaKey || event.ctrlKey || event.shiftKey || event.altKey )
62
70
  return
63
71
 
64
72
  // Ignore cross origin links
@@ -78,7 +86,6 @@ function handleClick(event, container, options) {
78
86
  url: link.href,
79
87
  container: $(link).attr('data-pjax'),
80
88
  target: link,
81
- clickedElement: $(link), // DEPRECATED: use target
82
89
  fragment: null
83
90
  }
84
91
 
@@ -153,18 +160,8 @@ function pjax(options) {
153
160
 
154
161
  var target = options.target
155
162
 
156
- // DEPRECATED: use options.target
157
- if (!target && options.clickedElement) target = options.clickedElement[0]
158
-
159
163
  var hash = parseURL(options.url).hash
160
164
 
161
- // DEPRECATED: Save references to original event callbacks. However,
162
- // listening for custom pjax:* events is prefered.
163
- var oldBeforeSend = options.beforeSend,
164
- oldComplete = options.complete,
165
- oldSuccess = options.success,
166
- oldError = options.error
167
-
168
165
  var context = options.context = findContainerFor(options.container)
169
166
 
170
167
  // We want the browser to maintain two separate internal caches: one
@@ -204,12 +201,6 @@ function pjax(options) {
204
201
 
205
202
  var result
206
203
 
207
- // DEPRECATED: Invoke original `beforeSend` handler
208
- if (oldBeforeSend) {
209
- result = oldBeforeSend.apply(this, arguments)
210
- if (result === false) return false
211
- }
212
-
213
204
  if (!fire('pjax:beforeSend', [xhr, settings]))
214
205
  return false
215
206
 
@@ -220,32 +211,25 @@ function pjax(options) {
220
211
  if (timeoutTimer)
221
212
  clearTimeout(timeoutTimer)
222
213
 
223
- // DEPRECATED: Invoke original `complete` handler
224
- if (oldComplete) oldComplete.apply(this, arguments)
225
-
226
214
  fire('pjax:complete', [xhr, textStatus, options])
227
215
 
228
216
  fire('pjax:end', [xhr, options])
229
- // end.pjax is deprecated
230
- fire('end.pjax', [xhr, options])
231
217
  }
232
218
 
233
219
  options.error = function(xhr, textStatus, errorThrown) {
234
220
  var container = extractContainer("", xhr, options)
235
221
 
236
- // DEPRECATED: Invoke original `error` handler
237
- if (oldError) oldError.apply(this, arguments)
238
-
239
222
  var allowed = fire('pjax:error', [xhr, textStatus, errorThrown, options])
240
- if (textStatus !== 'abort' && allowed)
241
- window.location.replace(container.url)
223
+ if (options.type == 'GET' && textStatus !== 'abort' && allowed) {
224
+ locationReplace(container.url)
225
+ }
242
226
  }
243
227
 
244
228
  options.success = function(data, status, xhr) {
245
229
  var container = extractContainer(data, xhr, options)
246
230
 
247
231
  if (!container.contents) {
248
- window.location.replace(container.url)
232
+ locationReplace(container.url)
249
233
  return
250
234
  }
251
235
 
@@ -291,9 +275,6 @@ function pjax(options) {
291
275
  if (target.length) $(window).scrollTop(target.offset().top)
292
276
  }
293
277
 
294
- // DEPRECATED: Invoke original `success` handler
295
- if (oldSuccess) oldSuccess.apply(this, arguments)
296
-
297
278
  fire('pjax:success', [data, status, xhr, options])
298
279
  }
299
280
 
@@ -325,9 +306,6 @@ function pjax(options) {
325
306
  var xhr = pjax.xhr = $.ajax(options)
326
307
 
327
308
  if (xhr.readyState > 0) {
328
- // pjax event is deprecated
329
- $(document).trigger('pjax', [xhr, options])
330
-
331
309
  if (options.push && !options.replace) {
332
310
  // Cache current container element before replacing it
333
311
  cachePush(pjax.state.id, context.clone().contents())
@@ -336,9 +314,6 @@ function pjax(options) {
336
314
  }
337
315
 
338
316
  fire('pjax:start', [xhr, options])
339
- // start.pjax is deprecated
340
- fire('start.pjax', [xhr, options])
341
-
342
317
  fire('pjax:send', [xhr, options])
343
318
  }
344
319
 
@@ -359,6 +334,17 @@ function pjaxReload(container, options) {
359
334
  return pjax($.extend(defaults, optionsFor(container, options)))
360
335
  }
361
336
 
337
+ // Internal: Hard replace current state with url.
338
+ //
339
+ // Work for around WebKit
340
+ // https://bugs.webkit.org/show_bug.cgi?id=93506
341
+ //
342
+ // Returns nothing.
343
+ function locationReplace(url) {
344
+ window.history.replaceState(null, "", "#")
345
+ window.location.replace(url)
346
+ }
347
+
362
348
  // popstate handler takes care of the back and forward buttons
363
349
  //
364
350
  // You probably shouldn't use pjax on pages with other pushState
@@ -398,19 +384,13 @@ function onPjaxPopstate(event) {
398
384
  }
399
385
 
400
386
  if (contents) {
401
- // pjax event is deprecated
402
- $(document).trigger('pjax', [null, options])
403
387
  container.trigger('pjax:start', [null, options])
404
- // end.pjax event is deprecated
405
- container.trigger('start.pjax', [null, options])
406
388
 
407
389
  if (state.title) document.title = state.title
408
390
  container.html(contents)
409
391
  pjax.state = state
410
392
 
411
393
  container.trigger('pjax:end', [null, options])
412
- // end.pjax event is deprecated
413
- container.trigger('end.pjax', [null, options])
414
394
  } else {
415
395
  pjax(options)
416
396
  }
@@ -419,7 +399,7 @@ function onPjaxPopstate(event) {
419
399
  // scroll position.
420
400
  container[0].offsetHeight
421
401
  } else {
422
- window.location.replace(location.href)
402
+ locationReplace(location.href)
423
403
  }
424
404
  }
425
405
  }
@@ -585,20 +565,29 @@ function extractContainer(data, xhr, options) {
585
565
  obj.url = stripPjaxParam(xhr.getResponseHeader('X-PJAX-URL') || options.requestUrl)
586
566
 
587
567
  // Attempt to parse response html into elements
588
- var $data = $(data)
568
+ if (/<html/i.test(data)) {
569
+ var $head = $(data.match(/<head[^>]*>([\s\S.]*)<\/head>/i)[0])
570
+ var $body = $(data.match(/<body[^>]*>([\s\S.]*)<\/body>/i)[0])
571
+ } else {
572
+ var $head = $body = $(data)
573
+ }
589
574
 
590
575
  // If response data is empty, return fast
591
- if ($data.length === 0)
576
+ if ($body.length === 0)
592
577
  return obj
593
578
 
594
- // If there's a <title> tag in the response, use it as
579
+ // If there's a <title> tag in the header, use it as
595
580
  // the page's title.
596
- obj.title = findAll($data, 'title').last().text()
581
+ obj.title = findAll($head, 'title').last().text()
597
582
 
598
583
  if (options.fragment) {
599
584
  // If they specified a fragment, look for it in the response
600
585
  // and pull it out.
601
- var $fragment = findAll($data, options.fragment).first()
586
+ if (options.fragment === 'body') {
587
+ var $fragment = $body
588
+ } else {
589
+ var $fragment = findAll($body, options.fragment).first()
590
+ }
602
591
 
603
592
  if ($fragment.length) {
604
593
  obj.contents = $fragment.contents()
@@ -610,7 +599,7 @@ function extractContainer(data, xhr, options) {
610
599
  }
611
600
 
612
601
  } else if (!/<html/i.test(data)) {
613
- obj.contents = $data
602
+ obj.contents = $body
614
603
  }
615
604
 
616
605
  // Clean up any <title> tags
@@ -728,7 +717,8 @@ function disable() {
728
717
  $.pjax.disable = $.noop
729
718
  $.pjax.click = $.noop
730
719
  $.pjax.submit = $.noop
731
- $.pjax.reload = window.location.reload
720
+ $.pjax.reload = function() { window.location.reload() }
721
+
732
722
  $(window).unbind('popstate.pjax', onPjaxPopstate)
733
723
  }
734
724
 
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.3.3
4
+ version: 0.3.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-08-22 00:00:00.000000000 Z
12
+ date: 2012-10-22 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: jquery-rails
@@ -59,7 +59,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
59
59
  version: '0'
60
60
  requirements: []
61
61
  rubyforge_project:
62
- rubygems_version: 1.8.23
62
+ rubygems_version: 1.8.24
63
63
  signing_key:
64
64
  specification_version: 3
65
65
  summary: PJAX integration for Rails 3.1+