forked from jquery-archive/jquery-mobile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpagecontainer.js
1278 lines (1005 loc) · 40.7 KB
/
pagecontainer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*!
* jQuery Mobile Page Container @VERSION
* http://jquerymobile.com
*
* Copyright jQuery Foundation and other contributors
* Released under the MIT license.
* http://jquery.org/license
*/
//>>label: Content Management
//>>group: Navigation
//>>description: Widget to create page container which manages pages and transitions
//>>docs: http://api.jquerymobile.com/pagecontainer/
//>>demos: http://demos.jquerymobile.com/@VERSION/navigation/
//>>css.theme: ../css/themes/default/jquery.mobile.theme.css
( function( factory ) {
if ( typeof define === "function" && define.amd ) {
// AMD. Register as an anonymous module.
define( [
"jquery",
"../core",
"jquery-ui/safe-active-element",
"jquery-ui/safe-blur",
"jquery-ui/widget",
"../navigation/path",
"../navigation/base",
"../events/navigate",
"../navigation/history",
"../navigation/navigator",
"../navigation/method",
"../events/scroll",
"../support",
"../widgets/page" ], factory );
} else {
// Browser globals
factory( jQuery );
}
} )( function( $ ) {
// These variables make all page containers use the same queue and only navigate one at a time
// queue to hold simultanious page transitions
var pageTransitionQueue = [],
// Indicates whether or not page is in process of transitioning
isPageTransitioning = false;
$.widget( "mobile.pagecontainer", {
version: "@VERSION",
options: {
theme: "a",
changeOptions: {
transition: undefined,
reverse: false,
changeUrl: true,
// Use changeUrl instead, changeHash is deprecated and will be removed in 1.6
changeHash: true,
fromHashChange: false,
duplicateCachedPage: undefined,
//loading message shows by default when pages are being fetched during change()
showLoadMsg: true,
dataUrl: undefined,
fromPage: undefined,
allowSamePageTransition: false
}
},
initSelector: false,
_create: function() {
var currentOptions = this.options;
currentOptions.changeUrl = currentOptions.changeUrl ? currentOptions.changeUrl :
( currentOptions.changeHash ? true : false );
// Maintain a global array of pagecontainers
$.mobile.pagecontainers = ( $.mobile.pagecontainers ? $.mobile.pagecontainers : [] )
.concat( [ this ] );
// In the future this will be tracked to give easy access to the active pagecontainer
// For now we just set it since multiple containers are not supported.
$.mobile.pagecontainers.active = this;
this._trigger( "beforecreate" );
this.setLastScrollEnabled = true;
this._on( this.window, {
// Disable a scroll setting when a hashchange has been fired, this only works because
// the recording of the scroll position is delayed for 100ms after the browser might
// have changed the position because of the hashchange
navigate: "_disableRecordScroll",
// Bind to scrollstop for the first page, "pagechange" won't be fired in that case
scrollstop: "_delayedRecordScroll"
} );
// TODO consider moving the navigation handler OUT of widget into
// some other object as glue between the navigate event and the
// content widget load and change methods
this._on( this.window, { navigate: "_filterNavigateEvents" } );
// TODO move from page* events to content* events
this._on( { pagechange: "_afterContentChange" } );
this._addClass( "ui-pagecontainer", "ui-mobile-viewport" );
// Handle initial hashchange from chrome :(
this.window.one( "navigate", $.proxy( function() {
this.setLastScrollEnabled = true;
}, this ) );
},
_setOptions: function( options ) {
if ( options.theme !== undefined && options.theme !== "none" ) {
this._removeClass( null, "ui-overlay-" + this.options.theme )
._addClass( null, "ui-overlay-" + options.theme );
} else if ( options.theme !== undefined ) {
this._removeClass( null, "ui-overlay-" + this.options.theme );
}
this._super( options );
},
_disableRecordScroll: function() {
this.setLastScrollEnabled = false;
},
_enableRecordScroll: function() {
this.setLastScrollEnabled = true;
},
// TODO consider the name here, since it's purpose specific
_afterContentChange: function() {
// Once the page has changed, re-enable the scroll recording
this.setLastScrollEnabled = true;
// Remove any binding that previously existed on the get scroll which may or may not be
// different than the scroll element determined for this page previously
this._off( this.window, "scrollstop" );
// Determine and bind to the current scoll element which may be the window or in the case
// of touch overflow the element touch overflow
this._on( this.window, { scrollstop: "_delayedRecordScroll" } );
},
_recordScroll: function() {
// This barrier prevents setting the scroll value based on the browser scrolling the window
// based on a hashchange
if ( !this.setLastScrollEnabled ) {
return;
}
var active = this._getActiveHistory(),
currentScroll, defaultScroll;
if ( active ) {
currentScroll = this._getScroll();
defaultScroll = this._getDefaultScroll();
// Set active page's lastScroll prop. If the location we're scrolling to is less than
// minScrollBack, let it go.
active.lastScroll = currentScroll < defaultScroll ? defaultScroll : currentScroll;
}
},
_delayedRecordScroll: function() {
setTimeout( $.proxy( this, "_recordScroll" ), 100 );
},
_getScroll: function() {
return this.window.scrollTop();
},
_getDefaultScroll: function() {
return $.mobile.defaultHomeScroll;
},
_filterNavigateEvents: function( e, data ) {
var url;
if ( e.originalEvent && e.originalEvent.isDefaultPrevented() ) {
return;
}
url = e.originalEvent.type.indexOf( "hashchange" ) > -1 ? data.state.hash : data.state.url;
if ( !url ) {
url = this._getHash();
}
if ( !url || url === "#" || url.indexOf( "#" + $.mobile.path.uiStateKey ) === 0 ) {
url = location.href;
}
this._handleNavigate( url, data.state );
},
_getHash: function() {
return $.mobile.path.parseLocation().hash;
},
// TODO active page should be managed by the container (ie, it should be a property)
getActivePage: function() {
return this.activePage;
},
// TODO the first page should be a property set during _create using the logic
// that currently resides in init
_getInitialContent: function() {
return $.mobile.firstPage;
},
// TODO each content container should have a history object
_getHistory: function() {
return $.mobile.navigate.history;
},
_getActiveHistory: function() {
return this._getHistory().getActive();
},
// TODO the document base should be determined at creation
_getDocumentBase: function() {
return $.mobile.path.documentBase;
},
back: function() {
this.go( -1 );
},
forward: function() {
this.go( 1 );
},
go: function( steps ) {
// If hashlistening is enabled use native history method
if ( $.mobile.hashListeningEnabled ) {
window.history.go( steps );
} else {
// We are not listening to the hash so handle history internally
var activeIndex = $.mobile.navigate.history.activeIndex,
index = activeIndex + parseInt( steps, 10 ),
url = $.mobile.navigate.history.stack[ index ].url,
direction = ( steps >= 1 ) ? "forward" : "back";
// Update the history object
$.mobile.navigate.history.activeIndex = index;
$.mobile.navigate.history.previousIndex = activeIndex;
// Change to the new page
this.change( url, { direction: direction, changeUrl: false, fromHashChange: true } );
}
},
// TODO rename _handleDestination
_handleDestination: function( to ) {
var history;
// Clean the hash for comparison if it's a url
if ( $.type( to ) === "string" ) {
to = $.mobile.path.stripHash( to );
}
if ( to ) {
history = this._getHistory();
// At this point, 'to' can be one of 3 things, a cached page
// element from a history stack entry, an id, or site-relative /
// absolute URL. If 'to' is an id, we need to resolve it against
// the documentBase, not the location.href, since the hashchange
// could've been the result of a forward/backward navigation
// that crosses from an external page/dialog to an internal
// page/dialog.
//
// TODO move check to history object or path object?
to = !$.mobile.path.isPath( to ) ? ( $.mobile.path.makeUrlAbsolute( "#" + to, this._getDocumentBase() ) ) : to;
}
return to || this._getInitialContent();
},
// The options by which a given page was reached are stored in the history entry for that
// page. When this function is called, history is already at the new entry. So, when moving
// back, this means we need to consult the old entry and reverse the meaning of the
// options. Otherwise, if we're moving forward, we need to consult the options for the
// current entry.
_optionFromHistory: function( direction, optionName, fallbackValue ) {
var history = this._getHistory(),
entry = ( direction === "back" ? history.getLast() : history.getActive() );
return ( ( entry && entry[ optionName ] ) || fallbackValue );
},
_handleDialog: function( changePageOptions, data ) {
var to, active,
activeContent = this.getActivePage();
// If current active page is not a dialog skip the dialog and continue
// in the same direction
// Note: The dialog widget is deprecated as of 1.4.0 and will be removed in 1.5.0.
// Thus, as of 1.5.0 activeContent.data( "mobile-dialog" ) will always evaluate to
// falsy, so the second condition in the if-statement below can be removed altogether.
if ( activeContent && !activeContent.data( "mobile-dialog" ) ) {
// determine if we're heading forward or backward and continue
// accordingly past the current dialog
if ( data.direction === "back" ) {
this.back();
} else {
this.forward();
}
// Prevent change() call
return false;
} else {
// If the current active page is a dialog and we're navigating
// to a dialog use the dialog objected saved in the stack
to = data.pageUrl;
active = this._getActiveHistory();
// Make sure to set the role, transition and reversal
// as most of this is lost by the domCache cleaning
$.extend( changePageOptions, {
role: active.role,
transition: this._optionFromHistory( data.direction, "transition",
changePageOptions.transition ),
reverse: data.direction === "back"
} );
}
return to;
},
_handleNavigate: function( url, data ) {
// Find first page via hash
// TODO stripping the hash twice with handleUrl
var to = $.mobile.path.stripHash( url ),
history = this._getHistory(),
// Transition is false if it's the first page, undefined otherwise (and may be
// overridden by default)
transition = history.stack.length === 0 ? "none" :
this._optionFromHistory( data.direction, "transition" ),
// Default options for the changPage calls made after examining the current state of
// the page and the hash, NOTE that the transition is derived from the previous history
// entry
changePageOptions = {
changeUrl: false,
fromHashChange: true,
reverse: data.direction === "back"
};
$.extend( changePageOptions, data, {
transition: transition,
allowSamePageTransition: this._optionFromHistory( data.direction,
"allowSamePageTransition" )
} );
// TODO move to _handleDestination ?
// If this isn't the first page, if the current url is a dialog hash
// key, and the initial destination isn't equal to the current target
// page, use the special dialog handling
if ( history.activeIndex > 0 &&
to.indexOf( $.mobile.dialogHashKey ) > -1 ) {
to = this._handleDialog( changePageOptions, data );
if ( to === false ) {
return;
}
}
this.change( this._handleDestination( to ), changePageOptions );
},
_getBase: function() {
return $.mobile.base;
},
_getNs: function() {
return $.mobile.ns;
},
_enhance: function( content, role ) {
// TODO consider supporting a custom callback, and passing in
// the settings which includes the role
return content.page( { role: role } );
},
_include: function( page, settings ) {
// Append to page and enhance
page.appendTo( this.element );
// Use the page widget to enhance
this._enhance( page, settings.role );
// Remove page on hide
page.page( "bindRemove" );
},
_find: function( absUrl ) {
// TODO consider supporting a custom callback
var fileUrl = this._createFileUrl( absUrl ),
dataUrl = this._createDataUrl( absUrl ),
page,
initialContent = this._getInitialContent();
// Check to see if the page already exists in the DOM.
// NOTE do _not_ use the :jqmData pseudo selector because parenthesis
// are a valid url char and it breaks on the first occurrence
page = this.element
.children( "[data-" + this._getNs() +
"url='" + $.mobile.path.hashToSelector( dataUrl ) + "']" );
// If we failed to find the page, check to see if the url is a
// reference to an embedded page. If so, it may have been dynamically
// injected by a developer, in which case it would be lacking a
// data-url attribute and in need of enhancement.
if ( page.length === 0 && dataUrl && !$.mobile.path.isPath( dataUrl ) ) {
page = this.element.children( $.mobile.path.hashToSelector( "#" + dataUrl ) )
.attr( "data-" + this._getNs() + "url", dataUrl )
.jqmData( "url", dataUrl );
}
// If we failed to find a page in the DOM, check the URL to see if it
// refers to the first page in the application. Also check to make sure
// our cached-first-page is actually in the DOM. Some user deployed
// apps are pruning the first page from the DOM for various reasons.
// We check for this case here because we don't want a first-page with
// an id falling through to the non-existent embedded page error case.
if ( page.length === 0 &&
$.mobile.path.isFirstPageUrl( fileUrl ) &&
initialContent &&
initialContent.parent().length ) {
page = $( initialContent );
}
return page;
},
_getLoader: function() {
return $.mobile.loading();
},
_showLoading: function( delay, theme, msg, textonly ) {
// This configurable timeout allows cached pages a brief
// delay to load without showing a message
if ( this._loadMsg ) {
return;
}
this._loadMsg = setTimeout( $.proxy( function() {
this._getLoader().loader( "show", theme, msg, textonly );
this._loadMsg = 0;
}, this ), delay );
},
_hideLoading: function() {
// Stop message show timer
clearTimeout( this._loadMsg );
this._loadMsg = 0;
// Hide loading message
this._getLoader().loader( "hide" );
},
_showError: function() {
// Make sure to remove the current loading message
this._hideLoading();
// Show the error message
this._showLoading( 0, $.mobile.pageLoadErrorMessageTheme, $.mobile.pageLoadErrorMessage, true );
// Hide the error message after a delay
// TODO configuration
setTimeout( $.proxy( this, "_hideLoading" ), 1500 );
},
_parse: function( html, fileUrl ) {
// TODO consider allowing customization of this method. It's very JQM specific
var page,
all = $( "<div></div>" );
// Workaround to allow scripts to execute when included in page divs
all.get( 0 ).innerHTML = html;
page = all.find( ":jqmData(role='page'), :jqmData(role='dialog')" ).first();
// If page elem couldn't be found, create one and insert the body element's contents
if ( !page.length ) {
page = $( "<div data-" + this._getNs() + "role='page'>" +
( html.split( /<\/?body[^>]*>/gmi )[ 1 ] || "" ) +
"</div>" );
}
// TODO tagging a page with external to make sure that embedded pages aren't
// removed by the various page handling code is bad. Having page handling code
// in many places is bad. Solutions post 1.0
page.attr( "data-" + this._getNs() + "url", this._createDataUrl( fileUrl ) )
.attr( "data-" + this._getNs() + "external-page", true );
return page;
},
_setLoadedTitle: function( page, html ) {
// Page title regexp
var newPageTitle = html.match( /<title[^>]*>([^<]*)/ ) && RegExp.$1;
if ( newPageTitle && !page.jqmData( "title" ) ) {
newPageTitle = $( "<div>" + newPageTitle + "</div>" ).text();
page.jqmData( "title", newPageTitle );
}
},
_createDataUrl: function( absoluteUrl ) {
return $.mobile.path.convertUrlToDataUrl( absoluteUrl );
},
_createFileUrl: function( absoluteUrl ) {
return $.mobile.path.getFilePath( absoluteUrl );
},
_triggerWithDeprecated: function( name, data, page ) {
var deprecatedEvent = $.Event( "page" + name ),
newEvent = $.Event( this.widgetName + name );
// DEPRECATED
// Trigger the old deprecated event on the page if it's provided
( page || this.element ).trigger( deprecatedEvent, data );
// Use the widget trigger method for the new content* event
this._trigger( name, newEvent, data );
return {
deprecatedEvent: deprecatedEvent,
event: newEvent
};
},
// TODO it would be nice to split this up more but everything appears to be "one off"
// or require ordering such that other bits are sprinkled in between parts that
// could be abstracted out as a group
_loadSuccess: function( absUrl, triggerData, settings, deferred ) {
var fileUrl = this._createFileUrl( absUrl );
return $.proxy( function( html, textStatus, xhr ) {
// Check that Content-Type is "text/html" (https://github.com/jquery/jquery-mobile/issues/8640)
if ( !/^text\/html\b/.test( xhr.getResponseHeader('Content-Type') ) ) {
// Display error message for unsupported content type
if ( settings.showLoadMsg ) {
this._showError();
}
return;
}
// Pre-parse html to check for a data-url, use it as the new fileUrl, base path, etc
var content,
// TODO handle dialogs again
pageElemRegex = new RegExp( "(<[^>]+\\bdata-" + this._getNs() + "role=[\"']?page[\"']?[^>]*>)" ),
dataUrlRegex = new RegExp( "\\bdata-" + this._getNs() + "url=[\"']?([^\"'>]*)[\"']?" );
// data-url must be provided for the base tag so resource requests can be directed to
// the correct url. loading into a temprorary element makes these requests immediately
if ( pageElemRegex.test( html ) &&
RegExp.$1 &&
dataUrlRegex.test( RegExp.$1 ) &&
RegExp.$1 ) {
fileUrl = $.mobile.path.getFilePath( $( "<div>" + RegExp.$1 + "</div>" ).text() );
// We specify that, if a data-url attribute is given on the page div, its value
// must be given non-URL-encoded. However, in this part of the code, fileUrl is
// assumed to be URL-encoded, so we URL-encode the retrieved value here
fileUrl = this.window[ 0 ].encodeURIComponent( fileUrl );
}
// Don't update the base tag if we are prefetching
if ( settings.prefetch === undefined ) {
this._getBase().set( fileUrl );
}
content = this._parse( html, fileUrl );
this._setLoadedTitle( content, html );
// Add the content reference and xhr to our triggerData.
triggerData.xhr = xhr;
triggerData.textStatus = textStatus;
// DEPRECATED
triggerData.page = content;
triggerData.content = content;
triggerData.toPage = content;
// If the default behavior is prevented, stop here!
// Note that it is the responsibility of the listener/handler
// that called preventDefault(), to resolve/reject the
// deferred object within the triggerData.
if ( this._triggerWithDeprecated( "load", triggerData ).event.isDefaultPrevented() ) {
return;
}
this._include( content, settings );
// Remove loading message.
if ( settings.showLoadMsg ) {
this._hideLoading();
}
deferred.resolve( absUrl, settings, content );
}, this );
},
_loadDefaults: {
type: "get",
data: undefined,
reload: false,
// By default we rely on the role defined by the @data-role attribute.
role: undefined,
showLoadMsg: false,
// This delay allows loads that pull from browser cache to
// occur without showing the loading message.
loadMsgDelay: 50
},
load: function( url, options ) {
// This function uses deferred notifications to let callers
// know when the content is done loading, or if an error has occurred.
var deferred = ( options && options.deferred ) || $.Deferred(),
// The default load options with overrides specified by the caller.
settings = $.extend( {}, this._loadDefaults, options ),
// The DOM element for the content after it has been loaded.
content = null,
// The absolute version of the URL passed into the function. This
// version of the URL may contain dialog/subcontent params in it.
absUrl = $.mobile.path.makeUrlAbsolute( url, this._findBaseWithDefault() ),
fileUrl, dataUrl, pblEvent, triggerData;
// If the caller provided data, and we're using "get" request,
// append the data to the URL.
if ( settings.data && settings.type === "get" ) {
absUrl = $.mobile.path.addSearchParams( absUrl, settings.data );
settings.data = undefined;
}
// If the caller is using a "post" request, reload must be true
if ( settings.data && settings.type === "post" ) {
settings.reload = true;
}
// The absolute version of the URL minus any dialog/subcontent params.
// In other words the real URL of the content to be loaded.
fileUrl = this._createFileUrl( absUrl );
// The version of the Url actually stored in the data-url attribute of the content. For
// embedded content, it is just the id of the page. For content within the same domain as
// the document base, it is the site relative path. For cross-domain content (PhoneGap
// only) the entire absolute Url is used to load the content.
dataUrl = this._createDataUrl( absUrl );
content = this._find( absUrl );
// If it isn't a reference to the first content and refers to missing embedded content
// reject the deferred and return
if ( content.length === 0 &&
$.mobile.path.isEmbeddedPage( fileUrl ) &&
!$.mobile.path.isFirstPageUrl( fileUrl ) ) {
deferred.reject( absUrl, settings );
return deferred.promise();
}
// Reset base to the default document base
// TODO figure out why we doe this
this._getBase().reset();
// If the content we are interested in is already in the DOM, and the caller did not
// indicate that we should force a reload of the file, we are done. Resolve the deferrred
// so that users can bind to .done on the promise
if ( content.length && !settings.reload ) {
this._enhance( content, settings.role );
deferred.resolve( absUrl, settings, content );
// If we are reloading the content make sure we update the base if its not a prefetch
if ( !settings.prefetch ) {
this._getBase().set( url );
}
return deferred.promise();
}
triggerData = {
url: url,
absUrl: absUrl,
toPage: url,
prevPage: options ? options.fromPage : undefined,
dataUrl: dataUrl,
deferred: deferred,
options: settings
};
// Let listeners know we're about to load content.
pblEvent = this._triggerWithDeprecated( "beforeload", triggerData );
// If the default behavior is prevented, stop here!
if ( pblEvent.deprecatedEvent.isDefaultPrevented() ||
pblEvent.event.isDefaultPrevented() ) {
return deferred.promise();
}
if ( settings.showLoadMsg ) {
this._showLoading( settings.loadMsgDelay );
}
// Reset base to the default document base. Only reset if we are not prefetching.
if ( settings.prefetch === undefined ) {
this._getBase().reset();
}
if ( !( $.mobile.allowCrossDomainPages ||
$.mobile.path.isSameDomain( $.mobile.path.documentUrl, absUrl ) ) ) {
deferred.reject( absUrl, settings );
return deferred.promise();
}
// Load the new content.
$.ajax( {
url: fileUrl,
type: settings.type,
data: settings.data,
contentType: settings.contentType,
dataType: "html",
success: this._loadSuccess( absUrl, triggerData, settings, deferred ),
error: this._loadError( absUrl, triggerData, settings, deferred )
} );
return deferred.promise();
},
_loadError: function( absUrl, triggerData, settings, deferred ) {
return $.proxy( function( xhr, textStatus, errorThrown ) {
// Set base back to current path
this._getBase().set( $.mobile.path.get() );
// Add error info to our triggerData.
triggerData.xhr = xhr;
triggerData.textStatus = textStatus;
triggerData.errorThrown = errorThrown;
// Clean up internal pending operations like the loader and the transition lock
this._hideLoading();
this._releaseTransitionLock();
// Let listeners know the page load failed.
var plfEvent = this._triggerWithDeprecated( "loadfailed", triggerData );
// If the default behavior is prevented, stop here!
// Note that it is the responsibility of the listener/handler
// that called preventDefault(), to resolve/reject the
// deferred object within the triggerData.
if ( plfEvent.deprecatedEvent.isDefaultPrevented() ||
plfEvent.event.isDefaultPrevented() ) {
return;
}
// Remove loading message.
if ( settings.showLoadMsg ) {
this._showError();
}
deferred.reject( absUrl, settings );
}, this );
},
_getTransitionHandler: function( transition ) {
transition = $.mobile._maybeDegradeTransition( transition );
// Find the transition handler for the specified transition. If there isn't one in our
// transitionHandlers dictionary, use the default one. call the handler immediately to
// kick off the transition.
return $.mobile.transitionHandlers[ transition ] || $.mobile.defaultTransitionHandler;
},
// TODO move into transition handlers?
_triggerCssTransitionEvents: function( to, from, prefix ) {
var samePage = false;
prefix = prefix || "";
// TODO decide if these events should in fact be triggered on the container
if ( from ) {
// Check if this is a same page transition and tell the handler in page
if ( to[ 0 ] === from[ 0 ] ) {
samePage = true;
}
// Trigger before show/hide events
// TODO deprecate nextPage in favor of next
this._triggerWithDeprecated( prefix + "hide", {
// Deprecated in 1.4 remove in 1.5
nextPage: to,
toPage: to,
prevPage: from,
samePage: samePage
}, from );
}
// TODO deprecate prevPage in favor of previous
this._triggerWithDeprecated( prefix + "show", {
prevPage: from || $( "" ),
toPage: to
}, to );
},
_performTransition: function( transition, reverse, to, from ) {
var transitionDeferred = $.Deferred();
if ( from ) {
from.removeClass( "ui-page-active" );
}
if ( to ) {
to.addClass( "ui-page-active" );
}
this._delay( function() {
transitionDeferred.resolve( transition, reverse, to, from, false );
}, 0 );
return transitionDeferred.promise();
},
// TODO make private once change has been defined in the widget
_cssTransition: function( to, from, options ) {
var transition = options.transition,
reverse = options.reverse,
deferred = options.deferred,
promise;
this._triggerCssTransitionEvents( to, from, "before" );
// TODO put this in a binding to events *outside* the widget
this._hideLoading();
promise = this._performTransition( transition, reverse, to, from );
promise.done( $.proxy( function() {
this._triggerCssTransitionEvents( to, from );
}, this ) );
// TODO temporary accomodation of argument deferred
promise.done( function() {
deferred.resolve.apply( deferred, arguments );
} );
},
_releaseTransitionLock: function() {
// Release transition lock so navigation is free again
isPageTransitioning = false;
if ( pageTransitionQueue.length > 0 ) {
this.change.apply( this, pageTransitionQueue.pop() );
}
},
_removeActiveLinkClass: function( force ) {
// Clear out the active button state
$.mobile.removeActiveLinkClass( force );
},
_loadUrl: function( to, triggerData, settings ) {
// Preserve the original target as the dataUrl value will be simplified eg, removing
// ui-state, and removing query params from the hash this is so that users who want to use
// query params have access to them in the event bindings for the page life cycle
// See issue #5085
settings.target = to;
settings.deferred = $.Deferred();
this.load( to, settings );
settings.deferred.done( $.proxy( function( url, options, content ) {
isPageTransitioning = false;
// Store the original absolute url so that it can be provided to events in the
// triggerData of the subsequent change() call
options.absUrl = triggerData.absUrl;
this.transition( content, triggerData, options );
}, this ) );
settings.deferred.fail( $.proxy( function( /* url, options */ ) {
this._removeActiveLinkClass( true );
this._releaseTransitionLock();
this._triggerWithDeprecated( "changefailed", triggerData );
}, this ) );
return settings.deferred.promise();
},
_triggerPageBeforeChange: function( to, triggerData, settings ) {
var returnEvents;
triggerData.prevPage = this.activePage;
$.extend( triggerData, {
toPage: to,
options: settings
} );
// NOTE: preserve the original target as the dataUrl value will be simplified eg, removing
// ui-state, and removing query params from the hash this is so that users who want to use
// query params have access to them in the event bindings for the page life cycle
// See issue #5085
if ( $.type( to ) === "string" ) {
// If the toPage is a string simply convert it
triggerData.absUrl = $.mobile.path.makeUrlAbsolute( to, this._findBaseWithDefault() );
} else {
// If the toPage is a jQuery object grab the absolute url stored in the load()
// callback where it exists
triggerData.absUrl = settings.absUrl;
}
// Let listeners know we're about to change the current page.
returnEvents = this._triggerWithDeprecated( "beforechange", triggerData );
// If the default behavior is prevented, stop here!
if ( returnEvents.event.isDefaultPrevented() ||
returnEvents.deprecatedEvent.isDefaultPrevented() ) {
return false;
}
return true;
},
change: function( to, options ) {
// If we are in the midst of a transition, queue the current request. We'll call
// change() once we're done with the current transition to service the request.
if ( isPageTransitioning ) {
pageTransitionQueue.unshift( arguments );
return;
}
var settings = $.extend( {}, this.options.changeOptions, options ),
triggerData = {};
// Make sure we have a fromPage.
settings.fromPage = settings.fromPage || this.activePage;
// If the page beforechange default is prevented return early
if ( !this._triggerPageBeforeChange( to, triggerData, settings ) ) {
return;
}
// We allow "pagebeforechange" observers to modify the to in the trigger data to allow for
// redirects. Make sure our to is updated. We also need to re-evaluate whether it is a
// string, because an object can also be replaced by a string
to = triggerData.toPage;
// If the caller passed us a url, call load() to make sure it is loaded into the DOM.
// We'll listen to the promise object it returns so we know when it is done loading or if
// an error ocurred.
if ( $.type( to ) === "string" ) {
// Set the isPageTransitioning flag to prevent any requests from entering this method
// while we are in the midst of loading a page or transitioning.