pagelayout : content min height bug fix
[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 = $( document ).find( ":jqmData(role='footer')" ),
161                                 $elFooterGroup = $elFooter.find( ":jqmData(role='fieldcontain')" ),
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                         if ( $elFooterGroup.find( "div" ).is( ".ui-controlgroup-label" ) ) {
189                                 $elFooterGroup.find( "div.ui-controlgroup-label" ).remove();
190                         }
191                 },
192
193                 _bindPageEvents: function () {
194                         var self = this,
195                                 o = self.options,
196                                 $el = self.element;
197
198                         //page event bindings
199                         // Fixed toolbars require page zoom to be disabled, otherwise usability issues crop up
200                         // This method is meant to disable zoom while a fixed-positioned toolbar page is visible
201                         $el.closest( ".ui-page" )
202                                 .bind( "pagebeforeshow", function ( event ) {
203                                         if ( o.disablePageZoom ) {
204                                                 $.mobile.zoom.disable( true );
205                                         }
206                                         if ( !o.visibleOnPageShow ) {
207                                                 self.hide( true );
208                                         }
209                                         self.setHeaderFooter( event );
210                                         self._setContentMinHeight( event );
211                                 } )
212                                 .bind( "webkitAnimationStart animationstart updatelayout", function ( e, data ) {
213                                         var thisPage = this;
214                                         if ( o.updatePagePadding ) {
215                                                 self.updatePagePadding(thisPage);
216                                                 self.updatePageLayout(data);
217                                         }
218                                 })
219
220                                 .bind( "pageshow", function ( event ) {
221                                         var thisPage = this;
222                                         self.updatePagePadding(thisPage);
223                                         self._updateHeaderArea();
224                                         self._setContentMinHeight( event );
225                                         if ( o.updatePagePadding ) {
226                                                 $( window ).bind( "throttledresize." + self.widgetName, function () {
227                                                         self.updatePagePadding(thisPage);
228
229                                                         self.updatePageLayout();
230                                                         self._updateHeaderArea();
231                                                         self._setContentMinHeight( event );
232                                                 });
233                                         }
234                                 })
235
236                                 .bind( "pagebeforehide", function ( e, ui ) {
237                                         if ( o.disablePageZoom ) {
238                                                 $.mobile.zoom.enable( true );
239                                         }
240                                         if ( o.updatePagePadding ) {
241                                                 $( window ).unbind( "throttledresize." + self.widgetName );
242                                         }
243
244                                         if ( o.trackPersistentToolbars ) {
245                                                 var thisFooter = $( ".ui-footer-fixed:jqmData(id)", this ),
246                                                         thisHeader = $( ".ui-header-fixed:jqmData(id)", this ),
247                                                         nextFooter = thisFooter.length && ui.nextPage && $( ".ui-footer-fixed:jqmData(id='" + thisFooter.jqmData( "id" ) + "')", ui.nextPage ),
248                                                         nextHeader = thisHeader.length && ui.nextPage && $( ".ui-header-fixed:jqmData(id='" + thisHeader.jqmData( "id" ) + "')", ui.nextPage );
249
250                                                 nextFooter = nextFooter || $();
251
252                                                 if ( nextFooter.length || nextHeader.length ) {
253
254                                                         nextFooter.add( nextHeader ).appendTo( $.mobile.pageContainer );
255
256                                                         ui.nextPage.one( "pageshow", function () {
257                                                                 nextFooter.add( nextHeader ).appendTo( this );
258                                                         });
259                                                 }
260                                         }
261                                 });
262
263                         window.addEventListener( "softkeyboardchange", function( e ) {
264                                 var $elFooter = $( ".ui-page-active .ui-footer" ),
265                                         thisPage = this;
266
267                                 if ( e.state == "on" ) {
268                                         $elFooter.hide();
269                                 } else if (e.state == "off") {
270                                         $elFooter.show();
271                                 }
272                                 self.updatePagePadding( thisPage, e.state );
273                         });
274                 },
275
276                 _bindContentControlEvents: function () {
277                         var self = this,
278                                 o = self.options,
279                                 $el = self.element;
280
281                         $el.closest( ".ui-page" )
282                                 .bind( "pagebeforeshow", function ( event ) {
283
284                                 });
285                 },
286
287                 _setContentMinHeight : function ( event ) {
288                         var $elPage = $( event.target ),
289                                 $elHeader = $elPage.find( ":jqmData(role='header')" ),
290                                 $elFooter = $elPage.find( ":jqmData(role='footer')" ),
291                                 $elContent = $elPage.find( ":jqmData(role='content')" ),
292                                 resultMinHeight;
293
294                         resultMinHeight = window.innerHeight - $elHeader.height() - $elFooter.height();
295
296                         if ( $.support.scrollview ) {
297                                 $elContent.css( "min-height", resultMinHeight - parseFloat( $elContent.css("padding-top") ) - parseFloat( $elContent.css("padding-bottom") ) + "px" );
298                         } else {
299                                 $elContent.css( "min-height", resultMinHeight + "px" );
300                         }
301                 },
302
303                 _updateHeaderArea : function () {
304                         var $elPage = $( ".ui-page-active" ),
305                                 $elHeader = $elPage.find( ":jqmData(role='header')" ).length ? $elPage.find( ":jqmData(role='header')") : $elPage.siblings( ":jqmData(role='header')"),
306                                 headerBtnNum = $elHeader.children("a").length,
307                                 headerSrcNum = $elHeader.children("img").length;
308
309                         $elHeader.find( "h1" ).css( "width", window.innerWidth - $elHeader.children( "a" ).width() * headerBtnNum - $elHeader.children( "a" ).width() / 4 - $elHeader.children( "img" ).width() * headerSrcNum * 4 );
310                         /* add half width for default space between text and button, and img tag area is too narrow, so multiply three for img width*/
311                 },
312
313                 _visible: true,
314 /* IME concenpt change after alpha2.0 */
315 /*              _IMEShown : false,
316                 _IMEindicatorHeight : window.outerHeight - window.innerHeight,
317
318                 layoutPageIME: function () {
319                         if ( $( document.activeElement ).is( "input" ) || $( document.activeElement ).is( "textarea" )
320                                         || $(".ui-page-active .ui-header .input-search-bar").length
321                                         || $(".ui-page-active .ui-content").find("input").length
322                                         || $(".ui-page-active .ui-content").find("textarea").length) {
323
324                                 if ( ( window.innerHeight + this._IMEindicatorHeight ) < window.outerHeight && window.innerWidth == window.outerWidth ) {
325                                         if ( this._IMEShown === false ) {
326                                                 $( ".ui-page-active .ui-footer" ).hide();
327                                                 this._IMEShown = true;
328                                         }
329                                 } else if ( ( window.innerHeight + this._IMEindicatorHeight ) >= window.outerHeight ) {
330                                         if ( this._IMEShown === true ) {
331                                                 $( ".ui-page-active .ui-footer" ).show();
332                                                 this._IMEShown = false;
333                                         }
334                                 }
335                         } else {
336                                 if ( ( window.innerHeight + this._IMEindicatorHeight ) >= window.outerHeight ) {
337                                         if ( this._IMEShown === true ) {
338                                                 $( ".ui-page-active .ui-footer" ).show();
339                                                 this._IMEShown = false;
340                                         }
341                                 }
342                         }
343                 },
344 */
345                 // This will set the content element's top or bottom padding equal to the toolbar's height
346                 updatePagePadding: function ( tbPage, imestatus ) {
347                         var $el = this.element,
348                                 header = $el.siblings( ".ui-header" ).length;
349
350                         // This behavior only applies to "fixed", not "fullscreen"
351                         if ( this.options.fullscreen && imestatus ) { return; }
352
353                         tbPage = tbPage || $el.closest( ".ui-page" );
354                         if ( imestatus == "on" ) {
355                                 $el.height( window.innerHeight - $el.siblings( ".ui-header" ).height() -
356                                         parseFloat( $el.css("padding-top") ) -
357                                         parseFloat( $el.css("padding-bottom") ) );
358                         }
359                         $( tbPage ).css( "padding-" + ( header ? "top" : "bottom" ), $el.siblings( ".ui-header" ).outerHeight() );
360                 },
361
362                 /* 1. Calculate and update content height   */
363                 updatePageLayout: function ( receiveType ) {
364                         var $elFooter,
365                                 $elFooterControlbar,
366                                 $elPage = $( document ).find( ".ui-page-active" ),
367                                 $elHeader = $elPage.find( ":jqmData(role='header')" ),
368                                 $elContent = $elPage.find( ":jqmData(role='content')" ),
369                                 resultContentHeight = 0,
370                                 resultFooterHeight = 0,
371                                 resultHeaderHeight = 0;
372
373                         if ( $elPage.length ) {
374                                 $elFooter = $( document ).find( ".ui-page-active" ).find( ":jqmData(role='footer')" );
375                         } else {
376                                 $elFooter = $( document ).find( ":jqmData(role='footer')" ).eq( 0 );
377                         }
378                         $elFooterControlbar = $elFooter.find( ".ui-navbar" );
379
380                         // calculate footer height
381                         resultFooterHeight = ( $elFooter.css( "display" ) == "none" ) ? 0 : $elFooter.height();
382                         resultHeaderHeight = ( $elHeader.css( "display" ) == "none" ) ? 0 : $elHeader.height();
383
384                         if (resultFooterHeight != 0 ) {
385                                 $elFooter.css( "bottom", 0 );
386                         }
387
388                         resultContentHeight = window.innerHeight - resultFooterHeight - resultHeaderHeight;
389
390                         if ( $.support.scrollview ) {
391                                 if ( $elHeader.css("position") != "fixed" ) {
392                                         $elHeader.css( "position", "fixed" );
393                                 }
394
395                                 $elContent.height( resultContentHeight -
396                                                 parseFloat( $elContent.css("padding-top") ) -
397                                                 parseFloat( $elContent.css("padding-bottom") ) );
398                         } else {
399                                 if ( $elHeader.css("position") != "fixed" ) {
400                                         $elHeader.css( "position", "relative" );
401                                 } else {
402                                         $elContent.height( resultContentHeight );
403                                 }
404                         }
405
406                         // check this line need
407                         // because another style title will be not supported to updatePageLayout
408
409                         // in case title changed
410                         if ( receiveType ) {
411                                 $elContent.css( "top", resultHeaderHeight + "px" );
412                         }
413                 },
414
415                 _useTransition: function ( notransition ) {
416                         var $win = $( window ),
417                                 $el = this.element,
418                                 scroll = $win.scrollTop(),
419                                 elHeight = $el.height(),
420                                 pHeight = $el.closest( ".ui-page" ).height(),
421                                 viewportHeight = $.mobile.getScreenHeight(),
422                                 tbtype = $el.is( ":jqmData(role='header')" ) ? "header" : "footer";
423
424                         return !notransition &&
425                                 ( this.options.transition && this.options.transition !== "none" &&
426                                 (
427                                                 ( tbtype === "header" && !this.options.fullscreen && scroll > elHeight ) ||
428                                                 ( tbtype === "footer" && !this.options.fullscreen && scroll + viewportHeight < pHeight - elHeight )
429                                         ) || this.options.fullscreen
430                                 );
431                 },
432
433                 show: function ( notransition ) {
434 /*                      var hideClass = "ui-fixed-hidden",
435                                 $el = this.element;
436
437                         if ( this._useTransition( notransition ) ){
438                                 $el
439                                         .removeClass( "out " + hideClass )
440                                         .addClass( "in" );
441                         }
442                         else {
443                                 $el.removeClass( hideClass );
444                         }
445                         this._visible = true;*/
446                 },
447
448                 hide: function ( notransition ) {
449 /*                      var hideClass = "ui-fixed-hidden",
450                                 $el = this.element,
451                                 // if it's a slide transition, our new transitions need the reverse class as well to slide outward
452                                 outclass = "out" + ( this.options.transition === "slide" ? " reverse" : "" );
453
454                         if ( this._useTransition( notransition ) ){
455                                 $el
456                                         .addClass( outclass )
457                                         .removeClass( "in" )
458                                         .animationComplete( function () {
459                                                 $el.addClass( hideClass ).removeClass( outclass );
460                                         });
461                         }
462                         else {
463                                 $el.addClass( hideClass ).removeClass( outclass );
464                         }
465                         this._visible = false;*/
466                 },
467
468                 toggle: function () {
469                         this[ this._visible ? "hide" : "show" ]();
470                 },
471
472                 /* support external api for adding backbutton via javascript */
473 /*              backButton: function ( target, status ){
474                         this._addBackbutton( target, "external" );
475                 },
476 */
477                 destroy: function () {
478                         this.element.removeClass( "ui-header-fixed ui-footer-fixed ui-header-fullscreen ui-footer-fullscreen in out fade slidedown slideup ui-fixed-hidden" );
479                         this.element.closest( ".ui-page" ).removeClass( "ui-page-header-fixed ui-page-footer-fixed ui-page-header-fullscreen ui-page-footer-fullscreen" );
480                 }
481
482         });
483
484         //auto self-init widgets
485         $( document )
486                 .bind( "pagecreate create", function ( e ) {
487                         // DEPRECATED in 1.1: support for data-fullscreen=true|false on the page element.
488                         // This line ensures it still works, but we recommend moving the attribute to the toolbars themselves.
489                         if ( $( e.target ).jqmData( "fullscreen" ) ) {
490                                 $( $.mobile.pagelayout.prototype.options.initSelector, e.target ).not( ":jqmData(fullscreen)" ).jqmData( "fullscreen", true );
491                         }
492                         $.mobile.pagelayout.prototype.enhanceWithin( e.target );
493                 });
494
495 })( jQuery );