2009-12-27 17:30:58 -05:00
|
|
|
// script.aculo.us slider.js v1.8.3, Thu Oct 08 11:23:33 +0200 2009
|
2007-01-25 22:41:17 -05:00
|
|
|
|
2009-12-27 17:30:58 -05:00
|
|
|
// Copyright (c) 2005-2009 Marty Haught, Thomas Fuchs
|
2006-12-07 19:10:58 -05:00
|
|
|
//
|
2007-01-25 22:41:17 -05:00
|
|
|
// script.aculo.us is freely distributable under the terms of an MIT-style license.
|
|
|
|
// For details, see the script.aculo.us web site: http://script.aculo.us/
|
2006-12-07 19:10:58 -05:00
|
|
|
|
2007-12-28 14:17:21 -05:00
|
|
|
if (!Control) var Control = { };
|
2006-12-07 19:10:58 -05:00
|
|
|
|
|
|
|
// options:
|
|
|
|
// axis: 'vertical', or 'horizontal' (default)
|
|
|
|
//
|
|
|
|
// callbacks:
|
|
|
|
// onChange(value)
|
|
|
|
// onSlide(value)
|
2007-12-28 14:17:21 -05:00
|
|
|
Control.Slider = Class.create({
|
2006-12-07 19:10:58 -05:00
|
|
|
initialize: function(handle, track, options) {
|
|
|
|
var slider = this;
|
2009-12-27 17:30:58 -05:00
|
|
|
|
2007-12-28 14:17:21 -05:00
|
|
|
if (Object.isArray(handle)) {
|
2006-12-07 19:10:58 -05:00
|
|
|
this.handles = handle.collect( function(e) { return $(e) });
|
|
|
|
} else {
|
|
|
|
this.handles = [$(handle)];
|
|
|
|
}
|
2009-12-27 17:30:58 -05:00
|
|
|
|
2006-12-07 19:10:58 -05:00
|
|
|
this.track = $(track);
|
2007-12-28 14:17:21 -05:00
|
|
|
this.options = options || { };
|
2006-12-07 19:10:58 -05:00
|
|
|
|
|
|
|
this.axis = this.options.axis || 'horizontal';
|
|
|
|
this.increment = this.options.increment || 1;
|
|
|
|
this.step = parseInt(this.options.step || '1');
|
|
|
|
this.range = this.options.range || $R(0,1);
|
2009-12-27 17:30:58 -05:00
|
|
|
|
2006-12-07 19:10:58 -05:00
|
|
|
this.value = 0; // assure backwards compat
|
|
|
|
this.values = this.handles.map( function() { return 0 });
|
|
|
|
this.spans = this.options.spans ? this.options.spans.map(function(s){ return $(s) }) : false;
|
|
|
|
this.options.startSpan = $(this.options.startSpan || null);
|
|
|
|
this.options.endSpan = $(this.options.endSpan || null);
|
|
|
|
|
|
|
|
this.restricted = this.options.restricted || false;
|
|
|
|
|
|
|
|
this.maximum = this.options.maximum || this.range.end;
|
|
|
|
this.minimum = this.options.minimum || this.range.start;
|
|
|
|
|
|
|
|
// Will be used to align the handle onto the track, if necessary
|
|
|
|
this.alignX = parseInt(this.options.alignX || '0');
|
|
|
|
this.alignY = parseInt(this.options.alignY || '0');
|
2009-12-27 17:30:58 -05:00
|
|
|
|
2006-12-07 19:10:58 -05:00
|
|
|
this.trackLength = this.maximumOffset() - this.minimumOffset();
|
2007-01-25 22:41:17 -05:00
|
|
|
|
2009-12-27 17:30:58 -05:00
|
|
|
this.handleLength = this.isVertical() ?
|
|
|
|
(this.handles[0].offsetHeight != 0 ?
|
|
|
|
this.handles[0].offsetHeight : this.handles[0].style.height.replace(/px$/,"")) :
|
|
|
|
(this.handles[0].offsetWidth != 0 ? this.handles[0].offsetWidth :
|
2007-01-25 22:41:17 -05:00
|
|
|
this.handles[0].style.width.replace(/px$/,""));
|
2006-12-07 19:10:58 -05:00
|
|
|
|
|
|
|
this.active = false;
|
|
|
|
this.dragging = false;
|
|
|
|
this.disabled = false;
|
|
|
|
|
2007-12-28 14:17:21 -05:00
|
|
|
if (this.options.disabled) this.setDisabled();
|
2006-12-07 19:10:58 -05:00
|
|
|
|
|
|
|
// Allowed values array
|
|
|
|
this.allowedValues = this.options.values ? this.options.values.sortBy(Prototype.K) : false;
|
2007-12-28 14:17:21 -05:00
|
|
|
if (this.allowedValues) {
|
2006-12-07 19:10:58 -05:00
|
|
|
this.minimum = this.allowedValues.min();
|
|
|
|
this.maximum = this.allowedValues.max();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.eventMouseDown = this.startDrag.bindAsEventListener(this);
|
|
|
|
this.eventMouseUp = this.endDrag.bindAsEventListener(this);
|
|
|
|
this.eventMouseMove = this.update.bindAsEventListener(this);
|
|
|
|
|
|
|
|
// Initialize handles in reverse (make sure first handle is active)
|
|
|
|
this.handles.each( function(h,i) {
|
|
|
|
i = slider.handles.length-1-i;
|
|
|
|
slider.setValue(parseFloat(
|
2009-12-27 17:30:58 -05:00
|
|
|
(Object.isArray(slider.options.sliderValue) ?
|
|
|
|
slider.options.sliderValue[i] : slider.options.sliderValue) ||
|
2006-12-07 19:10:58 -05:00
|
|
|
slider.range.start), i);
|
2007-12-28 14:17:21 -05:00
|
|
|
h.makePositioned().observe("mousedown", slider.eventMouseDown);
|
2006-12-07 19:10:58 -05:00
|
|
|
});
|
2009-12-27 17:30:58 -05:00
|
|
|
|
2007-12-28 14:17:21 -05:00
|
|
|
this.track.observe("mousedown", this.eventMouseDown);
|
|
|
|
document.observe("mouseup", this.eventMouseUp);
|
|
|
|
document.observe("mousemove", this.eventMouseMove);
|
2009-12-27 17:30:58 -05:00
|
|
|
|
2006-12-07 19:10:58 -05:00
|
|
|
this.initialized = true;
|
|
|
|
},
|
|
|
|
dispose: function() {
|
2009-12-27 17:30:58 -05:00
|
|
|
var slider = this;
|
2006-12-07 19:10:58 -05:00
|
|
|
Event.stopObserving(this.track, "mousedown", this.eventMouseDown);
|
|
|
|
Event.stopObserving(document, "mouseup", this.eventMouseUp);
|
|
|
|
Event.stopObserving(document, "mousemove", this.eventMouseMove);
|
|
|
|
this.handles.each( function(h) {
|
|
|
|
Event.stopObserving(h, "mousedown", slider.eventMouseDown);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
setDisabled: function(){
|
|
|
|
this.disabled = true;
|
|
|
|
},
|
|
|
|
setEnabled: function(){
|
|
|
|
this.disabled = false;
|
2009-12-27 17:30:58 -05:00
|
|
|
},
|
2006-12-07 19:10:58 -05:00
|
|
|
getNearestValue: function(value){
|
2007-12-28 14:17:21 -05:00
|
|
|
if (this.allowedValues){
|
|
|
|
if (value >= this.allowedValues.max()) return(this.allowedValues.max());
|
|
|
|
if (value <= this.allowedValues.min()) return(this.allowedValues.min());
|
2009-12-27 17:30:58 -05:00
|
|
|
|
2006-12-07 19:10:58 -05:00
|
|
|
var offset = Math.abs(this.allowedValues[0] - value);
|
|
|
|
var newValue = this.allowedValues[0];
|
|
|
|
this.allowedValues.each( function(v) {
|
|
|
|
var currentOffset = Math.abs(v - value);
|
2007-12-28 14:17:21 -05:00
|
|
|
if (currentOffset <= offset){
|
2006-12-07 19:10:58 -05:00
|
|
|
newValue = v;
|
|
|
|
offset = currentOffset;
|
2009-12-27 17:30:58 -05:00
|
|
|
}
|
2006-12-07 19:10:58 -05:00
|
|
|
});
|
|
|
|
return newValue;
|
|
|
|
}
|
2007-12-28 14:17:21 -05:00
|
|
|
if (value > this.range.end) return this.range.end;
|
|
|
|
if (value < this.range.start) return this.range.start;
|
2006-12-07 19:10:58 -05:00
|
|
|
return value;
|
|
|
|
},
|
|
|
|
setValue: function(sliderValue, handleIdx){
|
2007-12-28 14:17:21 -05:00
|
|
|
if (!this.active) {
|
2007-01-25 22:41:17 -05:00
|
|
|
this.activeHandleIdx = handleIdx || 0;
|
|
|
|
this.activeHandle = this.handles[this.activeHandleIdx];
|
2006-12-07 19:10:58 -05:00
|
|
|
this.updateStyles();
|
|
|
|
}
|
|
|
|
handleIdx = handleIdx || this.activeHandleIdx || 0;
|
2007-12-28 14:17:21 -05:00
|
|
|
if (this.initialized && this.restricted) {
|
|
|
|
if ((handleIdx>0) && (sliderValue<this.values[handleIdx-1]))
|
2006-12-07 19:10:58 -05:00
|
|
|
sliderValue = this.values[handleIdx-1];
|
2007-12-28 14:17:21 -05:00
|
|
|
if ((handleIdx < (this.handles.length-1)) && (sliderValue>this.values[handleIdx+1]))
|
2006-12-07 19:10:58 -05:00
|
|
|
sliderValue = this.values[handleIdx+1];
|
|
|
|
}
|
|
|
|
sliderValue = this.getNearestValue(sliderValue);
|
|
|
|
this.values[handleIdx] = sliderValue;
|
|
|
|
this.value = this.values[0]; // assure backwards compat
|
2009-12-27 17:30:58 -05:00
|
|
|
|
|
|
|
this.handles[handleIdx].style[this.isVertical() ? 'top' : 'left'] =
|
2006-12-07 19:10:58 -05:00
|
|
|
this.translateToPx(sliderValue);
|
2009-12-27 17:30:58 -05:00
|
|
|
|
2006-12-07 19:10:58 -05:00
|
|
|
this.drawSpans();
|
2007-12-28 14:17:21 -05:00
|
|
|
if (!this.dragging || !this.event) this.updateFinished();
|
2006-12-07 19:10:58 -05:00
|
|
|
},
|
|
|
|
setValueBy: function(delta, handleIdx) {
|
2009-12-27 17:30:58 -05:00
|
|
|
this.setValue(this.values[handleIdx || this.activeHandleIdx || 0] + delta,
|
2006-12-07 19:10:58 -05:00
|
|
|
handleIdx || this.activeHandleIdx || 0);
|
|
|
|
},
|
|
|
|
translateToPx: function(value) {
|
|
|
|
return Math.round(
|
2009-12-27 17:30:58 -05:00
|
|
|
((this.trackLength-this.handleLength)/(this.range.end-this.range.start)) *
|
2006-12-07 19:10:58 -05:00
|
|
|
(value - this.range.start)) + "px";
|
|
|
|
},
|
|
|
|
translateToValue: function(offset) {
|
2009-12-27 17:30:58 -05:00
|
|
|
return ((offset/(this.trackLength-this.handleLength) *
|
2006-12-07 19:10:58 -05:00
|
|
|
(this.range.end-this.range.start)) + this.range.start);
|
|
|
|
},
|
|
|
|
getRange: function(range) {
|
2009-12-27 17:30:58 -05:00
|
|
|
var v = this.values.sortBy(Prototype.K);
|
2006-12-07 19:10:58 -05:00
|
|
|
range = range || 0;
|
|
|
|
return $R(v[range],v[range+1]);
|
|
|
|
},
|
|
|
|
minimumOffset: function(){
|
|
|
|
return(this.isVertical() ? this.alignY : this.alignX);
|
|
|
|
},
|
|
|
|
maximumOffset: function(){
|
2009-12-27 17:30:58 -05:00
|
|
|
return(this.isVertical() ?
|
2007-01-25 22:41:17 -05:00
|
|
|
(this.track.offsetHeight != 0 ? this.track.offsetHeight :
|
2009-12-27 17:30:58 -05:00
|
|
|
this.track.style.height.replace(/px$/,"")) - this.alignY :
|
|
|
|
(this.track.offsetWidth != 0 ? this.track.offsetWidth :
|
2007-12-28 14:17:21 -05:00
|
|
|
this.track.style.width.replace(/px$/,"")) - this.alignX);
|
2009-12-27 17:30:58 -05:00
|
|
|
},
|
2006-12-07 19:10:58 -05:00
|
|
|
isVertical: function(){
|
|
|
|
return (this.axis == 'vertical');
|
|
|
|
},
|
|
|
|
drawSpans: function() {
|
|
|
|
var slider = this;
|
2007-12-28 14:17:21 -05:00
|
|
|
if (this.spans)
|
2006-12-07 19:10:58 -05:00
|
|
|
$R(0, this.spans.length-1).each(function(r) { slider.setSpan(slider.spans[r], slider.getRange(r)) });
|
2007-12-28 14:17:21 -05:00
|
|
|
if (this.options.startSpan)
|
2006-12-07 19:10:58 -05:00
|
|
|
this.setSpan(this.options.startSpan,
|
|
|
|
$R(0, this.values.length>1 ? this.getRange(0).min() : this.value ));
|
2007-12-28 14:17:21 -05:00
|
|
|
if (this.options.endSpan)
|
2009-12-27 17:30:58 -05:00
|
|
|
this.setSpan(this.options.endSpan,
|
2006-12-07 19:10:58 -05:00
|
|
|
$R(this.values.length>1 ? this.getRange(this.spans.length-1).max() : this.value, this.maximum));
|
|
|
|
},
|
|
|
|
setSpan: function(span, range) {
|
2007-12-28 14:17:21 -05:00
|
|
|
if (this.isVertical()) {
|
2006-12-07 19:10:58 -05:00
|
|
|
span.style.top = this.translateToPx(range.start);
|
|
|
|
span.style.height = this.translateToPx(range.end - range.start + this.range.start);
|
|
|
|
} else {
|
|
|
|
span.style.left = this.translateToPx(range.start);
|
|
|
|
span.style.width = this.translateToPx(range.end - range.start + this.range.start);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
updateStyles: function() {
|
|
|
|
this.handles.each( function(h){ Element.removeClassName(h, 'selected') });
|
|
|
|
Element.addClassName(this.activeHandle, 'selected');
|
|
|
|
},
|
|
|
|
startDrag: function(event) {
|
2007-12-28 14:17:21 -05:00
|
|
|
if (Event.isLeftClick(event)) {
|
|
|
|
if (!this.disabled){
|
2006-12-07 19:10:58 -05:00
|
|
|
this.active = true;
|
2009-12-27 17:30:58 -05:00
|
|
|
|
2006-12-07 19:10:58 -05:00
|
|
|
var handle = Event.element(event);
|
|
|
|
var pointer = [Event.pointerX(event), Event.pointerY(event)];
|
2007-01-25 22:41:17 -05:00
|
|
|
var track = handle;
|
2007-12-28 14:17:21 -05:00
|
|
|
if (track==this.track) {
|
2009-12-27 17:30:58 -05:00
|
|
|
var offsets = this.track.cumulativeOffset();
|
2006-12-07 19:10:58 -05:00
|
|
|
this.event = event;
|
2009-12-27 17:30:58 -05:00
|
|
|
this.setValue(this.translateToValue(
|
2006-12-07 19:10:58 -05:00
|
|
|
(this.isVertical() ? pointer[1]-offsets[1] : pointer[0]-offsets[0])-(this.handleLength/2)
|
|
|
|
));
|
2009-12-27 17:30:58 -05:00
|
|
|
var offsets = this.activeHandle.cumulativeOffset();
|
2006-12-07 19:10:58 -05:00
|
|
|
this.offsetX = (pointer[0] - offsets[0]);
|
|
|
|
this.offsetY = (pointer[1] - offsets[1]);
|
|
|
|
} else {
|
|
|
|
// find the handle (prevents issues with Safari)
|
2009-12-27 17:30:58 -05:00
|
|
|
while((this.handles.indexOf(handle) == -1) && handle.parentNode)
|
2006-12-07 19:10:58 -05:00
|
|
|
handle = handle.parentNode;
|
2009-12-27 17:30:58 -05:00
|
|
|
|
2007-12-28 14:17:21 -05:00
|
|
|
if (this.handles.indexOf(handle)!=-1) {
|
2007-01-25 22:41:17 -05:00
|
|
|
this.activeHandle = handle;
|
|
|
|
this.activeHandleIdx = this.handles.indexOf(this.activeHandle);
|
|
|
|
this.updateStyles();
|
2009-12-27 17:30:58 -05:00
|
|
|
|
|
|
|
var offsets = this.activeHandle.cumulativeOffset();
|
2007-01-25 22:41:17 -05:00
|
|
|
this.offsetX = (pointer[0] - offsets[0]);
|
|
|
|
this.offsetY = (pointer[1] - offsets[1]);
|
|
|
|
}
|
2006-12-07 19:10:58 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Event.stop(event);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
update: function(event) {
|
2007-12-28 14:17:21 -05:00
|
|
|
if (this.active) {
|
|
|
|
if (!this.dragging) this.dragging = true;
|
2006-12-07 19:10:58 -05:00
|
|
|
this.draw(event);
|
2007-12-28 14:17:21 -05:00
|
|
|
if (Prototype.Browser.WebKit) window.scrollBy(0,0);
|
2006-12-07 19:10:58 -05:00
|
|
|
Event.stop(event);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
draw: function(event) {
|
|
|
|
var pointer = [Event.pointerX(event), Event.pointerY(event)];
|
2009-12-27 17:30:58 -05:00
|
|
|
var offsets = this.track.cumulativeOffset();
|
2006-12-07 19:10:58 -05:00
|
|
|
pointer[0] -= this.offsetX + offsets[0];
|
|
|
|
pointer[1] -= this.offsetY + offsets[1];
|
|
|
|
this.event = event;
|
|
|
|
this.setValue(this.translateToValue( this.isVertical() ? pointer[1] : pointer[0] ));
|
2007-12-28 14:17:21 -05:00
|
|
|
if (this.initialized && this.options.onSlide)
|
2006-12-07 19:10:58 -05:00
|
|
|
this.options.onSlide(this.values.length>1 ? this.values : this.value, this);
|
|
|
|
},
|
|
|
|
endDrag: function(event) {
|
2007-12-28 14:17:21 -05:00
|
|
|
if (this.active && this.dragging) {
|
2006-12-07 19:10:58 -05:00
|
|
|
this.finishDrag(event, true);
|
|
|
|
Event.stop(event);
|
|
|
|
}
|
|
|
|
this.active = false;
|
|
|
|
this.dragging = false;
|
2009-12-27 17:30:58 -05:00
|
|
|
},
|
2006-12-07 19:10:58 -05:00
|
|
|
finishDrag: function(event, success) {
|
|
|
|
this.active = false;
|
|
|
|
this.dragging = false;
|
|
|
|
this.updateFinished();
|
|
|
|
},
|
|
|
|
updateFinished: function() {
|
2009-12-27 17:30:58 -05:00
|
|
|
if (this.initialized && this.options.onChange)
|
2006-12-07 19:10:58 -05:00
|
|
|
this.options.onChange(this.values.length>1 ? this.values : this.value, this);
|
|
|
|
this.event = null;
|
|
|
|
}
|
2009-12-27 17:30:58 -05:00
|
|
|
});
|