f8bcdef522dd10aac1922f0e5b2aac0b35c865d7
[platform/framework/web/web-ui-fw.git] / src / widgets / controlbar / js / jquery.mobile.tizen.controlbar.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 : "controlbar" 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  *  Controlbar can be created using data-role = "controlbar" inside footer 
32  *  Framework determine which controlbar will display with controlbar attribute
33  *
34  * Attributes:
35  *
36  *     data-style : determine which controlbar will use ( tabbar / toolbar )
37  *                    tabbar do not have back button, toolbar has back button 
38  *
39  * Examples:
40  *         
41  *     HTML markup for creating tabbar: ( 2 ~ 5 li item available )
42  *     icon can be changed data-icon attribute
43  *         <div data-role="footer"data-position ="fixed">
44  *              <div data-role="controlbar" data-style="tabbar" >
45  *                     <ul>
46  *                            <li><a href="#" data-icon="ctrlbar-menu" class="ui-btn-active">Menu</a></li>
47  *                            <li><a href="#" data-icon="ctrlbar-save" >Save</a></li>
48  *                            <li><a href="#" data-icon="ctrlbar-share" >Share</a></li>
49  *                     </ul>
50  *             </div>
51  *      </div>
52  *
53  *     HTML markup for creating toolbar: ( 2 ~ 5 li item available )
54  *     icon can be changed data-icon attribute
55  *         <div data-role="footer" data-position ="fixed">
56  *              <div data-role="controlbar" data-style="toolbar" >
57  *                     <ul>
58  *                            <li><a href="#" data-icon="ctrlbar-menu" class="ui-btn-active">Menu</a></li>
59  *                            <li><a href="#" data-icon="ctrlbar-save" >Save</a></li>
60  *                            <li><a href="#" data-icon="ctrlbar-share" >Share</a></li>
61  *                     </ul>
62  *             </div>
63  *      </div>
64 */
65
66 (function ( $, undefined ) {
67
68         $.widget( "tizen.controlbar", $.mobile.widget, {
69                 options: {
70                         iconpos: "top",
71                         grid: null,
72                         initSelector: ":jqmData(role='controlbar')"
73                 },
74
75                 _create: function () {
76
77                         var $controlbar = this.element,
78                                 $ctrlbtns = $controlbar.find( "a" ),
79                                 iconpos = $ctrlbtns.filter( ":jqmData(icon)" ).length ?
80                                                                                 this.options.iconpos : undefined,
81                                 textpos = $ctrlbtns.html().length ? true : false,
82                                 theme = $.mobile.listview.prototype.options.theme,      /* Get current theme */
83                                 ww = window.innerWidth || $( window ).width(),
84                                 wh = window.innerHeight || $( window ).height(),
85                                 isLandscape;
86
87                         isLandscape = ww > wh && ( ww - wh );
88
89                         if ( isLandscape ) {
90                                 $controlbar.removeClass( "ui-portrait-controlbar" ).addClass( "ui-landscape-controlbar" );
91                         } else {
92                                 $controlbar.removeClass( "ui-landscape-controlbar" ).addClass( "ui-portrait-controlbar" );
93                         }
94
95
96 /*
97                                 $controlbar
98                                         .addClass( "ui-navbar" )
99                                         .attr( "role", "navigation" )
100                                         .find( "ul" )
101                                 
102                                                 .grid( { grid: this.options.grid } );
103 */
104                                 $controlbar.addClass( "ui-navbar" )
105                                         .find( "ul" )
106                                         .grid( { grid: this.options.grid } );
107                         if ( $controlbar.parents( ".ui-footer" ).length  ) {
108                                 $controlbar.find( "li" ).addClass( "ui-ctrl-btn-style" );
109                         }
110
111
112                         /* title controlbar */
113                         if ( $controlbar.siblings( ".ui-title" ).length ) {
114                                 $controlbar.parents( ".ui-header" ).addClass( "ui-title-controlbar" );
115                         }
116
117                         if ( !iconpos ) {
118                                 $controlbar.addClass( "ui-controlbar-noicons" );
119                         }
120                         if ( !textpos ) {
121                                 $controlbar.addClass( "ui-controlbar-notext" );
122                         }
123                         if ( textpos && iconpos ) {
124                                 $controlbar.parents( ".ui-header" ).addClass( "ui-title-controlbar-multiline" );
125                         }
126
127                         $ctrlbtns.buttonMarkup({
128                                 corners:        false,
129                                 shadow:         false,
130                                 iconpos:        iconpos
131                         });
132
133                         if ( $controlbar.find( ".ui-state-persist" ).length ) {
134                                 $controlbar.addClass( "ui-controlbar-persist" );
135                         }
136
137                         $controlbar.delegate( "a", "vclick", function ( event ) {
138                                 $ctrlbtns.not( ".ui-state-persist" ).removeClass( $.mobile.activeBtnClass );
139                                 $( this ).addClass( $.mobile.activeBtnClass );
140                         });
141
142                                 $controlbar
143                                 .addClass( "ui-controlbar");
144
145
146                         $( document ).bind( "pagebeforeshow", function ( event, ui ) {
147                                 var footer_filter = $( event.target ).find( ":jqmData(role='footer')" ),
148                                         controlbar_filter = footer_filter.find( ":jqmData(role='controlbar')" ),
149                                         $elFooterMore = controlbar_filter.siblings( ":jqmData(icon='naviframe-more')" ),
150                                         $elFooterBack = controlbar_filter.siblings( ".ui-btn-back" );
151
152 /*
153                                         if ( !(controlbar_filter.find(".ui-btn-inner").children().is(".ui-icon")) ) {
154                                                 controlbar_filter.find( ".ui-btn-inner" ).addClass( "ui-navbar-textonly" );
155                                         } else {
156                                                 if ( controlbar_filter.find( ".ui-btn-text" ).text() == "" ) {
157                                                         controlbar_filter.find( ".ui-btn" ).addClass( "ui-ctrlbar-icononly" );
158                                                 }
159                                         }
160                                         */
161                                         footer_filter
162                                                 .css( "position", "fixed" )
163                                                 .css( "bottom", 0 )
164                                                 .css( "height", controlbar_filter.height() );
165                                         if ( $elFooterMore.length )
166                                                 controlbar_filter.addClass( "ui-controlbar-margin-more" );
167                                         if ( $elFooterBack.length )
168                                                 controlbar_filter.addClass( "ui-controlbar-margin-back" );
169
170
171                         });
172
173                         $( document ).bind( "pageshow", function ( e, ui ) {
174                                 var controlbar_filter = $( ".ui-page-active" ).find( ":jqmData(role='footer')" ).eq( 0 ).find( ":jqmData(role='controlbar')" ),
175                                         element_width = 0,
176                                         element_count = controlbar_filter.find( 'li' ).length;
177
178                                 if ( controlbar_filter.find(".ui-btn-active").length == 0 ) {
179                                         controlbar_filter.find( "div" ).css( "left", "0px" );
180                                 } else {
181                                         controlbar_filter.find( "div" ).css( "left", controlbar_filter.find( ".ui-btn-active" ).parent( "li" ).index() * controlbar_filter.width() / element_count );
182                                 }
183
184                                 if ( controlbar_filter.length ) {
185                                         element_width = controlbar_filter.find("li:first").width();
186                                         controlbar_filter.find("li:last").width( controlbar_filter.width() - element_width * ( element_count - 1 ) );
187                                 }
188                         });
189
190                         $( window ).bind( "resize", function ( e ) {
191                                 var controlbar_filter = $( ".ui-page-active" ).find( ":jqmData(role='footer')" ).eq( 0 ).find( ":jqmData(role='controlbar')" ),
192                                         element_width = 0,
193                                         element_count = controlbar_filter.find( 'li' ).length;
194
195                                 if ( controlbar_filter.length ) {
196                                         element_width = controlbar_filter.find("li:first").width();
197                                         controlbar_filter.find("li:last").width( controlbar_filter.width() - element_width * ( element_count - 1 ) );
198                                 }
199                         });
200
201                         this._bindControlbarEvents();
202                 },
203
204                 _bindControlbarEvents: function () {
205                         var $controlbar = this.element;
206
207                         $( window ).bind( "orientationchange", function ( e, ui ) {
208                                 var ww = window.innerWidth || $( window ).width(),
209                                         wh = window.innerHeight || $( window ).height(),
210                                         isLandscape = ww > wh && ( ww - wh );
211
212                                 if ( isLandscape ) {
213                                         $controlbar.removeClass( "ui-portrait-controlbar" ).addClass( "ui-landscape-controlbar" );
214                                 } else {
215                                         $controlbar.removeClass( "ui-landscape-controlbar" ).addClass( "ui-portrait-controlbar" );
216                                 }
217                         });
218
219                 },
220
221                 _setDisabled: function ( value, cnt ) {
222                         this.element.find( "li" ).eq( cnt ).attr( "disabled", value );
223                         this.element.find( "li" ).eq( cnt ).attr( "aria-disabled", value );
224                 },
225
226                 disable: function ( cnt ) {
227                         this._setDisabled( true, cnt );
228                         this.element.find( "li" ).eq( cnt ).addClass( "ui-disabled" );
229                 },
230
231                 enable: function ( cnt ) {
232                         this._setDisabled( false, cnt );
233                         this.element.find( "li" ).eq( cnt ).removeClass( "ui-disabled" );
234                 }
235         });
236
237         //auto self-init widgets
238         $( document ).bind( "pagecreate create", function ( e ) {
239                 $( $.tizen.controlbar.prototype.options.initSelector, e.target ).controlbar();
240         });
241 }( jQuery ) );