83df532dbe4f72528e4972096116a56b6842df62
[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                         // Browser detection! Weeee, here we go...
47                         // Unfortunately, position:fixed is costly, not to mention probably impossible, to feature-detect accurately.
48                         // Some tests exist, but they currently return false results in critical devices and browsers, which could lead to a broken experience.
49                         // Testing fixed positioning is also pretty obtrusive to page load, requiring injected elements and scrolling the window
50                         // The following function serves to rule out some popular browsers with known fixed-positioning issues
51                         // This is a plugin option like any other, so feel free to improve or overwrite it
52                         supportBlacklist: function () {
53                                 var w = window,
54                                         ua = navigator.userAgent,
55                                         platform = navigator.platform,
56                                         // Rendering engine is Webkit, and capture major version
57                                         wkmatch = ua.match( /AppleWebKit\/([0-9]+)/ ),
58                                         wkversion = !!wkmatch && wkmatch[ 1 ],
59                                         ffmatch = ua.match( /Fennec\/([0-9]+)/ ),
60                                         ffversion = !!ffmatch && ffmatch[ 1 ],
61                                         operammobilematch = ua.match( /Opera Mobi\/([0-9]+)/ ),
62                                         omversion = !!operammobilematch && operammobilematch[ 1 ];
63
64                                 if (
65                                         // iOS 4.3 and older : Platform is iPhone/Pad/Touch and Webkit version is less than 534 (ios5)
66                                         ( ( platform.indexOf( "iPhone" ) > -1 || platform.indexOf( "iPad" ) > -1  || platform.indexOf( "iPod" ) > -1 ) && wkversion && wkversion < 534 ) ||
67                                                 // Opera Mini
68                                                 ( w.operamini && ({}).toString.call( w.operamini ) === "[object OperaMini]" ) ||
69                                                 ( operammobilematch && omversion < 7458 ) ||
70                                                 //Android lte 2.1: Platform is Android and Webkit version is less than 533 (Android 2.2)
71                                                 ( ua.indexOf( "Android" ) > -1 && wkversion && wkversion < 533 ) ||
72                                                 // Firefox Mobile before 6.0 -
73                                                 ( ffversion && ffversion < 6 ) ||
74                                                 // WebOS less than 3
75                                                 ( window.palmGetResource !== undefined && wkversion && wkversion < 534 ) ||
76                                                 // MeeGo
77                                                 ( ua.indexOf( "MeeGo" ) > -1 && ua.indexOf( "NokiaBrowser/8.5.0" ) > -1 )
78                                 ) {
79                                         return true;
80                                 }
81
82                                 return false;
83                         },
84                         initSelector: ":jqmData(role='content')"
85                 },
86
87                 _create: function () {
88
89                         var self = this,
90                                 o = self.options,
91                                 $el = self.element;
92
93                         // Feature detecting support for
94                         if ( o.supportBlacklist() ) {
95                                 self.destroy();
96                                 return;
97                         }
98
99                         self._addFixedClass();
100                         self._addTransitionClass();
101                         self._bindPageEvents();
102
103                         // only content
104                         self._bindContentControlEvents();
105                 },
106
107                 /* add minimum fixed css style to bar(header/footer) and content
108                 *  it need to update when core source modified(jquery.mobile.page.section.js)
109                 *  modified from core source cuz initSelector different */
110                 _addFixedClass: function () {
111                         var self = this,
112                                 o = self.options,
113                                 $el = self.element,
114                                 $elHeader = $el.siblings( ":jqmData(role='header')" ),
115                                 $elFooter = $el.siblings( ":jqmData(role='footer')" ),
116                                 $elPage = $el.closest(".ui-page");
117
118                         $elHeader.addClass( "ui-header-fixed" );
119                         $elFooter.addClass( "ui-footer-fixed" );
120
121                         // "fullscreen" overlay positioning
122                         if ( o.fullscreen ) {
123                                 $elHeader.addClass( "ui-header-fullscreen" );
124                                 $elFooter.addClass( "ui-footer-fullscreen" );
125                                 $elPage
126                                         .addClass( "ui-page-header-fullscreen" )
127                                         .addClass( "ui-page-footer-fullscreen" );
128                         } else {
129                         // If not fullscreen, add class to page to set top or bottom padding
130                                 $elPage.addClass( "ui-page-header-fixed" )
131                                         .addClass( "ui-page-footer-fixed" );
132                         }
133                 },
134
135                 /* original core source(jquery.mobile.fixedToolbar.js)
136                 * never changed */
137                 _addTransitionClass: function () {
138                         var tclass = this.options.transition;
139
140                         if ( tclass && tclass !== "none" ) {
141                                 // use appropriate slide for header or footer
142                                 if ( tclass === "slide" ) {
143                                         tclass = this.element.is( ".ui-header" ) ? "slidedown" : "slideup";
144                                 }
145
146                                 this.element.addClass( tclass );
147                         }
148                 },
149
150
151                 /* Set default page positon
152                 * 1. add title style to header
153                 * 2. Set default header/footer position */
154                 setHeaderFooter: function ( thisPage ) {
155                         var $elPage = $( thisPage ),
156                                 $elHeader = $elPage.find( ":jqmData(role='header')" ).length ? $elPage.find( ":jqmData(role='header')") : $elPage.siblings( ":jqmData(role='header')"),
157                                 $elContent = $elPage.find( ".ui-content" ),
158                                 $elFooter = $elPage.find( ":jqmData(role='footer')" ),
159                                 $elFooterGroup = $elFooter.find( ":jqmData(role='fieldcontain')" ),
160                                 $elFooterControlGroup = $elFooter.find( ".ui-controlgroup" );
161
162                         // divide content mode scrollview and non-scrollview
163                         if ( !$elPage.is( ".ui-dialog" ) ) {
164                                 if ( $elHeader.jqmData("position") == "fixed" || ( $.support.scrollview && $.tizen.frameworkData.theme.match(/tizen/) ) ) {
165                                         $elHeader
166                                                 .css( "position", "fixed" )
167                                                 .css( "top", "0px" );
168                                 } else if ( !$.support.scrollview && $elHeader.jqmData("position") != "fixed" ) {
169                                         $elHeader.css( "position", "relative" );
170                                 }
171                         }
172
173                         /* set Title style */
174                         if ( $elHeader.find("span.ui-title-text-sub").length ) {
175                                 $elHeader.addClass( "ui-title-multiline");
176                         }
177
178                         if ( $elFooterGroup.find( "div" ).is( ".ui-controlgroup-label" ) ) {
179                                 $elFooterGroup.find( "div.ui-controlgroup-label" ).remove();
180                         }
181
182                         if ( $elFooterControlGroup.length ) {
183                                 var anchorPer = 100 / $elFooterControlGroup.find( "a" ).length;
184                                 $elFooterControlGroup.find( "a" ).each( function ( i ) {
185                                         $elFooterControlGroup.find( "a" ).eq( i ).width( anchorPer + "%" );
186                                 });
187                         }
188                 },
189
190                 _bindPageEvents: function () {
191                         var self = this,
192                                 o = self.options,
193                                 $el = self.element,
194                                 $elCurrentFooter;
195
196                         //page event bindings
197                         // Fixed toolbars require page zoom to be disabled, otherwise usability issues crop up
198                         // This method is meant to disable zoom while a fixed-positioned toolbar page is visible
199                         $el.closest( ".ui-page" )
200                                 .bind( "pagebeforeshow", function ( event ) {
201                                         var thisPage = this;
202                                         if ( o.disablePageZoom ) {
203                                                 $.mobile.zoom.disable( true );
204                                         }
205                                         if ( !o.visibleOnPageShow ) {
206                                                 self.hide( true );
207                                         }
208                                         self.setHeaderFooter( thisPage );
209                                         self._setContentMinHeight( thisPage );
210                                 } )
211                                 .bind( "webkitAnimationStart animationstart updatelayout", function ( e, data ) {
212                                         var thisPage = this;
213                                         if ( o.updatePagePadding ) {
214                                                 self.updatePagePadding(thisPage);
215                                                 self.updatePageLayout( thisPage, data);
216                                         }
217                                 })
218
219                                 .bind( "pageshow", function ( event ) {
220                                         var thisPage = this;
221                                         self._setContentMinHeight( thisPage );
222                                         self.updatePagePadding( thisPage );
223                                         self._updateHeaderArea( thisPage );
224                                         if ( o.updatePagePadding ) {
225                                                 $( window ).bind( "throttledresize." + self.widgetName, function () {
226                                                         self.updatePagePadding(thisPage);
227
228                                                         self.updatePageLayout( thisPage, false);
229                                                         self._updateHeaderArea( thisPage );
230                                                         self._setContentMinHeight( thisPage );
231                                                 });
232                                         }
233                                 })
234
235                                 .bind( "pagebeforehide", function ( e, ui ) {
236                                         if ( o.disablePageZoom ) {
237                                                 $.mobile.zoom.enable( true );
238                                         }
239                                         if ( o.updatePagePadding ) {
240                                                 $( window ).unbind( "throttledresize." + self.widgetName );
241                                         }
242                                 });
243
244                         window.addEventListener( "softkeyboardchange", function ( e ) {
245                                 var thisPage = $( ".ui-page-active" );
246
247                                 if ( e.state == "on" ) {
248                                         $elCurrentFooter = $( ".ui-page-active .ui-footer" );
249                                         $elCurrentFooter.hide();
250                                 } else if (e.state == "off") {
251                                         $elCurrentFooter.show();
252                                 }
253                                 self.updatePagePadding( thisPage );
254                                 self.updatePageLayout( thisPage, true );
255                         });
256                 },
257
258                 _bindContentControlEvents: function () {
259                         var self = this,
260                                 o = self.options,
261                                 $el = self.element;
262
263                         $el.closest( ".ui-page" )
264                                 .bind( "pagebeforeshow", function ( event ) {
265
266                                 });
267                 },
268
269                 _setContentMinHeight : function ( thisPage ) {
270                         var $elPage = $( thisPage ),
271                                 $elHeader = $elPage.find( ":jqmData(role='header')" ),
272                                 $elFooter = $elPage.find( ":jqmData(role='footer')" ),
273                                 $elContent = $elPage.find( ":jqmData(role='content')" ),
274                                 resultMinHeight;
275
276                         resultMinHeight = window.innerHeight - $elHeader.height() - $elFooter.height();
277
278                         $elContent.css( "min-height", resultMinHeight - parseFloat( $elContent.css("padding-top") ) - parseFloat( $elContent.css("padding-bottom") ) + "px" );
279                 },
280
281                 _updateHeaderArea : function ( thisPage ) {
282                         var $elPage = $( thisPage ),
283                                 $elHeader = $elPage.find( ":jqmData(role='header')" ).length ? $elPage.find( ":jqmData(role='header')") : $elPage.siblings( ":jqmData(role='header')"),
284                                 headerBtnNum = $elHeader.children("a").length,
285                                 headerSrcNum = $elHeader.children("img").length;
286
287                         if ( !$elPage.is( ".ui-dialog" ) ) {
288                                 $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 );
289                         }
290                         /* add half width for default space between text and button, and img tag area is too narrow, so multiply three for img width*/
291                 },
292
293                 _visible: true,
294
295                 // This will set the content element's top or bottom padding equal to the toolbar's height
296                 updatePagePadding: function ( tbPage ) {
297                         var $el = this.element,
298                                 header = $el.siblings( ".ui-header" ).length,
299                                 footer = $el.siblings( ".ui-footer" ).length;
300
301                         // This behavior only applies to "fixed", not "fullscreen"
302                         if ( this.options.fullscreen ) {
303                                 return;
304                         }
305
306                         tbPage = tbPage || $el.closest( ".ui-page" );
307
308                         if ( $el.siblings( ".ui-header" ).jqmData("position") == "fixed" || $.support.scrollview ) {
309                                 $( tbPage ).css( "padding-top", ( header ? $el.siblings( ".ui-header" ).outerHeight() : 0 ) );
310                         }
311                         $( tbPage ).css( "padding-bottom", ( footer ? $el.siblings( ".ui-footer" ).outerHeight() : 0 ) );
312                 },
313
314                 /* 1. Calculate and update content height   */
315                 updatePageLayout: function ( thisPage, receiveType ) {
316                         var $elFooter,
317                                 $elPage = $( thisPage ),
318                                 $elHeader = $elPage.find( ":jqmData(role='header')" ),
319                                 $elContent = $elPage.find( ":jqmData(role='content')" ),
320                                 resultContentHeight = 0,
321                                 resultFooterHeight = 0,
322                                 resultHeaderHeight = 0;
323
324                         if ( $elPage.length ) {
325                                 $elFooter = $elPage.find( ":jqmData(role='footer')" );
326                         } else {
327                                 $elFooter = $( document ).find( ":jqmData(role='footer')" ).eq( 0 );
328                         }
329
330                         // calculate footer height
331                         resultFooterHeight = ( $elFooter.css( "display" ) == "none" || $elFooter.length == 0 ) ? 0 : $elFooter.height();
332                         resultHeaderHeight = ( $elHeader.css( "display" ) == "none" || $elHeader.length == 0 ) ? 0 : $elHeader.height();
333
334                         if (resultFooterHeight != 0 ) {
335                                 $elFooter.css( "bottom", 0 );
336                         }
337
338                         resultContentHeight = window.innerHeight - resultFooterHeight - resultHeaderHeight;
339
340                         if ( $.support.scrollview ) {
341                                 $elContent.height( resultContentHeight -
342                                                 parseFloat( $elContent.css("padding-top") ) -
343                                                 parseFloat( $elContent.css("padding-bottom") ) );
344                         }
345
346                         // External call page( "refresh") - in case title changed
347                         if ( receiveType ) {
348                                 $elPage
349                                         .css( "min-height", resultContentHeight )
350                                         .css( "padding-top", resultHeaderHeight )
351                                         .css( "padding-bottom", resultFooterHeight );
352                         }
353                 },
354
355                 show: function ( notransition ) {
356                         /* blank function: deprecated */
357                 },
358
359                 hide: function ( notransition ) {
360                         /* blank function: deprecated */
361                 },
362
363                 toggle: function () {
364                         this[ this._visible ? "hide" : "show" ]();
365                 },
366
367                 destroy: function () {
368                         this.element.removeClass( "ui-header-fixed ui-footer-fixed ui-header-fullscreen ui-footer-fullscreen in out fade slidedown slideup ui-fixed-hidden" );
369                         this.element.closest( ".ui-page" ).removeClass( "ui-page-header-fixed ui-page-footer-fixed ui-page-header-fullscreen ui-page-footer-fullscreen" );
370                 }
371         });
372
373         //auto self-init widgets
374         $( document )
375                 .bind( "pagecreate create", function ( e ) {
376                         // DEPRECATED in 1.1: support for data-fullscreen=true|false on the page element.
377                         // This line ensures it still works, but we recommend moving the attribute to the toolbars themselves.
378                         if ( $( e.target ).jqmData( "fullscreen" ) ) {
379                                 $( $.mobile.pagelayout.prototype.options.initSelector, e.target ).not( ":jqmData(fullscreen)" ).jqmData( "fullscreen", true );
380                         }
381                         $.mobile.pagelayout.prototype.enhanceWithin( e.target );
382                 });
383
384 }( jQuery ));
385
386 //>>excludeStart("jqmBuildExclude", pragmas.jqmBuildExclude);
387 } );
388 //>>excludeEnd("jqmBuildExclude");