136 lines
6.5 KiB
JavaScript
136 lines
6.5 KiB
JavaScript
// Five visible steps, with one offscreen row at either end for the rolling motion.
|
|
export function assemblyStepWindow(total, current) {
|
|
// Keep the active step in the middle, including the first and last steps.
|
|
return { size: total ? 5 : 0, start: current - 2 };
|
|
}
|
|
|
|
export function assemblyStepTitle(step) {
|
|
const label = String(step.label || step.id);
|
|
return label.split('·').map(s => s.trim()).filter(Boolean).at(-1) || label;
|
|
}
|
|
|
|
export class AssemblyTimeline {
|
|
constructor(element, steps, onChoose, onScrubStart = () => {}) {
|
|
this.element = element;
|
|
this.track = element.querySelector('ol');
|
|
this.steps = steps;
|
|
this.enabled = true;
|
|
this.animations = [];
|
|
this.onClick = event => {
|
|
if (performance.now() < (this.ignoreClickUntil || 0)) return;
|
|
const button = event.target.closest('[data-step-index]');
|
|
if (!button || button.disabled || button.parentElement.inert || !this.enabled) return;
|
|
onChoose(Number(button.dataset.stepIndex));
|
|
};
|
|
this.onPointerDown = event => {
|
|
if (!this.enabled || !event.isPrimary || event.button !== 0 || this.gesture || this.current === undefined) return;
|
|
this.gesture = { id: event.pointerId, y: event.clientY, start: this.current, moved: false };
|
|
onScrubStart();
|
|
};
|
|
this.onPointerMove = event => {
|
|
const gesture = this.gesture;
|
|
if (!gesture || gesture.id !== event.pointerId) return;
|
|
const distance = gesture.y - event.clientY;
|
|
if (!gesture.moved && Math.abs(distance) < 8) return;
|
|
if (!gesture.moved) {
|
|
gesture.moved = true;
|
|
element.setPointerCapture(event.pointerId);
|
|
element.classList.add('is-scrubbing');
|
|
}
|
|
const height = parseFloat(getComputedStyle(element).getPropertyValue('--assembly-row-height'));
|
|
const index = Math.max(0, Math.min(this.steps.length - 1, gesture.start + Math.round(distance / height)));
|
|
if (index !== this.current) onChoose(index, false);
|
|
};
|
|
this.onPointerEnd = event => {
|
|
if (!this.gesture || this.gesture.id !== event.pointerId) return;
|
|
if (this.gesture.moved) this.ignoreClickUntil = performance.now() + 350;
|
|
this.gesture = null;
|
|
element.classList.remove('is-scrubbing');
|
|
if (element.hasPointerCapture(event.pointerId)) element.releasePointerCapture(event.pointerId);
|
|
};
|
|
element.addEventListener('click', this.onClick);
|
|
element.addEventListener('pointerdown', this.onPointerDown);
|
|
element.addEventListener('pointermove', this.onPointerMove);
|
|
element.addEventListener('pointerup', this.onPointerEnd);
|
|
element.addEventListener('pointercancel', this.onPointerEnd);
|
|
}
|
|
|
|
update(current, animate = true) {
|
|
const { size, start } = assemblyStepWindow(this.steps.length, current);
|
|
const delta = this.start === undefined ? 0 : start - this.start;
|
|
const restoreFocus = this.track.contains(document.activeElement);
|
|
// Capture the in-flight appearance so a quick second click never snaps widths.
|
|
const previous = new Map([...this.track.querySelectorAll('button')].map(button => {
|
|
const style = getComputedStyle(button);
|
|
return [Number(button.dataset.stepIndex), {
|
|
width: style.width, color: style.color, backgroundColor: style.backgroundColor,
|
|
borderColor: style.borderColor, boxShadow: style.boxShadow, opacity: style.opacity,
|
|
}];
|
|
}));
|
|
this.animations.forEach(animation => animation.cancel());
|
|
this.animations = [];
|
|
this.element.style.setProperty('--assembly-visible-rows', size);
|
|
const rows = [];
|
|
for (let slot = -1; slot <= size; slot++) {
|
|
const index = start + slot, step = this.steps[index], row = document.createElement('li');
|
|
const offscreen = slot < 0 || slot >= size;
|
|
row.inert = offscreen;
|
|
if (offscreen || !step) row.setAttribute('aria-hidden', 'true');
|
|
if (step) {
|
|
const button = document.createElement('button');
|
|
button.type = 'button';
|
|
button.dataset.stepIndex = index;
|
|
button.disabled = !this.enabled;
|
|
button.title = `${index + 1}. ${step.label}`;
|
|
button.setAttribute('aria-label', `第 ${index + 1} 步:${step.label}`);
|
|
button.tabIndex = offscreen ? -1 : 0;
|
|
button.className = `assembly-timeline-item ${index === current ? 'is-current' : index < current ? 'is-done' : 'is-upcoming'}`;
|
|
if (index === current) button.setAttribute('aria-current', 'step');
|
|
const number = document.createElement('span');
|
|
number.className = 'assembly-timeline-number';
|
|
number.textContent = String(index + 1).padStart(2, '0');
|
|
const title = document.createElement('span');
|
|
title.className = 'assembly-timeline-name';
|
|
title.textContent = assemblyStepTitle(step);
|
|
button.append(number, title);
|
|
row.append(button);
|
|
}
|
|
rows.push(row);
|
|
}
|
|
this.track.replaceChildren(...rows);
|
|
// Only adjacent changes roll one row. Seeking must not fly past hundreds of steps.
|
|
if (animate && this.element.clientHeight > 0 && Math.abs(delta) === 1 && !matchMedia('(prefers-reduced-motion: reduce)').matches) {
|
|
const height = parseFloat(getComputedStyle(this.element).getPropertyValue('--assembly-row-height'));
|
|
const timing = { duration: 460, easing: 'cubic-bezier(.22, 1, .36, 1)' };
|
|
this.animations.push(this.track.animate([
|
|
{ transform: `translateY(${(delta - 1) * height}px)` },
|
|
{ transform: `translateY(${-height}px)` },
|
|
], timing));
|
|
for (const button of this.track.querySelectorAll('button')) {
|
|
const before = previous.get(Number(button.dataset.stepIndex));
|
|
if (!before) continue;
|
|
const style = getComputedStyle(button);
|
|
const after = Object.fromEntries(Object.keys(before).map(key => [key, style[key]]));
|
|
this.animations.push(button.animate([before, after], timing));
|
|
}
|
|
}
|
|
this.start = start;
|
|
this.current = current;
|
|
if (restoreFocus) this.track.querySelector('[aria-current="step"]')?.focus({ preventScroll: true });
|
|
}
|
|
|
|
setEnabled(enabled) {
|
|
this.enabled = enabled;
|
|
this.track.querySelectorAll('button').forEach(button => { button.disabled = !enabled; });
|
|
}
|
|
|
|
dispose() {
|
|
this.animations.forEach(animation => animation.cancel());
|
|
this.element.removeEventListener('click', this.onClick);
|
|
this.element.removeEventListener('pointerdown', this.onPointerDown);
|
|
this.element.removeEventListener('pointermove', this.onPointerMove);
|
|
this.element.removeEventListener('pointerup', this.onPointerEnd);
|
|
this.element.removeEventListener('pointercancel', this.onPointerEnd);
|
|
}
|
|
}
|