Repository navigation
Expand file tree
/
Copy pathtools.js
More file actions
179 lines (173 loc) · 7.8 KB
/
Copy pathtools.js
File metadata and controls
179 lines (173 loc) · 7.8 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
// The tool section as the SQLStack mark (stack.js), opened up: SQLBuild, SQLTide and SQLLoad as the
// brim's strips, the other tools as the discs above them, each named beside its layer, next to the
// card for the tool that is picked. The picked layer lights in its tool's colour and the stack opens
// around it; a click on the mark knocks the layer it hits. Each name links to its tool's site.
// Without this script the cards show as a plain grid.
(() => {
const TOWER = ['sqlclean', 'fensu', 'sqlbabel', 'sqlbuild', 'sqltide', 'sqlload']; // top to bottom
const BRIM = ['sqlbuild', 'sqltide', 'sqlload'];
const CYCLE_MS = 4500;
const DWELL_MS = 50; // how long the pointer stays on a layer 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 mark = document.createElement('div');
mark.className = 'stack-visual tool-mark';
mark.setAttribute('data-labels-beside', '');
const canvas = document.createElement('canvas');
canvas.setAttribute('aria-hidden', 'true');
const names = document.createElement('nav');
names.className = 'stack-labels';
names.setAttribute('aria-label', 'All tools');
for (const key of TOWER) {
const tool = byKey(key);
const link = document.createElement('a');
link.href = tool.article.querySelector('.tool-head').getAttribute('href');
link.className = key;
link.dataset.layer = key;
if (BRIM.includes(key)) link.setAttribute('data-brim', '');
const label = document.createElement('span');
label.className = 'label';
const name = document.createElement('span');
name.className = 'name';
name.textContent = tool.name;
label.append(...logos(tool), name);
link.append(label);
names.append(link);
}
mark.append(canvas, names);
stack.append(mark);
grid.before(layout);
layout.append(stack, grid);
section.classList.add('is-exploded');
window.SQLStackMark(mark);
// A layer or name of the mark under the pointer, if any.
const layerAt = (x, y) => document.elementFromPoint(x, y)?.closest('.tool-mark a[data-layer]')?.dataset.layer;
mark.addEventListener('pointermove', (event) => {
const key = event.target.closest?.('a[data-layer]')?.dataset.layer;
if (key && event.pointerType === 'mouse' && moved) aim(key, event);
});
mark.addEventListener('pointerleave', () => {
clearTimeout(dwell);
candidate = null;
from = null;
});
// A click on a layer picks its tool (stack.js knocks it).
mark.addEventListener('stack:choose', ({ detail }) => pick(detail, true));
let current = null;
let timer = null;
let visible = false;
// Hover picks a layer 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 && layerAt(lastX, lastY) === candidate) pick(candidate, true);
}, heading ? DWELL_MS * 2 : due);
}
function pick(key, byPerson) {
if (byPerson) stopCycle();
if (key === current) return;
current = key;
mark.dispatchEvent(new CustomEvent('stack:pick', { detail: key }));
for (const tool of tools) {
tool.article.classList.toggle('is-active', tool.key === key);
// A card fading out stays visible for a moment; inert keeps Tab from landing in it.
tool.article.inert = 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);
// Focusing a name here, or pointing at a layer in the hero, shows that tool. Pointing at this
// mark goes through aim() above instead.
document.addEventListener('stack:active', ({ detail }) => {
if (!detail.tool || !byKey(detail.tool)) return;
if (detail.link === document.activeElement) pick(detail.tool, true);
else if (!mark.contains(detail.link)) ifPointerMoved(() => pick(detail.tool, true));
});
pick(tools[0].key, false);
}
document.addEventListener('DOMContentLoaded', () => {
document.querySelectorAll('.tools').forEach(init);
});
})();