Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add touchscreen support (for e.g. ipad) #30

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 12 additions & 4 deletions src/slider.js
Expand Up @@ -77,21 +77,29 @@ Control.Slider = Class.create({
slider.options.sliderValue[i] : slider.options.sliderValue) || slider.options.sliderValue[i] : slider.options.sliderValue) ||
slider.range.start), i); slider.range.start), i);
h.makePositioned().observe("mousedown", slider.eventMouseDown); h.makePositioned().observe("mousedown", slider.eventMouseDown);
h.observe("touchstart", slider.eventMouseDown);
}); });


this.track.observe("mousedown", this.eventMouseDown); this.track.observe("mousedown", this.eventMouseDown);
this.track.observe("touchstart", this.eventMouseDown);
document.observe("mouseup", this.eventMouseUp); document.observe("mouseup", this.eventMouseUp);
document.observe("touchend", this.eventMouseUp);
document.observe("mousemove", this.eventMouseMove); document.observe("mousemove", this.eventMouseMove);
document.observe("touchmove", this.eventMouseMove);


this.initialized = true; this.initialized = true;
}, },
dispose: function() { dispose: function() {
var slider = this; var slider = this;
Event.stopObserving(this.track, "mousedown", this.eventMouseDown); Event.stopObserving(this.track, "mousedown", this.eventMouseDown);
Event.stopObserving(this.track, "touchstart", this.eventMouseDown);
Event.stopObserving(document, "mouseup", this.eventMouseUp); Event.stopObserving(document, "mouseup", this.eventMouseUp);
Event.stopObserving(document, "touchend", this.eventMouseUp);
Event.stopObserving(document, "mousemove", this.eventMouseMove); Event.stopObserving(document, "mousemove", this.eventMouseMove);
Event.stopObserving(document, "touchmove", this.eventMouseMove);
this.handles.each( function(h) { this.handles.each( function(h) {
Event.stopObserving(h, "mousedown", slider.eventMouseDown); Event.stopObserving(h, "mousedown", slider.eventMouseDown);
Event.stopObserving(h, "touchstart", slider.eventMouseDown);
}); });
}, },
setDisabled: function(){ setDisabled: function(){
Expand Down Expand Up @@ -199,12 +207,12 @@ Control.Slider = Class.create({
Element.addClassName(this.activeHandle, 'selected'); Element.addClassName(this.activeHandle, 'selected');
}, },
startDrag: function(event) { startDrag: function(event) {
if (Event.isLeftClick(event)) { if (Event.isLeftClick(event) || event.touches) {
if (!this.disabled){ if (!this.disabled){
this.active = true; this.active = true;


var handle = Event.element(event); var handle = Event.element(event);
var pointer = [Event.pointerX(event), Event.pointerY(event)]; var pointer = (event.touches ? [event.touches[0].clientX, event.touches[0].clientY] : [Event.pointerX(event), Event.pointerY(event)]);
var track = handle; var track = handle;
if (track==this.track) { if (track==this.track) {
var offsets = this.track.cumulativeOffset(); var offsets = this.track.cumulativeOffset();
Expand Down Expand Up @@ -238,12 +246,12 @@ Control.Slider = Class.create({
if (this.active) { if (this.active) {
if (!this.dragging) this.dragging = true; if (!this.dragging) this.dragging = true;
this.draw(event); this.draw(event);
if (Prototype.Browser.WebKit) window.scrollBy(0,0); if (Prototype.Browser.WebKit && !event.touches) window.scrollBy(0,0);
Event.stop(event); Event.stop(event);
} }
}, },
draw: function(event) { draw: function(event) {
var pointer = [Event.pointerX(event), Event.pointerY(event)]; var pointer = (event.touches ? [event.touches[0].clientX, event.touches[0].clientY] : [Event.pointerX(event), Event.pointerY(event)]);
var offsets = this.track.cumulativeOffset(); var offsets = this.track.cumulativeOffset();
pointer[0] -= this.offsetX + offsets[0]; pointer[0] -= this.offsetX + offsets[0];
pointer[1] -= this.offsetY + offsets[1]; pointer[1] -= this.offsetY + offsets[1];
Expand Down