Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 33 additions & 46 deletions content/patterns/slider/examples/js/slider-color-viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,7 @@ class ColorViewerSliders {
this.initSliderRefs(this.sliders, 'green');
this.initSliderRefs(this.sliders, 'blue');

document.body.addEventListener(
'pointerup',
this.onThumbPointerUp.bind(this)
);
document.body.addEventListener('pointerup', this.onPointerUp.bind(this));

this.colorBoxNode = domNode.querySelector('.color-box');
this.colorValueHexNode = domNode.querySelector('input.color-value-hex');
Expand Down Expand Up @@ -103,34 +100,24 @@ class ColorViewerSliders {
this.sliders[slider].sliderNode.tabIndex = 0;
}

this.sliders[slider].railNode.addEventListener(
'click',
this.onRailClick.bind(this)
);

this.sliders[slider].sliderNode.addEventListener(
'keydown',
this.onSliderKeyDown.bind(this)
);

this.sliders[slider].sliderNode.addEventListener(
'pointerdown',
this.onThumbPointerDown.bind(this)
this.onSliderPointerDown.bind(this)
);

this.sliders[slider].valueNode.addEventListener(
'keydown',
this.onSliderKeyDown.bind(this)
);

this.sliders[slider].valueNode.addEventListener(
'pointerdown',
this.onThumbPointerDown.bind(this)
);

this.sliders[slider].sliderNode.addEventListener(
'pointermove',
this.onThumbPointerMove.bind(this)
this.onPointerMove.bind(this)
);

this.moveSliderTo(
Expand Down Expand Up @@ -269,55 +256,55 @@ class ColorViewerSliders {
}
}

onThumbPointerDown(event) {
this.pointerSlider = this.getSlider(event.currentTarget);
// Calculate the slider value from the horizontal position of a pointer event
getValueFromPointer(slider, event) {
var x = this.getSVGPoint(slider, event).x;
var min = this.getValueMin(slider);
var max = this.getValueMax(slider);
var diffX = x - this.railX;
return Math.round((diffX * (max - min)) / this.railWidth);
}

onSliderPointerDown(event) {
var slider = this.getSlider(event.currentTarget);

// Set focus to the clicked on
this.pointerSlider.sliderNode.focus();
this.pointerSlider = slider;

// Move the thumb to the pointer position on the down event, unless the
// thumb itself was pressed, then track the pointer until it is released
if (
event.target !== slider.thumbNode &&
event.target !== slider.focusNode
) {
this.moveSliderTo(slider, this.getValueFromPointer(slider, event));
}

// Set focus to the slider
slider.sliderNode.focus();

event.preventDefault();
event.stopPropagation();
}

onThumbPointerUp() {
onPointerUp() {
this.pointerSlider = false;
}

onThumbPointerMove(event) {
onPointerMove(event) {
if (
this.pointerSlider &&
this.pointerSlider.sliderNode.contains(event.target)
) {
let x = this.getSVGPoint(this.pointerSlider, event).x;
let min = this.getValueMin(this.pointerSlider);
let max = this.getValueMax(this.pointerSlider);
let diffX = x - this.railX;
let value = Math.round((diffX * (max - min)) / this.railWidth);
this.moveSliderTo(this.pointerSlider, value);
this.moveSliderTo(
this.pointerSlider,
this.getValueFromPointer(this.pointerSlider, event)
);

event.preventDefault();
event.stopPropagation();
}
}

// handle click event on the rail
onRailClick(event) {
var slider = this.getSlider(event.currentTarget);

var x = this.getSVGPoint(slider, event).x;
var min = this.getValueMin(slider);
var max = this.getValueMax(slider);
var diffX = x - this.railX;
var value = Math.round((diffX * (max - min)) / this.railWidth);
this.moveSliderTo(slider, value);

event.preventDefault();
event.stopPropagation();

// Set focus to the clicked handle
slider.sliderNode.focus();
}

getColorHex() {
var r = parseInt(
this.sliders.red.sliderNode.getAttribute('aria-valuenow')
Expand Down
27 changes: 9 additions & 18 deletions content/patterns/slider/examples/js/slider-rating.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ class RatingSlider {
);

this.labelTexts.forEach((lt) => {
lt.addEventListener('click', this.onTargetClick.bind(this));
lt.addEventListener('pointerdown', this.onTargetPointerDown.bind(this));
lt.addEventListener('pointermove', this.onPointerMove.bind(this));
});

this.targetRects.forEach((tr) => {
tr.addEventListener('click', this.onTargetClick.bind(this));
tr.addEventListener('pointerdown', this.onSliderPointerDown.bind(this));
tr.addEventListener('pointerdown', this.onTargetPointerDown.bind(this));
tr.addEventListener('pointermove', this.onPointerMove.bind(this));
});

Expand Down Expand Up @@ -382,26 +382,21 @@ class RatingSlider {
this.sliderNode.setAttribute('aria-valuetext', valuetext);
}

onTargetClick(event) {
onTargetPointerDown(event) {
// Move the thumb to the pressed rating on the down event, then track the
// pointer until it is released
this.isMoving = true;
this.moveSliderTo(
event.currentTarget.parentNode.getAttribute('data-value')
);

event.preventDefault();
event.stopPropagation();

// Set focus to the clicked handle
// Set focus to the slider
this.sliderNode.focus();
}

onSliderPointerDown(event) {
if (!this.isMoving) {
this.isMoving = true;
}
event.preventDefault();
event.stopPropagation();
}

onPointerMove(event) {
if (this.isMoving) {
this.moveSliderTo(
Expand All @@ -413,11 +408,7 @@ class RatingSlider {
}

onPointerUp() {
if (this.isMoving) {
this.isMoving = false;
// Set focus to the clicked handle
this.sliderNode.focus();
}
this.isMoving = false;
}

onResize() {
Expand Down
40 changes: 16 additions & 24 deletions content/patterns/slider/examples/js/slider-seek.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,12 @@ class SliderSeek {

// define possible slider positions

this.svgNode.addEventListener('click', this.onRailClick.bind(this));
this.svgNode.addEventListener('pointerdown', this.onPointerDown.bind(this));
this.sliderNode.addEventListener(
'keydown',
this.onSliderKeydown.bind(this)
);

this.sliderNode.addEventListener(
'pointerdown',
this.onSliderPointerDown.bind(this)
);

// bind a pointermove event handler to move pointer
this.svgNode.addEventListener('pointermove', this.onPointerMove.bind(this));

Expand Down Expand Up @@ -308,39 +303,36 @@ class SliderSeek {
);
}

onRailClick(event) {
var x = this.getSVGPoint(event).x;
// Calculate the slider value from the horizontal position of a pointer event
calcValue(x) {
var min = this.getValueMin();
var max = this.getValueMax();
var diffX = x - this.railX;
var value = Math.round((diffX * (max - min)) / this.railWidth);
this.moveSliderTo(value);

event.preventDefault();
event.stopPropagation();

// Set focus to the clicked handle
this.sliderNode.focus();
return Math.round((diffX * (max - min)) / this.railWidth);
}

onSliderPointerDown(event) {
onPointerDown(event) {
this.isMoving = true;

// Move the thumb to the pointer position on the down event, unless the
// thumb itself was pressed, then track the pointer until it is released
if (
event.target !== this.sliderThumbNode &&
event.target !== this.sliderFocusNode
) {
this.moveSliderTo(this.calcValue(this.getSVGPoint(event).x));
}

event.preventDefault();
event.stopPropagation();

// Set focus to the clicked handle
// Set focus to the slider
this.sliderNode.focus();
}

onPointerMove(event) {
if (this.isMoving) {
var x = this.getSVGPoint(event).x;
var min = this.getValueMin();
var max = this.getValueMax();
var diffX = x - this.railX;
var value = Math.round((diffX * (max - min)) / this.railWidth);
this.moveSliderTo(value);
this.moveSliderTo(this.calcValue(this.getSVGPoint(event).x));

event.preventDefault();
event.stopPropagation();
Expand Down
30 changes: 12 additions & 18 deletions content/patterns/slider/examples/js/slider-temperature.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,11 @@ class SliderTemperature {
this.sliderValueNode.setAttribute('x', this.valueX);
this.sliderFocusNode.setAttribute('x', this.focusX);

this.svgNode.addEventListener('click', this.onRailClick.bind(this));
this.svgNode.addEventListener('pointerdown', this.onPointerDown.bind(this));
this.sliderNode.addEventListener(
'keydown',
this.onSliderKeydown.bind(this)
);
this.sliderNode.addEventListener(
'pointerdown',
this.onSliderPointerDown.bind(this)
);

// bind a pointermove event handler to move pointer
this.svgNode.addEventListener('pointermove', this.onPointerMove.bind(this));
Expand Down Expand Up @@ -212,24 +208,22 @@ class SliderTemperature {
return max - (diffY * (max - min)) / this.railHeight;
}

onRailClick(event) {
var value = this.calcValue(this.getSVGPoint(event).y);
this.moveSliderTo(value);

event.preventDefault();
event.stopPropagation();

// Set focus to the clicked handle
this.sliderNode.focus();
}

onSliderPointerDown(event) {
onPointerDown(event) {
this.isMoving = true;

// Move the thumb to the pointer position on the down event, unless the
// thumb itself was pressed, then track the pointer until it is released
if (
event.target !== this.sliderThumbNode &&
event.target !== this.sliderFocusNode
) {
this.moveSliderTo(this.calcValue(this.getSVGPoint(event).y));
}

event.preventDefault();
event.stopPropagation();

// Set focus to the clicked handle
// Set focus to the slider
this.sliderNode.focus();
}

Expand Down