19
19
// pjax specific options:
20
20
//
21
21
//
22
- // container - Where to stick the response body. Usually a String selector.
23
- // $(container).html(xhr.responseBody)
24
- // (default: current jquery context)
22
+ // container - String selector for the element where to place the response body.
25
23
// push - Whether to pushState the URL. Defaults to true (of course).
26
24
// replace - Want to use replaceState instead? That's cool.
27
25
//
30
28
//
31
29
// Returns the jQuery object
32
30
function fnPjax ( selector , container , options ) {
33
- var context = this
31
+ options = optionsFor ( container , options )
34
32
return this . on ( 'click.pjax' , selector , function ( event ) {
35
- var opts = $ . extend ( { } , optionsFor ( container , options ) )
36
- if ( ! opts . container )
37
- opts . container = $ ( this ) . attr ( 'data-pjax' ) || context
33
+ var opts = options
34
+ if ( ! opts . container ) {
35
+ opts = $ . extend ( { } , options )
36
+ opts . container = $ ( this ) . attr ( 'data-pjax' )
37
+ }
38
38
handleClick ( event , opts )
39
39
} )
40
40
}
@@ -52,16 +52,12 @@ function fnPjax(selector, container, options) {
52
52
// // is the same as
53
53
// $(document).pjax('a')
54
54
//
55
- // $(document).on('click', 'a', function(event) {
56
- // var container = $(this).closest('[data-pjax-container]')
57
- // $.pjax.click(event, container)
58
- // })
59
- //
60
55
// Returns nothing.
61
56
function handleClick ( event , container , options ) {
62
57
options = optionsFor ( container , options )
63
58
64
59
var link = event . currentTarget
60
+ var $link = $ ( link )
65
61
66
62
if ( link . tagName . toUpperCase ( ) !== 'A' )
67
63
throw "$.fn.pjax or $.pjax.click requires an anchor element"
@@ -85,18 +81,18 @@ function handleClick(event, container, options) {
85
81
86
82
var defaults = {
87
83
url : link . href ,
88
- container : $ ( link ) . attr ( 'data-pjax' ) ,
84
+ container : $link . attr ( 'data-pjax' ) ,
89
85
target : link
90
86
}
91
87
92
88
var opts = $ . extend ( { } , defaults , options )
93
89
var clickEvent = $ . Event ( 'pjax:click' )
94
- $ ( link ) . trigger ( clickEvent , [ opts ] )
90
+ $link . trigger ( clickEvent , [ opts ] )
95
91
96
92
if ( ! clickEvent . isDefaultPrevented ( ) ) {
97
93
pjax ( opts )
98
94
event . preventDefault ( )
99
- $ ( link ) . trigger ( 'pjax:clicked' , [ opts ] )
95
+ $link . trigger ( 'pjax:clicked' , [ opts ] )
100
96
}
101
97
}
102
98
@@ -110,8 +106,7 @@ function handleClick(event, container, options) {
110
106
// Examples
111
107
//
112
108
// $(document).on('submit', 'form', function(event) {
113
- // var container = $(this).closest('[data-pjax-container]')
114
- // $.pjax.submit(event, container)
109
+ // $.pjax.submit(event, '[data-pjax-container]')
115
110
// })
116
111
//
117
112
// Returns nothing.
@@ -132,17 +127,17 @@ function handleSubmit(event, container, options) {
132
127
}
133
128
134
129
if ( defaults . type !== 'GET' && window . FormData !== undefined ) {
135
- defaults . data = new FormData ( form ) ;
136
- defaults . processData = false ;
137
- defaults . contentType = false ;
130
+ defaults . data = new FormData ( form )
131
+ defaults . processData = false
132
+ defaults . contentType = false
138
133
} else {
139
134
// Can't handle file uploads, exit
140
- if ( $ ( form ) . find ( ':file' ) . length ) {
141
- return ;
135
+ if ( $form . find ( ':file' ) . length ) {
136
+ return
142
137
}
143
138
144
139
// Fallback to manually serializing the fields
145
- defaults . data = $ ( form ) . serializeArray ( ) ;
140
+ defaults . data = $form . serializeArray ( )
146
141
}
147
142
148
143
pjax ( $ . extend ( { } , defaults , options ) )
@@ -158,8 +153,7 @@ function handleSubmit(event, container, options) {
158
153
//
159
154
// Accepts these extra keys:
160
155
//
161
- // container - Where to stick the response body.
162
- // $(container).html(xhr.responseBody)
156
+ // container - String selector for where to stick the response body.
163
157
// push - Whether to pushState the URL. Defaults to true (of course).
164
158
// replace - Want to use replaceState instead? That's cool.
165
159
//
@@ -176,26 +170,31 @@ function pjax(options) {
176
170
options . url = options . url ( )
177
171
}
178
172
179
- var target = options . target
180
-
181
173
var hash = parseURL ( options . url ) . hash
182
174
183
- var context = options . context = findContainerFor ( options . container )
175
+ var containerType = $ . type ( options . container )
176
+ if ( containerType !== 'string' ) {
177
+ throw "expected string value for 'container' option; got " + containerType
178
+ }
179
+ var context = options . context = $ ( options . container )
180
+ if ( ! context . length ) {
181
+ throw "the container selector '" + options . container + "' did not match anything"
182
+ }
184
183
185
184
// We want the browser to maintain two separate internal caches: one
186
185
// for pjax'd partial page loads and one for normal page loads.
187
186
// Without adding this secret parameter, some browsers will often
188
187
// confuse the two.
189
188
if ( ! options . data ) options . data = { }
190
189
if ( $ . isArray ( options . data ) ) {
191
- options . data . push ( { name : '_pjax' , value : context . selector } )
190
+ options . data . push ( { name : '_pjax' , value : options . container } )
192
191
} else {
193
- options . data . _pjax = context . selector
192
+ options . data . _pjax = options . container
194
193
}
195
194
196
195
function fire ( type , args , props ) {
197
196
if ( ! props ) props = { }
198
- props . relatedTarget = target
197
+ props . relatedTarget = options . target
199
198
var event = $ . Event ( type , props )
200
199
context . trigger ( event , args )
201
200
return ! event . isDefaultPrevented ( )
@@ -211,7 +210,7 @@ function pjax(options) {
211
210
}
212
211
213
212
xhr . setRequestHeader ( 'X-PJAX' , 'true' )
214
- xhr . setRequestHeader ( 'X-PJAX-Container' , context . selector )
213
+ xhr . setRequestHeader ( 'X-PJAX-Container' , options . container )
215
214
216
215
if ( ! fire ( 'pjax:beforeSend' , [ xhr , settings ] ) )
217
216
return false
@@ -250,7 +249,7 @@ function pjax(options) {
250
249
}
251
250
252
251
options . success = function ( data , status , xhr ) {
253
- var previousState = pjax . state ;
252
+ var previousState = pjax . state
254
253
255
254
// If $.pjax.defaults.version is a function, invoke it first.
256
255
// Otherwise it can be a static string.
@@ -284,7 +283,7 @@ function pjax(options) {
284
283
id : options . id || uniqueId ( ) ,
285
284
url : container . url ,
286
285
title : container . title ,
287
- container : context . selector ,
286
+ container : options . container ,
288
287
fragment : options . fragment ,
289
288
timeout : options . timeout
290
289
}
@@ -318,7 +317,7 @@ function pjax(options) {
318
317
// http://www.w3.org/html/wg/drafts/html/master/forms.html
319
318
var autofocusEl = context . find ( 'input[autofocus], textarea[autofocus]' ) . last ( ) [ 0 ]
320
319
if ( autofocusEl && document . activeElement !== autofocusEl ) {
321
- autofocusEl . focus ( ) ;
320
+ autofocusEl . focus ( )
322
321
}
323
322
324
323
executeScriptTags ( container . scripts )
@@ -347,7 +346,7 @@ function pjax(options) {
347
346
id : uniqueId ( ) ,
348
347
url : window . location . href ,
349
348
title : document . title ,
350
- container : context . selector ,
349
+ container : options . container ,
351
350
fragment : options . fragment ,
352
351
timeout : options . timeout
353
352
}
@@ -363,7 +362,7 @@ function pjax(options) {
363
362
if ( xhr . readyState > 0 ) {
364
363
if ( options . push && ! options . replace ) {
365
364
// Cache current container element before replacing it
366
- cachePush ( pjax . state . id , cloneContents ( context ) )
365
+ cachePush ( pjax . state . id , [ options . container , cloneContents ( context ) ] )
367
366
368
367
window . history . pushState ( null , "" , options . requestUrl )
369
368
}
@@ -448,13 +447,14 @@ function onPjaxPopstate(event) {
448
447
}
449
448
450
449
var cache = cacheMapping [ state . id ] || [ ]
451
- var container = $ ( cache [ 0 ] || state . container ) , contents = cache [ 1 ]
450
+ var containerSelector = cache [ 0 ] || state . container
451
+ var container = $ ( containerSelector ) , contents = cache [ 1 ]
452
452
453
453
if ( container . length ) {
454
454
if ( previousState ) {
455
455
// Cache current container before replacement and inform the
456
456
// cache which direction the history shifted.
457
- cachePop ( direction , previousState . id , cloneContents ( container ) )
457
+ cachePop ( direction , previousState . id , [ containerSelector , cloneContents ( container ) ] )
458
458
}
459
459
460
460
var popstateEvent = $ . Event ( 'pjax:popstate' , {
@@ -466,7 +466,7 @@ function onPjaxPopstate(event) {
466
466
var options = {
467
467
id : state . id ,
468
468
url : state . url ,
469
- container : container ,
469
+ container : containerSelector ,
470
470
push : false ,
471
471
fragment : state . fragment ,
472
472
timeout : state . timeout ,
@@ -568,7 +568,7 @@ function cloneContents(container) {
568
568
cloned . find ( 'script' ) . each ( function ( ) {
569
569
if ( ! this . src ) jQuery . _data ( this , 'globalEval' , false )
570
570
} )
571
- return [ container . selector , cloned . contents ( ) ]
571
+ return cloned . contents ( )
572
572
}
573
573
574
574
// Internal: Strip internal query params from parsed URL.
@@ -618,44 +618,14 @@ function stripHash(location) {
618
618
//
619
619
// Returns options Object.
620
620
function optionsFor ( container , options ) {
621
- // Both container and options
622
- if ( container && options )
621
+ if ( container && options ) {
622
+ options = $ . extend ( { } , options )
623
623
options . container = container
624
-
625
- // First argument is options Object
626
- else if ( $ . isPlainObject ( container ) )
627
- options = container
628
-
629
- // Only container
630
- else
631
- options = { container : container }
632
-
633
- // Find and validate container
634
- if ( options . container )
635
- options . container = findContainerFor ( options . container )
636
-
637
- return options
638
- }
639
-
640
- // Internal: Find container element for a variety of inputs.
641
- //
642
- // Because we can't persist elements using the history API, we must be
643
- // able to find a String selector that will consistently find the Element.
644
- //
645
- // container - A selector String, jQuery object, or DOM Element.
646
- //
647
- // Returns a jQuery object whose context is `document` and has a selector.
648
- function findContainerFor ( container ) {
649
- container = $ ( container )
650
-
651
- if ( ! container . length ) {
652
- throw "no pjax container for " + container . selector
653
- } else if ( container . selector !== '' && container . context === document ) {
624
+ return options
625
+ } else if ( $ . isPlainObject ( container ) ) {
654
626
return container
655
- } else if ( container . attr ( 'id' ) ) {
656
- return $ ( '#' + container . attr ( 'id' ) )
657
627
} else {
658
- throw "cant get selector for pjax container!"
628
+ return { container : container }
659
629
}
660
630
}
661
631
@@ -669,7 +639,7 @@ function findContainerFor(container) {
669
639
//
670
640
// Returns a jQuery object.
671
641
function findAll ( elems , selector ) {
672
- return elems . filter ( selector ) . add ( elems . find ( selector ) ) ;
642
+ return elems . filter ( selector ) . add ( elems . find ( selector ) )
673
643
}
674
644
675
645
function parseHTML ( html ) {
@@ -911,8 +881,11 @@ function disable() {
911
881
912
882
// Add the state property to jQuery's event object so we can use it in
913
883
// $(window).bind('popstate')
914
- if ( $ . inArray ( 'state' , $ . event . props ) < 0 )
884
+ if ( $ . event . props && $ . inArray ( 'state' , $ . event . props ) < 0 ) {
915
885
$ . event . props . push ( 'state' )
886
+ } else if ( ! ( 'state' in $ . Event . prototype ) ) {
887
+ $ . event . addProp ( 'state' )
888
+ }
916
889
917
890
// Is pjax supported by this browser?
918
891
$ . support . pjax =
@@ -922,4 +895,4 @@ $.support.pjax =
922
895
923
896
$ . support . pjax ? enable ( ) : disable ( )
924
897
925
- } ) ( jQuery ) ;
898
+ } ) ( jQuery )
0 commit comments