upload tizen1.0 source
[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                                 $navbtns = $controlbar.find( "a" ),
79                                 iconpos = $navbtns.filter( ":jqmData(icon)" ).length ?
80                                                                                 this.options.iconpos : undefined,
81                                 theme = $.mobile.listview.prototype.options.theme,      /* Get current theme */
82                                 style = $controlbar.attr( "data-style" );
83
84                         if ( style === "left" || style === "right" ) {
85                                 $controlbar
86                                         .parents( ".ui-content" )
87                                         .css( 'padding', '0' );
88                         } else {
89                                 $controlbar
90                                         .addClass( "ui-navbar" )
91                                         .attr( "role", "navigation" )
92                                         .find( "ul" )
93                                                 .grid( { grid: this.options.grid } );
94                         }
95
96                         if ( !iconpos ) {
97                                 $controlbar.addClass( "ui-navbar-noicons" );
98                         }
99
100                         $navbtns.buttonMarkup({
101                                 corners:        false,
102                                 shadow:         false,
103                                 iconpos:        iconpos
104                         });
105
106                         $controlbar.delegate( "a", "vclick", function ( event ) {
107                                 $navbtns.not( ".ui-state-persist" ).removeClass( $.mobile.activeBtnClass );
108                                 $( this ).addClass( $.mobile.activeBtnClass );
109                         });
110
111                         if ( style === "tabbar" || style === "toolbar" ) {
112                                 $controlbar
113                                         .addClass( "ui-controlbar-" + theme )
114                                         .addClass( "ui-" + style + "-" + theme );
115                         } else {
116                                 $controlbar
117                                         .addClass( "ui-controlbar-" + style )
118                                         .end();
119                         }
120
121                         $( document ).bind( "pagebeforeshow", function ( event, ui ) {
122                                 var footer_filter = $( event.target ).find( ":jqmData(role='footer')" ),
123                                         controlbar_filter = footer_filter.find( ":jqmData(role='controlbar')" ),
124                                         style = controlbar_filter.jqmData( "style" );
125
126                                 if ( style == "toolbar" || style == "tabbar" ) {
127                                         /* Need to add text only style */
128                                         if ( !(controlbar_filter.find(".ui-btn-inner").children().is(".ui-icon")) ) {
129                                                 controlbar_filter.find( ".ui-btn-inner" ).addClass( "ui-navbar-textonly" );
130                                         } else {
131                                                 if ( controlbar_filter.find( ".ui-btn-text" ).text() == "" ) {
132                                                         controlbar_filter.find( ".ui-btn" ).addClass( "ui-ctrlbar-icononly" );
133                                                 }
134                                         }
135                                         footer_filter
136                                                 .css( "position", "fixed" )
137                                                 .css( "height", controlbar_filter.height() )
138                                                 .css( "top", window.innerHeight - footer_filter.height() );
139                                         if ( style == "toolbar" ) {
140                                                 controlbar_filter
141                                                         .css( "width", window.innerWidth - controlbar_filter.siblings(".ui-btn").width() );
142                                         }
143                                 }
144                         });
145
146                         $( document ).bind( "pageshow", function ( e, ui ) {
147                                 var controlbar_filter = $( ".ui-page" ).find( ":jqmData(role='footer')" ).eq( 0 ).find( ":jqmData(role='controlbar')" ),
148                                         element_count = controlbar_filter.find( 'li' ).length;
149
150                                 if ( controlbar_filter.find(".ui-btn-active").length == 0 ) {
151                                         controlbar_filter.find( "div" ).css( "left", "0px" );
152                                 } else {
153                                         controlbar_filter.find( "div" ).css( "left", controlbar_filter.find( ".ui-btn-active" ).parent( "li" ).index() * controlbar_filter.width() / element_count );
154                                 }
155
156                                 /* Increase Content size with dummy <div> because of footer height */
157                                 if ( controlbar_filter.length != 0 && $( ".ui-page-active" ).find( ".dummy-div" ).length == 0 && $( ".ui-page-active" ).find( ":jqmData(role='footer')" ).find( ":jqmData(role='controlbar')" ).length != 0 ) {
158                                         $( ".ui-page-active" ).find( ":jqmData(role='content')" ).append( '<div class="dummy-div"></div>' );
159                                         $( ".ui-page-active" ).find( ".dummy-div" )
160                                                 .css( "width", controlbar_filter.width() )
161                                                 .css( "height", controlbar_filter.height() );
162                                 }
163                         });
164                 },
165
166                 _setDisabled: function ( value, cnt ) {
167                         this.element.find( "li" ).eq( cnt ).attr( "disabled", value );
168                         this.element.find( "li" ).eq( cnt ).attr( "aria-disabled", value );
169                 },
170
171                 disable: function ( cnt ) {
172                         this._setDisabled( true, cnt );
173                         this.element.find( "li" ).eq( cnt ).addClass( "ui-disabled" );
174                 },
175
176                 enable: function ( cnt ) {
177                         this._setDisabled( false, cnt );
178                         this.element.find( "li" ).eq( cnt ).removeClass( "ui-disabled" );
179                 }
180         });
181
182         //auto self-init widgets
183         $( document ).bind( "pagecreate create", function ( e ) {
184                 $( $.tizen.controlbar.prototype.options.initSelector, e.target ).controlbar();
185         });
186 }( jQuery ) );