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