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: Gabriel Schulhof <gabriel.schulhof@intel.com>
33 // Displays a 2D hue/saturation spectrum and a lightness slider.
35 // To apply, add the attribute data-role="colorpicker" to a <div>
36 // element inside a page. Alternatively, call colorpicker()
37 // on an element (see below).
40 // color: String; can be specified in html using the
41 // data-color="#ff00ff" attribute or when constructed
42 // $("#mycolorpicker").colorpicker({ color: "#ff00ff" });
43 // where the html might be :
44 // <div id="mycolorpicker"/>
46 (function ( $, undefined ) {
48 $.widget( "tizen.colorpicker", $.tizen.colorwidget, {
50 initSelector: ":jqmData(role='colorpicker')"
55 clrpicker: "#colorpicker",
57 hueGradient: "#colorpicker-hs-hue-gradient",
58 gradient: "#colorpicker-hs-sat-gradient",
59 eventSource: "[data-event-source='hs']",
60 valMask: "#colorpicker-hs-val-mask",
61 selector: "#colorpicker-hs-selector"
64 gradient: "#colorpicker-l-gradient",
65 eventSource: "[data-event-source='l']",
66 selector: "#colorpicker-l-selector"
71 _create: function () {
75 .css( "display", "none" )
76 .after( this._ui.clrpicker );
78 this._ui.hs.hueGradient.huegradient();
83 selectorDraggingOffset: {
87 dragging_hsl: undefined
91 .bind( "vmousemove", function ( event ) {
92 if ( self.dragging ) {
93 event.stopPropagation();
94 event.preventDefault();
97 .bind( "vmouseup", function ( event ) {
98 if ( self.dragging ) {
99 self.dragging = false;
103 this._bindElements( "hs" );
104 this._bindElements( "l" );
107 _bindElements: function ( which ) {
109 stopDragging = function ( event ) {
110 self.dragging = false;
111 event.stopPropagation();
112 event.preventDefault();
115 this._ui[which].eventSource
116 .bind( "vmousedown mousedown", function ( event ) { self._handleMouseDown( event, which, false ); } )
117 .bind( "vmousemove" , function ( event ) { self._handleMouseMove( event, which, false ); } )
118 .bind( "vmouseup" , stopDragging );
120 this._ui[which].selector
121 .bind( "vmousedown mousedown", function ( event ) { self._handleMouseDown( event, which, true); } )
122 .bind( "touchmove vmousemove", function ( event ) { self._handleMouseMove( event, which, true); } )
123 .bind( "vmouseup" , stopDragging );
126 _handleMouseDown: function ( event, containerStr, isSelector ) {
127 var coords = $.mobile.tizen.targetRelativeCoordsFromEvent( event ),
128 widgetStr = isSelector ? "selector" : "eventSource";
129 if ( ( coords.x >= 0 && coords.x <= this._ui[containerStr][widgetStr].width() &&
130 coords.y >= 0 && coords.y <= this._ui[containerStr][widgetStr].height() ) || isSelector ) {
131 this.dragging = true;
132 this.draggingHS = ( "hs" === containerStr );
135 this.selectorDraggingOffset.x = coords.x;
136 this.selectorDraggingOffset.y = coords.y;
139 this._handleMouseMove( event, containerStr, isSelector, coords );
143 _handleMouseMove: function ( event, containerStr, isSelector, coords ) {
148 if ( this.dragging &&
149 !( ( this.draggingHS && containerStr === "l" ) ||
150 ( !this.draggingHS && containerStr === "hs" ) ) ) {
151 coords = ( coords || $.mobile.tizen.targetRelativeCoordsFromEvent( event ) );
153 if ( this.draggingHS ) {
154 potential_h = isSelector
155 ? this.dragging_hsl[0] / 360 + ( coords.x - this.selectorDraggingOffset.x ) / this._ui[containerStr].eventSource.width()
156 : coords.x / this._ui[containerStr].eventSource.width();
157 potential_s = isSelector
158 ? this.dragging_hsl[1] + ( coords.y - this.selectorDraggingOffset.y ) / this._ui[containerStr].eventSource.height()
159 : coords.y / this._ui[containerStr].eventSource.height();
161 this.dragging_hsl[0] = Math.min( 1.0, Math.max( 0.0, potential_h ) ) * 360;
162 this.dragging_hsl[1] = Math.min( 1.0, Math.max( 0.0, potential_s ) );
164 potential_l = isSelector
165 ? this.dragging_hsl[2] + ( coords.y - this.selectorDraggingOffset.y ) / this._ui[containerStr].eventSource.height()
166 : coords.y / this._ui[containerStr].eventSource.height();
168 this.dragging_hsl[2] = Math.min( 1.0, Math.max( 0.0, potential_l ) );
172 this.selectorDraggingOffset.x = Math.ceil( this._ui[containerStr].selector.outerWidth() / 2.0 );
173 this.selectorDraggingOffset.y = Math.ceil( this._ui[containerStr].selector.outerHeight() / 2.0 );
176 this._updateSelectors( this.dragging_hsl );
177 event.stopPropagation();
178 event.preventDefault();
182 _updateSelectors: function ( hsl ) {
183 var clr = $.tizen.colorwidget.prototype._setElementColor.call( this, this._ui.hs.selector, [hsl[0], 1.0 - hsl[1], hsl[2]], "background" ).clr,
184 gray = $.tizen.colorwidget.clrlib.RGBToHTML( [hsl[2], hsl[2], hsl[2]] );
186 this._ui.hs.valMask.css((hsl[2] < 0.5)
187 ? { background : "#000000" , opacity : ( 1.0 - hsl[2] * 2.0 ) }
188 : { background : "#ffffff" , opacity : ( ( hsl[2] - 0.5 ) * 2.0 ) } );
189 this._ui.hs.selector.css( {
190 left : ( hsl[0] / 360 * this._ui.hs.eventSource.width() ),
191 top : ( hsl[1] * this._ui.hs.eventSource.height() )
193 this._ui.l.selector.css({
194 top : ( hsl[2] * this._ui.l.eventSource.height() ),
197 $.tizen.colorwidget.prototype._setColor.call( this, clr );
200 widget: function () { return this._ui.clrpicker; },
202 _setDisabled: function ( value ) {
203 $.tizen.widgetex.prototype._setDisabled.call( this, value );
204 this._ui.hs.hueGradient.huegradient( "option", "disabled", value );
205 this._ui.clrpicker[value ? "addClass" : "removeClass"]( "ui-disabled" );
206 $.tizen.colorwidget.prototype._displayDisabledState.call( this, this._ui.clrpicker );
209 _setColor: function ( clr ) {
210 if ( $.tizen.colorwidget.prototype._setColor.call( this, clr ) ) {
211 this.dragging_hsl = $.tizen.colorwidget.clrlib.RGBToHSL( $.tizen.colorwidget.clrlib.HTMLToRGB( this.options.color ) );
212 this.dragging_hsl[1] = 1.0 - this.dragging_hsl[1];
213 this._updateSelectors( this.dragging_hsl );
218 $( document ).bind( "pagecreate create", function ( e ) {
219 $( $.tizen.colorpicker.prototype.options.initSelector, e.target )
220 .not( ":jqmData(role='none'), :jqmData(role='nojs')" )