Subversion-Projekte lars-tiefland.prado

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
/**
2
 * TSlider client class.
3
 * This clas is mainly based on Scriptaculous Slider control (http://script.aculo.us)
4
 */
5
 
6
Prado.WebUI.TSlider = Class.extend(Prado.WebUI.PostBackControl,
7
{
8
	onInit : function (options)
9
	{
10
		var slider = this;
11
		this.options=options || {};
12
		this.track = $(options.ID+'_track');
13
		this.handle =$(options.ID+'_handle');
14
		this.progress = $(options.ID+'_progress');
15
		this.axis  = this.options.axis || 'horizontal';
16
		this.range = this.options.range || $R(0,1);
17
		this.value = 0;
18
		this.maximum   = this.options.maximum || this.range.end;
19
		this.minimum   = this.options.minimum || this.range.start;
20
		this.hiddenField=$(this.options.ID+'_1');
21
 
22
		// Will be used to align the handle onto the track, if necessary
23
		this.alignX = parseInt(this.options.alignX || - this.track.offsetLeft);
24
		this.alignY = parseInt(this.options.alignY || - this.track.offsetTop);
25
 
26
		this.trackLength = this.maximumOffset() - this.minimumOffset();
27
		this.handleLength = this.isVertical() ?
28
			(this.handle.offsetHeight != 0 ?
29
				this.handle.offsetHeight : this.handles.style.height.replace(/px$/,"")) :
30
				(this.handle.offsetWidth != 0 ? this.handle.offsetWidth :
31
					this.handle.style.width.replace(/px$/,""));
32
 
33
		this.active   = false;
34
		this.dragging = false;
35
		this.disabled = false;
36
 
37
		if(this.options.disabled) this.setDisabled();
38
 
39
		// Allowed values array
40
		this.allowedValues = this.options.values ? this.options.values.sortBy(Prototype.K) : false;
41
		if(this.allowedValues) {
42
			this.minimum = this.allowedValues.min();
43
			this.maximum = this.allowedValues.max();
44
		}
45
 
46
		this.eventMouseDown = this.startDrag.bindAsEventListener(this);
47
		this.eventMouseUp   = this.endDrag.bindAsEventListener(this);
48
		this.eventMouseMove = this.update.bindAsEventListener(this);
49
 
50
		// Initialize handle
51
		this.setValue(parseFloat(slider.options.sliderValue));
52
		Element.makePositioned(this.handle); // fix IE
53
		Event.observe (this.handle, "mousedown", this.eventMouseDown);
54
 
55
		Event.observe (this.track, "mousedown", this.eventMouseDown);
56
		if (this.progress) Event.observe (this.progress, "mousedown", this.eventMouseDown);
57
		Event.observe (document, "mouseup", this.eventMouseUp);
58
		Event.observe (document, "mousemove", this.eventMouseMove);
59
 
60
		this.initialized=true;
61
 
62
 
63
		if(this.options['AutoPostBack']==true)
64
			Event.observe(this.hiddenField, "change", Prado.PostBack.bindEvent(this,options));
65
 
66
	},
67
 
68
	dispose: function() {
69
		var slider = this;
70
		Event.stopObserving(this.track, "mousedown", this.eventMouseDown);
71
		Event.stopObserving(document, "mouseup", this.eventMouseUp);
72
		Event.stopObserving(document, "mousemove", this.eventMouseMove);
73
 
74
		Event.stopObserving(this.handle, "mousedown", slider.eventMouseDown);
75
 	},
76
 
77
	setDisabled: function(){
78
		this.disabled = true;
79
	},
80
	setEnabled: function(){
81
		this.disabled = false;
82
	},
83
	getNearestValue: function(value){
84
		if(this.allowedValues){
85
			if(value >= this.allowedValues.max()) return(this.allowedValues.max());
86
			if(value <= this.allowedValues.min()) return(this.allowedValues.min());
87
 
88
			var offset = Math.abs(this.allowedValues[0] - value);
89
			var newValue = this.allowedValues[0];
90
			this.allowedValues.each( function(v) {
91
				var currentOffset = Math.abs(v - value);
92
				if(currentOffset <= offset){
93
					newValue = v;
94
					offset = currentOffset;
95
				}
96
			});
97
			return newValue;
98
		}
99
		if(value > this.range.end) return this.range.end;
100
		if(value < this.range.start) return this.range.start;
101
		return value;
102
	},
103
 
104
	setValue: function(sliderValue){
105
		if(!this.active) {
106
			this.updateStyles();
107
		}
108
		this.value = this.getNearestValue(sliderValue);
109
		var pixelValue= this.translateToPx(this.value);
110
		this.handle.style[this.isVertical() ? 'top' : 'left'] =	pixelValue;
111
		if (this.progress)
112
			this.progress.style[this.isVertical() ? 'height' : 'width'] = pixelValue;
113
 
114
		//this.drawSpans();
115
		if(!this.dragging || !this.event) this.updateFinished();
116
	},
117
 
118
	setValueBy: function(delta) {
119
    	this.setValue(this.value + delta);
120
	},
121
 
122
	translateToPx: function(value) {
123
		return Math.round(
124
      		((this.trackLength-this.handleLength)/(this.range.end-this.range.start)) * (value - this.range.start)) + "px";
125
	},
126
 
127
	translateToValue: function(offset) {
128
		return ((offset/(this.trackLength-this.handleLength) * (this.range.end-this.range.start)) + this.range.start);
129
	},
130
 
131
	getRange: function(range) {
132
		var v = this.values.sortBy(Prototype.K);
133
		range = range || 0;
134
		return $R(v[range],v[range+1]);
135
	},
136
 
137
	minimumOffset: function(){
138
		return(this.isVertical() ? this.alignY : this.alignX);
139
  	},
140
 
141
	maximumOffset: function(){
142
		return(this.isVertical() ?
143
			(this.track.offsetHeight != 0 ? this.track.offsetHeight :
144
				this.track.style.height.replace(/px$/,"")) - this.alignY :
145
				(this.track.offsetWidth != 0 ? this.track.offsetWidth :
146
				this.track.style.width.replace(/px$/,"")) - this.alignX);
147
	},
148
 
149
	isVertical:  function(){
150
		return (this.axis == 'vertical');
151
	},
152
 
153
	updateStyles: function() {
154
		if (this.active)
155
			Element.addClassName(this.handle, 'selected');
156
		else
157
			Element.removeClassName(this.handle, 'selected');
158
	},
159
 
160
	startDrag: function(event) {
161
		if(Event.isLeftClick(event)) {
162
			if(!this.disabled){
163
				this.active = true;
164
				var handle = Event.element(event);
165
				var pointer  = [Event.pointerX(event), Event.pointerY(event)];
166
				var track = handle;
167
				if(track==this.track) {
168
					var offsets  = this.track.cumulativeOffset();
169
					this.event = event;
170
					this.setValue(this.translateToValue(
171
						(this.isVertical() ? pointer[1]-offsets[1] : pointer[0]-offsets[0])-(this.handleLength/2)
172
					));
173
					var offsets  = this.handle.cumulativeOffset();
174
					this.offsetX = (pointer[0] - offsets[0]);
175
					this.offsetY = (pointer[1] - offsets[1]);
176
				} else {
177
					this.updateStyles();
178
					var offsets  = this.handle.cumulativeOffset();
179
					this.offsetX = (pointer[0] - offsets[0]);
180
					this.offsetY = (pointer[1] - offsets[1]);
181
				}
182
			}
183
			Event.stop(event);
184
		}
185
	},
186
 
187
	update: function(event) {
188
		if(this.active) {
189
			if(!this.dragging) this.dragging = true;
190
			this.draw(event);
191
			if(Prototype.Browser.WebKit) window.scrollBy(0,0);
192
			Event.stop(event);
193
		}
194
	},
195
 
196
	draw: function(event) {
197
		var pointer = [Event.pointerX(event), Event.pointerY(event)];
198
		var offsets = this.track.cumulativeOffset();
199
		pointer[0] -= this.offsetX + offsets[0];
200
		pointer[1] -= this.offsetY + offsets[1];
201
		this.event = event;
202
		this.setValue(this.translateToValue( this.isVertical() ? pointer[1] : pointer[0] ));
203
		if(this.initialized && this.options.onSlide)
204
			this.options.onSlide(this.value, this);
205
	},
206
 
207
	endDrag: function(event) {
208
		if(this.active && this.dragging) {
209
			this.finishDrag(event, true);
210
			Event.stop(event);
211
		}
212
		this.active = false;
213
		this.dragging = false;
214
	},
215
 
216
	finishDrag: function(event, success) {
217
		this.active = false;
218
		this.dragging = false;
219
		this.updateFinished();
220
	},
221
 
222
	updateFinished: function() {
223
		this.hiddenField.value=this.value;
224
		this.updateStyles();
225
		if(this.initialized && this.options.onChange)
226
			this.options.onChange(this.value, this);
227
		this.event = null;
228
		if (this.options['AutoPostBack']==true)
229
		{
230
			Event.fireEvent(this.hiddenField,"change");
231
		}
232
	}
233
 
234
});