@plusguard/reels 0.0.0-stage → 1.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +303 -2
- package/dist/index.d.ts +2 -0
- package/dist/index.js +1162 -0
- package/index.css +579 -0
- package/index.ts +1146 -0
- package/package.json +21 -5
package/dist/index.js
ADDED
|
@@ -0,0 +1,1162 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* reels — shoppable reels for a store: a strip of video cards on the page,
|
|
3
|
+
* each card a group of clips, each clip with its own product.
|
|
4
|
+
*
|
|
5
|
+
* <section data-pg-reels>
|
|
6
|
+
* <button data-pg-reels-prev aria-controls="strip">‹</button>
|
|
7
|
+
* <div data-pg-reels-strip id="strip">
|
|
8
|
+
* <article data-pg-reels-group>
|
|
9
|
+
* <div data-pg-reels-card> the 9:16 card
|
|
10
|
+
* <figure data-pg-reels-clip> a clip: a video…
|
|
11
|
+
* <video src="a.mp4" poster="a.jpg" muted playsinline preload="none"></video>
|
|
12
|
+
* </figure>
|
|
13
|
+
* <figure data-pg-reels-clip data-pg-reels-duration="5s">
|
|
14
|
+
* <img src="b.jpg" alt="" /> …or a picture, for a while
|
|
15
|
+
* </figure>
|
|
16
|
+
* <button data-pg-reels-sound aria-pressed="false">…</button>
|
|
17
|
+
* <button data-pg-reels-open>…</button>
|
|
18
|
+
* </div>
|
|
19
|
+
* <div data-pg-reels-bars></div> filled here: a bar per clip
|
|
20
|
+
* <div data-pg-reels-products> one per clip, in order
|
|
21
|
+
* <a data-pg-reels-product href="/a">…</a>
|
|
22
|
+
* <a data-pg-reels-product href="/b">…</a>
|
|
23
|
+
* </div>
|
|
24
|
+
* </article>
|
|
25
|
+
* </div>
|
|
26
|
+
* <button data-pg-reels-next aria-controls="strip">›</button>
|
|
27
|
+
* </section>
|
|
28
|
+
*
|
|
29
|
+
* On the page: every card in sight plays, muted until its sound button is
|
|
30
|
+
* pressed (sound is one card's alone: another card's button pressed gives
|
|
31
|
+
* it to that card; the card going out of sight takes it away) — its clips
|
|
32
|
+
* one after another, round and round, as stories do, a picture for its
|
|
33
|
+
* data-pg-reels-duration;
|
|
34
|
+
* the bars under it fill as each plays. A bar pressed is that clip at
|
|
35
|
+
* once. The clip playing shows its product: the products under the card
|
|
36
|
+
* are one per clip, and only the playing one's shows. The others wait on
|
|
37
|
+
* their posters. Out of sight, nothing plays.
|
|
38
|
+
*
|
|
39
|
+
* The strip scrolls sideways — a finger, a wheel, its arrows; it never
|
|
40
|
+
* moves by itself. data-pg-reels-start / -end on the section while it
|
|
41
|
+
* stands at either end (the arrows' to hide or dim).
|
|
42
|
+
*
|
|
43
|
+
* A card pressed — anywhere but its own buttons and links — or its
|
|
44
|
+
* data-pg-reels-open: the full view opens on that group and clip. First
|
|
45
|
+
* the event pg-reels:open on the section (detail: group, clip); a site
|
|
46
|
+
* that calls preventDefault() on it opens its own.
|
|
47
|
+
*
|
|
48
|
+
* The full view — a <dialog>, built here on <body>, its contents copies:
|
|
49
|
+
* the clip playing, the group's data-pg-reels-head, the clip's product.
|
|
50
|
+
* Within a group: the bars on top, a press on the video's right or left,
|
|
51
|
+
* → and ←. Between groups: a finger (or the mouse) dragging the video up
|
|
52
|
+
* or down — it follows, the next group coming in under it, and let go
|
|
53
|
+
* goes on or back — its ↑ and ↓ buttons, a wheel, ↑ and ↓; after a
|
|
54
|
+
* group's last clip, the next group. The product changes once it has
|
|
55
|
+
* landed. Held
|
|
56
|
+
* down — a finger, the mouse — it stands still; its own play / pause, its
|
|
57
|
+
* sound, its close — under the close; left of it, the group's view count
|
|
58
|
+
* (its data-pg-reels-views, the site's). Esc, Back, the dark around it
|
|
59
|
+
* close it too. The
|
|
60
|
+
* product: on a computer a panel beside the video — the product's
|
|
61
|
+
* <template data-pg-reels-detail>, the site's markup: a title (its
|
|
62
|
+
* data-pg-reels-title goes to the panel's head, beside the close), a big
|
|
63
|
+
* picture (data-pg-reels-big), small ones (data-pg-reels-thumb, a press
|
|
64
|
+
* shows it big), variants (data-pg-reels-variant buttons: each its
|
|
65
|
+
* price, its gallery — data-pg-reels-gallery of the same name — and its
|
|
66
|
+
* link), the price, a link (its data-pg-reels-foot is pinned to
|
|
67
|
+
* the panel's foot, the rest scrolling above it) — on a phone under the
|
|
68
|
+
* video, the product as on the card, and its details opened by a button.
|
|
69
|
+
*
|
|
70
|
+
* Marks written here: data-pg-reels-current on the playing clip and its
|
|
71
|
+
* product, on each group's own clip and product too; data-pg-reels-playing
|
|
72
|
+
* on the group that plays; aria-current on its bar, data-pg-reels-done on
|
|
73
|
+
* the bars before it, and --pg-reels-progress (0 to 1) on the bar playing.
|
|
74
|
+
*
|
|
75
|
+
* Seen: a group shown in the full view is seen — data-pg-reels-seen="true"
|
|
76
|
+
* on it ("false" before), for its ring to go grey as stories' do. With a
|
|
77
|
+
* data-pg-reels-id on the group it is remembered in this browser;
|
|
78
|
+
* forgetReelsSeen() forgets it all — every ring bright again, at once.
|
|
79
|
+
*
|
|
80
|
+
* CSS, per screen size: --pg-reels-show (cards across: 4; 1.5 on a phone
|
|
81
|
+
* shows one and half the next), --pg-reels-gap, --pg-reels-duration (a
|
|
82
|
+
* picture's time, 5s) — see index.css.
|
|
83
|
+
*
|
|
84
|
+
* With reduced motion asked for, nothing plays by itself: the posters, and
|
|
85
|
+
* a bar pressed shows its clip. React's markup is never moved: the bars go
|
|
86
|
+
* into the empty data-pg-reels-bars; everything else only gets marks.
|
|
87
|
+
*
|
|
88
|
+
* No dependencies.
|
|
89
|
+
*/
|
|
90
|
+
const ROOT = "data-pg-reels";
|
|
91
|
+
const STRIP = "data-pg-reels-strip";
|
|
92
|
+
const GROUP = "data-pg-reels-group";
|
|
93
|
+
const CARD = "data-pg-reels-card";
|
|
94
|
+
const CLIP = "data-pg-reels-clip";
|
|
95
|
+
const BARS = "data-pg-reels-bars";
|
|
96
|
+
const BAR = "data-pg-reels-bar";
|
|
97
|
+
const PRODUCT = "data-pg-reels-product";
|
|
98
|
+
const CURRENT = "data-pg-reels-current";
|
|
99
|
+
const PLAYING = "data-pg-reels-playing";
|
|
100
|
+
const DONE = "data-pg-reels-done";
|
|
101
|
+
const DURATION = "data-pg-reels-duration";
|
|
102
|
+
const SOUND = "data-pg-reels-sound";
|
|
103
|
+
const SOUND_ON = "data-pg-reels-sound-on";
|
|
104
|
+
const OPEN = "data-pg-reels-open";
|
|
105
|
+
const PREV = "data-pg-reels-prev";
|
|
106
|
+
const NEXT = "data-pg-reels-next";
|
|
107
|
+
const START = "data-pg-reels-start";
|
|
108
|
+
const END = "data-pg-reels-end";
|
|
109
|
+
const PROGRESS = "--pg-reels-progress";
|
|
110
|
+
const HEAD = "data-pg-reels-head";
|
|
111
|
+
const DETAIL = "data-pg-reels-detail";
|
|
112
|
+
const BIG = "data-pg-reels-big";
|
|
113
|
+
const THUMB = "data-pg-reels-thumb";
|
|
114
|
+
const VIEWER = "data-pg-reels-viewer";
|
|
115
|
+
const VIEW_BAR = "data-pg-reels-view-bar";
|
|
116
|
+
const PAUSED = "data-pg-reels-paused";
|
|
117
|
+
const HELD = "data-pg-reels-held";
|
|
118
|
+
const DETAILS = "data-pg-reels-details";
|
|
119
|
+
const NO_PRODUCT = "data-pg-reels-no-product";
|
|
120
|
+
const TITLE = "data-pg-reels-title";
|
|
121
|
+
const FOOT = "data-pg-reels-foot";
|
|
122
|
+
const ID = "data-pg-reels-id";
|
|
123
|
+
const VIEWS = "data-pg-reels-views";
|
|
124
|
+
const VARIANT = "data-pg-reels-variant";
|
|
125
|
+
const VARIANT_PRICE = "data-pg-reels-variant-price";
|
|
126
|
+
const VARIANT_HREF = "data-pg-reels-variant-href";
|
|
127
|
+
const GALLERY = "data-pg-reels-gallery";
|
|
128
|
+
const PRICE = "data-pg-reels-price";
|
|
129
|
+
const SEEN = "data-pg-reels-seen";
|
|
130
|
+
const SEEN_KEY = "pg-reels-seen";
|
|
131
|
+
const SLIDING = "data-pg-reels-sliding";
|
|
132
|
+
/* A finger this far up or down, more than across: the video follows it. */
|
|
133
|
+
const DRAG_PX = 10;
|
|
134
|
+
/* Let go this fast (px a ms) the way it was going: on, however short. */
|
|
135
|
+
const FLICK = 0.45;
|
|
136
|
+
/* Held this long, a press stops it — not a tap. */
|
|
137
|
+
const HOLD_MS = 220;
|
|
138
|
+
/* A wheel's turn this far is a step; then a pause before the next. */
|
|
139
|
+
const WHEEL_PX = 60;
|
|
140
|
+
const WHEEL_REST_MS = 700;
|
|
141
|
+
/* A picture's time when nothing says. */
|
|
142
|
+
const PICTURE_MS = 5000;
|
|
143
|
+
/* In sight enough to play: this much of the card. */
|
|
144
|
+
const IN_SIGHT = 0.6;
|
|
145
|
+
const px = (value) => parseFloat(value) || 0;
|
|
146
|
+
/* A time — seconds or ms written — in ms. */
|
|
147
|
+
function timeOf(raw, fallback) {
|
|
148
|
+
const text = (raw ?? "").trim();
|
|
149
|
+
const n = Number.parseFloat(text);
|
|
150
|
+
if (!Number.isFinite(n))
|
|
151
|
+
return fallback;
|
|
152
|
+
return Math.max(0, text.endsWith("ms") ? n : n * 1000);
|
|
153
|
+
}
|
|
154
|
+
const reduced = () => matchMedia("(prefers-reduced-motion: reduce)").matches;
|
|
155
|
+
const videoOf = (clip) => clip?.querySelector("video") ?? null;
|
|
156
|
+
/* A picture's time: its own data-pg-reels-duration, or the page's
|
|
157
|
+
--pg-reels-duration, or 5s. */
|
|
158
|
+
function pictureTime(g, clip) {
|
|
159
|
+
const own = clip.getAttribute(DURATION);
|
|
160
|
+
if (own)
|
|
161
|
+
return timeOf(own, PICTURE_MS);
|
|
162
|
+
return timeOf(getComputedStyle(g.el).getPropertyValue("--pg-reels-duration"), PICTURE_MS);
|
|
163
|
+
}
|
|
164
|
+
/* ---------- Sound ----------
|
|
165
|
+
One sound at a time: the playing card's, or the full view's. */
|
|
166
|
+
let sound = false;
|
|
167
|
+
/* ---------- Copies for the full view ---------- */
|
|
168
|
+
/* A copy of the site's markup for the full view: no ids — they stay
|
|
169
|
+
unique — and none of this file's marks. */
|
|
170
|
+
function copyOf(el) {
|
|
171
|
+
const copy = el.cloneNode(true);
|
|
172
|
+
[copy, ...copy.querySelectorAll("*")].forEach((x) => {
|
|
173
|
+
x.removeAttribute("id");
|
|
174
|
+
[CURRENT, PLAYING, DONE].forEach((name) => x.removeAttribute(name));
|
|
175
|
+
});
|
|
176
|
+
copy.querySelectorAll(`[${DETAIL}]`).forEach((t) => t.remove());
|
|
177
|
+
return copy;
|
|
178
|
+
}
|
|
179
|
+
const svg = (paths) => `<svg viewBox="0 0 24 24" aria-hidden="true" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">${paths}</svg>`;
|
|
180
|
+
const ICONS = {
|
|
181
|
+
soundOff: '<path d="M4 9h4l5-4v14l-5-4H4zM17 9l5 6M22 9l-5 6"/>',
|
|
182
|
+
soundOn: '<path d="M4 9h4l5-4v14l-5-4H4zM17 8.5a5 5 0 0 1 0 7M19.5 6a8.5 8.5 0 0 1 0 12"/>',
|
|
183
|
+
pause: '<path d="M8 5v14M16 5v14"/>',
|
|
184
|
+
play: '<path d="M7 5v14l12-7z"/>',
|
|
185
|
+
close: '<path d="M6 6l12 12M18 6 6 18"/>',
|
|
186
|
+
up: '<path d="M12 19V5M5 12l7-7 7 7"/>',
|
|
187
|
+
down: '<path d="M12 5v14M19 12l-7 7-7-7"/>',
|
|
188
|
+
chevron: '<path d="m6 15 6-6 6 6"/>',
|
|
189
|
+
};
|
|
190
|
+
/* ---------- One group ---------- */
|
|
191
|
+
function partsOf(el, root) {
|
|
192
|
+
const own = (x) => x.closest(`[${GROUP}]`) === el;
|
|
193
|
+
const clips = [...el.querySelectorAll(`[${CLIP}]`)].filter(own);
|
|
194
|
+
const products = [...el.querySelectorAll(`[${PRODUCT}]`)].filter(own);
|
|
195
|
+
const holder = [...el.querySelectorAll(`[${BARS}]`)].find(own);
|
|
196
|
+
// One bar a clip — made here, in the empty holder, once.
|
|
197
|
+
let bars = [];
|
|
198
|
+
if (holder) {
|
|
199
|
+
if (holder.children.length !== clips.length) {
|
|
200
|
+
holder.replaceChildren(...clips.map((_, i) => {
|
|
201
|
+
const bar = document.createElement("button");
|
|
202
|
+
bar.type = "button";
|
|
203
|
+
bar.setAttribute(BAR, "");
|
|
204
|
+
bar.setAttribute("aria-label", `Clip ${i + 1} of ${clips.length}`);
|
|
205
|
+
bar.append(document.createElement("span"));
|
|
206
|
+
return bar;
|
|
207
|
+
}));
|
|
208
|
+
}
|
|
209
|
+
bars = [...holder.children];
|
|
210
|
+
}
|
|
211
|
+
const current = clips.findIndex((c) => c.hasAttribute(CURRENT));
|
|
212
|
+
return { el, root, clips, products, bars, at: Math.max(0, current), ratio: 0, shown: 0, since: 0 };
|
|
213
|
+
}
|
|
214
|
+
/* Show clip `i` of the group: its clip, its product, its bar. */
|
|
215
|
+
function show(g, i) {
|
|
216
|
+
const n = g.clips.length;
|
|
217
|
+
if (!n)
|
|
218
|
+
return;
|
|
219
|
+
const was = g.clips[g.at];
|
|
220
|
+
if (was && g.at !== i) {
|
|
221
|
+
const v = videoOf(was);
|
|
222
|
+
if (v) {
|
|
223
|
+
v.pause();
|
|
224
|
+
v.currentTime = 0;
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
g.at = ((i % n) + n) % n;
|
|
228
|
+
g.shown = 0;
|
|
229
|
+
g.since = performance.now();
|
|
230
|
+
g.clips.forEach((c, k) => c.toggleAttribute(CURRENT, k === g.at));
|
|
231
|
+
g.products.forEach((p, k) => p.toggleAttribute(CURRENT, k === g.at));
|
|
232
|
+
g.bars.forEach((b, k) => {
|
|
233
|
+
b.toggleAttribute(DONE, k < g.at);
|
|
234
|
+
if (k === g.at)
|
|
235
|
+
b.setAttribute("aria-current", "true");
|
|
236
|
+
else
|
|
237
|
+
b.removeAttribute("aria-current");
|
|
238
|
+
b.style.setProperty(PROGRESS, k < g.at ? "1" : "0");
|
|
239
|
+
});
|
|
240
|
+
}
|
|
241
|
+
/* ---------- Seen, kept in this browser ---------- */
|
|
242
|
+
// Groups with a data-pg-reels-id: in localStorage. Without: this visit.
|
|
243
|
+
const seenHere = new WeakSet();
|
|
244
|
+
function seenSet() {
|
|
245
|
+
try {
|
|
246
|
+
return new Set(JSON.parse(localStorage.getItem(SEEN_KEY) || "[]"));
|
|
247
|
+
}
|
|
248
|
+
catch {
|
|
249
|
+
return new Set();
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
function isSeen(el, set = seenSet()) {
|
|
253
|
+
const id = el.getAttribute(ID);
|
|
254
|
+
return id ? set.has(id) : seenHere.has(el);
|
|
255
|
+
}
|
|
256
|
+
function markSeen(el) {
|
|
257
|
+
const id = el.getAttribute(ID);
|
|
258
|
+
if (!id)
|
|
259
|
+
seenHere.add(el);
|
|
260
|
+
else {
|
|
261
|
+
const set = seenSet();
|
|
262
|
+
if (!set.has(id)) {
|
|
263
|
+
set.add(id);
|
|
264
|
+
try {
|
|
265
|
+
localStorage.setItem(SEEN_KEY, JSON.stringify([...set]));
|
|
266
|
+
}
|
|
267
|
+
catch {
|
|
268
|
+
// Private mode, or storage full: seen for this visit only.
|
|
269
|
+
seenHere.add(el);
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
paintSeen();
|
|
274
|
+
}
|
|
275
|
+
function paintSeen() {
|
|
276
|
+
const set = seenSet();
|
|
277
|
+
document.querySelectorAll(`[${GROUP}]`).forEach((el) => {
|
|
278
|
+
const value = String(isSeen(el, set));
|
|
279
|
+
if (el.getAttribute(SEEN) !== value)
|
|
280
|
+
el.setAttribute(SEEN, value);
|
|
281
|
+
});
|
|
282
|
+
}
|
|
283
|
+
/* Forget what was seen, in this browser: every ring bright again, at once
|
|
284
|
+
— no reload. */
|
|
285
|
+
export function forgetReelsSeen() {
|
|
286
|
+
try {
|
|
287
|
+
localStorage.removeItem(SEEN_KEY);
|
|
288
|
+
}
|
|
289
|
+
catch {
|
|
290
|
+
// Storage blocked: nothing kept, nothing to forget.
|
|
291
|
+
}
|
|
292
|
+
document.querySelectorAll(`[${GROUP}]`).forEach((el) => seenHere.delete(el));
|
|
293
|
+
paintSeen();
|
|
294
|
+
}
|
|
295
|
+
/* ---------- Everything on the page ---------- */
|
|
296
|
+
let live = null;
|
|
297
|
+
/* Start watching every reels section on the page, those rendered later
|
|
298
|
+
too; returns the cleanup for a React effect. Only the newest call
|
|
299
|
+
answers. */
|
|
300
|
+
export function initReels() {
|
|
301
|
+
if (typeof document === "undefined")
|
|
302
|
+
return () => { };
|
|
303
|
+
const self = {};
|
|
304
|
+
live = self;
|
|
305
|
+
const groups = new Map();
|
|
306
|
+
// The groups playing: every one in sight.
|
|
307
|
+
const playing = new Set();
|
|
308
|
+
let frame = 0;
|
|
309
|
+
// Which groups play: every one at least IN_SIGHT in sight.
|
|
310
|
+
const sights = typeof IntersectionObserver === "undefined"
|
|
311
|
+
? null
|
|
312
|
+
: new IntersectionObserver((entries) => {
|
|
313
|
+
entries.forEach((e) => {
|
|
314
|
+
const g = groups.get(e.target.closest(`[${GROUP}]`));
|
|
315
|
+
if (g)
|
|
316
|
+
g.ratio = e.isIntersecting ? e.intersectionRatio : 0;
|
|
317
|
+
});
|
|
318
|
+
choose();
|
|
319
|
+
}, { threshold: [0, 0.25, 0.5, 0.6, 0.75, 0.9, 1] });
|
|
320
|
+
const choose = () => {
|
|
321
|
+
if (live !== self || view.open)
|
|
322
|
+
return;
|
|
323
|
+
let changed = false;
|
|
324
|
+
groups.forEach((g) => {
|
|
325
|
+
const on = g.ratio >= IN_SIGHT;
|
|
326
|
+
if (on === playing.has(g))
|
|
327
|
+
return;
|
|
328
|
+
changed = true;
|
|
329
|
+
if (on) {
|
|
330
|
+
playing.add(g);
|
|
331
|
+
start(g);
|
|
332
|
+
}
|
|
333
|
+
else {
|
|
334
|
+
playing.delete(g);
|
|
335
|
+
stop(g);
|
|
336
|
+
}
|
|
337
|
+
});
|
|
338
|
+
// The card with the sound gone out of sight: the sound goes too.
|
|
339
|
+
if (sound && (!owner || !playing.has(owner))) {
|
|
340
|
+
sound = false;
|
|
341
|
+
changed = true;
|
|
342
|
+
}
|
|
343
|
+
if (changed)
|
|
344
|
+
applySound();
|
|
345
|
+
};
|
|
346
|
+
const stopAll = () => {
|
|
347
|
+
playing.forEach(stop);
|
|
348
|
+
playing.clear();
|
|
349
|
+
};
|
|
350
|
+
// The group whose sound button turned it on — or the one last seen in
|
|
351
|
+
// the full view.
|
|
352
|
+
let owner = null;
|
|
353
|
+
const applySound = () => {
|
|
354
|
+
groups.forEach((g) => {
|
|
355
|
+
const on = sound && !view.open && g === owner && playing.has(g);
|
|
356
|
+
g.clips.forEach((c) => {
|
|
357
|
+
const v = videoOf(c);
|
|
358
|
+
if (v)
|
|
359
|
+
v.muted = !on;
|
|
360
|
+
});
|
|
361
|
+
g.el.querySelectorAll(`[${SOUND}]`).forEach((b) => b.setAttribute("aria-pressed", String(on)));
|
|
362
|
+
});
|
|
363
|
+
document.querySelectorAll(`[${ROOT}]`).forEach((r) => r.toggleAttribute(SOUND_ON, sound && !view.open && !!owner && playing.has(owner) && owner.root === r));
|
|
364
|
+
const v = viewVideo();
|
|
365
|
+
if (v)
|
|
366
|
+
v.muted = !sound;
|
|
367
|
+
dialog.querySelector(`[${SOUND}]`)?.setAttribute("aria-pressed", String(sound && view.open));
|
|
368
|
+
};
|
|
369
|
+
const start = (g) => {
|
|
370
|
+
g.el.setAttribute(PLAYING, "");
|
|
371
|
+
if (reduced())
|
|
372
|
+
return;
|
|
373
|
+
const clip = g.clips[g.at];
|
|
374
|
+
const v = videoOf(clip);
|
|
375
|
+
g.since = performance.now();
|
|
376
|
+
if (v) {
|
|
377
|
+
v.muted = !(sound && g === owner);
|
|
378
|
+
v.preload = "auto";
|
|
379
|
+
v.play().catch(() => { });
|
|
380
|
+
}
|
|
381
|
+
tick();
|
|
382
|
+
};
|
|
383
|
+
const stop = (g) => {
|
|
384
|
+
g.el.removeAttribute(PLAYING);
|
|
385
|
+
const clip = g.clips[g.at];
|
|
386
|
+
videoOf(clip)?.pause();
|
|
387
|
+
// A picture keeps the time it has shown.
|
|
388
|
+
g.shown += performance.now() - g.since;
|
|
389
|
+
};
|
|
390
|
+
// One frame loop for every group that plays: its bar fills; a picture
|
|
391
|
+
// whose time is up — data-pg-reels-duration, --pg-reels-duration — or
|
|
392
|
+
// a video ended, is the next clip.
|
|
393
|
+
const tick = () => {
|
|
394
|
+
if (frame)
|
|
395
|
+
return;
|
|
396
|
+
frame = requestAnimationFrame(function step(now) {
|
|
397
|
+
frame = 0;
|
|
398
|
+
if (live !== self || !playing.size || reduced())
|
|
399
|
+
return;
|
|
400
|
+
const due = [];
|
|
401
|
+
playing.forEach((g) => {
|
|
402
|
+
const clip = g.clips[g.at];
|
|
403
|
+
const v = videoOf(clip);
|
|
404
|
+
let progress;
|
|
405
|
+
if (v)
|
|
406
|
+
progress = v.duration ? v.currentTime / v.duration : 0;
|
|
407
|
+
else
|
|
408
|
+
progress = (g.shown + now - g.since) / Math.max(1, pictureTime(g, clip));
|
|
409
|
+
g.bars[g.at]?.style.setProperty(PROGRESS, String(Math.min(1, progress)));
|
|
410
|
+
if (!v && progress >= 1)
|
|
411
|
+
due.push(g);
|
|
412
|
+
});
|
|
413
|
+
// The loop goes on first: next() asks for it too, and is
|
|
414
|
+
// answered by this one.
|
|
415
|
+
frame = requestAnimationFrame(step);
|
|
416
|
+
due.forEach(next);
|
|
417
|
+
});
|
|
418
|
+
};
|
|
419
|
+
const next = (g) => {
|
|
420
|
+
show(g, g.at + 1);
|
|
421
|
+
if (playing.has(g))
|
|
422
|
+
start(g);
|
|
423
|
+
};
|
|
424
|
+
// A video that ends: the next clip.
|
|
425
|
+
const onEnded = (event) => {
|
|
426
|
+
const clip = event.target.closest(`[${CLIP}]`);
|
|
427
|
+
const g = clip && groups.get(clip.closest(`[${GROUP}]`));
|
|
428
|
+
if (g && g.clips[g.at] === clip)
|
|
429
|
+
next(g);
|
|
430
|
+
};
|
|
431
|
+
// The strip's ends: data-pg-reels-start / -end on the section.
|
|
432
|
+
const ends = (root) => {
|
|
433
|
+
const strip = root.querySelector(`[${STRIP}]`);
|
|
434
|
+
if (!strip)
|
|
435
|
+
return;
|
|
436
|
+
root.toggleAttribute(START, strip.scrollLeft <= 1);
|
|
437
|
+
root.toggleAttribute(END, strip.scrollLeft + strip.clientWidth >= strip.scrollWidth - 1);
|
|
438
|
+
};
|
|
439
|
+
const onScroll = (event) => {
|
|
440
|
+
const strip = event.target;
|
|
441
|
+
if (strip instanceof HTMLElement && strip.hasAttribute(STRIP))
|
|
442
|
+
ends(strip.closest(`[${ROOT}]`));
|
|
443
|
+
};
|
|
444
|
+
// Look round the page: every group, its parts.
|
|
445
|
+
const look = () => {
|
|
446
|
+
if (live !== self)
|
|
447
|
+
return;
|
|
448
|
+
const now = new Set();
|
|
449
|
+
document.querySelectorAll(`[${ROOT}]`).forEach((root) => {
|
|
450
|
+
root.querySelectorAll(`[${GROUP}]`).forEach((el) => {
|
|
451
|
+
if (el.closest(`[${ROOT}]`) !== root)
|
|
452
|
+
return;
|
|
453
|
+
now.add(el);
|
|
454
|
+
if (groups.has(el))
|
|
455
|
+
return;
|
|
456
|
+
const g = partsOf(el, root);
|
|
457
|
+
groups.set(el, g);
|
|
458
|
+
show(g, g.at);
|
|
459
|
+
const card = el.querySelector(`[${CARD}]`) ?? el;
|
|
460
|
+
sights?.observe(card);
|
|
461
|
+
});
|
|
462
|
+
ends(root);
|
|
463
|
+
});
|
|
464
|
+
paintSeen();
|
|
465
|
+
groups.forEach((g, el) => {
|
|
466
|
+
if (now.has(el))
|
|
467
|
+
return;
|
|
468
|
+
if (playing.has(g)) {
|
|
469
|
+
stop(g);
|
|
470
|
+
playing.delete(g);
|
|
471
|
+
}
|
|
472
|
+
groups.delete(el);
|
|
473
|
+
});
|
|
474
|
+
applySound();
|
|
475
|
+
if (!sights) {
|
|
476
|
+
// No observer: the first plays.
|
|
477
|
+
const first = groups.values().next().value;
|
|
478
|
+
if (first && !playing.size) {
|
|
479
|
+
first.ratio = 1;
|
|
480
|
+
choose();
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
};
|
|
484
|
+
// Presses: a bar, the sound, the arrows, the card.
|
|
485
|
+
const onClick = (event) => {
|
|
486
|
+
if (live !== self || !(event.target instanceof Element))
|
|
487
|
+
return;
|
|
488
|
+
const t = event.target;
|
|
489
|
+
const bar = t.closest(`[${BAR}]`);
|
|
490
|
+
if (bar) {
|
|
491
|
+
const g = groups.get(bar.closest(`[${GROUP}]`));
|
|
492
|
+
if (!g)
|
|
493
|
+
return;
|
|
494
|
+
const was = playing.has(g);
|
|
495
|
+
show(g, g.bars.indexOf(bar));
|
|
496
|
+
if (was)
|
|
497
|
+
start(g);
|
|
498
|
+
return;
|
|
499
|
+
}
|
|
500
|
+
const soundButton = t.closest(`[${SOUND}]`);
|
|
501
|
+
if (soundButton) {
|
|
502
|
+
const g = groups.get(soundButton.closest(`[${GROUP}]`));
|
|
503
|
+
if (!g || view.open)
|
|
504
|
+
sound = !sound;
|
|
505
|
+
// The card with the sound: off. Another card's: the sound is
|
|
506
|
+
// that card's — it plays, the others are muted.
|
|
507
|
+
else if (g === owner && sound)
|
|
508
|
+
sound = false;
|
|
509
|
+
else {
|
|
510
|
+
if (!playing.has(g)) {
|
|
511
|
+
playing.add(g);
|
|
512
|
+
start(g);
|
|
513
|
+
}
|
|
514
|
+
sound = true;
|
|
515
|
+
owner = g;
|
|
516
|
+
}
|
|
517
|
+
applySound();
|
|
518
|
+
return;
|
|
519
|
+
}
|
|
520
|
+
const arrow = t.closest(`[${PREV}], [${NEXT}]`);
|
|
521
|
+
if (arrow) {
|
|
522
|
+
const id = arrow.getAttribute("aria-controls");
|
|
523
|
+
const strip = (id ? document.getElementById(id) : null) ?? arrow.closest(`[${ROOT}]`)?.querySelector(`[${STRIP}]`);
|
|
524
|
+
const first = strip?.querySelector(`[${GROUP}]`);
|
|
525
|
+
if (!strip || !first)
|
|
526
|
+
return;
|
|
527
|
+
const gap = px(getComputedStyle(strip).columnGap);
|
|
528
|
+
const by = first.getBoundingClientRect().width + gap;
|
|
529
|
+
strip.scrollBy({ left: arrow.hasAttribute(NEXT) ? by : -by, behavior: reduced() ? "instant" : "smooth" });
|
|
530
|
+
return;
|
|
531
|
+
}
|
|
532
|
+
// The card — not a link or a button of its own — or its open
|
|
533
|
+
// button: the full view's to show.
|
|
534
|
+
const openButton = t.closest(`[${OPEN}]`);
|
|
535
|
+
const card = t.closest(`[${CARD}]`);
|
|
536
|
+
if (!openButton && (!card || t.closest("a, button, input, select, textarea")))
|
|
537
|
+
return;
|
|
538
|
+
const groupEl = (openButton ?? card).closest(`[${GROUP}]`);
|
|
539
|
+
const g = groupEl && groups.get(groupEl);
|
|
540
|
+
if (!g)
|
|
541
|
+
return;
|
|
542
|
+
const asked = new CustomEvent("pg-reels:open", { bubbles: true, cancelable: true, detail: { group: g.el, clip: g.at } });
|
|
543
|
+
if (g.root.dispatchEvent(asked))
|
|
544
|
+
openView(g, g.at);
|
|
545
|
+
};
|
|
546
|
+
/* ---------- The full view ---------- */
|
|
547
|
+
const dialog = document.createElement("dialog");
|
|
548
|
+
dialog.setAttribute(VIEWER, "");
|
|
549
|
+
dialog.setAttribute("aria-label", "Reels");
|
|
550
|
+
dialog.setAttribute("data-lenis-prevent", "");
|
|
551
|
+
// Opened, the focus is the view's own — not its first button's: the
|
|
552
|
+
// keys work at once, no ring on a button nobody chose.
|
|
553
|
+
dialog.tabIndex = -1;
|
|
554
|
+
dialog.innerHTML = `
|
|
555
|
+
<div data-pg-reels-stage>
|
|
556
|
+
<div data-pg-reels-feed>
|
|
557
|
+
<div data-pg-reels-view-peek="before" aria-hidden="true" hidden></div>
|
|
558
|
+
<div data-pg-reels-screen>
|
|
559
|
+
<div data-pg-reels-view-media></div>
|
|
560
|
+
<button type="button" data-pg-reels-view-back tabindex="-1" aria-label="Previous clip"></button>
|
|
561
|
+
<button type="button" data-pg-reels-view-on tabindex="-1" aria-label="Next clip"></button>
|
|
562
|
+
<div data-pg-reels-view-top>
|
|
563
|
+
<div data-pg-reels-view-bars></div>
|
|
564
|
+
<div data-pg-reels-view-row>
|
|
565
|
+
<div data-pg-reels-view-head></div>
|
|
566
|
+
<div data-pg-reels-view-tools>
|
|
567
|
+
<div data-pg-reels-view-views></div>
|
|
568
|
+
<button type="button" data-pg-reels-view-close aria-label="Close">${svg(ICONS.close)}</button>
|
|
569
|
+
<button type="button" ${SOUND} aria-pressed="false" aria-label="Sound">${svg(ICONS.soundOff)}${svg(ICONS.soundOn)}</button>
|
|
570
|
+
<button type="button" data-pg-reels-view-play aria-label="Pause">${svg(ICONS.pause)}${svg(ICONS.play)}</button>
|
|
571
|
+
</div>
|
|
572
|
+
</div>
|
|
573
|
+
</div>
|
|
574
|
+
</div>
|
|
575
|
+
<button type="button" data-pg-reels-view-peek="after" tabindex="-1" aria-label="Next reel" hidden></button>
|
|
576
|
+
</div>
|
|
577
|
+
<div data-pg-reels-view-steps>
|
|
578
|
+
<button type="button" data-pg-reels-view-up aria-label="Previous reel">${svg(ICONS.up)}</button>
|
|
579
|
+
<button type="button" data-pg-reels-view-down aria-label="Next reel">${svg(ICONS.down)}</button>
|
|
580
|
+
</div>
|
|
581
|
+
<aside data-pg-reels-panel>
|
|
582
|
+
<div data-pg-reels-panel-head>
|
|
583
|
+
<div data-pg-reels-panel-title></div>
|
|
584
|
+
<button type="button" data-pg-reels-panel-close aria-label="Close">${svg(ICONS.close)}</button>
|
|
585
|
+
</div>
|
|
586
|
+
<div data-pg-reels-panel-band>
|
|
587
|
+
<div data-pg-reels-panel-summary></div>
|
|
588
|
+
<button type="button" data-pg-reels-panel-toggle aria-expanded="false">Details ${svg(ICONS.chevron)}</button>
|
|
589
|
+
</div>
|
|
590
|
+
<div data-pg-reels-panel-detail></div>
|
|
591
|
+
<div data-pg-reels-panel-foot></div>
|
|
592
|
+
</aside>
|
|
593
|
+
</div>`;
|
|
594
|
+
const part = (name) => dialog.querySelector(`[data-pg-reels-${name}]`);
|
|
595
|
+
const media = part("view-media");
|
|
596
|
+
const feed = part("feed");
|
|
597
|
+
const screen = part("screen");
|
|
598
|
+
const before = dialog.querySelector('[data-pg-reels-view-peek="before"]');
|
|
599
|
+
const after = dialog.querySelector('[data-pg-reels-view-peek="after"]');
|
|
600
|
+
const viewBars = part("view-bars");
|
|
601
|
+
const playButton = part("view-play");
|
|
602
|
+
const upButton = part("view-up");
|
|
603
|
+
const downButton = part("view-down");
|
|
604
|
+
const toggle = part("panel-toggle");
|
|
605
|
+
document.body.append(dialog);
|
|
606
|
+
// The band at a phone's foot, its height for the site:
|
|
607
|
+
// --pg-reels-band on the view, 0 where the product is beside it.
|
|
608
|
+
const band = part("panel-band");
|
|
609
|
+
const panel = part("panel");
|
|
610
|
+
const bandSize = new ResizeObserver(() => {
|
|
611
|
+
const cs = getComputedStyle(panel);
|
|
612
|
+
const h = band.offsetHeight ? band.offsetHeight + px(cs.paddingTop) + px(cs.paddingBottom) : 0;
|
|
613
|
+
dialog.style.setProperty("--pg-reels-band", `${h}px`);
|
|
614
|
+
});
|
|
615
|
+
bandSize.observe(band);
|
|
616
|
+
const view = {
|
|
617
|
+
open: false,
|
|
618
|
+
list: [],
|
|
619
|
+
gi: 0,
|
|
620
|
+
ci: 0,
|
|
621
|
+
paused: false,
|
|
622
|
+
// Held down, dragged, sliding: standing still for now.
|
|
623
|
+
held: false,
|
|
624
|
+
shown: 0,
|
|
625
|
+
since: 0,
|
|
626
|
+
pushed: false,
|
|
627
|
+
frame: 0,
|
|
628
|
+
};
|
|
629
|
+
const viewVideo = () => media.querySelector("video");
|
|
630
|
+
const current = () => view.list[view.gi];
|
|
631
|
+
let headOf = null;
|
|
632
|
+
// A group's first clip, for the video above or under the one shown:
|
|
633
|
+
// a picture, or a video on its poster — never playing.
|
|
634
|
+
const peekOf = (holder, g) => {
|
|
635
|
+
holder.replaceChildren(...(g ? [copyOf(g.clips[0])] : []));
|
|
636
|
+
holder.hidden = !g;
|
|
637
|
+
holder.querySelectorAll("video").forEach((x) => {
|
|
638
|
+
x.muted = true;
|
|
639
|
+
x.preload = "none";
|
|
640
|
+
x.removeAttribute("autoplay");
|
|
641
|
+
});
|
|
642
|
+
};
|
|
643
|
+
// Show the view's clip, with its head, its bars, its product — and the
|
|
644
|
+
// groups above and under it, ready to slide in.
|
|
645
|
+
const render = () => {
|
|
646
|
+
const g = current();
|
|
647
|
+
const n = g.clips.length;
|
|
648
|
+
const clip = g.clips[view.ci];
|
|
649
|
+
viewVideo()?.pause();
|
|
650
|
+
const head = g.el.querySelector(`[${HEAD}]`);
|
|
651
|
+
part("view-head").replaceChildren(...(head ? [copyOf(head)] : []));
|
|
652
|
+
// The group's view count — the site's, written in its card — left
|
|
653
|
+
// of the close.
|
|
654
|
+
const views = g.el.querySelector(`[${VIEWS}]`);
|
|
655
|
+
part("view-views").replaceChildren(...(views ? [copyOf(views)] : []));
|
|
656
|
+
// The head as it was before this group was looked at — its ring
|
|
657
|
+
// bright the first time, through all its clips — and from now on,
|
|
658
|
+
// seen.
|
|
659
|
+
if (headOf !== g) {
|
|
660
|
+
headOf = g;
|
|
661
|
+
part("view-head").setAttribute(SEEN, String(isSeen(g.el)));
|
|
662
|
+
}
|
|
663
|
+
markSeen(g.el);
|
|
664
|
+
viewBars.replaceChildren(...g.clips.map((_, i) => {
|
|
665
|
+
const bar = document.createElement("button");
|
|
666
|
+
bar.type = "button";
|
|
667
|
+
bar.setAttribute(VIEW_BAR, "");
|
|
668
|
+
bar.setAttribute("aria-label", `Clip ${i + 1} of ${n}`);
|
|
669
|
+
bar.append(document.createElement("span"));
|
|
670
|
+
bar.toggleAttribute(DONE, i < view.ci);
|
|
671
|
+
if (i === view.ci)
|
|
672
|
+
bar.setAttribute("aria-current", "true");
|
|
673
|
+
bar.style.setProperty(PROGRESS, i < view.ci ? "1" : "0");
|
|
674
|
+
return bar;
|
|
675
|
+
}));
|
|
676
|
+
viewBars.hidden = n < 2;
|
|
677
|
+
const copy = copyOf(clip);
|
|
678
|
+
const v = copy.querySelector("video");
|
|
679
|
+
if (v) {
|
|
680
|
+
v.muted = !sound;
|
|
681
|
+
v.preload = "auto";
|
|
682
|
+
v.loop = false;
|
|
683
|
+
}
|
|
684
|
+
media.replaceChildren(copy);
|
|
685
|
+
peekOf(before, view.list[view.gi - 1]);
|
|
686
|
+
peekOf(after, view.list[view.gi + 1]);
|
|
687
|
+
const product = g.products[view.ci];
|
|
688
|
+
part("panel-summary").replaceChildren(...(product ? [copyOf(product)] : []));
|
|
689
|
+
// The details: in the product, or in the clip — a <template>, or
|
|
690
|
+
// any element kept hidden (React: a <template>'s markup is not
|
|
691
|
+
// hydrated, and a link cannot sit inside the product's link but
|
|
692
|
+
// in a template). A template parsed from HTML keeps its markup in
|
|
693
|
+
// .content; one rendered by script holds it as children. Its
|
|
694
|
+
// data-pg-reels-title goes to the panel's head, beside the close.
|
|
695
|
+
const detail = product?.querySelector(`[${DETAIL}]`) ?? clip.querySelector(`[${DETAIL}]`);
|
|
696
|
+
const from = detail instanceof HTMLTemplateElement && detail.content.childNodes.length ? detail.content : detail;
|
|
697
|
+
const holder = document.createElement("div");
|
|
698
|
+
holder.append(...(from ? [...from.childNodes].map((x) => x.cloneNode(true)) : []));
|
|
699
|
+
const title = holder.querySelector(`[${TITLE}]`);
|
|
700
|
+
part("panel-title").replaceChildren(...(title ? [title] : []));
|
|
701
|
+
// Its data-pg-reels-foot — the link to the product — to the
|
|
702
|
+
// panel's foot, always in sight under what scrolls.
|
|
703
|
+
const foot = holder.querySelector(`[${FOOT}]`);
|
|
704
|
+
part("panel-foot").replaceChildren(...(foot ? [foot] : []));
|
|
705
|
+
part("panel-detail").replaceChildren(...holder.childNodes);
|
|
706
|
+
part("panel-detail").scrollTop = 0;
|
|
707
|
+
// Its variants: the one marked current, or the first, chosen.
|
|
708
|
+
const variants = panel.querySelectorAll(`[${VARIANT}]`);
|
|
709
|
+
if (variants.length)
|
|
710
|
+
chooseVariant([...variants].find((x) => x.hasAttribute(CURRENT)) ?? variants[0]);
|
|
711
|
+
dialog.toggleAttribute(NO_PRODUCT, !product);
|
|
712
|
+
upButton.disabled = view.gi === 0;
|
|
713
|
+
downButton.disabled = view.gi === view.list.length - 1;
|
|
714
|
+
view.shown = 0;
|
|
715
|
+
view.since = performance.now();
|
|
716
|
+
playView();
|
|
717
|
+
};
|
|
718
|
+
const playView = () => {
|
|
719
|
+
const v = viewVideo();
|
|
720
|
+
const still = view.paused || view.held;
|
|
721
|
+
dialog.toggleAttribute(PAUSED, view.paused);
|
|
722
|
+
playButton.setAttribute("aria-label", view.paused ? "Play" : "Pause");
|
|
723
|
+
if (still) {
|
|
724
|
+
v?.pause();
|
|
725
|
+
return;
|
|
726
|
+
}
|
|
727
|
+
view.since = performance.now();
|
|
728
|
+
v?.play().catch(() => { });
|
|
729
|
+
tickView();
|
|
730
|
+
};
|
|
731
|
+
// Still for now — held, dragged, sliding — its time kept.
|
|
732
|
+
const holdView = () => {
|
|
733
|
+
if (view.held)
|
|
734
|
+
return;
|
|
735
|
+
if (!view.paused)
|
|
736
|
+
view.shown += performance.now() - view.since;
|
|
737
|
+
view.held = true;
|
|
738
|
+
viewVideo()?.pause();
|
|
739
|
+
};
|
|
740
|
+
const letGo = () => {
|
|
741
|
+
view.held = false;
|
|
742
|
+
playView();
|
|
743
|
+
};
|
|
744
|
+
const tickView = () => {
|
|
745
|
+
if (view.frame)
|
|
746
|
+
return;
|
|
747
|
+
view.frame = requestAnimationFrame(function step(now) {
|
|
748
|
+
view.frame = 0;
|
|
749
|
+
if (!view.open || view.paused || view.held)
|
|
750
|
+
return;
|
|
751
|
+
const g = current();
|
|
752
|
+
const clip = g.clips[view.ci];
|
|
753
|
+
const v = viewVideo();
|
|
754
|
+
const progress = v ? (v.duration ? v.currentTime / v.duration : 0) : (view.shown + now - view.since) / Math.max(1, pictureTime(g, clip));
|
|
755
|
+
viewBars.children[view.ci]?.setAttribute("style", `${PROGRESS}: ${Math.min(1, progress)}`);
|
|
756
|
+
if (!v && progress >= 1) {
|
|
757
|
+
nextClip(true);
|
|
758
|
+
return;
|
|
759
|
+
}
|
|
760
|
+
view.frame = requestAnimationFrame(step);
|
|
761
|
+
});
|
|
762
|
+
};
|
|
763
|
+
/* Between groups: the video and the one above or under it move
|
|
764
|
+
together — after a finger, or by themselves — by a video and its
|
|
765
|
+
gap; landed, the one that came in is the one shown, its product in
|
|
766
|
+
the panel. */
|
|
767
|
+
let sliding = false;
|
|
768
|
+
const shift = () => screen.offsetHeight + px(getComputedStyle(feed).rowGap);
|
|
769
|
+
const setY = (y) => {
|
|
770
|
+
feed.style.transform = y ? `translateY(${y}px)` : "";
|
|
771
|
+
};
|
|
772
|
+
const can = (dir) => view.gi + dir >= 0 && view.gi + dir < view.list.length;
|
|
773
|
+
const EASE = "cubic-bezier(0.2, 0, 0, 1)";
|
|
774
|
+
// `last`: land on the group's last clip — going back from a first clip.
|
|
775
|
+
const slide = (dir, from = 0, last = false) => {
|
|
776
|
+
if (sliding)
|
|
777
|
+
return;
|
|
778
|
+
const land = () => {
|
|
779
|
+
view.gi += dir;
|
|
780
|
+
view.ci = last ? current().clips.length - 1 : 0;
|
|
781
|
+
view.held = false;
|
|
782
|
+
dialog.removeAttribute(SLIDING);
|
|
783
|
+
render();
|
|
784
|
+
};
|
|
785
|
+
if (!can(dir))
|
|
786
|
+
return settle(from);
|
|
787
|
+
if (reduced()) {
|
|
788
|
+
setY(0);
|
|
789
|
+
return land();
|
|
790
|
+
}
|
|
791
|
+
sliding = true;
|
|
792
|
+
holdView();
|
|
793
|
+
dialog.setAttribute(SLIDING, "");
|
|
794
|
+
const by = shift();
|
|
795
|
+
const to = -dir * by;
|
|
796
|
+
setY(0);
|
|
797
|
+
const run = feed.animate({ transform: [`translateY(${from}px)`, `translateY(${to}px)`] }, { duration: Math.max(140, (340 * Math.abs(to - from)) / by), easing: EASE, fill: "forwards" });
|
|
798
|
+
run.onfinish = () => {
|
|
799
|
+
land();
|
|
800
|
+
run.cancel();
|
|
801
|
+
sliding = false;
|
|
802
|
+
};
|
|
803
|
+
};
|
|
804
|
+
// Not far enough: back where it was.
|
|
805
|
+
const settle = (from) => {
|
|
806
|
+
const done = () => {
|
|
807
|
+
dialog.removeAttribute(SLIDING);
|
|
808
|
+
letGo();
|
|
809
|
+
};
|
|
810
|
+
setY(0);
|
|
811
|
+
if (!from || reduced())
|
|
812
|
+
return done();
|
|
813
|
+
sliding = true;
|
|
814
|
+
const run = feed.animate({ transform: [`translateY(${from}px)`, "none"] }, { duration: 220, easing: EASE });
|
|
815
|
+
run.onfinish = () => {
|
|
816
|
+
sliding = false;
|
|
817
|
+
done();
|
|
818
|
+
};
|
|
819
|
+
};
|
|
820
|
+
// Within a group, and on to the next — after the last group, the first.
|
|
821
|
+
const nextClip = (auto = false) => {
|
|
822
|
+
if (view.ci < current().clips.length - 1) {
|
|
823
|
+
view.ci += 1;
|
|
824
|
+
render();
|
|
825
|
+
}
|
|
826
|
+
else if (can(1))
|
|
827
|
+
slide(1);
|
|
828
|
+
else if (auto && view.list.length > 1) {
|
|
829
|
+
view.gi = 0;
|
|
830
|
+
view.ci = 0;
|
|
831
|
+
render();
|
|
832
|
+
}
|
|
833
|
+
else {
|
|
834
|
+
view.ci = 0;
|
|
835
|
+
render();
|
|
836
|
+
}
|
|
837
|
+
};
|
|
838
|
+
const prevClip = () => {
|
|
839
|
+
if (view.ci > 0) {
|
|
840
|
+
view.ci -= 1;
|
|
841
|
+
render();
|
|
842
|
+
}
|
|
843
|
+
else if (can(-1))
|
|
844
|
+
slide(-1, 0, true);
|
|
845
|
+
else
|
|
846
|
+
render();
|
|
847
|
+
};
|
|
848
|
+
const nextGroup = () => slide(1);
|
|
849
|
+
const prevGroup = () => slide(-1);
|
|
850
|
+
const openView = (g, clip) => {
|
|
851
|
+
// Opened from a card the sound was not on: none.
|
|
852
|
+
if (g !== owner)
|
|
853
|
+
sound = false;
|
|
854
|
+
stopAll();
|
|
855
|
+
// Every group of its section, in the page's order.
|
|
856
|
+
headOf = null;
|
|
857
|
+
view.list = [...groups.values()].filter((x) => x.root === g.root).sort((a, b) => (a.el.compareDocumentPosition(b.el) & Node.DOCUMENT_POSITION_FOLLOWING ? -1 : 1));
|
|
858
|
+
view.gi = view.list.indexOf(g);
|
|
859
|
+
view.ci = clip;
|
|
860
|
+
view.open = true;
|
|
861
|
+
// Reduced motion: it waits for its play button.
|
|
862
|
+
view.paused = reduced();
|
|
863
|
+
view.held = false;
|
|
864
|
+
dialog.removeAttribute(DETAILS);
|
|
865
|
+
dialog.removeAttribute(SLIDING);
|
|
866
|
+
setY(0);
|
|
867
|
+
toggle.setAttribute("aria-expanded", "false");
|
|
868
|
+
if (!dialog.open)
|
|
869
|
+
dialog.showModal();
|
|
870
|
+
dialog.focus({ preventScroll: true });
|
|
871
|
+
try {
|
|
872
|
+
history.pushState({ ...(history.state ?? {}), pgReels: true }, "");
|
|
873
|
+
view.pushed = true;
|
|
874
|
+
}
|
|
875
|
+
catch {
|
|
876
|
+
view.pushed = false;
|
|
877
|
+
}
|
|
878
|
+
render();
|
|
879
|
+
applySound();
|
|
880
|
+
};
|
|
881
|
+
// Closed: the card it came from shows the clip last seen; the strip
|
|
882
|
+
// plays again. A Back step of its own is taken back — unless Back
|
|
883
|
+
// closed it.
|
|
884
|
+
const closeView = (byBack = false) => {
|
|
885
|
+
if (!view.open)
|
|
886
|
+
return;
|
|
887
|
+
view.open = false;
|
|
888
|
+
viewVideo()?.pause();
|
|
889
|
+
if (view.frame)
|
|
890
|
+
cancelAnimationFrame(view.frame);
|
|
891
|
+
view.frame = 0;
|
|
892
|
+
feed.getAnimations().forEach((a) => a.cancel());
|
|
893
|
+
sliding = false;
|
|
894
|
+
press = null;
|
|
895
|
+
const g = current();
|
|
896
|
+
if (g)
|
|
897
|
+
show(g, view.ci);
|
|
898
|
+
// Its sound goes on with the card last seen, if that one plays.
|
|
899
|
+
owner = g ?? null;
|
|
900
|
+
if (dialog.open)
|
|
901
|
+
dialog.close();
|
|
902
|
+
media.replaceChildren();
|
|
903
|
+
if (view.pushed && !byBack)
|
|
904
|
+
history.back();
|
|
905
|
+
view.pushed = false;
|
|
906
|
+
choose();
|
|
907
|
+
applySound();
|
|
908
|
+
};
|
|
909
|
+
const onViewClick = (event) => {
|
|
910
|
+
if (!(event.target instanceof Element))
|
|
911
|
+
return;
|
|
912
|
+
const t = event.target;
|
|
913
|
+
if (swallow) {
|
|
914
|
+
swallow = false;
|
|
915
|
+
event.preventDefault();
|
|
916
|
+
return;
|
|
917
|
+
}
|
|
918
|
+
// The dark around it.
|
|
919
|
+
if (t === dialog || t.hasAttribute("data-pg-reels-stage"))
|
|
920
|
+
return closeView();
|
|
921
|
+
if (t.closest("[data-pg-reels-view-close], [data-pg-reels-panel-close]"))
|
|
922
|
+
return closeView();
|
|
923
|
+
if (sliding)
|
|
924
|
+
return;
|
|
925
|
+
if (t.closest("[data-pg-reels-view-on]"))
|
|
926
|
+
return nextClip();
|
|
927
|
+
if (t.closest("[data-pg-reels-view-back]"))
|
|
928
|
+
return prevClip();
|
|
929
|
+
if (t.closest("[data-pg-reels-view-up]"))
|
|
930
|
+
return prevGroup();
|
|
931
|
+
if (t.closest('[data-pg-reels-view-down], [data-pg-reels-view-peek="after"]'))
|
|
932
|
+
return nextGroup();
|
|
933
|
+
const bar = t.closest(`[${VIEW_BAR}]`);
|
|
934
|
+
if (bar) {
|
|
935
|
+
view.ci = [...viewBars.children].indexOf(bar);
|
|
936
|
+
return render();
|
|
937
|
+
}
|
|
938
|
+
if (t.closest("[data-pg-reels-view-play]"))
|
|
939
|
+
return togglePause();
|
|
940
|
+
if (t.closest("[data-pg-reels-panel-toggle]")) {
|
|
941
|
+
const open = !dialog.hasAttribute(DETAILS);
|
|
942
|
+
dialog.toggleAttribute(DETAILS, open);
|
|
943
|
+
toggle.setAttribute("aria-expanded", String(open));
|
|
944
|
+
return;
|
|
945
|
+
}
|
|
946
|
+
// A variant pressed: its price, its pictures, its link.
|
|
947
|
+
const variant = t.closest(`[${VARIANT}]`);
|
|
948
|
+
if (variant && panel.contains(variant))
|
|
949
|
+
return chooseVariant(variant);
|
|
950
|
+
// A small picture of the product, pressed: shown big — in its own
|
|
951
|
+
// gallery, when each variant has one — and brought into sight,
|
|
952
|
+
// when the row of them scrolls.
|
|
953
|
+
const thumb = t.closest(`[${THUMB}]`);
|
|
954
|
+
if (thumb) {
|
|
955
|
+
const detail = thumb.closest(`[${GALLERY}]`) ?? thumb.closest("[data-pg-reels-panel-detail]");
|
|
956
|
+
const big = detail?.querySelector(`[${BIG}] img, img[${BIG}]`);
|
|
957
|
+
const src = thumb.getAttribute(THUMB) || thumb.querySelector("img")?.src;
|
|
958
|
+
if (big && src)
|
|
959
|
+
big.src = src;
|
|
960
|
+
detail?.querySelectorAll(`[${THUMB}]`).forEach((x) => x.toggleAttribute(CURRENT, x === thumb));
|
|
961
|
+
thumb.scrollIntoView({ block: "nearest", inline: "nearest", behavior: reduced() ? "instant" : "smooth" });
|
|
962
|
+
}
|
|
963
|
+
};
|
|
964
|
+
/* A variant chosen — 1 kg, 500 g; a colour — in the panel: it is
|
|
965
|
+
data-pg-reels-current (aria-pressed too); the gallery of the same
|
|
966
|
+
name shows, the others hide; data-pg-reels-price shows its price;
|
|
967
|
+
the foot's link goes to its page. */
|
|
968
|
+
const chooseVariant = (variant) => {
|
|
969
|
+
const name = variant.getAttribute(VARIANT);
|
|
970
|
+
panel.querySelectorAll(`[${VARIANT}]`).forEach((x) => {
|
|
971
|
+
x.toggleAttribute(CURRENT, x === variant);
|
|
972
|
+
if (x.tagName === "BUTTON")
|
|
973
|
+
x.setAttribute("aria-pressed", String(x === variant));
|
|
974
|
+
});
|
|
975
|
+
const galleries = panel.querySelectorAll(`[${GALLERY}]`);
|
|
976
|
+
if ([...galleries].some((x) => x.getAttribute(GALLERY) === name))
|
|
977
|
+
galleries.forEach((x) => x.toggleAttribute(CURRENT, x.getAttribute(GALLERY) === name));
|
|
978
|
+
const price = variant.getAttribute(VARIANT_PRICE);
|
|
979
|
+
if (price !== null)
|
|
980
|
+
panel.querySelectorAll(`[${PRICE}]`).forEach((x) => (x.textContent = price));
|
|
981
|
+
const href = variant.getAttribute(VARIANT_HREF);
|
|
982
|
+
if (href)
|
|
983
|
+
panel.querySelectorAll(`a[${FOOT}]`).forEach((x) => (x.href = href));
|
|
984
|
+
};
|
|
985
|
+
const togglePause = () => {
|
|
986
|
+
if (!view.paused && !view.held)
|
|
987
|
+
view.shown += performance.now() - view.since;
|
|
988
|
+
view.paused = !view.paused;
|
|
989
|
+
playView();
|
|
990
|
+
};
|
|
991
|
+
dialog.addEventListener("click", onViewClick);
|
|
992
|
+
dialog.addEventListener("cancel", (event) => {
|
|
993
|
+
event.preventDefault();
|
|
994
|
+
closeView();
|
|
995
|
+
});
|
|
996
|
+
// A video that ends in the view: the next clip.
|
|
997
|
+
media.addEventListener("ended", () => nextClip(true), true);
|
|
998
|
+
/* A press on the video. Held: still, the controls out of the way.
|
|
999
|
+
Dragged up or down — at once, or after holding it: the video follows the finger — the next group
|
|
1000
|
+
coming in under it, or the one before above it — and let go, goes
|
|
1001
|
+
on to it if pulled far enough or flicked, or back if not. A press
|
|
1002
|
+
that was either is no tap. */
|
|
1003
|
+
let press = null;
|
|
1004
|
+
let swallow = false;
|
|
1005
|
+
const noTap = () => {
|
|
1006
|
+
swallow = true;
|
|
1007
|
+
setTimeout(() => (swallow = false));
|
|
1008
|
+
};
|
|
1009
|
+
// How far the video moves for a finger's `dy`: past the first or the
|
|
1010
|
+
// last group, a quarter — it gives, and comes back.
|
|
1011
|
+
const pulled = (dy) => (can(dy < 0 ? 1 : -1) ? dy : dy / 4);
|
|
1012
|
+
screen.addEventListener("pointerdown", (event) => {
|
|
1013
|
+
if (event.button !== 0 || sliding || event.target.closest("button:not([data-pg-reels-view-on]):not([data-pg-reels-view-back]), a"))
|
|
1014
|
+
return;
|
|
1015
|
+
const timer = window.setTimeout(() => {
|
|
1016
|
+
if (!press || press.drag)
|
|
1017
|
+
return;
|
|
1018
|
+
dialog.setAttribute(HELD, "");
|
|
1019
|
+
holdView();
|
|
1020
|
+
}, HOLD_MS);
|
|
1021
|
+
press = { id: event.pointerId, x: event.clientX, y: event.clientY, timer, drag: false, lastY: event.clientY, lastT: event.timeStamp, speed: 0 };
|
|
1022
|
+
});
|
|
1023
|
+
screen.addEventListener("pointermove", (event) => {
|
|
1024
|
+
if (!press || press.id !== event.pointerId)
|
|
1025
|
+
return;
|
|
1026
|
+
const dx = event.clientX - press.x;
|
|
1027
|
+
const dy = event.clientY - press.y;
|
|
1028
|
+
if (!press.drag) {
|
|
1029
|
+
if (Math.hypot(dx, dy) > 10)
|
|
1030
|
+
clearTimeout(press.timer);
|
|
1031
|
+
if (Math.abs(dy) < DRAG_PX || Math.abs(dy) < Math.abs(dx))
|
|
1032
|
+
return;
|
|
1033
|
+
// Held first, then dragged: a drag all the same.
|
|
1034
|
+
press.drag = true;
|
|
1035
|
+
dialog.removeAttribute(HELD);
|
|
1036
|
+
screen.setPointerCapture(event.pointerId);
|
|
1037
|
+
holdView();
|
|
1038
|
+
dialog.setAttribute(SLIDING, "");
|
|
1039
|
+
}
|
|
1040
|
+
// The finger's speed, px a ms, smoothed.
|
|
1041
|
+
const dt = Math.max(1, event.timeStamp - press.lastT);
|
|
1042
|
+
press.speed = press.speed * 0.6 + ((event.clientY - press.lastY) / dt) * 0.4;
|
|
1043
|
+
press.lastY = event.clientY;
|
|
1044
|
+
press.lastT = event.timeStamp;
|
|
1045
|
+
setY(pulled(dy));
|
|
1046
|
+
});
|
|
1047
|
+
const release = (event) => {
|
|
1048
|
+
if (!press || press.id !== event.pointerId)
|
|
1049
|
+
return;
|
|
1050
|
+
const p = press;
|
|
1051
|
+
press = null;
|
|
1052
|
+
clearTimeout(p.timer);
|
|
1053
|
+
if (p.drag) {
|
|
1054
|
+
noTap();
|
|
1055
|
+
const dy = event.clientY - p.y;
|
|
1056
|
+
const dir = dy < 0 ? 1 : -1;
|
|
1057
|
+
const far = Math.abs(dy) > shift() * 0.2;
|
|
1058
|
+
const flick = Math.abs(p.speed) > FLICK && Math.sign(p.speed) === Math.sign(dy);
|
|
1059
|
+
if (event.type === "pointerup" && can(dir) && (far || flick))
|
|
1060
|
+
slide(dir, pulled(dy));
|
|
1061
|
+
else
|
|
1062
|
+
settle(pulled(dy));
|
|
1063
|
+
return;
|
|
1064
|
+
}
|
|
1065
|
+
if (dialog.hasAttribute(HELD)) {
|
|
1066
|
+
dialog.removeAttribute(HELD);
|
|
1067
|
+
noTap();
|
|
1068
|
+
letGo();
|
|
1069
|
+
}
|
|
1070
|
+
};
|
|
1071
|
+
screen.addEventListener("pointerup", release);
|
|
1072
|
+
screen.addEventListener("pointercancel", release);
|
|
1073
|
+
// A wheel, a trackpad: a step per turn, then a rest.
|
|
1074
|
+
let wheelSum = 0;
|
|
1075
|
+
let wheelRest = 0;
|
|
1076
|
+
dialog.addEventListener("wheel", (event) => {
|
|
1077
|
+
if (event.target.closest("[data-pg-reels-panel]"))
|
|
1078
|
+
return;
|
|
1079
|
+
event.preventDefault();
|
|
1080
|
+
if (event.timeStamp < wheelRest || sliding || press)
|
|
1081
|
+
return;
|
|
1082
|
+
wheelSum += event.deltaY;
|
|
1083
|
+
if (Math.abs(wheelSum) < WHEEL_PX)
|
|
1084
|
+
return;
|
|
1085
|
+
if (wheelSum > 0)
|
|
1086
|
+
nextGroup();
|
|
1087
|
+
else
|
|
1088
|
+
prevGroup();
|
|
1089
|
+
wheelSum = 0;
|
|
1090
|
+
wheelRest = event.timeStamp + WHEEL_REST_MS;
|
|
1091
|
+
}, { passive: false });
|
|
1092
|
+
dialog.addEventListener("keydown", (event) => {
|
|
1093
|
+
if (!view.open || event.altKey || event.ctrlKey || event.metaKey)
|
|
1094
|
+
return;
|
|
1095
|
+
const onControl = event.target.closest("button, a, input, textarea, select");
|
|
1096
|
+
if (sliding && event.key.startsWith("Arrow"))
|
|
1097
|
+
return event.preventDefault();
|
|
1098
|
+
if (event.key === "ArrowDown" || event.key === "PageDown")
|
|
1099
|
+
nextGroup();
|
|
1100
|
+
else if (event.key === "ArrowUp" || event.key === "PageUp")
|
|
1101
|
+
prevGroup();
|
|
1102
|
+
else if (event.key === "ArrowRight")
|
|
1103
|
+
nextClip();
|
|
1104
|
+
else if (event.key === "ArrowLeft")
|
|
1105
|
+
prevClip();
|
|
1106
|
+
else if ((event.key === " " || event.key === "k") && !onControl)
|
|
1107
|
+
togglePause();
|
|
1108
|
+
else
|
|
1109
|
+
return;
|
|
1110
|
+
event.preventDefault();
|
|
1111
|
+
});
|
|
1112
|
+
// Back closes it.
|
|
1113
|
+
const onPop = () => {
|
|
1114
|
+
if (view.open) {
|
|
1115
|
+
view.pushed = false;
|
|
1116
|
+
closeView(true);
|
|
1117
|
+
}
|
|
1118
|
+
};
|
|
1119
|
+
addEventListener("popstate", onPop);
|
|
1120
|
+
// Rendered later — React, a list that arrives — or taken away.
|
|
1121
|
+
const mo = new MutationObserver((records) => {
|
|
1122
|
+
if (records.some((r) => [...r.addedNodes, ...r.removedNodes].some((n) => n instanceof Element && (n.matches(`[${ROOT}], [${GROUP}]`) || !!n.querySelector(`[${GROUP}]`)))))
|
|
1123
|
+
look();
|
|
1124
|
+
});
|
|
1125
|
+
mo.observe(document.body, { childList: true, subtree: true });
|
|
1126
|
+
document.addEventListener("click", onClick);
|
|
1127
|
+
document.addEventListener("ended", onEnded, true);
|
|
1128
|
+
document.addEventListener("scroll", onScroll, true);
|
|
1129
|
+
const onResize = () => document.querySelectorAll(`[${ROOT}]`).forEach(ends);
|
|
1130
|
+
addEventListener("resize", onResize);
|
|
1131
|
+
const onHidden = () => {
|
|
1132
|
+
if (view.open) {
|
|
1133
|
+
if (document.hidden)
|
|
1134
|
+
holdView();
|
|
1135
|
+
else if (!press && !sliding)
|
|
1136
|
+
letGo();
|
|
1137
|
+
return;
|
|
1138
|
+
}
|
|
1139
|
+
playing.forEach(document.hidden ? stop : start);
|
|
1140
|
+
};
|
|
1141
|
+
document.addEventListener("visibilitychange", onHidden);
|
|
1142
|
+
look();
|
|
1143
|
+
return () => {
|
|
1144
|
+
mo.disconnect();
|
|
1145
|
+
sights?.disconnect();
|
|
1146
|
+
document.removeEventListener("click", onClick);
|
|
1147
|
+
document.removeEventListener("ended", onEnded, true);
|
|
1148
|
+
document.removeEventListener("scroll", onScroll, true);
|
|
1149
|
+
removeEventListener("resize", onResize);
|
|
1150
|
+
document.removeEventListener("visibilitychange", onHidden);
|
|
1151
|
+
if (frame)
|
|
1152
|
+
cancelAnimationFrame(frame);
|
|
1153
|
+
stopAll();
|
|
1154
|
+
removeEventListener("popstate", onPop);
|
|
1155
|
+
if (view.open)
|
|
1156
|
+
closeView();
|
|
1157
|
+
bandSize.disconnect();
|
|
1158
|
+
dialog.remove();
|
|
1159
|
+
if (live === self)
|
|
1160
|
+
live = null;
|
|
1161
|
+
};
|
|
1162
|
+
}
|