tabbar : bug fix
[platform/framework/web/web-ui-fw.git] / src / widgets / tabbar / js / jquery.mobile.tizen.tabbar.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  * jQuery Mobile Framework : "tabbar" plugin
24  * Copyright (c) jQuery Project
25  * Dual licensed under the MIT or GPL Version 2 licenses.
26  * http://jquery.org/license
27  * Authors: Jinhyuk Jun <jinhyuk.jun@samsung.com>
28 */
29
30 /**
31  *  Tabbar can be created using data-role = "tabbar" inside footer 
32  *  Framework determine which tabbar will display with tabbar attribute
33  *
34  * Examples:
35  *         
36  *     HTML markup for creating tabbar: ( 2 ~ 5 li item available )
37  *     icon can be changed data-icon attribute (customized icon need)
38  *         <div data-role="footer" data-position ="fixed">
39  *              <div data-role="tabbar">
40  *                     <ul>
41  *                            <li><a href="#" class="ui-btn-active">Menu</a></li>
42  *                            <li><a href="#">Save</a></li>
43  *                            <li><a href="#">Share</a></li>
44  *                     </ul>
45  *             </div>
46  *      </div>
47 */
48
49 (function ( $, undefined ) {
50
51         $.widget( "tizen.tabbar", $.mobile.widget, {
52                 options: {
53                         iconpos: "top",
54                         grid: null,
55                         defaultList : 4,
56                         initSelector: ":jqmData(role='tabbar')"
57                 },
58
59                 _create: function () {
60
61                         var $tabbar = this.element,
62                                 $tabbtns,
63                                 textpos,
64                                 iconpos,
65                                 theme = $.mobile.listview.prototype.options.theme,      /* Get current theme */
66                                 ww = window.innerWidth || $( window ).width(),
67                                 wh = window.innerHeight || $( window ).height(),
68                                 tabbarDividerLeft = "<div class='ui-tabbar-divider ui-tabbar-divider-left'></div>",
69                                 tabbarDividerRight = "<div class='ui-tabbar-divider ui-tabbar-divider-right'></div>",
70                                 isScrollingStart = false,
71                                 isScrollingEnd = false,
72                                 isLandscape;
73
74                         isLandscape = ww > wh && ( ww - wh );
75
76                         if ( isLandscape ) {
77                                 $tabbar.removeClass( "ui-portrait-tabbar" ).addClass( "ui-landscape-tabbar" );
78                         } else {
79                                 $tabbar.removeClass( "ui-landscape-tabbar" ).addClass( "ui-portrait-tabbar" );
80                         }
81
82                         if ( $tabbar.find( "a" ).length ) {
83                                 $tabbtns = $tabbar.find( "a" );
84                                 iconpos = $tabbtns.filter( ":jqmData(icon)" ).length ? this.options.iconpos : undefined;
85                                 textpos = $tabbtns.html().length ? true : false;
86                         }
87
88                         if ( $tabbar.parents( ".ui-header" ).length && $tabbar.parents( ".ui-scrollview-view" ).length ) {
89                                 $tabbar.find( "li" ).addClass( "tabbar-scroll-li" );
90                                 $tabbar.find( "ul" ).addClass( "tabbar-scroll-ul" );
91
92                                 /* add shadow divider */
93                                 $( tabbarDividerLeft ).appendTo( $tabbar.parents( ".ui-scrollview-clip" ) );
94                                 $( tabbarDividerRight ).appendTo( $tabbar.parents( ".ui-scrollview-clip" ) );
95
96                                 $( ".ui-tabbar-divider-left" ).hide();
97                                 $( ".ui-tabbar-divider-right" ).hide();
98
99                                 /* add width calculation*/
100                                 if ( $tabbar.parents( ".ui-scrollview-view" ).data("default-list") ) {
101                                         this.options.defaultList = $tabbar.parents( ".ui-scrollview-view" ).data( "default-list" );
102                                 }
103                                 $tabbar.find( "li" ).css( "width", window.innerWidth / this.options.defaultList + "px" );
104                         } else {
105                                 if ( $tabbar.find( "ul" ).children().length ) {
106                                         $tabbar.addClass( "ui-navbar" )
107                                                 .find( "ul" )
108                                                 .grid( { grid: this.options.grid } );
109                                 }
110                         }
111
112                         if ( $tabbar.parents( ".ui-footer" ).length  ) {
113                                 $tabbar.find( "li" ).addClass( "ui-tab-btn-style" );
114                         }
115
116                         /* title tabbar */
117                         if ( $tabbar.siblings( ".ui-title" ).length ) {
118                                 $tabbar.parents( ".ui-header" ).addClass( "ui-title-tabbar" );
119                         }
120
121                         if ( !iconpos ) {
122                                 $tabbar.addClass( "ui-tabbar-noicons" );
123                         }
124                         if ( !textpos ) {
125                                 $tabbar.addClass( "ui-tabbar-notext" );
126                         }
127                         if ( textpos && iconpos ) {
128                                 $tabbar.parents( ".ui-header" ).addClass( "ui-title-tabbar-multiline" );
129                         }
130
131                         if ( $tabbar.find( "a" ).length ) {
132                                 $tabbtns.buttonMarkup({
133                                         corners:        false,
134                                         shadow:         false,
135                                         iconpos:        iconpos
136                                 });
137                         }
138
139                         if ( $tabbar.find( ".ui-state-persist" ).length ) {
140                                 $tabbar.addClass( "ui-tabbar-persist" );
141                         }
142
143                         $tabbar.delegate( "a", "vclick", function ( event ) {
144                                 $tabbtns.not( ".ui-state-persist" ).removeClass( $.mobile.activeBtnClass );
145                                 $( this ).addClass( $.mobile.activeBtnClass );
146                         });
147
148                         $tabbar.addClass( "ui-tabbar");
149
150                         $( document ).bind( "pagebeforeshow", function ( event, ui ) {
151                                 var footer_filter = $( event.target ).find( ":jqmData(role='footer')" ),
152                                         tabbar_filter = footer_filter.find( ":jqmData(role='tabbar')" ),
153                                         $elFooterMore = tabbar_filter.siblings( ":jqmData(icon='naviframe-more')" ),
154                                         $elFooterBack = tabbar_filter.siblings( ".ui-btn-back" );
155
156                                         footer_filter
157                                                 .css( "position", "fixed" )
158                                                 .css( "bottom", 0 )
159                                                 .css( "height", tabbar_filter.height() );
160                                         if ( $elFooterMore.length )
161                                                 tabbar_filter.addClass( "ui-tabbar-margin-more" );
162                                         if ( $elFooterBack.length )
163                                                 tabbar_filter.addClass( "ui-tabbar-margin-back" );
164
165                                 isScrollingStart = false;
166                         });
167
168                         $( window ).bind( "tabbar.scrollstart", function ( e ) {
169                                 if ( $( e.target ).find( ".ui-tabbar" ).length ) {
170                                         isScrollingStart = true;
171                                         isScrollingEnd = false;
172                                 }
173                         });
174
175                         $( window ).bind( "tabbar.scrollstop", function ( e ) {
176                                 var $tabbarScrollview = $( e.target ),
177                                         $minElement = $tabbar.find( "li" ).eq( 0 ),
178                                         minElementIndexVal = Math.abs( $tabbar.find( "li" ).eq( 0 ).offset().left ),
179                                         minElementIndex = -1;
180
181                                 isScrollingEnd = true;
182                                 if ( $( e.target ).find( ".ui-tabbar" ).length && isScrollingStart == true ) {
183                                         $tabbar.find( "li" ).each( function ( i ) {
184                                                 var offset      = $tabbar.find( "li" ).eq( i ).offset();
185                                                 if ( Math.abs( offset.left ) < minElementIndexVal ) {
186                                                         minElementIndexVal = Math.abs( offset.left );
187                                                         minElementIndex = i;
188                                                         $minElement = $tabbar.find( "li" ).eq( i );
189                                                 }
190                                         });
191
192                                         if ( $tabbarScrollview.length && isScrollingStart == isScrollingEnd && minElementIndex != -1) {
193                                                 isScrollingStart = false;
194                                                 isScrolling = false;
195                                                 $tabbarScrollview.scrollview( "scrollTo", -( window.innerWidth / $( e.target ).find( ".ui-tabbar" ).data( "defaultList" ) * minElementIndex ) , 0, 357);
196                                         }
197                                 }
198
199                                 $( ".ui-tabbar-divider-left" ).hide();
200                                 $( ".ui-tabbar-divider-right" ).hide();
201                         });
202
203                         $tabbar.bind( "touchstart vmousedown", function ( e ) {
204                                 var $tabbarScroll = $( e.target ).parents( ".ui-scrollview-view" );
205                                 if ( $tabbarScroll.offset() ) {
206                                         $tabbarScroll.offset().left < 0 ? 
207                                                 $( ".ui-tabbar-divider-left" ).show() : $( ".ui-tabbar-divider-left" ).hide();
208                                         ( $tabbarScroll.width() - $tabbarScroll.parents( ".ui-scrollview-clip" ).width() ) ==  Math.abs( $tabbarScroll.offset().left ) ? 
209                                                 $( ".ui-tabbar-divider-right" ).hide() : $( ".ui-tabbar-divider-right" ).show();
210                                 }
211                         });
212
213                         this._bindTabbarEvents();
214                 },
215
216                 _bindTabbarEvents: function () {
217                         var $tabbar = this.element;
218
219                         $( window ).bind( "orientationchange", function ( e, ui ) {
220                                 var ww = window.innerWidth || $( window ).width(),
221                                         wh = window.innerHeight || $( window ).height(),
222                                         isLandscape = ww > wh && ( ww - wh );
223
224                                 if ( isLandscape ) {
225                                         $tabbar.removeClass( "ui-portrait-tabbar" ).addClass( "ui-landscape-tabbar" );
226                                 } else {
227                                         $tabbar.removeClass( "ui-landscape-tabbar" ).addClass( "ui-portrait-tabbar" );
228                                 }
229                         });
230                 },
231
232                 _setDisabled: function ( value, cnt ) {
233                         this.element.find( "li" ).eq( cnt ).attr( "disabled", value );
234                         this.element.find( "li" ).eq( cnt ).attr( "aria-disabled", value );
235                 },
236
237                 disable: function ( cnt ) {
238                         this._setDisabled( true, cnt );
239                         this.element.find( "li" ).eq( cnt ).addClass( "ui-disabled" );
240                 },
241
242                 enable: function ( cnt ) {
243                         this._setDisabled( false, cnt );
244                         this.element.find( "li" ).eq( cnt ).removeClass( "ui-disabled" );
245                 }
246         });
247
248         //auto self-init widgets
249         $( document ).bind( "pagecreate create", function ( e ) {
250                 $( $.tizen.tabbar.prototype.options.initSelector, e.target ).tabbar();
251         });
252 }( jQuery ) );