1 //>>excludeStart("jqmBuildExclude", pragmas.jqmBuildExclude);
2 //>>description: Global initialization of the library.
7 define( [ "jquery", "./jquery.mobile.core", "./jquery.mobile.support", "./jquery.mobile.navigation",
8 "./jquery.mobile.navigation.pushstate", "../external/requirejs/depend!./jquery.mobile.hashchange[jquery]" ], function( $ ) {
9 //>>excludeEnd("jqmBuildExclude");
10 ( function( $, window, undefined ) {
11 var $html = $( "html" ),
13 $window = $( window );
15 // trigger mobileinit event - useful hook for configuring $.mobile settings before they're used
16 $( window.document ).trigger( "mobileinit" );
19 // if device support condition(s) aren't met, leave things as they are -> a basic, usable experience,
20 // otherwise, proceed with the enhancements
21 if ( !$.mobile.gradeA() ) {
25 // override ajaxEnabled on platforms that have known conflicts with hash history updates
26 // or generally work better browsing in regular http for full page refreshes (BB5, Opera Mini)
27 if ( $.mobile.ajaxBlacklist ) {
28 $.mobile.ajaxEnabled = false;
31 // Add mobile, initial load "rendering" classes to docEl
32 $html.addClass( "ui-mobile ui-mobile-rendering" );
34 // This is a fallback. If anything goes wrong (JS errors, etc), or events don't fire,
35 // this ensures the rendering class is removed after 5 seconds, so content is visible and accessible
36 setTimeout( hideRenderingClass, 5000 );
38 // loading div which appears during Ajax requests
39 // will not appear if $.mobile.loadingMessage is false
40 var loaderClass = "ui-loader",
41 $loader = $( "<div class='" + loaderClass + "'><span class='ui-icon ui-icon-loading'></span><h1></h1></div>" );
43 // For non-fixed supportin browsers. Position at y center (if scrollTop supported), above the activeBtn (if defined), or just 100px from top
44 function fakeFixLoader(){
45 var activeBtn = $( "." + $.mobile.activeBtnClass ).first();
49 top: $.support.scrollTop && $window.scrollTop() + $window.height() / 2 ||
50 activeBtn.length && activeBtn.offset().top || 100
54 // check position of loader to see if it appears to be "fixed" to center
55 // if not, use abs positioning
56 function checkLoaderPosition(){
57 var offset = $loader.offset(),
58 scrollTop = $window.scrollTop(),
59 screenHeight = $.mobile.getScreenHeight();
61 if( offset.top < scrollTop || (offset.top - scrollTop) > screenHeight ) {
62 $loader.addClass( "ui-loader-fakefix" );
65 .unbind( "scroll", checkLoaderPosition )
66 .bind( "scroll", fakeFixLoader );
70 //remove initial build class (only present on first pageshow)
71 function hideRenderingClass(){
72 $html.removeClass( "ui-mobile-rendering" );
76 // turn on/off page loading message.
77 showPageLoadingMsg: function( theme, msgText, textonly ) {
78 $html.addClass( "ui-loading" );
80 if ( $.mobile.loadingMessage ) {
81 // text visibility from argument takes priority
82 var textVisible = textonly || $.mobile.loadingMessageTextVisible;
84 theme = theme || $.mobile.loadingMessageTheme,
87 .attr( "class", loaderClass + " ui-corner-all ui-body-" + ( theme || "a" ) + " ui-loader-" + ( textVisible ? "verbose" : "default" ) + ( textonly ? " ui-loader-textonly" : "" ) )
89 .text( msgText || $.mobile.loadingMessage )
91 .appendTo( $.mobile.pageContainer );
93 checkLoaderPosition();
94 $window.bind( "scroll", checkLoaderPosition );
98 hidePageLoadingMsg: function() {
99 $html.removeClass( "ui-loading" );
101 if( $.mobile.loadingMessage ){
102 $loader.removeClass( "ui-loader-fakefix" );
105 $( window ).unbind( "scroll", fakeFixLoader );
106 $( window ).unbind( "scroll", checkLoaderPosition );
109 // find and enhance the pages in the dom and transition to the first page.
110 initializePage: function() {
111 // find present pages
112 var $pages = $( ":jqmData(role='page'), :jqmData(role='dialog')" );
114 // if no pages are found, create one with body's inner html
115 if ( !$pages.length ) {
116 $pages = $( "body" ).wrapInner( "<div data-" + $.mobile.ns + "role='page'></div>" ).children( 0 );
119 // add dialogs, set data-url attrs
120 $pages.each(function() {
123 // unless the data url is already set set it to the pathname
124 if ( !$this.jqmData("url") ) {
125 $this.attr( "data-" + $.mobile.ns + "url", $this.attr( "id" ) || location.pathname + location.search );
129 // define first page in dom case one backs out to the directory root (not always the first page visited, but defined as fallback)
130 $.mobile.firstPage = $pages.first();
132 // define page container
133 $.mobile.pageContainer = $pages.first().parent().addClass( "ui-mobile-viewport" );
135 // alert listeners that the pagecontainer has been determined for binding
136 // to events triggered on it
137 $window.trigger( "pagecontainercreate" );
139 // cue page loading message
140 $.mobile.showPageLoadingMsg();
142 //remove initial build class (only present on first pageshow)
143 hideRenderingClass();
145 // if hashchange listening is disabled or there's no hash deeplink, change to the first page in the DOM
146 if ( !$.mobile.hashListeningEnabled || !$.mobile.path.stripHash( location.hash ) ) {
147 $.mobile.changePage( $.mobile.firstPage, { transition: "none", reverse: true, changeHash: false, fromHashChange: true } );
149 // otherwise, trigger a hashchange to load a deeplink
151 $window.trigger( "hashchange", [ true ] );
156 // initialize events now, after mobileinit has occurred
157 $.mobile._registerInternalEvents();
159 // check which scrollTop value should be used by scrolling to 1 immediately at domready
160 // then check what the scroll top is. Android will report 0... others 1
161 // note that this initial scroll won't hide the address bar. It's just for the check.
163 window.scrollTo( 0, 1 );
165 // if defaultHomeScroll hasn't been set yet, see if scrollTop is 1
166 // it should be 1 in most browsers, but android treats 1 as 0 (for hiding addr bar)
167 // so if it's 1, use 0 from now on
168 $.mobile.defaultHomeScroll = ( !$.support.scrollTop || $(window).scrollTop() === 1 ) ? 0 : 1;
171 // TODO: Implement a proper registration mechanism with dependency handling in order to not have exceptions like the one below
172 //auto self-init widgets for those widgets that have a soft dependency on others
173 if ( $.fn.controlgroup ) {
174 $( document ).bind( "pagecreate create", function( e ){
175 $( ":jqmData(role='controlgroup')", e.target )
177 .controlgroup({ excludeInvisible: false });
182 if( $.mobile.autoInitializePage ){
183 $.mobile.initializePage();
187 // hide iOS browser chrome on load
188 $window.load( $.mobile.silentScroll );
191 //>>excludeStart("jqmBuildExclude", pragmas.jqmBuildExclude);
193 //>>excludeEnd("jqmBuildExclude");