Export 0.2.1
[platform/framework/web/web-ui-fw.git] / libs / js / jquery-mobile-1.2.0 / js / jquery.mobile.core.js
1 //>>excludeStart("jqmBuildExclude", pragmas.jqmBuildExclude);
2 //>>description: Base file for jQuery Mobile
3 //>>label: Core
4 //>>group: Core
5 //>>css.structure: ../css/structure/jquery.mobile.core.css
6 //>>css.theme: ../css/themes/default/jquery.mobile.theme.css
7
8 define( [ "jquery", "text!../version.txt" ], function( $, __version__ ) {
9 //>>excludeEnd("jqmBuildExclude");
10 (function( $, window, undefined ) {
11
12         var nsNormalizeDict = {};
13
14         // jQuery.mobile configurable options
15         $.mobile = $.extend( {}, {
16
17                 // Version of the jQuery Mobile Framework
18                 version: __version__,
19
20                 // Namespace used framework-wide for data-attrs. Default is no namespace
21                 ns: "",
22
23                 // Define the url parameter used for referencing widget-generated sub-pages.
24                 // Translates to to example.html&ui-page=subpageIdentifier
25                 // hash segment before &ui-page= is used to make Ajax request
26                 subPageUrlKey: "ui-page",
27
28                 // Class assigned to page currently in view, and during transitions
29                 activePageClass: "ui-page-active",
30
31                 // Class used for "active" button state, from CSS framework
32                 activeBtnClass: "ui-btn-active",
33
34                 // Class used for "focus" form element state, from CSS framework
35                 focusClass: "ui-focus",
36
37                 // Automatically handle clicks and form submissions through Ajax, when same-domain
38                 ajaxEnabled: true,
39
40                 // Automatically load and show pages based on location.hash
41                 hashListeningEnabled: true,
42
43                 // disable to prevent jquery from bothering with links
44                 linkBindingEnabled: true,
45
46                 // Set default page transition - 'none' for no transitions
47                 defaultPageTransition: "fade",
48
49                 // Set maximum window width for transitions to apply - 'false' for no limit
50                 maxTransitionWidth: false,
51
52                 // Minimum scroll distance that will be remembered when returning to a page
53                 minScrollBack: 250,
54
55                 // DEPRECATED: the following property is no longer in use, but defined until 2.0 to prevent conflicts
56                 touchOverflowEnabled: false,
57
58                 // Set default dialog transition - 'none' for no transitions
59                 defaultDialogTransition: "pop",
60
61                 // Error response message - appears when an Ajax page request fails
62                 pageLoadErrorMessage: "Error Loading Page",
63
64                 // For error messages, which theme does the box uses?
65                 pageLoadErrorMessageTheme: "e",
66
67                 // replace calls to window.history.back with phonegaps navigation helper
68                 // where it is provided on the window object
69                 phonegapNavigationEnabled: false,
70
71                 //automatically initialize the DOM when it's ready
72                 autoInitializePage: true,
73
74                 pushStateEnabled: true,
75
76                 // allows users to opt in to ignoring content by marking a parent element as
77                 // data-ignored
78                 ignoreContentEnabled: false,
79
80                 // turn of binding to the native orientationchange due to android orientation behavior
81                 orientationChangeEnabled: true,
82
83                 buttonMarkup: {
84                         hoverDelay: 200
85                 },
86
87                 // TODO might be useful upstream in jquery itself ?
88                 keyCode: {
89                         ALT: 18,
90                         BACKSPACE: 8,
91                         CAPS_LOCK: 20,
92                         COMMA: 188,
93                         COMMAND: 91,
94                         COMMAND_LEFT: 91, // COMMAND
95                         COMMAND_RIGHT: 93,
96                         CONTROL: 17,
97                         DELETE: 46,
98                         DOWN: 40,
99                         END: 35,
100                         ENTER: 13,
101                         ESCAPE: 27,
102                         HOME: 36,
103                         INSERT: 45,
104                         LEFT: 37,
105                         MENU: 93, // COMMAND_RIGHT
106                         NUMPAD_ADD: 107,
107                         NUMPAD_DECIMAL: 110,
108                         NUMPAD_DIVIDE: 111,
109                         NUMPAD_ENTER: 108,
110                         NUMPAD_MULTIPLY: 106,
111                         NUMPAD_SUBTRACT: 109,
112                         PAGE_DOWN: 34,
113                         PAGE_UP: 33,
114                         PERIOD: 190,
115                         RIGHT: 39,
116                         SHIFT: 16,
117                         SPACE: 32,
118                         TAB: 9,
119                         UP: 38,
120                         WINDOWS: 91 // COMMAND
121                 },
122
123                 // Scroll page vertically: scroll to 0 to hide iOS address bar, or pass a Y value
124                 silentScroll: function( ypos ) {
125                         if ( $.type( ypos ) !== "number" ) {
126                                 ypos = $.mobile.defaultHomeScroll;
127                         }
128
129                         // prevent scrollstart and scrollstop events
130                         $.event.special.scrollstart.enabled = false;
131
132                         setTimeout( function() {
133                                 window.scrollTo( 0, ypos );
134                                 $( document ).trigger( "silentscroll", { x: 0, y: ypos });
135                         }, 20 );
136
137                         setTimeout( function() {
138                                 $.event.special.scrollstart.enabled = true;
139                         }, 150 );
140                 },
141
142                 // Expose our cache for testing purposes.
143                 nsNormalizeDict: nsNormalizeDict,
144
145                 // Take a data attribute property, prepend the namespace
146                 // and then camel case the attribute string. Add the result
147                 // to our nsNormalizeDict so we don't have to do this again.
148                 nsNormalize: function( prop ) {
149                         if ( !prop ) {
150                                 return;
151                         }
152
153                         return nsNormalizeDict[ prop ] || ( nsNormalizeDict[ prop ] = $.camelCase( $.mobile.ns + prop ) );
154                 },
155
156                 // Find the closest parent with a theme class on it. Note that
157                 // we are not using $.fn.closest() on purpose here because this
158                 // method gets called quite a bit and we need it to be as fast
159                 // as possible.
160                 getInheritedTheme: function( el, defaultTheme ) {
161                         var e = el[ 0 ],
162                                 ltr = "",
163                                 re = /ui-(bar|body|overlay)-([a-z])\b/,
164                                 c, m;
165
166                         while ( e ) {
167                                 c = e.className || "";
168                                 if ( c && ( m = re.exec( c ) ) && ( ltr = m[ 2 ] ) ) {
169                                         // We found a parent with a theme class
170                                         // on it so bail from this loop.
171                                         break;
172                                 }
173
174                                 e = e.parentNode;
175                         }
176
177                         // Return the theme letter we found, if none, return the
178                         // specified default.
179
180                         return ltr || defaultTheme || "a";
181                 },
182
183                 // TODO the following $ and $.fn extensions can/probably should be moved into jquery.mobile.core.helpers
184                 //
185                 // Find the closest javascript page element to gather settings data jsperf test
186                 // http://jsperf.com/single-complex-selector-vs-many-complex-selectors/edit
187                 // possibly naive, but it shows that the parsing overhead for *just* the page selector vs
188                 // the page and dialog selector is negligable. This could probably be speed up by
189                 // doing a similar parent node traversal to the one found in the inherited theme code above
190                 closestPageData: function( $target ) {
191                         return $target
192                                 .closest( ':jqmData(role="page"), :jqmData(role="dialog")' )
193                                 .data( "page" );
194                 },
195
196                 enhanceable: function( $set ) {
197                         return this.haveParents( $set, "enhance" );
198                 },
199
200                 hijackable: function( $set ) {
201                         return this.haveParents( $set, "ajax" );
202                 },
203
204                 haveParents: function( $set, attr ) {
205                         if ( !$.mobile.ignoreContentEnabled ) {
206                                 return $set;
207                         }
208
209                         var count = $set.length,
210                                 $newSet = $(),
211                                 e, $element, excluded;
212
213                         for ( var i = 0; i < count; i++ ) {
214                                 $element = $set.eq( i );
215                                 excluded = false;
216                                 e = $set[ i ];
217
218                                 while ( e ) {
219                                         var c = e.getAttribute ? e.getAttribute( "data-" + $.mobile.ns + attr ) : "";
220
221                                         if ( c === "false" ) {
222                                                 excluded = true;
223                                                 break;
224                                         }
225
226                                         e = e.parentNode;
227                                 }
228
229                                 if ( !excluded ) {
230                                         $newSet = $newSet.add( $element );
231                                 }
232                         }
233
234                         return $newSet;
235                 },
236
237                 getScreenHeight: function() {
238                         // Native innerHeight returns more accurate value for this across platforms,
239                         // jQuery version is here as a normalized fallback for platforms like Symbian
240                         return window.innerHeight || $( window ).height();
241                 }
242         }, $.mobile );
243
244         // Mobile version of data and removeData and hasData methods
245         // ensures all data is set and retrieved using jQuery Mobile's data namespace
246         $.fn.jqmData = function( prop, value ) {
247                 var result;
248                 if ( typeof prop !== "undefined" ) {
249                         if ( prop ) {
250                                 prop = $.mobile.nsNormalize( prop );
251                         }
252
253                         // undefined is permitted as an explicit input for the second param
254                         // in this case it returns the value and does not set it to undefined
255                         if( arguments.length < 2 || value === undefined ){
256                                 result = this.data( prop );
257                         } else {
258                                 result = this.data( prop, value );
259                         }
260                 }
261                 return result;
262         };
263
264         $.jqmData = function( elem, prop, value ) {
265                 var result;
266                 if ( typeof prop !== "undefined" ) {
267                         result = $.data( elem, prop ? $.mobile.nsNormalize( prop ) : prop, value );
268                 }
269                 return result;
270         };
271
272         $.fn.jqmRemoveData = function( prop ) {
273                 return this.removeData( $.mobile.nsNormalize( prop ) );
274         };
275
276         $.jqmRemoveData = function( elem, prop ) {
277                 return $.removeData( elem, $.mobile.nsNormalize( prop ) );
278         };
279
280         $.fn.removeWithDependents = function() {
281                 $.removeWithDependents( this );
282         };
283
284         $.removeWithDependents = function( elem ) {
285                 var $elem = $( elem );
286
287                 ( $elem.jqmData( 'dependents' ) || $() ).remove();
288                 $elem.remove();
289         };
290
291         $.fn.addDependents = function( newDependents ) {
292                 $.addDependents( $( this ), newDependents );
293         };
294
295         $.addDependents = function( elem, newDependents ) {
296                 var dependents = $( elem ).jqmData( 'dependents' ) || $();
297
298                 $( elem ).jqmData( 'dependents', $.merge( dependents, newDependents ) );
299         };
300
301         // note that this helper doesn't attempt to handle the callback
302         // or setting of an html elements text, its only purpose is
303         // to return the html encoded version of the text in all cases. (thus the name)
304         $.fn.getEncodedText = function() {
305                 return $( "<div/>" ).text( $( this ).text() ).html();
306         };
307
308         // fluent helper function for the mobile namespaced equivalent
309         $.fn.jqmEnhanceable = function() {
310                 return $.mobile.enhanceable( this );
311         };
312
313         $.fn.jqmHijackable = function() {
314                 return $.mobile.hijackable( this );
315         };
316
317         // Monkey-patching Sizzle to filter the :jqmData selector
318         var oldFind = $.find,
319                 jqmDataRE = /:jqmData\(([^)]*)\)/g;
320
321         $.find = function( selector, context, ret, extra ) {
322                 selector = selector.replace( jqmDataRE, "[data-" + ( $.mobile.ns || "" ) + "$1]" );
323
324                 return oldFind.call( this, selector, context, ret, extra );
325         };
326
327         $.extend( $.find, oldFind );
328
329         $.find.matches = function( expr, set ) {
330                 return $.find( expr, null, null, set );
331         };
332
333         $.find.matchesSelector = function( node, expr ) {
334                 return $.find( expr, null, null, [ node ] ).length > 0;
335         };
336 })( jQuery, this );
337 //>>excludeStart("jqmBuildExclude", pragmas.jqmBuildExclude);
338 });
339 //>>excludeEnd("jqmBuildExclude");
340