Export 0.2.3
[platform/framework/web/web-ui-fw.git] / src / widgets / pagelayout / js / jquery.mobile.tizen.pagelayout.js
1 /* ***************************************************************************
2  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20  * DEALINGS IN THE SOFTWARE.
21  * ***************************************************************************
22  *
23  *      Author: Jinhyuk Jun <jinhyuk.jun@samsung.com>
24  */
25
26 (function ( $, undefined ) {
27
28         $.widget( "mobile.pagelayout", $.mobile.widget, {
29                 options: {
30                         visibleOnPageShow: true,
31                         disablePageZoom: true,
32                         transition: "slide", //can be none, fade, slide (slide maps to slideup or slidedown)
33                         fullscreen: false,
34                         tapToggle: true,
35                         tapToggleBlacklist: "a, input, select, textarea, .ui-header-fixed, .ui-footer-fixed",
36                         hideDuringFocus: "input, textarea, select",
37                         updatePagePadding: true,
38                         trackPersistentToolbars: true,
39                         // Browser detection! Weeee, here we go...
40                         // Unfortunately, position:fixed is costly, not to mention probably impossible, to feature-detect accurately.
41                         // Some tests exist, but they currently return false results in critical devices and browsers, which could lead to a broken experience.
42                         // Testing fixed positioning is also pretty obtrusive to page load, requiring injected elements and scrolling the window
43                         // The following function serves to rule out some popular browsers with known fixed-positioning issues
44                         // This is a plugin option like any other, so feel free to improve or overwrite it
45                         supportBlacklist: function () {
46                                 var w = window,
47                                         ua = navigator.userAgent,
48                                         platform = navigator.platform,
49                                         // Rendering engine is Webkit, and capture major version
50                                         wkmatch = ua.match( /AppleWebKit\/([0-9]+)/ ),
51                                         wkversion = !!wkmatch && wkmatch[ 1 ],
52                                         ffmatch = ua.match( /Fennec\/([0-9]+)/ ),
53                                         ffversion = !!ffmatch && ffmatch[ 1 ],
54                                         operammobilematch = ua.match( /Opera Mobi\/([0-9]+)/ ),
55                                         omversion = !!operammobilematch && operammobilematch[ 1 ];
56
57                                 if (
58                                                 // iOS 4.3 and older : Platform is iPhone/Pad/Touch and Webkit version is less than 534 (ios5)
59                                                 ( ( platform.indexOf( "iPhone" ) > -1 || platform.indexOf( "iPad" ) > -1  || platform.indexOf( "iPod" ) > -1 ) && wkversion && wkversion < 534 )
60                                                 ||
61                                                 // Opera Mini
62                                                 ( w.operamini && ({}).toString.call( w.operamini ) === "[object OperaMini]" )
63                                                 ||
64                                                 ( operammobilematch && omversion < 7458 )
65                                                 ||
66                                                 //Android lte 2.1: Platform is Android and Webkit version is less than 533 (Android 2.2)
67                                                 ( ua.indexOf( "Android" ) > -1 && wkversion && wkversion < 533 )
68                                                 ||
69                                                 // Firefox Mobile before 6.0 -
70                                                 ( ffversion && ffversion < 6 )
71                                                 ||
72                                                 // WebOS less than 3
73                                                 ( "palmGetResource" in window && wkversion && wkversion < 534 )
74                                                 ||
75                                                 // MeeGo
76                                                 ( ua.indexOf( "MeeGo" ) > -1 && ua.indexOf( "NokiaBrowser/8.5.0" ) > -1 )
77                                 ) {
78                                         return true;
79                                 }
80
81                                 return false;
82                         },
83                         initSelector: ":jqmData(role='content')"
84                 },
85
86                 _create: function () {
87
88                         var self = this,
89                                 o = self.options,
90                                 $el = self.element;
91
92                         // Feature detecting support for
93                         if ( o.supportBlacklist() ) {
94                                 self.destroy();
95                                 return;
96                         }
97
98                         self._addFixedClass();
99                         self._addTransitionClass();
100                         self._bindPageEvents();
101
102                         // only content
103                         self._bindContentControlEvents();
104                 },
105
106                 /* add minimum fixed css style to bar(header/footer) and content
107                 *  it need to update when core source modified(jquery.mobile.page.section.js)
108                 *  modified from core source cuz initSelector different */
109                 _addFixedClass: function () {
110                         var self = this,
111                                 o = self.options,
112                                 $el = self.element,
113                                 $elHeader = $el.siblings( ":jqmData(role='header')" ),
114                                 $elFooter = $el.siblings( ":jqmData(role='footer')" ),
115                                 $elPage = $el.closest(".ui-page");
116
117                         $elHeader.addClass( "ui-header-fixed" );
118                         $elFooter.addClass( "ui-footer-fixed" );
119
120                         // "fullscreen" overlay positioning
121                         if ( o.fullscreen ) {
122                                 $elHeader.addClass( "ui-header-fullscreen" );
123                                 $elFooter.addClass( "ui-footer-fullscreen" );
124                                 $elPage
125                                         .addClass( "ui-page-header-fullscreen" )
126                                         .addClass( "ui-page-footer-fullscreen" );
127                         } else {
128                         // If not fullscreen, add class to page to set top or bottom padding
129                                 $elPage.addClass( "ui-page-header-fixed" )
130                                         .addClass( "ui-page-footer-fixed" );
131                         }
132                 },
133
134                 /* original core source(jquery.mobile.fixedToolbar.js)
135                 * never changed */
136                 _addTransitionClass: function () {
137                         var tclass = this.options.transition;
138
139                         if ( tclass && tclass !== "none" ) {
140                                 // use appropriate slide for header or footer
141                                 if ( tclass === "slide" ) {
142                                         tclass = this.element.is( ".ui-header" ) ? "slidedown" : "slideup";
143                                 }
144
145                                 this.element.addClass( tclass );
146                         }
147                 },
148
149
150                 /* Set default page positon
151                 * 1. add title style to header
152                 * 2. Set default header/footer position */
153                 setHeaderFooter: function ( event ) {
154                         var $elPage = $( event.target ),
155                                 $elHeader = $elPage.find( ":jqmData(role='header')" ).length ? $elPage.find( ":jqmData(role='header')") : $elPage.siblings( ":jqmData(role='header')"),
156                                 $elFieldcontain = $elHeader.find( ":jqmData(role='fieldcontain')" ),
157                                 $elControlgroup = $elHeader.find( ":jqmData(role='controlgroup')" ),
158                                 $elContent = $elPage.find( ".ui-content" ),
159                                 next_id,
160                                 $elFooter,
161                                 $elFooterGroup,
162                                 gLength,
163                                 footerButton;
164
165                         if ( $elHeader.jqmData("position") == "fixed" || $.tizen.frameworkData.theme.match(/tizen/) || $elHeader.css("position") == "fixed" ) {
166                                 $elHeader
167                                         .css( "position", "fixed" )
168                                         .css( "top", "0px" );
169                         }
170
171                         /* set Title style */
172                         if ( $elHeader.find("span.ui-title-text-sub").length ) {
173                                 $elHeader.addClass( "ui-title-multiline");
174                         }
175
176                         // divide content mode scrollview and non-scrollview
177                         // recalculate content area when resize callback occur
178                         if ( $.support.scrollview ) {
179                                 if ( $elHeader.css( "position" ) != "fixed" ) {
180                                         $elHeader.css( "position", "fixed" );
181                                 }
182                         } else {
183                                 if ( $elHeader.css("position") != "fixed" ) {
184                                         $elHeader.css( "position", "relative" );
185                                 }
186                         }
187
188                         $elFooter = $( document ).find( ":jqmData(role='footer')" );
189                         footerButton = $elFooter.children( "a" );
190                         footerButton.each( function ( i ) {
191                                 if ( footerButton.eq( i ).is(".ui-btn") && !footerButton.eq( i ).is(".ui-btn-back") ) {
192                                         footerButton.eq( i )
193                                                 .removeClass( "ui-btn-left" )
194                                                 .addClass( "ui-btn-footer-right" );
195                                 }
196                         });
197
198                         if ( $elFooter.find( ".ui-controlgroup" ).length ) {
199                                 footerControlButton = $elFooter.find( ".ui-controlgroup a" );
200                                 footerControlButtonWidth = 100 / $elFooter.find( ".ui-controlgroup a" ).length;
201                                 footerControlButton.each( function ( i ) {
202                                         footerControlButton.eq( i ).css( "max-width", footerControlButtonWidth + "%" );
203                                 });
204                         }
205                 },
206
207                 _bindPageEvents: function () {
208                         var self = this,
209                                 o = self.options,
210                                 $el = self.element;
211
212                         //page event bindings
213                         // Fixed toolbars require page zoom to be disabled, otherwise usability issues crop up
214                         // This method is meant to disable zoom while a fixed-positioned toolbar page is visible
215                         $el.closest( ".ui-page" )
216                                 .bind( "pagebeforeshow", function ( event ) {
217                                         if ( o.disablePageZoom ) {
218                                                 $.mobile.zoom.disable( true );
219                                         }
220                                         if ( !o.visibleOnPageShow ) {
221                                                 self.hide( true );
222                                         }
223                                         self.setHeaderFooter( event );
224                                 } )
225                                 .bind( "webkitAnimationStart animationstart updatelayout", function ( e, data ) {
226                                         var thisPage = this;
227                                         if ( o.updatePagePadding ) {
228                                                 self.updatePagePadding(thisPage);
229                                                 self.updatePageLayout(data);
230                                         }
231                                 })
232
233                                 .bind( "pageshow", function ( event ) {
234                                         var thisPage = this;
235                                         self.updatePagePadding(thisPage);
236                                         self._updateHeaderArea();
237                                         if ( o.updatePagePadding ) {
238                                                 $( window ).bind( "throttledresize." + self.widgetName, function () {
239                                                         self.updatePagePadding(thisPage);
240
241                                                         self.updatePageLayout();
242                                                         self._updateHeaderArea();
243                                                 });
244                                         }
245                                 })
246
247                                 .bind( "pagebeforehide", function ( e, ui ) {
248                                         if ( o.disablePageZoom ) {
249                                                 $.mobile.zoom.enable( true );
250                                         }
251                                         if ( o.updatePagePadding ) {
252                                                 $( window ).unbind( "throttledresize." + self.widgetName );
253                                         }
254
255                                         if ( o.trackPersistentToolbars ) {
256                                                 var thisFooter = $( ".ui-footer-fixed:jqmData(id)", this ),
257                                                         thisHeader = $( ".ui-header-fixed:jqmData(id)", this ),
258                                                         nextFooter = thisFooter.length && ui.nextPage && $( ".ui-footer-fixed:jqmData(id='" + thisFooter.jqmData( "id" ) + "')", ui.nextPage ),
259                                                         nextHeader = thisHeader.length && ui.nextPage && $( ".ui-header-fixed:jqmData(id='" + thisHeader.jqmData( "id" ) + "')", ui.nextPage );
260
261                                                 nextFooter = nextFooter || $();
262
263                                                 if ( nextFooter.length || nextHeader.length ) {
264
265                                                         nextFooter.add( nextHeader ).appendTo( $.mobile.pageContainer );
266
267                                                         ui.nextPage.one( "pageshow", function () {
268                                                                 nextFooter.add( nextHeader ).appendTo( this );
269                                                         });
270                                                 }
271                                         }
272                                 });
273
274                         window.addEventListener( "softkeyboardchange", function( e ) {
275                                 var $elFooter = $( ".ui-page-active .ui-footer" ),
276                                         thisPage = this;
277
278                                 if ( e.state == "on" ) {
279                                         $elFooter.hide();
280                                 } else if (e.state == "off") {
281                                         $elFooter.show();
282                                 }
283                                 self.updatePagePadding( thisPage, e.state );
284                         });
285                 },
286
287                 _bindContentControlEvents: function () {
288                         var self = this,
289                                 o = self.options,
290                                 $el = self.element;
291
292                         $el.closest( ".ui-page" )
293                                 .bind( "pagebeforeshow", function ( event ) {
294
295                                 });
296                 },
297
298                 _updateHeaderArea : function () {
299                         var $elPage = $( ".ui-page-active" ),
300                                 $elHeader = $elPage.find( ":jqmData(role='header')" ).length ? $elPage.find( ":jqmData(role='header')") : $elPage.siblings( ":jqmData(role='header')"),
301                                 headerBtnNum = $elHeader.children("a").length,
302                                 headerSrcNum = $elHeader.children("img").length;
303
304                         $elHeader.find( "h1" ).css( "width", window.innerWidth - $elHeader.children( "a" ).width() * headerBtnNum - $elHeader.children( "a" ).width() / 4 - $elHeader.children( "img" ).width() * headerSrcNum * 4 );
305                         /* add half width for default space between text and button, and img tag area is too narrow, so multiply three for img width*/
306                 },
307
308                 _visible: true,
309 /* IME concenpt change after alpha2.0 */
310 /*              _IMEShown : false,
311                 _IMEindicatorHeight : window.outerHeight - window.innerHeight,
312
313                 layoutPageIME: function () {
314                         if ( $( document.activeElement ).is( "input" ) || $( document.activeElement ).is( "textarea" )
315                                         || $(".ui-page-active .ui-header .input-search-bar").length
316                                         || $(".ui-page-active .ui-content").find("input").length
317                                         || $(".ui-page-active .ui-content").find("textarea").length) {
318
319                                 if ( ( window.innerHeight + this._IMEindicatorHeight ) < window.outerHeight && window.innerWidth == window.outerWidth ) {
320                                         if ( this._IMEShown === false ) {
321                                                 $( ".ui-page-active .ui-footer" ).hide();
322                                                 this._IMEShown = true;
323                                         }
324                                 } else if ( ( window.innerHeight + this._IMEindicatorHeight ) >= window.outerHeight ) {
325                                         if ( this._IMEShown === true ) {
326                                                 $( ".ui-page-active .ui-footer" ).show();
327                                                 this._IMEShown = false;
328                                         }
329                                 }
330                         } else {
331                                 if ( ( window.innerHeight + this._IMEindicatorHeight ) >= window.outerHeight ) {
332                                         if ( this._IMEShown === true ) {
333                                                 $( ".ui-page-active .ui-footer" ).show();
334                                                 this._IMEShown = false;
335                                         }
336                                 }
337                         }
338                 },
339 */
340                 // This will set the content element's top or bottom padding equal to the toolbar's height
341                 updatePagePadding: function ( tbPage, imestatus ) {
342                         var $el = this.element,
343                                 header = $el.siblings( ".ui-header" ).length;
344
345                         // This behavior only applies to "fixed", not "fullscreen"
346                         if ( this.options.fullscreen && imestatus ) { return; }
347
348                         tbPage = tbPage || $el.closest( ".ui-page" );
349                         if ( imestatus == "on" ) {
350                                 $el.height( window.innerHeight - $el.siblings( ".ui-header" ).height() -
351                                         parseFloat( $el.css("padding-top") ) -
352                                         parseFloat( $el.css("padding-bottom") ) );
353                         }
354                         $( tbPage ).css( "padding-" + ( header ? "top" : "bottom" ), $el.siblings( ".ui-header" ).outerHeight() );
355                 },
356
357                 /* 1. Calculate and update content height   */
358                 updatePageLayout: function ( receiveType ) {
359                         var $elFooter,
360                                 $elFooterControlbar,
361                                 $elPage = $( document ).find( ".ui-page-active" ),
362                                 $elHeader = $elPage.find( ":jqmData(role='header')" ),
363                                 $elContent = $elPage.find( ":jqmData(role='content')" ),
364                                 resultContentHeight = 0,
365                                 resultFooterHeight = 0,
366                                 resultHeaderHeight = 0;
367
368                         if ( $elPage.length ) {
369                                 $elFooter = $( document ).find( ".ui-page-active" ).find( ":jqmData(role='footer')" );
370                         } else {
371                                 $elFooter = $( document ).find( ":jqmData(role='footer')" ).eq( 0 );
372                         }
373                         $elFooterControlbar = $elFooter.find( ".ui-navbar" );
374
375                         // calculate footer height
376                         resultFooterHeight = ( $elFooter.css( "display" ) == "none" ) ? 0 : $elFooter.height();
377                         resultHeaderHeight = ( $elHeader.css( "display" ) == "none" ) ? 0 : $elHeader.height();
378
379                         if (resultFooterHeight != 0 ) {
380                                 $elFooter.css( "bottom", 0 );
381                         }
382
383                         resultContentHeight = window.innerHeight - resultFooterHeight - resultHeaderHeight;
384
385                         if ( $.support.scrollview ) {
386                                 if ( $elHeader.css("position") != "fixed" ) {
387                                         $elHeader.css( "position", "fixed" );
388                                 }
389
390                                 $elContent.height( resultContentHeight -
391                                                 parseFloat( $elContent.css("padding-top") ) -
392                                                 parseFloat( $elContent.css("padding-bottom") ) );
393                         } else {
394                                 if ( $elHeader.css("position") != "fixed" ) {
395                                         $elHeader.css( "position", "relative" );
396                                 } else {
397                                         $elContent.height( resultContentHeight );
398                                 }
399                         }
400
401                         // check this line need
402                         // because another style title will be not supported to updatePageLayout
403
404                         // in case title changed
405                         if ( receiveType ) {
406                                 $elContent.css( "top", resultHeaderHeight + "px" );
407                         }
408                 },
409
410                 _useTransition: function ( notransition ) {
411                         var $win = $( window ),
412                                 $el = this.element,
413                                 scroll = $win.scrollTop(),
414                                 elHeight = $el.height(),
415                                 pHeight = $el.closest( ".ui-page" ).height(),
416                                 viewportHeight = $.mobile.getScreenHeight(),
417                                 tbtype = $el.is( ":jqmData(role='header')" ) ? "header" : "footer";
418
419                         return !notransition &&
420                                 ( this.options.transition && this.options.transition !== "none" &&
421                                 (
422                                                 ( tbtype === "header" && !this.options.fullscreen && scroll > elHeight ) ||
423                                                 ( tbtype === "footer" && !this.options.fullscreen && scroll + viewportHeight < pHeight - elHeight )
424                                         ) || this.options.fullscreen
425                                 );
426                 },
427
428                 show: function ( notransition ) {
429 /*                      var hideClass = "ui-fixed-hidden",
430                                 $el = this.element;
431
432                         if ( this._useTransition( notransition ) ){
433                                 $el
434                                         .removeClass( "out " + hideClass )
435                                         .addClass( "in" );
436                         }
437                         else {
438                                 $el.removeClass( hideClass );
439                         }
440                         this._visible = true;*/
441                 },
442
443                 hide: function ( notransition ) {
444 /*                      var hideClass = "ui-fixed-hidden",
445                                 $el = this.element,
446                                 // if it's a slide transition, our new transitions need the reverse class as well to slide outward
447                                 outclass = "out" + ( this.options.transition === "slide" ? " reverse" : "" );
448
449                         if ( this._useTransition( notransition ) ){
450                                 $el
451                                         .addClass( outclass )
452                                         .removeClass( "in" )
453                                         .animationComplete( function () {
454                                                 $el.addClass( hideClass ).removeClass( outclass );
455                                         });
456                         }
457                         else {
458                                 $el.addClass( hideClass ).removeClass( outclass );
459                         }
460                         this._visible = false;*/
461                 },
462
463                 toggle: function () {
464                         this[ this._visible ? "hide" : "show" ]();
465                 },
466
467                 /* support external api for adding backbutton via javascript */
468 /*              backButton: function ( target, status ){
469                         this._addBackbutton( target, "external" );
470                 },
471 */
472                 destroy: function () {
473                         this.element.removeClass( "ui-header-fixed ui-footer-fixed ui-header-fullscreen ui-footer-fullscreen in out fade slidedown slideup ui-fixed-hidden" );
474                         this.element.closest( ".ui-page" ).removeClass( "ui-page-header-fixed ui-page-footer-fixed ui-page-header-fullscreen ui-page-footer-fullscreen" );
475                 }
476
477         });
478
479         //auto self-init widgets
480         $( document )
481                 .bind( "pagecreate create", function ( e ) {
482                         // DEPRECATED in 1.1: support for data-fullscreen=true|false on the page element.
483                         // This line ensures it still works, but we recommend moving the attribute to the toolbars themselves.
484                         if ( $( e.target ).jqmData( "fullscreen" ) ) {
485                                 $( $.mobile.pagelayout.prototype.options.initSelector, e.target ).not( ":jqmData(fullscreen)" ).jqmData( "fullscreen", true );
486                         }
487                         $.mobile.pagelayout.prototype.enhanceWithin( e.target );
488                 });
489
490 })( jQuery );