Revert "Export"
[framework/web/web-ui-fw.git] / libs / js / jquery-mobile-1.1.0 / js / jquery.mobile.init.js
1 //>>excludeStart("jqmBuildExclude", pragmas.jqmBuildExclude);
2 //>>description: Global initialization of the library.
3 //>>label: Init
4 //>>group: Core
5
6
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" ),
12                         $head = $( "head" ),
13                         $window = $( window );
14
15         // trigger mobileinit event - useful hook for configuring $.mobile settings before they're used
16         $( window.document ).trigger( "mobileinit" );
17
18         // support conditions
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() ) {
22                 return;
23         }
24
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;
29         }
30
31         // Add mobile, initial load "rendering" classes to docEl
32         $html.addClass( "ui-mobile ui-mobile-rendering" );
33
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 );
37
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>" );
42
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();
46
47                 $loader
48                         .css({
49                                 top: $.support.scrollTop && $window.scrollTop() + $window.height() / 2 ||
50                                 activeBtn.length && activeBtn.offset().top || 100
51                         });
52         }
53
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();
60
61                 if( offset.top < scrollTop || (offset.top - scrollTop) > screenHeight ) {
62                         $loader.addClass( "ui-loader-fakefix" );
63                         fakeFixLoader();
64                         $window
65                                 .unbind( "scroll", checkLoaderPosition )
66                                 .bind( "scroll", fakeFixLoader );
67                 }
68         }
69
70         //remove initial build class (only present on first pageshow)
71         function hideRenderingClass(){
72                 $html.removeClass( "ui-mobile-rendering" );
73         }
74
75         $.extend($.mobile, {
76                 // turn on/off page loading message.
77                 showPageLoadingMsg: function( theme, msgText, textonly ) {
78                         $html.addClass( "ui-loading" );
79
80                         if ( $.mobile.loadingMessage ) {
81                                 // text visibility from argument takes priority
82                                 var textVisible = textonly || $.mobile.loadingMessageTextVisible;
83
84                                 theme = theme || $.mobile.loadingMessageTheme,
85
86                                 $loader
87                                         .attr( "class", loaderClass + " ui-corner-all ui-body-" + ( theme || "a" ) + " ui-loader-" + ( textVisible ? "verbose" : "default" ) + ( textonly ? " ui-loader-textonly" : "" ) )
88                                         .find( "h1" )
89                                                 .text( msgText || $.mobile.loadingMessage )
90                                                 .end()
91                                         .appendTo( $.mobile.pageContainer );
92
93                                 checkLoaderPosition();
94                                 $window.bind( "scroll", checkLoaderPosition );
95                         }
96                 },
97
98                 hidePageLoadingMsg: function() {
99                         $html.removeClass( "ui-loading" );
100
101                         if( $.mobile.loadingMessage ){
102                                 $loader.removeClass( "ui-loader-fakefix" );
103                         }
104
105                         $( window ).unbind( "scroll", fakeFixLoader );
106                         $( window ).unbind( "scroll", checkLoaderPosition );
107                 },
108
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')" );
113
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 );
117                         }
118
119                         // add dialogs, set data-url attrs
120                         $pages.each(function() {
121                                 var $this = $(this);
122
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 );
126                                 }
127                         });
128
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();
131
132                         // define page container
133                         $.mobile.pageContainer = $pages.first().parent().addClass( "ui-mobile-viewport" );
134
135                         // alert listeners that the pagecontainer has been determined for binding
136                         // to events triggered on it
137                         $window.trigger( "pagecontainercreate" );
138
139                         // cue page loading message
140                         $.mobile.showPageLoadingMsg();
141
142                         //remove initial build class (only present on first pageshow)
143                         hideRenderingClass();
144
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 } );
148                         }
149                         // otherwise, trigger a hashchange to load a deeplink
150                         else {
151                                 $window.trigger( "hashchange", [ true ] );
152                         }
153                 }
154         });
155
156         // initialize events now, after mobileinit has occurred
157         $.mobile._registerInternalEvents();
158
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.
162         $(function() {
163                 window.scrollTo( 0, 1 );
164
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;
169
170
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 )
176                                         .jqmEnhanceable()
177                                         .controlgroup({ excludeInvisible: false });
178                         });
179                 }
180
181                 //dom-ready inits
182                 if( $.mobile.autoInitializePage ){
183                         $.mobile.initializePage();
184                 }
185
186                 // window load event
187                 // hide iOS browser chrome on load
188                 $window.load( $.mobile.silentScroll );
189         });
190 }( jQuery, this ));
191 //>>excludeStart("jqmBuildExclude", pragmas.jqmBuildExclude);
192 });
193 //>>excludeEnd("jqmBuildExclude");