pagelayout : change to call relayout with this page
[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 ( thisPage ) {
154                         var $elPage = $( thisPage ),
155                                 $elHeader = $elPage.find( ":jqmData(role='header')" ).length ? $elPage.find( ":jqmData(role='header')") : $elPage.siblings( ":jqmData(role='header')"),
156                                 $elContent = $elPage.find( ".ui-content" ),
157                                 $elFooter = $elPage.find( ":jqmData(role='footer')" ),
158                                 $elFooterGroup = $elFooter.find( ":jqmData(role='fieldcontain')" );
159
160                         // divide content mode scrollview and non-scrollview
161                         if ( !$elPage.is( ".ui-dialog" ) ) {
162                                 if ( $elHeader.jqmData("position") == "fixed" || ( $.support.scrollview && $.tizen.frameworkData.theme.match(/tizen/) ) ) {
163                                         $elHeader
164                                                 .css( "position", "fixed" )
165                                                 .css( "top", "0px" );
166                                 } else if ( !$.support.scrollview && $elHeader.jqmData("position") != "fixed" ) {
167                                         $elHeader.css( "position", "relative" );
168                                 }
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                         if ( $elFooterGroup.find( "div" ).is( ".ui-controlgroup-label" ) ) {
177                                 $elFooterGroup.find( "div.ui-controlgroup-label" ).remove();
178                         }
179                 },
180
181                 _bindPageEvents: function () {
182                         var self = this,
183                                 o = self.options,
184                                 $el = self.element,
185                                 $elCurrentFooter;
186
187                         //page event bindings
188                         // Fixed toolbars require page zoom to be disabled, otherwise usability issues crop up
189                         // This method is meant to disable zoom while a fixed-positioned toolbar page is visible
190                         $el.closest( ".ui-page" )
191                                 .bind( "pagebeforeshow", function ( event ) {
192                                         var thisPage = this;
193                                         if ( o.disablePageZoom ) {
194                                                 $.mobile.zoom.disable( true );
195                                         }
196                                         if ( !o.visibleOnPageShow ) {
197                                                 self.hide( true );
198                                         }
199                                         self.setHeaderFooter( thisPage );
200                                         self._setContentMinHeight( thisPage );
201                                 } )
202                                 .bind( "webkitAnimationStart animationstart updatelayout", function ( e, data ) {
203                                         var thisPage = this;
204                                         if ( o.updatePagePadding ) {
205                                                 self.updatePagePadding(thisPage);
206                                                 self.updatePageLayout( false, thisPage);
207                                         }
208                                 })
209
210                                 .bind( "pageshow", function ( event ) {
211                                         var thisPage = this;
212                                         self._setContentMinHeight( thisPage );
213                                         self.updatePagePadding( thisPage );
214                                         self._updateHeaderArea( thisPage );
215                                         if ( o.updatePagePadding ) {
216                                                 $( window ).bind( "throttledresize." + self.widgetName, function () {
217                                                         self.updatePagePadding(thisPage);
218
219                                                         self.updatePageLayout( false, thisPage);
220                                                         self._updateHeaderArea( thisPage );
221                                                         self._setContentMinHeight( thisPage );
222                                                 });
223                                         }
224                                 })
225
226                                 .bind( "pagebeforehide", function ( e, ui ) {
227                                         if ( o.disablePageZoom ) {
228                                                 $.mobile.zoom.enable( true );
229                                         }
230                                         if ( o.updatePagePadding ) {
231                                                 $( window ).unbind( "throttledresize." + self.widgetName );
232                                         }
233
234                                         if ( o.trackPersistentToolbars ) {
235                                                 var thisFooter = $( ".ui-footer-fixed:jqmData(id)", this ),
236                                                         thisHeader = $( ".ui-header-fixed:jqmData(id)", this ),
237                                                         nextFooter = thisFooter.length && ui.nextPage && $( ".ui-footer-fixed:jqmData(id='" + thisFooter.jqmData( "id" ) + "')", ui.nextPage ),
238                                                         nextHeader = thisHeader.length && ui.nextPage && $( ".ui-header-fixed:jqmData(id='" + thisHeader.jqmData( "id" ) + "')", ui.nextPage );
239
240                                                 nextFooter = nextFooter || $();
241
242                                                 if ( nextFooter.length || nextHeader.length ) {
243
244                                                         nextFooter.add( nextHeader ).appendTo( $.mobile.pageContainer );
245
246                                                         ui.nextPage.one( "pageshow", function () {
247                                                                 nextFooter.add( nextHeader ).appendTo( this );
248                                                         });
249                                                 }
250                                         }
251                                 });
252
253                         window.addEventListener( "softkeyboardchange", function( e ) {
254                                 var thisPage = this;
255
256                                 if ( e.state == "on" ) {
257                                         $elCurrentFooter = $( ".ui-page-active .ui-footer" );
258                                         $elCurrentFooter.hide();
259                                 } else if (e.state == "off") {
260                                         $elCurrentFooter.show();
261                                 }
262                                 self.updatePagePadding( thisPage );
263                                 self.updatePageLayout( true, thisPage );
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                 _setContentMinHeight : function ( thisPage ) {
279                         var $elPage = $( thisPage ),
280                                 $elHeader = $elPage.find( ":jqmData(role='header')" ),
281                                 $elFooter = $elPage.find( ":jqmData(role='footer')" ),
282                                 $elContent = $elPage.find( ":jqmData(role='content')" ),
283                                 resultMinHeight;
284
285                         resultMinHeight = window.innerHeight - $elHeader.height() - $elFooter.height();
286
287                         $elContent.css( "min-height", resultMinHeight - parseFloat( $elContent.css("padding-top") ) - parseFloat( $elContent.css("padding-bottom") ) + "px" );
288                 },
289
290                 _updateHeaderArea : function ( thisPage ) {
291                         var $elPage = $( thisPage ),
292                                 $elHeader = $elPage.find( ":jqmData(role='header')" ).length ? $elPage.find( ":jqmData(role='header')") : $elPage.siblings( ":jqmData(role='header')"),
293                                 headerBtnNum = $elHeader.children("a").length,
294                                 headerSrcNum = $elHeader.children("img").length;
295
296                         if ( !$elPage.is( ".ui-dialog" ) ) {
297                                 $elHeader.find( "h1" ).css( "width", window.innerWidth - $elHeader.children( "a" ).width() * headerBtnNum - $elHeader.children( "a" ).width() / 4 - $elHeader.children( "img" ).width() * headerSrcNum * 4 );
298                         }
299                         /* add half width for default space between text and button, and img tag area is too narrow, so multiply three for img width*/
300                 },
301
302                 _visible: true,
303
304                 // This will set the content element's top or bottom padding equal to the toolbar's height
305                 updatePagePadding: function ( tbPage ) {
306                         var $el = this.element,
307                                 header = $el.siblings( ".ui-header" ).length,
308                                 footer = $el.siblings( ".ui-footer" ).length;
309
310                         // This behavior only applies to "fixed", not "fullscreen"
311                         if ( this.options.fullscreen && imestatus ) { return; }
312
313                         tbPage = tbPage || $el.closest( ".ui-page" );
314
315                         if ( $el.siblings( ".ui-header" ).jqmData("position") == "fixed" || $.support.scrollview ) {
316                                 $( tbPage ).css( "padding-top", ( header ? $el.siblings( ".ui-header" ).outerHeight() : 0 ) );
317                         }
318                         $( tbPage ).css( "padding-bottom", ( footer ? $el.siblings( ".ui-footer" ).outerHeight() : 0 ) );
319                 },
320
321                 /* 1. Calculate and update content height   */
322                 updatePageLayout: function ( receiveType, thisPage ) {
323                         var $elFooter,
324                                 $elPage = $( thisPage ),
325                                 $elHeader = $elPage.find( ":jqmData(role='header')" ),
326                                 $elContent = $elPage.find( ":jqmData(role='content')" ),
327                                 resultContentHeight = 0,
328                                 resultFooterHeight = 0,
329                                 resultHeaderHeight = 0;
330
331                         if ( $elPage.length ) {
332                                 $elFooter = $elPage.find( ":jqmData(role='footer')" );
333                         } else {
334                                 $elFooter = $( document ).find( ":jqmData(role='footer')" ).eq( 0 );
335                         }
336
337                         // calculate footer height
338                         resultFooterHeight = ( $elFooter.css( "display" ) == "none" ) ? 0 : $elFooter.height();
339                         resultHeaderHeight = ( $elHeader.css( "display" ) == "none" ) ? 0 : $elHeader.height();
340
341                         if (resultFooterHeight != 0 ) {
342                                 $elFooter.css( "bottom", 0 );
343                         }
344
345                         resultContentHeight = window.innerHeight - resultFooterHeight - resultHeaderHeight;
346
347                         if ( $.support.scrollview ) {
348                                 $elContent.height( resultContentHeight -
349                                                 parseFloat( $elContent.css("padding-top") ) -
350                                                 parseFloat( $elContent.css("padding-bottom") ) );
351                         }
352
353                         // External call page( "refresh") - in case title changed
354                         if ( receiveType ) {
355                                 $elPage
356                                         .css( "min-height", resultContentHeight )
357                                         .css( "padding-top", resultHeaderHeight )
358                                         .css( "padding-bottom", resultFooterHeight );
359                         }
360                 },
361
362                 _useTransition: function ( notransition ) {
363                         var $win = $( window ),
364                                 $el = this.element,
365                                 scroll = $win.scrollTop(),
366                                 elHeight = $el.height(),
367                                 pHeight = $el.closest( ".ui-page" ).height(),
368                                 viewportHeight = $.mobile.getScreenHeight(),
369                                 tbtype = $el.is( ":jqmData(role='header')" ) ? "header" : "footer";
370
371                         return !notransition &&
372                                 ( this.options.transition && this.options.transition !== "none" &&
373                                 (
374                                                 ( tbtype === "header" && !this.options.fullscreen && scroll > elHeight ) ||
375                                                 ( tbtype === "footer" && !this.options.fullscreen && scroll + viewportHeight < pHeight - elHeight )
376                                         ) || this.options.fullscreen
377                                 );
378                 },
379
380                 show: function ( notransition ) {
381 /*                      var hideClass = "ui-fixed-hidden",
382                                 $el = this.element;
383
384                         if ( this._useTransition( notransition ) ){
385                                 $el
386                                         .removeClass( "out " + hideClass )
387                                         .addClass( "in" );
388                         }
389                         else {
390                                 $el.removeClass( hideClass );
391                         }
392                         this._visible = true;*/
393                 },
394
395                 hide: function ( notransition ) {
396 /*                      var hideClass = "ui-fixed-hidden",
397                                 $el = this.element,
398                                 // if it's a slide transition, our new transitions need the reverse class as well to slide outward
399                                 outclass = "out" + ( this.options.transition === "slide" ? " reverse" : "" );
400
401                         if ( this._useTransition( notransition ) ){
402                                 $el
403                                         .addClass( outclass )
404                                         .removeClass( "in" )
405                                         .animationComplete( function () {
406                                                 $el.addClass( hideClass ).removeClass( outclass );
407                                         });
408                         }
409                         else {
410                                 $el.addClass( hideClass ).removeClass( outclass );
411                         }
412                         this._visible = false;*/
413                 },
414
415                 toggle: function () {
416                         this[ this._visible ? "hide" : "show" ]();
417                 },
418
419                 destroy: function () {
420                         this.element.removeClass( "ui-header-fixed ui-footer-fixed ui-header-fullscreen ui-footer-fullscreen in out fade slidedown slideup ui-fixed-hidden" );
421                         this.element.closest( ".ui-page" ).removeClass( "ui-page-header-fixed ui-page-footer-fixed ui-page-header-fullscreen ui-page-footer-fullscreen" );
422                 }
423         });
424
425         //auto self-init widgets
426         $( document )
427                 .bind( "pagecreate create", function ( e ) {
428                         // DEPRECATED in 1.1: support for data-fullscreen=true|false on the page element.
429                         // This line ensures it still works, but we recommend moving the attribute to the toolbars themselves.
430                         if ( $( e.target ).jqmData( "fullscreen" ) ) {
431                                 $( $.mobile.pagelayout.prototype.options.initSelector, e.target ).not( ":jqmData(fullscreen)" ).jqmData( "fullscreen", true );
432                         }
433                         $.mobile.pagelayout.prototype.enhanceWithin( e.target );
434                 });
435
436 })( jQuery );