1 /* ***************************************************************************
2 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd.
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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 * ***************************************************************************
23 * Author: Youmin Ha <youmin.ha@samsung.com>
27 * Pagecontrol widget shows number bullets, receives touch event for each bullet,
28 * and runs your callback for each touch event.
32 * Pagecontrol widget uses <div> element as an element itself. It takes following attributes.
34 * data-role: This widget must have 'pagecontrol' as data-role value.
35 * data-max: Maximum nimber of pagecontrol bullets. This property must not exceed 10.
36 * data-value: Initially selected value of the pagecontrol widget. Must between 1 and data-max. If this attribute is not given, initial value is set to 1.
41 * : Set current value. Actually triggers 'change' event to the widget with given value.
42 * @param[in] value A value to be changed.
45 * : Get current value.
46 * @return Current value.
50 * change: Raised when a value is changed, by setting it by javascript, or by user's touch event.
54 * <div id="foo" data-role="pagecontrol" data-max="10"></div>
56 * <script language="text/javascript">
58 * // Bind callback to value change
59 * $('foo').bind('change', function (event, value) {
61 * // value: changed value
65 * $('foo').trigger('change', 3);
69 (function ($, undefined) {
70 $.widget( "tizen.pagecontrol", $.mobile.widget, {
72 initSelector: ":jqmData(role='pagecontrol')"
75 // subroutine: find a child by value
76 _getBtn: function ( value ) {
77 return $( this.element ).children( ":jqmData(value='" + value + "')" );
80 // subroutine: change active button by value
81 _changeActiveBtn: function ( newNum ) {
82 var oldNum = $( this.element ).data( 'value' );
85 if ( newNum < 1 || newNum > $( this.element ).data( "max" ) ) {
89 this._getBtn( oldNum ).removeClass( 'page_n_' + oldNum )
90 .addClass( 'page_n_dot' );
91 this._getBtn( newNum ).removeClass( 'page_n_dot' )
92 .addClass( 'page_n_' + newNum );
95 _triggerChange: function ( event ) {
96 // Trigger change event
97 $( this ).trigger( 'change', $( this ).data( 'value' ) );
100 _create: function ( ) {
103 _init: function ( ) {
106 maxVal = e.data( "max" ),
107 value = e.attr( "data-value" ),
111 page_margin_class = 'page_n_margin_44';
114 // Set default values
117 } else if ( maxVal > 10 ) {
120 e.data( "max", maxVal );
125 e.data( "value", value );
127 // Set pagecontrol class
128 e.addClass( 'pagecontrol' );
130 // Set empty callback variable
131 self.changeCallback = null;
133 // Calculate left/right margin
135 page_margin_class = 'page_n_margin_44';
136 } else if ( maxVal == 8 ) {
137 page_margin_class = 'page_n_margin_35';
138 } else if ( maxVal == 9 ) {
139 page_margin_class = 'page_n_margin_26';
141 page_margin_class = 'page_n_margin_19';
146 for ( i = 1; i <= maxVal; i++ ) {
147 btn = $( '<div class="page_n page_n_dot ' + page_margin_class + '" data-value="' + i + '"></div>' );
150 btn.removeClass( 'page_n_dot' )
151 .addClass( 'page_n_' + i );
153 // bind vclick event to each icon
154 btn.bind( 'vclick', this._triggerChange );
157 // pagecontrol element's change event
158 e.bind( 'change', function ( event, value ) {
159 // 1. Change activated button
160 self._changeActiveBtn( value );
162 // 2. Store new value (DO NOT change this order!)
163 e.data( 'value', value );
168 value: function ( val ) {
169 var pc = $( this.element );
171 if ( val && typeof val == "number" ) {
172 this._changeActiveBtn( val );
173 pc.data( 'value', val );
175 return pc.data( "value" );
179 }); // end: $.widget()
182 $( document ).bind( "pagecreate create", function ( e ) {
183 $( $.tizen.pagecontrol.prototype.options.initSelector, e.target )
184 .not( ":jqmData(role='none'), :jqmData(role='nojs')" )