Repository navigation
Expand file tree
/
Copy pathtools.js
More file actions
181 lines (176 loc) · 7.66 KB
/
Copy pathtools.js
File metadata and controls
181 lines (176 loc) · 7.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// The tool section as an exploded stack: one tilted pane per tool, stacked as in
// the hero, beside the card for the tool that is picked. Without this script the
// cards show as a plain grid.
(() => {
const TOWER = ['sqlload', 'sqltide', 'sqlbuild', 'sqlbabel', 'fensu', 'sqlclean']; // bottom to top
const CYCLE_MS = 4500;
const DWELL_MS = 120; // how long the pointer stays on a pane before it takes over
function init(section) {
const grid = section.querySelector('.tool-grid');
const reduced = matchMedia('(prefers-reduced-motion: reduce)');
const tools = [...grid.querySelectorAll(':scope > .tool')].map((article) => {
const key = [...article.classList].find((name) => name !== 'tool');
article.id = `tool-${key}`;
return { key, article, name: article.querySelector('.tool-name').textContent };
});
const byKey = (key) => tools.find((tool) => tool.key === key);
// Scrolling under a still pointer fires pointerenter on whatever is now beneath it. Hover
// picks a tool only when the pointer itself moved, so scrolling never undoes a choice.
let lastX = null;
let lastY = null;
let moved = false; // whether the pointermove being dispatched moved the pointer
let movedAt = -Infinity;
window.addEventListener('pointermove', (event) => {
moved = event.clientX !== lastX || event.clientY !== lastY;
lastX = event.clientX;
lastY = event.clientY;
if (moved) movedAt = performance.now();
}, { capture: true, passive: true });
// A hero layer announces itself on pointerenter, just before the pointermove that caused it.
const ifPointerMoved = (fn) => setTimeout(() => {
if (performance.now() - movedAt < 60) fn();
});
const logos = (tool) => [...tool.article.querySelectorAll('.tool-head img')].map((img) => img.cloneNode());
const layout = document.createElement('div');
layout.className = 'tool-x';
const stack = document.createElement('div');
stack.className = 'tool-x-stack';
const tower = document.createElement('div');
tower.className = 'tool-x-tower';
tower.setAttribute('aria-hidden', 'true');
// Each pane has an invisible twin that never lifts and takes the pointer, so lifting a pane
// can't change which one is under it; otherwise the edge between two panes flickers.
const hits = [];
const panes = TOWER.map((key, i) => {
const tool = byKey(key);
const pane = document.createElement('div');
pane.className = `tool-x-pane ${key}`;
pane.style.setProperty('--i', i);
const hit = document.createElement('div');
hit.className = 'tool-x-hit';
hit.dataset.pane = key;
hit.style.setProperty('--i', i);
hit.addEventListener('pointermove', (event) => {
if (event.pointerType === 'mouse' && moved) aim(key, event);
});
hit.addEventListener('click', () => pick(key, true));
hits.push(hit);
const name = document.createElement('span');
name.className = 'tool-x-name';
name.append(...logos(tool), tool.name);
const mini = tool.article.querySelector('.terminal').cloneNode(true);
mini.className = 'tool-x-mini';
pane.append(name, mini);
tower.append(pane);
return pane;
});
tower.append(...hits);
const chips = document.createElement('div');
chips.className = 'tool-x-chips';
chips.setAttribute('role', 'group');
chips.setAttribute('aria-label', 'Show a tool');
for (const tool of tools) {
const chip = document.createElement('button');
chip.type = 'button';
chip.className = `tool-x-chip ${tool.key}`;
chip.dataset.tool = tool.key;
chip.setAttribute('aria-controls', tool.article.id);
chip.append(...logos(tool), tool.name);
chip.addEventListener('click', () => pick(tool.key, true));
chips.append(chip);
}
stack.append(tower, chips);
grid.before(layout);
layout.append(stack, grid);
section.classList.add('is-exploded');
let current = null;
let timer = null;
let visible = false;
// Hover picks a pane once the pointer has stayed on it for DWELL_MS, and not while the pointer
// is heading for the card, so moving across to the card doesn't switch tools on the way.
let candidate = null;
let since = 0;
let dwell = null;
let from = null; // where the pointer was a moment ago, to tell which way it is going
function headingForCard(x, y) {
if (!from) return false;
const dx = x - from.x;
const dy = y - from.y;
if (Math.hypot(dx, dy) < 2) return false;
const card = grid.getBoundingClientRect();
// Does the ray from `from` through (x, y) reach the card?
let near = 0;
let far = Infinity;
for (const [d, p, lo, hi] of [[dx, from.x, card.left, card.right], [dy, from.y, card.top, card.bottom]]) {
if (d === 0) {
if (p < lo || p > hi) return false;
continue;
}
const a = (lo - p) / d;
const b = (hi - p) / d;
near = Math.max(near, Math.min(a, b));
far = Math.min(far, Math.max(a, b));
}
return near <= far;
}
function aim(key, event) {
const now = performance.now();
if (key !== candidate) {
candidate = key;
since = now;
}
const heading = headingForCard(event.clientX, event.clientY);
if (!from || now - from.t > 40) from = { x: event.clientX, y: event.clientY, t: now };
clearTimeout(dwell);
if (candidate === current) return;
const due = Math.max(0, since + DWELL_MS - now);
if (!heading && due === 0) {
pick(candidate, true);
return;
}
// Check again shortly: by then the pointer has either stopped here or moved on.
dwell = setTimeout(() => {
from = null;
if (candidate && document.elementFromPoint(lastX, lastY)?.dataset.pane === candidate) pick(candidate, true);
}, heading ? DWELL_MS * 2 : due);
}
tower.addEventListener('pointerleave', () => {
clearTimeout(dwell);
candidate = null;
from = null;
});
function pick(key, byPerson) {
if (byPerson) stopCycle();
if (key === current) return;
current = key;
for (const pane of panes) pane.classList.toggle('is-active', pane.classList.contains(key));
for (const chip of chips.children) chip.setAttribute('aria-pressed', String(chip.dataset.tool === key));
for (const tool of tools) tool.article.classList.toggle('is-active', tool.key === key);
}
function stopCycle() {
clearInterval(timer);
timer = 'stopped';
}
// Until someone picks a tool, show each in turn while the section is on screen. Not on a phone,
// where the card sits below the stack at its own height, so cycling would move the page.
if (!reduced.matches && !matchMedia('(max-width: 620px)').matches) {
timer = setInterval(() => {
if (!visible || document.hidden) return;
const at = tools.findIndex((tool) => tool.key === current);
pick(tools[(at + 1) % tools.length].key, false);
}, CYCLE_MS);
}
reduced.addEventListener('change', () => { if (reduced.matches) stopCycle(); });
new IntersectionObserver(([entry]) => { visible = entry.isIntersecting; }, { threshold: 0.4 }).observe(layout);
// Pointing at a layer in the hero shows that tool here too.
document.addEventListener('stack:active', ({ detail }) => {
if (!detail.tool || !byKey(detail.tool)) return;
if (detail.link === document.activeElement) pick(detail.tool, true);
else ifPointerMoved(() => pick(detail.tool, true));
});
pick(tools[0].key, false);
}
document.addEventListener('DOMContentLoaded', () => {
document.querySelectorAll('.tools').forEach(init);
});
})();