1 // script.aculo.us slider.js v1.6.4, Wed Sep 06 11:30:58 CEST 2006
3 // Copyright (c) 2005 Marty Haught, Thomas Fuchs
5 // See http://script.aculo.us for more info
7 // Permission is hereby granted, free of charge, to any person obtaining
8 // a copy of this software and associated documentation files (the
9 // "Software"), to deal in the Software without restriction, including
10 // without limitation the rights to use, copy, modify, merge, publish,
11 // distribute, sublicense, and/or sell copies of the Software, and to
12 // permit persons to whom the Software is furnished to do so, subject to
13 // the following conditions:
15 // The above copyright notice and this permission notice shall be
16 // included in all copies or substantial portions of the Software.
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 if(!Control) var Control = {};
27 Control.Slider = Class.create();
30 // axis: 'vertical', or 'horizontal' (default)
35 Control.Slider.prototype = {
36 initialize: function(handle, track, options) {
39 if(handle instanceof Array) {
40 this.handles = handle.collect( function(e) { return $(e) });
42 this.handles = [$(handle)];
45 this.track = $(track);
46 this.options = options || {};
48 this.axis = this.options.axis || 'horizontal';
49 this.increment = this.options.increment || 1;
50 this.step = parseInt(this.options.step || '1');
51 this.range = this.options.range || $R(0,1);
53 this.value = 0; // assure backwards compat
54 this.values = this.handles.map( function() { return 0 });
55 this.spans = this.options.spans ? this.options.spans.map(function(s){ return $(s) }) : false;
56 this.options.startSpan = $(this.options.startSpan || null);
57 this.options.endSpan = $(this.options.endSpan || null);
59 this.restricted = this.options.restricted || false;
61 this.maximum = this.options.maximum || this.range.end;
62 this.minimum = this.options.minimum || this.range.start;
64 // Will be used to align the handle onto the track, if necessary
65 this.alignX = parseInt(this.options.alignX || '0');
66 this.alignY = parseInt(this.options.alignY || '0');
68 this.trackLength = this.maximumOffset() - this.minimumOffset();
70 this.handleLength = this.isVertical() ?
71 (this.handles[0].offsetHeight != 0 ?
72 this.handles[0].offsetHeight : this.handles[0].style.height.replace(/px$/,"")) :
73 (this.handles[0].offsetWidth != 0 ? this.handles[0].offsetWidth :
74 this.handles[0].style.width.replace(/px$/,""));
77 this.dragging = false;
78 this.disabled = false;
80 if(this.options.disabled) this.setDisabled();
82 // Allowed values array
83 this.allowedValues = this.options.values ? this.options.values.sortBy(Prototype.K) : false;
84 if(this.allowedValues) {
85 this.minimum = this.allowedValues.min();
86 this.maximum = this.allowedValues.max();
89 this.eventMouseDown = this.startDrag.bindAsEventListener(this);
90 this.eventMouseUp = this.endDrag.bindAsEventListener(this);
91 this.eventMouseMove = this.update.bindAsEventListener(this);
93 // Initialize handles in reverse (make sure first handle is active)
94 this.handles.each( function(h,i) {
95 i = slider.handles.length-1-i;
96 slider.setValue(parseFloat(
97 (slider.options.sliderValue instanceof Array ?
98 slider.options.sliderValue[i] : slider.options.sliderValue) ||
99 slider.range.start), i);
100 Element.makePositioned(h); // fix IE
101 Event.observe(h, "mousedown", slider.eventMouseDown);
104 Event.observe(this.track, "mousedown", this.eventMouseDown);
105 Event.observe(document, "mouseup", this.eventMouseUp);
106 Event.observe(document, "mousemove", this.eventMouseMove);
108 this.initialized = true;
110 dispose: function() {
112 Event.stopObserving(this.track, "mousedown", this.eventMouseDown);
113 Event.stopObserving(document, "mouseup", this.eventMouseUp);
114 Event.stopObserving(document, "mousemove", this.eventMouseMove);
115 this.handles.each( function(h) {
116 Event.stopObserving(h, "mousedown", slider.eventMouseDown);
119 setDisabled: function(){
120 this.disabled = true;
122 setEnabled: function(){
123 this.disabled = false;
125 getNearestValue: function(value){
126 if(this.allowedValues){
127 if(value >= this.allowedValues.max()) return(this.allowedValues.max());
128 if(value <= this.allowedValues.min()) return(this.allowedValues.min());
130 var offset = Math.abs(this.allowedValues[0] - value);
131 var newValue = this.allowedValues[0];
132 this.allowedValues.each( function(v) {
133 var currentOffset = Math.abs(v - value);
134 if(currentOffset <= offset){
136 offset = currentOffset;
141 if(value > this.range.end) return this.range.end;
142 if(value < this.range.start) return this.range.start;
145 setValue: function(sliderValue, handleIdx){
147 this.activeHandleIdx = handleIdx || 0;
148 this.activeHandle = this.handles[this.activeHandleIdx];
151 handleIdx = handleIdx || this.activeHandleIdx || 0;
152 if(this.initialized && this.restricted) {
153 if((handleIdx>0) && (sliderValue<this.values[handleIdx-1]))
154 sliderValue = this.values[handleIdx-1];
155 if((handleIdx < (this.handles.length-1)) && (sliderValue>this.values[handleIdx+1]))
156 sliderValue = this.values[handleIdx+1];
158 sliderValue = this.getNearestValue(sliderValue);
159 this.values[handleIdx] = sliderValue;
160 this.value = this.values[0]; // assure backwards compat
162 this.handles[handleIdx].style[this.isVertical() ? 'top' : 'left'] =
163 this.translateToPx(sliderValue);
166 if(!this.dragging || !this.event) this.updateFinished();
168 setValueBy: function(delta, handleIdx) {
169 this.setValue(this.values[handleIdx || this.activeHandleIdx || 0] + delta,
170 handleIdx || this.activeHandleIdx || 0);
172 translateToPx: function(value) {
174 ((this.trackLength-this.handleLength)/(this.range.end-this.range.start)) *
175 (value - this.range.start)) + "px";
177 translateToValue: function(offset) {
178 return ((offset/(this.trackLength-this.handleLength) *
179 (this.range.end-this.range.start)) + this.range.start);
181 getRange: function(range) {
182 var v = this.values.sortBy(Prototype.K);
184 return $R(v[range],v[range+1]);
186 minimumOffset: function(){
187 return(this.isVertical() ? this.alignY : this.alignX);
189 maximumOffset: function(){
190 return(this.isVertical() ?
191 (this.track.offsetHeight != 0 ? this.track.offsetHeight :
192 this.track.style.height.replace(/px$/,"")) - this.alignY :
193 (this.track.offsetWidth != 0 ? this.track.offsetWidth :
194 this.track.style.width.replace(/px$/,"")) - this.alignY);
196 isVertical: function(){
197 return (this.axis == 'vertical');
199 drawSpans: function() {
202 $R(0, this.spans.length-1).each(function(r) { slider.setSpan(slider.spans[r], slider.getRange(r)) });
203 if(this.options.startSpan)
204 this.setSpan(this.options.startSpan,
205 $R(0, this.values.length>1 ? this.getRange(0).min() : this.value ));
206 if(this.options.endSpan)
207 this.setSpan(this.options.endSpan,
208 $R(this.values.length>1 ? this.getRange(this.spans.length-1).max() : this.value, this.maximum));
210 setSpan: function(span, range) {
211 if(this.isVertical()) {
212 span.style.top = this.translateToPx(range.start);
213 span.style.height = this.translateToPx(range.end - range.start + this.range.start);
215 span.style.left = this.translateToPx(range.start);
216 span.style.width = this.translateToPx(range.end - range.start + this.range.start);
219 updateStyles: function() {
220 this.handles.each( function(h){ Element.removeClassName(h, 'selected') });
221 Element.addClassName(this.activeHandle, 'selected');
223 startDrag: function(event) {
224 if(Event.isLeftClick(event)) {
228 var handle = Event.element(event);
229 var pointer = [Event.pointerX(event), Event.pointerY(event)];
231 if(track==this.track) {
232 var offsets = Position.cumulativeOffset(this.track);
234 this.setValue(this.translateToValue(
235 (this.isVertical() ? pointer[1]-offsets[1] : pointer[0]-offsets[0])-(this.handleLength/2)
237 var offsets = Position.cumulativeOffset(this.activeHandle);
238 this.offsetX = (pointer[0] - offsets[0]);
239 this.offsetY = (pointer[1] - offsets[1]);
241 // find the handle (prevents issues with Safari)
242 while((this.handles.indexOf(handle) == -1) && handle.parentNode)
243 handle = handle.parentNode;
245 this.activeHandle = handle;
246 this.activeHandleIdx = this.handles.indexOf(this.activeHandle);
249 var offsets = Position.cumulativeOffset(this.activeHandle);
250 this.offsetX = (pointer[0] - offsets[0]);
251 this.offsetY = (pointer[1] - offsets[1]);
257 update: function(event) {
259 if(!this.dragging) this.dragging = true;
261 // fix AppleWebKit rendering
262 if(navigator.appVersion.indexOf('AppleWebKit')>0) window.scrollBy(0,0);
266 draw: function(event) {
267 var pointer = [Event.pointerX(event), Event.pointerY(event)];
268 var offsets = Position.cumulativeOffset(this.track);
269 pointer[0] -= this.offsetX + offsets[0];
270 pointer[1] -= this.offsetY + offsets[1];
272 this.setValue(this.translateToValue( this.isVertical() ? pointer[1] : pointer[0] ));
273 if(this.initialized && this.options.onSlide)
274 this.options.onSlide(this.values.length>1 ? this.values : this.value, this);
276 endDrag: function(event) {
277 if(this.active && this.dragging) {
278 this.finishDrag(event, true);
282 this.dragging = false;
284 finishDrag: function(event, success) {
286 this.dragging = false;
287 this.updateFinished();
289 updateFinished: function() {
290 if(this.initialized && this.options.onChange)
291 this.options.onChange(this.values.length>1 ? this.values : this.value, this);