66 * Requires: jQuery v1.7+
77 *
88 * Copyright (c) 2012 Anthony Terrien
9- * Under MIT and GPL licenses:
10- * http://www.opensource.org/licenses/mit-license.php
11- * http://www.gnu.org/licenses/gpl.html
9+ * Under MIT License (http://www.opensource.org/licenses/mit-license.php)
1210 *
1311 * Thanks to vor, eskimoblood, spiffistan, FabrizioC
1412 */
141139 s . v [ k ] = $this . val ( ) ;
142140
143141 $this . bind (
144- 'change keyup '
142+ 'change blur '
145143 , function ( ) {
146144 var val = { } ;
147145 val [ k ] = $this . val ( ) ;
159157 ( this . v == '' ) && ( this . v = this . o . min ) ;
160158
161159 this . $ . bind (
162- 'change keyup '
160+ 'change blur '
163161 , function ( ) {
164162 s . val ( s . _validate ( s . $ . val ( ) ) ) ;
165163 }
170168 ( ! this . o . displayInput ) && this . $ . hide ( ) ;
171169
172170 // adds needed DOM elements (canvas, div)
173- this . $c = $ ( document . createElement ( 'canvas' ) ) ;
171+ this . $c = $ ( document . createElement ( 'canvas' ) ) . attr ( {
172+ width : this . o . width ,
173+ height : this . o . height
174+ } ) ;
175+
176+ // wraps all elements in a div
177+ // add to DOM before Canvas init is triggered
178+ this . $div = $ ( '<div style="'
179+ + ( this . o . inline ? 'display:inline;' : '' )
180+ + 'width:' + this . o . width + 'px;height:' + this . o . height + 'px;'
181+ + '"></div>' ) ;
182+
183+ this . $ . wrap ( this . $div ) . before ( this . $c ) ;
184+ this . $div = this . $ . parent ( ) ;
185+
174186 if ( typeof G_vmlCanvasManager !== 'undefined' ) {
175187 G_vmlCanvasManager . initElement ( this . $c [ 0 ] ) ;
176188 }
189+
177190 this . c = this . $c [ 0 ] . getContext ? this . $c [ 0 ] . getContext ( '2d' ) : null ;
191+
178192 if ( ! this . c ) {
179193 this . o . error && this . o . error ( ) ;
180194 return ;
195209 && this . o . width . indexOf ( '%' ) ) ;
196210 this . relativeHeight = ( ( this . o . height % 1 !== 0 )
197211 && this . o . height . indexOf ( '%' ) ) ;
198-
199212 this . relative = ( this . relativeWidth || this . relativeHeight ) ;
200213
201- // wraps all elements in a div
202- this . $div = $ ( '<div style="'
203- + ( this . o . inline ? 'display:inline;' : '' )
204- + '"></div>' ) ;
205-
206- this . $ . wrap ( this . $div ) . before ( this . $c ) ;
207- this . $div = this . $ . parent ( ) ;
208-
209214 // computes size and carves the component
210215 this . _carve ( ) ;
211216
302307 e . originalEvent . touches [ s . t ] . pageX ,
303308 e . originalEvent . touches [ s . t ] . pageY
304309 ) ;
310+
311+ if ( v == s . cv ) return ;
312+
313+ if (
314+ s . cH
315+ && ( s . cH ( v ) === false )
316+ ) return ;
317+
305318 s . change ( s . _validate ( v ) ) ;
306319 s . _draw ( ) ;
307320 } ;
319332 "touchend.k"
320333 , function ( ) {
321334 k . c . d . unbind ( 'touchmove.k touchend.k' ) ;
322-
323- if (
324- s . rH
325- && ( s . rH ( s . cv ) === false )
326- ) return ;
327-
328335 s . val ( s . cv ) ;
329336 }
330337 ) ;
336343
337344 var mouseMove = function ( e ) {
338345 var v = s . xy2val ( e . pageX , e . pageY ) ;
346+
347+ if ( v == s . cv ) return ;
348+
349+ if (
350+ s . cH
351+ && ( s . cH ( v ) === false )
352+ ) return ;
353+
339354 s . change ( s . _validate ( v ) ) ;
340355 s . _draw ( ) ;
341356 } ;
366381 "mouseup.k"
367382 , function ( e ) {
368383 k . c . d . unbind ( 'mousemove.k mouseup.k keyup.k' ) ;
369-
370- if (
371- s . rH
372- && ( s . rH ( s . cv ) === false )
373- ) return ;
374-
375384 s . val ( s . cv ) ;
376385 }
377386 ) ;
498507 ) ;
499508 } ;
500509
501- this . val = function ( v ) {
510+ this . val = function ( v , triggerRelease ) {
502511 if ( null != v ) {
512+
513+ if (
514+ triggerRelease !== false
515+ && ( v != this . v )
516+ && this . rH
517+ && ( this . rH ( v ) === false )
518+ ) return ;
519+
503520 this . cv = this . o . stopper ? max ( min ( v , this . o . max ) , this . o . min ) : v ;
504- this . v = this . cv ;
521+ this . v = this . cv ;
505522 this . $ . val ( this . v ) ;
506523 this . _draw ( ) ;
507524 } else {
537554 // bind MouseWheel
538555 var s = this , mwTimerStop , mwTimerRelease ,
539556 mw = function ( e ) {
540- e . preventDefault ( ) ;
541-
542- var ori = e . originalEvent
543- , deltaX = ori . detail || ori . wheelDeltaX
544- , deltaY = ori . detail || ori . wheelDeltaY
545- , v = s . _validate ( s . $ . val ( ) )
546- + ( deltaX > 0 || deltaY > 0 ? s . o . step : deltaX < 0 || deltaY < 0 ? - s . o . step : 0 ) ;
547-
548- v = max ( min ( v , s . o . max ) , s . o . min ) ;
549-
550- s . val ( v ) ;
551-
552- if ( s . rH ) {
553- // Handle mousewheel stop
554- clearTimeout ( mwTimerStop ) ;
555- mwTimerStop = setTimeout ( function ( ) {
556- s . rH ( v ) ;
557- mwTimerStop = null ;
558- } , 100 ) ;
559-
560- // Handle mousewheel releases
561- if ( ! mwTimerRelease ) {
562- mwTimerRelease = setTimeout ( function ( ) {
563- if ( mwTimerStop ) s . rH ( v ) ;
564- mwTimerRelease = null ;
565- } , 200 ) ;
566- }
567- }
557+ e . preventDefault ( ) ;
558+
559+ var ori = e . originalEvent
560+ , deltaX = ori . detail || ori . wheelDeltaX
561+ , deltaY = ori . detail || ori . wheelDeltaY
562+ , v = s . _validate ( s . $ . val ( ) )
563+ + ( deltaX > 0 || deltaY > 0 ? s . o . step : deltaX < 0 || deltaY < 0 ? - s . o . step : 0 ) ;
564+
565+ v = max ( min ( v , s . o . max ) , s . o . min ) ;
566+
567+ s . val ( v , false ) ;
568+
569+ if ( s . rH ) {
570+ // Handle mousewheel stop
571+ clearTimeout ( mwTimerStop ) ;
572+ mwTimerStop = setTimeout ( function ( ) {
573+ s . rH ( v ) ;
574+ mwTimerStop = null ;
575+ } , 100 ) ;
576+
577+ // Handle mousewheel releases
578+ if ( ! mwTimerRelease ) {
579+ mwTimerRelease = setTimeout ( function ( ) {
580+ if ( mwTimerStop ) s . rH ( v ) ;
581+ mwTimerRelease = null ;
582+ } , 200 ) ;
568583 }
584+ }
585+ }
569586 , kval , to , m = 1 , kv = { 37 :- s . o . step , 38 :s . o . step , 39 :s . o . step , 40 :- s . o . step } ;
570587
571588 this . $
603620
604621 // long time keydown speed-up
605622 to = window . setTimeout (
606- function ( ) { m *= 2 ; }
607- , 30
623+ function ( ) { m *= 2 ; } , 30
608624 ) ;
609625 }
610626 }
691707 } ;
692708
693709 this . change = function ( v ) {
694- if ( v == this . cv ) return ;
695710 this . cv = v ;
696- if (
697- this . cH
698- && ( this . cH ( v ) === false )
699- ) return ;
711+ this . $ . val ( v ) ;
700712 } ;
701713
702714 this . angle = function ( v ) {
761773 ) . parent ( ) ;
762774 } ;
763775
764- } ) ( jQuery ) ;
776+ } ) ( jQuery ) ;
0 commit comments