0e2980ecd3be2ce0d7dfc7e38ea988023f5548af
[platform/framework/web/web-ui-fw.git] / src / js / widgets / jquery.mobile.tizen.pagelayout.js
1 //>>excludeStart("jqmBuildExclude", pragmas.jqmBuildExclude);
2 //>>description: Set a layout of pages
3 //>>label: Pagelayout
4 //>>group: Tizen:Widgets
5
6 define( [ '../jquery.mobile.tizen.core' ], function ( ) {
7 //>>excludeEnd("jqmBuildExclude");
8
9 /* ***************************************************************************
10  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd.
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a
13  * copy of this software and associated documentation files (the "Software"),
14  * to deal in the Software without restriction, including without limitation
15  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
16  * and/or sell copies of the Software, and to permit persons to whom the
17  * Software is furnished to do so, subject to the following conditions:
18  *
19  * The above copyright notice and this permission notice shall be included in
20  * all copies or substantial portions of the Software.
21  *
22  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
28  * DEALINGS IN THE SOFTWARE.
29  * ***************************************************************************
30  *
31  *      Author: Jinhyuk Jun <jinhyuk.jun@samsung.com>
32  */
33
34 (function ( $, undefined ) {
35
36         $.widget( "mobile.pagelayout", $.mobile.widget, {
37                 options: {
38                         visibleOnPageShow: true,
39                         disablePageZoom: true,
40                         transition: "slide", //can be none, fade, slide (slide maps to slideup or slidedown)
41                         fullscreen: false,
42                         tapToggle: true,
43                         tapToggleBlacklist: "a, input, select, textarea, .ui-header-fixed, .ui-footer-fixed",
44                         hideDuringFocus: "input, textarea, select",
45                         updatePagePadding: true,
46                         trackPersistentToolbars: true,
47                         // Browser detection! Weeee, here we go...
48                         // Unfortunately, position:fixed is costly, not to mention probably impossible, to feature-detect accurately.
49                         // Some tests exist, but they currently return false results in critical devices and browsers, which could lead to a broken experience.
50                         // Testing fixed positioning is also pretty obtrusive to page load, requiring injected elements and scrolling the window
51                         // The following function serves to rule out some popular browsers with known fixed-positioning issues
52                         // This is a plugin option like any other, so feel free to improve or overwrite it
53                         supportBlacklist: function () {
54                                 var w = window,
55                                         ua = navigator.userAgent,
56                                         platform = navigator.platform,
57                                         // Rendering engine is Webkit, and capture major version
58                                         wkmatch = ua.match( /AppleWebKit\/([0-9]+)/ ),
59                                         wkversion = !!wkmatch && wkmatch[ 1 ],
60                                         ffmatch = ua.match( /Fennec\/([0-9]+)/ ),
61                                         ffversion = !!ffmatch && ffmatch[ 1 ],
62                                         operammobilematch = ua.match( /Opera Mobi\/([0-9]+)/ ),
63                                         omversion = !!operammobilematch && operammobilematch[ 1 ];
64
65                                 if (
66                                         // iOS 4.3 and older : Platform is iPhone/Pad/Touch and Webkit version is less than 534 (ios5)
67                                         ( ( platform.indexOf( "iPhone" ) > -1 || platform.indexOf( "iPad" ) > -1  || platform.indexOf( "iPod" ) > -1 ) && wkversion && wkversion < 534 ) ||
68                                                 // Opera Mini
69                                                 ( w.operamini && ({}).toString.call( w.operamini ) === "[object OperaMini]" ) ||
70                                                 ( operammobilematch && omversion < 7458 ) ||
71                                                 //Android lte 2.1: Platform is Android and Webkit version is less than 533 (Android 2.2)
72                                                 ( ua.indexOf( "Android" ) > -1 && wkversion && wkversion < 533 ) ||
73                                                 // Firefox Mobile before 6.0 -
74                                                 ( ffversion && ffversion < 6 ) ||
75                                                 // WebOS less than 3
76                                                 ( window.palmGetResource !== undefined && wkversion && wkversion < 534 ) ||
77                                                 // MeeGo
78                                                 ( ua.indexOf( "MeeGo" ) > -1 && ua.indexOf( "NokiaBrowser/8.5.0" ) > -1 )
79                                 ) {
80                                         return true;
81                                 }
82
83                                 return false;
84                         },
85                         initSelector: ":jqmData(role='content')"
86                 },
87
88                 _create: function () {
89
90                         var self = this,
91                                 o = self.options,
92                                 $el = self.element;
93
94                         // Feature detecting support for
95                         if ( o.supportBlacklist() ) {
96                                 self.destroy();
97                                 return;
98                         }
99
100                         self._addFixedClass();
101                         self._addTransitionClass();
102                         self._bindPageEvents();
103
104                         // only content
105                         self._bindContentControlEvents();
106                 },
107
108                 /* add minimum fixed css style to bar(header/footer) and content
109                 *  it need to update when core source modified(jquery.mobile.page.section.js)
110                 *  modified from core source cuz initSelector different */
111                 _addFixedClass: function () {
112                         var self = this,
113                                 o = self.options,
114                                 $el = self.element,
115                                 $elHeader = $el.siblings( ":jqmData(role='header')" ),
116                                 $elFooter = $el.siblings( ":jqmData(role='footer')" ),
117                                 $elPage = $el.closest(".ui-page");
118
119                         $elHeader.addClass( "ui-header-fixed" );
120                         $elFooter.addClass( "ui-footer-fixed" );
121
122                         // "fullscreen" overlay positioning
123                         if ( o.fullscreen ) {
124                                 $elHeader.addClass( "ui-header-fullscreen" );
125                                 $elFooter.addClass( "ui-footer-fullscreen" );
126                                 $elPage
127                                         .addClass( "ui-page-header-fullscreen" )
128                                         .addClass( "ui-page-footer-fullscreen" );
129                         } else {
130                         // If not fullscreen, add class to page to set top or bottom padding
131                                 $elPage.addClass( "ui-page-header-fixed" )
132                                         .addClass( "ui-page-footer-fixed" );
133                         }
134                 },
135
136                 /* original core source(jquery.mobile.fixedToolbar.js)
137                 * never changed */
138                 _addTransitionClass: function () {
139                         var tclass = this.options.transition;
140
141                         if ( tclass && tclass !== "none" ) {
142                                 // use appropriate slide for header or footer
143                                 if ( tclass === "slide" ) {
144                                         tclass = this.element.is( ".ui-header" ) ? "slidedown" : "slideup";
145                                 }
146
147                                 this.element.addClass( tclass );
148                         }
149                 },
150
151
152                 /* Set default page positon
153                 * 1. add title style to header
154                 * 2. Set default header/footer position */
155                 setHeaderFooter: function ( thisPage ) {
156                         var $elPage = $( thisPage ),
157                                 $elHeader = $elPage.find( ":jqmData(role='header')" ).length ? $elPage.find( ":jqmData(role='header')") : $elPage.siblings( ":jqmData(role='header')"),
158                                 $elContent = $elPage.find( ".ui-content" ),
159                                 $elFooter = $elPage.find( ":jqmData(role='footer')" ),
160                                 $elFooterGroup = $elFooter.find( ":jqmData(role='fieldcontain')" ),
161                                 $elFooterControlGroup = $elFooter.find( ".ui-controlgroup" );
162
163                         // divide content mode scrollview and non-scrollview
164                         if ( !$elPage.is( ".ui-dialog" ) ) {
165                                 if ( $elHeader.jqmData("position") == "fixed" || ( $.support.scrollview && $.tizen.frameworkData.theme.match(/tizen/) ) ) {
166                                         $elHeader
167                                                 .css( "position", "fixed" )
168                                                 .css( "top", "0px" );
169                                 } else if ( !$.support.scrollview && $elHeader.jqmData("position") != "fixed" ) {
170                                         $elHeader.css( "position", "relative" );
171                                 }
172                         }
173
174                         /* set Title style */
175                         if ( $elHeader.find("span.ui-title-text-sub").length ) {
176                                 $elHeader.addClass( "ui-title-multiline");
177                         }
178
179                         if ( $elFooterGroup.find( "div" ).is( ".ui-controlgroup-label" ) ) {
180                                 $elFooterGroup.find( "div.ui-controlgroup-label" ).remove();
181                         }
182
183                         if ( $elFooterControlGroup.length ) {
184                                 var anchorPer = 100 / $elFooterControlGroup.find( "a" ).length;
185                                 $elFooterControlGroup.find( "a" ).each( function ( i ) {
186                                         $elFooterControlGroup.find( "a" ).eq( i ).width( anchorPer + "%" );
187                                 });
188                         }
189                 },
190
191                 _bindPageEvents: function () {
192                         var self = this,
193                                 o = self.options,
194                                 $el = self.element,
195                                 $elCurrentFooter;
196
197                         //page event bindings
198                         // Fixed toolbars require page zoom to be disabled, otherwise usability issues crop up
199                         // This method is meant to disable zoom while a fixed-positioned toolbar page is visible
200                         $el.closest( ".ui-page" )
201                                 .bind( "pagebeforeshow", function ( event ) {
202                                         var thisPage = this;
203                                         if ( o.disablePageZoom ) {
204                                                 $.mobile.zoom.disable( true );
205                                         }
206                                         if ( !o.visibleOnPageShow ) {
207                                                 self.hide( true );
208                                         }
209                                         self.setHeaderFooter( thisPage );
210                                         self._setContentMinHeight( thisPage );
211                                 } )
212                                 .bind( "webkitAnimationStart animationstart updatelayout", function ( e, data ) {
213                                         var thisPage = this;
214                                         if ( o.updatePagePadding ) {
215                                                 self.updatePagePadding(thisPage);
216                                                 self.updatePageLayout( thisPage, data);
217                                         }
218                                 })
219
220                                 .bind( "pageshow", function ( event ) {
221                                         var thisPage = this;
222                                         self._setContentMinHeight( thisPage );
223                                         self.updatePagePadding( thisPage );
224                                         self._updateHeaderArea( thisPage );
225                                         if ( o.updatePagePadding ) {
226                                                 $( window ).bind( "throttledresize." + self.widgetName, function () {
227                                                         self.updatePagePadding(thisPage);
228
229                                                         self.updatePageLayout( thisPage, false);
230                                                         self._updateHeaderArea( thisPage );
231                                                         self._setContentMinHeight( thisPage );
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 thisPage = $( ".ui-page-active" );
265
266                                 if ( e.state == "on" ) {
267                                         $elCurrentFooter = $( ".ui-page-active .ui-footer" );
268                                         $elCurrentFooter.hide();
269                                 } else if (e.state == "off") {
270                                         $elCurrentFooter.show();
271                                 }
272                                 self.updatePagePadding( thisPage );
273                                 self.updatePageLayout( thisPage, true );
274                         });
275                 },
276
277                 _bindContentControlEvents: function () {
278                         var self = this,
279                                 o = self.options,
280                                 $el = self.element;
281
282                         $el.closest( ".ui-page" )
283                                 .bind( "pagebeforeshow", function ( event ) {
284
285                                 });
286                 },
287
288                 _setContentMinHeight : function ( thisPage ) {
289                         var $elPage = $( thisPage ),
290                                 $elHeader = $elPage.find( ":jqmData(role='header')" ),
291                                 $elFooter = $elPage.find( ":jqmData(role='footer')" ),
292                                 $elContent = $elPage.find( ":jqmData(role='content')" ),
293                                 resultMinHeight;
294
295                         resultMinHeight = window.innerHeight - $elHeader.height() - $elFooter.height();
296
297                         $elContent.css( "min-height", resultMinHeight - parseFloat( $elContent.css("padding-top") ) - parseFloat( $elContent.css("padding-bottom") ) + "px" );
298                 },
299
300                 _updateHeaderArea : function ( thisPage ) {
301                         var $elPage = $( thisPage ),
302                                 $elHeader = $elPage.find( ":jqmData(role='header')" ).length ? $elPage.find( ":jqmData(role='header')") : $elPage.siblings( ":jqmData(role='header')"),
303                                 headerBtnNum = $elHeader.children("a").length,
304                                 headerSrcNum = $elHeader.children("img").length;
305
306                         if ( !$elPage.is( ".ui-dialog" ) ) {
307                                 $elHeader.find( "h1" ).css( "width", window.innerWidth - parseInt( $elHeader.find( "h1" ).css( "margin-left" ), 10 ) * 2 - $elHeader.children( "a" ).width() * headerBtnNum - $elHeader.children( "a" ).width() / 4 - $elHeader.children( "img" ).width() * headerSrcNum * 4 );
308                         }
309                         /* add half width for default space between text and button, and img tag area is too narrow, so multiply three for img width*/
310                 },
311
312                 _visible: true,
313
314                 // This will set the content element's top or bottom padding equal to the toolbar's height
315                 updatePagePadding: function ( tbPage ) {
316                         var $el = this.element,
317                                 header = $el.siblings( ".ui-header" ).length,
318                                 footer = $el.siblings( ".ui-footer" ).length;
319
320                         // This behavior only applies to "fixed", not "fullscreen"
321                         if ( this.options.fullscreen ) {
322                                 return;
323                         }
324
325                         tbPage = tbPage || $el.closest( ".ui-page" );
326
327                         if ( $el.siblings( ".ui-header" ).jqmData("position") == "fixed" || $.support.scrollview ) {
328                                 $( tbPage ).css( "padding-top", ( header ? $el.siblings( ".ui-header" ).outerHeight() : 0 ) );
329                         }
330                         $( tbPage ).css( "padding-bottom", ( footer ? $el.siblings( ".ui-footer" ).outerHeight() : 0 ) );
331                 },
332
333                 /* 1. Calculate and update content height   */
334                 updatePageLayout: function ( thisPage, receiveType ) {
335                         var $elFooter,
336                                 $elPage = $( thisPage ),
337                                 $elHeader = $elPage.find( ":jqmData(role='header')" ),
338                                 $elContent = $elPage.find( ":jqmData(role='content')" ),
339                                 resultContentHeight = 0,
340                                 resultFooterHeight = 0,
341                                 resultHeaderHeight = 0;
342
343                         if ( $elPage.length ) {
344                                 $elFooter = $elPage.find( ":jqmData(role='footer')" );
345                         } else {
346                                 $elFooter = $( document ).find( ":jqmData(role='footer')" ).eq( 0 );
347                         }
348
349                         // calculate footer height
350                         resultFooterHeight = ( $elFooter.css( "display" ) == "none" || $elFooter.length == 0 ) ? 0 : $elFooter.height();
351                         resultHeaderHeight = ( $elHeader.css( "display" ) == "none" || $elHeader.length == 0 ) ? 0 : $elHeader.height();
352
353                         if (resultFooterHeight != 0 ) {
354                                 $elFooter.css( "bottom", 0 );
355                         }
356
357                         resultContentHeight = window.innerHeight - resultFooterHeight - resultHeaderHeight;
358
359                         if ( $.support.scrollview ) {
360                                 $elContent.height( resultContentHeight -
361                                                 parseFloat( $elContent.css("padding-top") ) -
362                                                 parseFloat( $elContent.css("padding-bottom") ) );
363                         }
364
365                         // External call page( "refresh") - in case title changed
366                         if ( receiveType ) {
367                                 $elPage
368                                         .css( "min-height", resultContentHeight )
369                                         .css( "padding-top", resultHeaderHeight )
370                                         .css( "padding-bottom", resultFooterHeight );
371                         }
372                 },
373
374                 show: function ( notransition ) {
375                         /* blank function: deprecated */
376                 },
377
378                 hide: function ( notransition ) {
379                         /* blank function: deprecated */
380                 },
381
382                 toggle: function () {
383                         this[ this._visible ? "hide" : "show" ]();
384                 },
385
386                 destroy: function () {
387                         this.element.removeClass( "ui-header-fixed ui-footer-fixed ui-header-fullscreen ui-footer-fullscreen in out fade slidedown slideup ui-fixed-hidden" );
388                         this.element.closest( ".ui-page" ).removeClass( "ui-page-header-fixed ui-page-footer-fixed ui-page-header-fullscreen ui-page-footer-fullscreen" );
389                 }
390         });
391
392         //auto self-init widgets
393         $( document )
394                 .bind( "pagecreate create", function ( e ) {
395                         // DEPRECATED in 1.1: support for data-fullscreen=true|false on the page element.
396                         // This line ensures it still works, but we recommend moving the attribute to the toolbars themselves.
397                         if ( $( e.target ).jqmData( "fullscreen" ) ) {
398                                 $( $.mobile.pagelayout.prototype.options.initSelector, e.target ).not( ":jqmData(fullscreen)" ).jqmData( "fullscreen", true );
399                         }
400                         $.mobile.pagelayout.prototype.enhanceWithin( e.target );
401                 });
402
403 }( jQuery ));
404
405 //>>excludeStart("jqmBuildExclude", pragmas.jqmBuildExclude);
406 } );
407 //>>excludeEnd("jqmBuildExclude");