zhuguifei
2026-03-10 2c1fd10c6fbabb8e9f0e9f07fe66fb36c008e883
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/**
    @name Edo.controls.Spinner
    @class
    @typeName spinner
    @description 数值调节器
    @extend Edo.controls.Trigger
*/
Edo.controls.Spinner = function(config){        
/**
    @name Edo.controls.Spinner#beforevaluechange
    @event
    @description 值改变前激发
    @property {Object} value 要改变的值     
*/        
/**
    @name Edo.controls.Spinner#valuechange
    @event
    @description 值改变事件
    @property {Object} value 当前值    
*/        
/**
    @name Edo.controls.Spinner#spin
    @event
    @description 激发spin事件,就是用户通过操作,发生了up/down的值调整
    @property {String} direction 调节方向, up或down
*/        
 
    Edo.controls.Spinner.superclass.constructor.call(this);    
    
};
Edo.controls.Spinner.extend(Edo.controls.Trigger,{
    valueField: 'value',
    defaultValue: 0,
    /**
        @name Edo.controls.Spinner#value
        @property
        @type Number
        @description 值
    */
    value: 0,
    /**
        @name Edo.controls.Spinner#minValue
        @property
        @type Number
        @description 最小值
    */
//    minValue: 0,
    /**
        @name Edo.controls.Spinner#maxValue
        @property
        @type Number
        @description 最大值
    */
//    maxValue: 100,    
    /**
        @name Edo.controls.Spinner#incrementValue
        @property
        @type Number
        @description 增加值
    */
    incrementValue: 1,
    /**
        @name Edo.controls.Spinner#alternateIncrementValue
        @property
        @type Number
        @description 快速增加值
    */
    alternateIncrementValue: 5, //shift+click时
    /**
        @name Edo.controls.Spinner#alternateKey
        @property
        @type String
        @description 快速增加键,默认是"shiftKey"
    */
    alternateKey: 'shiftKey',
 
    spinTime: 100,
    //triggerCls: 'e-trigger-empty',
    //withKeyUp: false,
    valueField: 'value',
    
    getTriggerHtml: function(){
        this.elCls += ' e-spinner';        
        return '<div class="e-spinner-up" unselectable="on"><div class="e-trigger-icon" unselectable="on"></div></div><div class="e-spinner-split"></div><div class="e-spinner-down" unselectable="on"><div class="e-trigger-icon" unselectable="on"></div></div>'
    },
    
    createChildren: function(el){
        Edo.controls.Spinner.superclass.createChildren.call(this, el);
        this.upEl = this.trigger.firstChild;
        this.downEl = this.trigger.lastChild;
        
        var v = this.value;
        this.value = null;
        this._setValue(v);
    },
    initEvents: function(){
        if(!this.design){
            
            Edo.util.Dom.addClassOnOver(this.upEl, 'e-spinner-up-over');
            Edo.util.Dom.addClassOnClick(this.upEl, 'e-spinner-up-pressed');
            Edo.util.Dom.addClassOnOver(this.downEl, 'e-spinner-down-over');
            Edo.util.Dom.addClassOnClick(this.downEl, 'e-spinner-down-pressed');
            
            Edo.util.Dom.on(this.field, 'keydown', this._onKeyDown, this);
            
            Edo.util.Dom.on(document.body, 'mouseup', this.stopSpin, this);
            
            this.on('spin', this._onSpin, this);
        }
        
        Edo.controls.Spinner.superclass.initEvents.call(this);
    },
    _onTrigger: function(e){    
        if(!this.enable) return;
        Edo.controls.Spinner.superclass._onTrigger.call(this, e);
        var direction;
        
        if(e.within(this.upEl)){
            direction = 'up';
        }else if(e.within(this.downEl)){
            direction = 'down';
        }
        if(direction){
            this.startSpin(direction, e);
        }
    },
    startSpin: function(direction, e){
        this.fireSpin(direction, e);
        
        this.spinTimer = this.fireSpin.time(this.spinTime, this, [direction, e]);
        
        
    },
    stopSpin: function(e){
        var sf = this;
        setTimeout(function(){
            clearInterval(sf.spinTimer);
            sf.spinTimer = null;
        }, 1);
        //Edo.util.Dom.un(document.body, 'mouseup', this.stopSpin, this);
    },
    _onKeyDown: function(e){
        switch(e.keyCode){
        case 38:
            this.fireSpin('up', e);
        break;
        case 40:
            this.fireSpin('down', e);
        break;
        }
    },
    fireSpin: function(direction, e){
        //
    
        this.fireEvent('spin', {
            type: 'spin',
            direction: direction,
            source: this,
            event: e
        });
    },
    _onSpin: function(e){
        this.text = this.field.value;
        this.spin(this.field.value, e.direction, e.event[this.alternateKey]);
    },
    spin: function(value, direction, alternate){
        var v = this.normalizeValue(value);
        
        var incr = (alternate == true) ? this.alternateIncrementValue : this.incrementValue;
        if(Edo.isValue(direction)){
            (direction == 'down') ? v -= incr : v += incr ;
        }                        
        this._setValue(v);
    },
    //确保值的有效性
    normalizeValue : function(value){        
        var v = value;
        
        var v = parseFloat(v);
        if(isNaN(v)) v = this.value;
        
        if(this.minValue != undefined && v < this.minValue){
            v = this.minValue;
        }
        if(this.maxValue != undefined && v > this.maxValue){
            v = this.maxValue;
        }        
        return v;
    },
    _setValue: function(v){    
        v = this.normalizeValue(v);       
        
        var e = {
            type: 'beforevaluechange',
            source: this,
            name: this.name,
            value: v,            
            text: v
        };
        if(this.fireEvent('beforevaluechange', e) !== false){                        
        //if(v !== this.value && this.fireEvent('beforevaluechange', e !== false)){
            this.value = e.value;                        
            //this.setText(e.text);
            this._setText(e.text);
            
            e.type = 'valuechange';
            //this.fireEvent('valuechange', e);
            
            this.changeProperty('value', this.value);
        }
    },
    _onTextChange: function(e){
    
        this.text = this.field.value;
        
        this.spin(this.field.value);
    }
});
 
Edo.controls.Spinner.regType('spinner');