2 * jQuery Mobile Widget @VERSION
4 * This software is licensed under the MIT licence (as defined by the OSI at
5 * http://www.opensource.org/licenses/mit-license.php)
7 * ***************************************************************************
8 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd.
9 * Copyright (c) 2011 by Intel Corporation Ltd.
11 * Permission is hereby granted, free of charge, to any person obtaining a
12 * copy of this software and associated documentation files (the "Software"),
13 * to deal in the Software without restriction, including without limitation
14 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15 * and/or sell copies of the Software, and to permit persons to whom the
16 * Software is furnished to do so, subject to the following conditions:
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27 * DEALINGS IN THE SOFTWARE.
28 * ***************************************************************************
30 * Authors: Max Waterman <max.waterman@intel.com>
31 * Authors: Minkyu Kang <mk7.kang@samsung.com>
35 * tizenslider modifies the JQuery Mobile slider and is created in the same way.
37 * See the JQuery Mobile slider widget for more information :
38 * http://jquerymobile.com/demos/1.0a4.1/docs/forms/forms-slider.html
40 * The JQuery Mobile slider option:
41 * theme: specify the theme using the 'data-theme' attribute
44 * theme: string; the theme to use if none is specified using the 'data-theme' attribute
46 * popup: boolean; controls whether the popup is displayed or not
47 * specify if the popup is enabled using the 'data-popup' attribute
48 * set from javascript using .tizenslider('option','popup',newValue)
51 * changed: triggers when the value is changed (rather than when the handle is moved)
55 * <a href="#" id="popupEnabler" data-role="button" data-inline="true">Enable popup</a>
56 * <a href="#" id="popupDisabler" data-role="button" data-inline="true">Disable popup</a>
57 * <div data-role="fieldcontain">
58 * <input id="mySlider" data-theme='a' data-popup='false' type="range" name="slider" value="7" min="0" max="9" />
60 * <div data-role="fieldcontain">
61 * <input id="mySlider2" type="range" name="slider" value="77" min="0" max="777" />
64 * // disable popup from javascript
65 * $('#mySlider').tizenslider('option','popup',false);
68 * $('#popupEnabler').bind('vclick', function() {
69 * $('#mySlider').tizenslider('option','popup',true);
71 * $('#popupDisabler').bind('vclick', function() {
72 * $('#mySlider').tizenslider('option','popup',false);
76 (function ($, window, undefined) {
77 $.widget("tizen.tizenslider", $.mobile.widget, {
86 _create: function () {
87 this.currentValue = null;
88 this.popupVisible = false;
91 inputElement = $( this.element ),
101 inputElement.slider();
103 // hide the slider input element proper
106 // FIXME: workaround for list elipse
107 if ( inputElement.parent().hasClass("ui-li") ) {
108 inputElement.parent().css( "overflow", "visible" );
111 self.popup = $('<div class="ui-slider-popup"></div>');
113 // set the popup according to the html attribute
114 popupEnabledAttr = inputElement.jqmData('popup');
115 if ( popupEnabledAttr !== undefined ) {
116 self.options.popup = ( popupEnabledAttr == true );
119 // get the actual slider added by jqm
120 slider = inputElement.next('.ui-slider');
122 icon = inputElement.attr('data-icon');
124 // wrap the background
125 if ( icon === undefined ) {
126 slider.wrap('<div class="ui-slider-bg"></div>');
128 slider.wrap('<div class="ui-slider-icon-bg"></div>');
132 self.handle = slider.find('.ui-slider-handle');
134 // remove the rounded corners from the slider and its children
135 slider.removeClass('ui-btn-corner-all');
136 slider.find('*').removeClass('ui-btn-corner-all');
143 slider.before( $('<div class="ui-slider-left-' +
144 icon + '"></div>') );
145 slider.after( $('<div class="ui-slider-right-' +
146 icon + '"></div>') );
150 text_left = ( inputElement.attr('data-text-left') === undefined ) ? '' : inputElement.attr('data-text-left').substring( 0, 3 );
151 text_right = ( inputElement.attr('data-text-right') === undefined ) ? '' : inputElement.attr('data-text-right').substring( 0, 3 );
152 slider.before( $('<div class="ui-slider-left-text">' +
153 '<span style="position:relative;top:0.4em;">' +
156 slider.after( $('<div class="ui-slider-right-text">' +
157 '<span style="position:relative;top:0.4em;">' +
164 slider.append($('<div class="ui-slider-bar"></div>'));
165 self.slider_bar = slider.find('.ui-slider-bar');
168 slider.append($('<div class="ui-slider-handle-press"></div>'));
169 self.handle_press = slider.find('.ui-slider-handle-press');
170 self.handle_press.css('display', 'none');
172 // add a popup element (hidden initially)
173 slider.before( self.popup );
176 // get the element where value can be displayed
177 self.handleText = slider.find('.ui-btn-text');
182 // bind to changes in the slider's value to update handle text
183 this.element.bind('change', function () {
187 // bind clicks on the handle to show the popup
188 self.handle.bind('vmousedown', function () {
192 // watch events on the document to turn off the slider popup
193 slider.add( document ).bind('vmouseup', function () {
198 _handle_press_show: function () {
199 this.handle_press.css('display', '');
202 _handle_press_hide: function () {
203 this.handle_press.css('display', 'none');
206 // position the popup
207 positionPopup: function () {
208 var dstOffset = this.handle.offset();
211 left: dstOffset.left + ( this.handle.width() - this.popup.width() ) / 2,
212 top: dstOffset.top - this.popup.outerHeight() + 15
215 this.handle_press.offset({
216 left: dstOffset.left,
221 // show value on the handle and in popup
222 updateSlider: function () {
226 if ( this.popupVisible ) {
227 this.positionPopup();
230 // remove the title attribute from the handle (which is
231 // responsible for the annoying tooltip); NB we have
232 // to do it here as the jqm slider sets it every time
233 // the slider's value changes :(
234 this.handle.removeAttr('title');
236 this.slider_bar.width( this.handle.css('left') );
238 newValue = this.element.val();
240 if ( newValue === this.currentValue ) {
244 if ( newValue > 999 ) {
246 } else if ( newValue > 99 ) {
248 } else if ( newValue > 9 ) {
254 if ( font_size != this.handleText.css('font-size') ) {
255 this.handleText.css( 'font-size', font_size );
258 this.currentValue = newValue;
259 this.handleText.text( newValue );
260 this.popup.html( newValue );
262 this.element.trigger( 'update', newValue );
266 showPopup: function () {
267 if ( !this.options.popup || this.popupVisible ) {
272 this.popupVisible = true;
273 this._handle_press_show();
277 hidePopup: function () {
278 if ( !this.options.popup || !this.popupVisible ) {
283 this.popupVisible = false;
284 this._handle_press_hide();
287 _setOption: function (key, value) {
288 var needToChange = ( value !== this.options[key] );
290 if ( !needToChange ) {
296 this.options.popup = value;
298 if ( this.options.popup) {
309 // stop jqm from initialising sliders
310 $( document ).bind( "pagebeforecreate", function ( e ) {
311 if ( $.data( window, "jqmSliderInitSelector" ) === undefined ) {
312 $.data( window, "jqmSliderInitSelector",
313 $.mobile.slider.prototype.options.initSelector );
314 $.mobile.slider.prototype.options.initSelector = null;
318 // initialise sliders with our own slider
319 $( document ).bind( "pagecreate", function ( e ) {
320 var jqmSliderInitSelector = $.data( window, "jqmSliderInitSelector" );
321 $( e.target ).find(jqmSliderInitSelector).not('select').tizenslider();
322 $( e.target ).find(jqmSliderInitSelector).filter('select').slider();