1+ // jquery.stepper.js
2+ // ------------------------------------------------------
3+ // Author: NCOU
4+ ; ( function ( root , $ , undefined ) {
5+
6+ var pluginName = "stepper" ;
7+ var defaults = {
8+ selectorProgressBar : '.stepper-progress' ,
9+ selectorInputNumber : '.stepper-number' ,
10+ classNameChanging : 'is-changing' ,
11+ decimals : 0 ,
12+ unit : '%' ,
13+ initialValue : null ,
14+ min : 0 ,
15+ max : 100 ,
16+ stepSize : 1
17+ } ;
18+
19+ // The actual plugin constructor
20+ function Plugin ( element , options ) {
21+ this . element = element ;
22+
23+ this . options = $ . extend ( { } , defaults , options ) ;
24+
25+ this . _defaults = defaults ;
26+ this . _name = pluginName ;
27+
28+ return this . init ( ) ;
29+ }
30+
31+ Plugin . prototype = {
32+
33+ init : function ( ) {
34+
35+ // local variable
36+ this . curDown = ! 0 ;
37+ this . mouseDownX = 0 ;
38+ this . mouseDownValue = 0 ;
39+
40+ // Cache elements
41+ this . $el = $ ( this . element ) ;
42+ this . $input = this . $el . find ( this . options . selectorInputNumber ) ;
43+ this . $progress = this . $el . find ( this . options . selectorProgressBar ) ;
44+
45+ this . min = this . $input . attr ( 'min' ) || this . options . min ;
46+ this . max = this . $input . attr ( 'max' ) || this . options . max ;
47+
48+ this . initialValue = this . getValue ( ) || this . options . initialValue || this . max ;
49+
50+ this . setValue ( this . initialValue ) ;
51+
52+ // Bind events
53+ this . $input . on ( 'blur' , this . onBlur . bind ( this ) ) ;
54+ this . $input . on ( 'change keyup paste input' , this . onChange . bind ( this ) ) ;
55+ this . $el . on ( 'mousedown' , this . onMouseDown . bind ( this ) ) ;
56+ $ ( document ) . on ( 'mouseup' , this . onMouseUp . bind ( this ) ) ;
57+ $ ( document ) . on ( 'mousemove' , this . onMouseMove . bind ( this ) ) ;
58+
59+ return this ;
60+ } ,
61+
62+ onMouseDown : function ( e ) {
63+
64+ this . mouseDownX = e . clientX ;
65+ this . mouseDownValue = this . getValue ( ) ;
66+
67+ this . _changeStart ( ) ;
68+
69+ return this ;
70+ } ,
71+
72+ onMouseUp : function ( e ) {
73+
74+ this . _changeEnd ( ) ;
75+
76+ return this ;
77+ } ,
78+
79+ onMouseMove : function ( e ) {
80+
81+ if ( this . curDown === ! 1 ) {
82+ var t = e . clientX - this . mouseDownX ;
83+ this . setValue ( this . mouseDownValue + t * this . options . stepSize ) ;
84+ }
85+
86+ return this ;
87+ } ,
88+
89+ onChange : function ( e ) {
90+ var r = this . _valueToPercent ( this . getValue ( ) ) / 100 ;
91+ this . $progress . css ( "transform" , "scaleX(" + r + ")" ) ;
92+
93+ return this ;
94+ } ,
95+
96+ onBlur : function ( e ) {
97+
98+ this . _changeEnd ( ) ;
99+ this . setValue ( this . getValue ( ) ) ;
100+
101+ return this ;
102+ } ,
103+
104+ getValue : function ( ) {
105+ return parseFloat ( this . $input . val ( ) ) || 0 ;
106+ } ,
107+
108+ setValue : function ( amount ) {
109+ var value ;
110+
111+ value = Math . max ( Math . min ( amount , this . max ) , this . min ) ;
112+ value = this . _roundValue ( value ) ;
113+
114+ var n = value ;
115+ n = n . toFixed ( this . options . decimals ) ;
116+
117+ n += this . options . unit ;
118+ this . $input . val ( n ) ;
119+
120+ var r = this . _valueToPercent ( value ) / 100 ;
121+ this . $progress . css ( "transform" , "scaleX(" + r + ")" )
122+
123+ return this ;
124+ } ,
125+
126+ _percentToValue : function ( v ) {
127+ return this . min + v / 100 * ( this . max - this . min ) ;
128+ } ,
129+
130+ _valueToPercent : function ( v ) {
131+ var t = ( v - this . min ) / ( this . max - this . min ) * 100 ;
132+ return Math . max ( Math . min ( t , 100 ) , 0 ) ;
133+ } ,
134+
135+ _roundValue : function ( v ) {
136+ var nbrDecimals = 2 ;
137+
138+ var t = Math . pow ( 10 , nbrDecimals ) ;
139+ return Math . round ( v * t ) / t
140+ } ,
141+
142+ _changeStart : function ( ) {
143+ this . curDown = ! 1 ;
144+ this . $el . addClass ( "is-changing" ) ;
145+ } ,
146+
147+ _changeEnd : function ( ) {
148+ this . curDown = ! 0 ;
149+ this . $el . removeClass ( "is-changing" ) ;
150+ } ,
151+
152+
153+
154+
155+ } ;
156+
157+ // A really lightweight plugin wrapper around the constructor,
158+ // preventing against multiple instantiations
159+ $ . fn [ pluginName ] = function ( options ) {
160+ return this . each ( function ( ) {
161+ if ( ! $ . data ( this , "plugin-" + pluginName ) ) {
162+ $ . data ( this , "plugin-" + pluginName ,
163+ new Plugin ( this , options ) ) ;
164+ }
165+ } ) ;
166+ } ;
167+
168+ } ) ( window , jQuery ) ;
0 commit comments