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