@vanduo-oss/vd3-cbun 1.0.0

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.
Files changed (45) hide show
  1. package/CHANGELOG.md +50 -0
  2. package/LICENSE +22 -0
  3. package/README.md +160 -0
  4. package/SKILL.md +119 -0
  5. package/dist/charts/core.d.ts +273 -0
  6. package/dist/charts/index.cjs +1828 -0
  7. package/dist/charts/index.cjs.map +7 -0
  8. package/dist/charts/index.d.ts +65 -0
  9. package/dist/charts/index.js +1805 -0
  10. package/dist/charts/index.js.map +7 -0
  11. package/dist/charts/vd3-charts.css +51 -0
  12. package/dist/charts/vue.d.ts +86 -0
  13. package/dist/flowchart/core.d.ts +288 -0
  14. package/dist/flowchart/index.cjs +3447 -0
  15. package/dist/flowchart/index.cjs.map +7 -0
  16. package/dist/flowchart/index.d.ts +54 -0
  17. package/dist/flowchart/index.js +3424 -0
  18. package/dist/flowchart/index.js.map +7 -0
  19. package/dist/flowchart/vd3-flowchart.css +600 -0
  20. package/dist/flowchart/vue.d.ts +66 -0
  21. package/dist/hex-grid/core.d.ts +200 -0
  22. package/dist/hex-grid/hex-math.cjs +162 -0
  23. package/dist/hex-grid/hex-math.cjs.map +7 -0
  24. package/dist/hex-grid/hex-math.d.ts +119 -0
  25. package/dist/hex-grid/hex-math.js +141 -0
  26. package/dist/hex-grid/hex-math.js.map +7 -0
  27. package/dist/hex-grid/index.cjs +915 -0
  28. package/dist/hex-grid/index.cjs.map +7 -0
  29. package/dist/hex-grid/index.d.ts +15 -0
  30. package/dist/hex-grid/index.js +894 -0
  31. package/dist/hex-grid/index.js.map +7 -0
  32. package/dist/hex-grid/vue.d.ts +14 -0
  33. package/dist/index.d.ts +13 -0
  34. package/dist/index.js +11 -0
  35. package/dist/index.js.map +7 -0
  36. package/dist/meta.json +551 -0
  37. package/dist/music-player/core.d.ts +88 -0
  38. package/dist/music-player/index.cjs +1227 -0
  39. package/dist/music-player/index.cjs.map +7 -0
  40. package/dist/music-player/index.d.ts +12 -0
  41. package/dist/music-player/index.js +1204 -0
  42. package/dist/music-player/index.js.map +7 -0
  43. package/dist/music-player/vd3-music-player.css +829 -0
  44. package/dist/music-player/vue.d.ts +32 -0
  45. package/package.json +105 -0
@@ -0,0 +1,1204 @@
1
+ // src/music-player/vue.js
2
+ import { defineComponent, h, ref, onMounted, onBeforeUnmount, watch } from "vue";
3
+
4
+ // src/music-player/core.js
5
+ var VD_MUSIC_PLAYER_VERSION = "1.0.0";
6
+ var CORNER_POSITIONS = ["bottom-left", "bottom-right", "top-left", "top-right"];
7
+ var REPEAT_MODES = ["off", "one", "all"];
8
+ var REPEAT_CYCLE = { off: "one", one: "all", all: "off" };
9
+ var REPEAT_LABELS = { off: "Repeat", one: "Repeat one", all: "Repeat all" };
10
+ function normalizeRepeat(value) {
11
+ return REPEAT_MODES.includes(value) ? value : "off";
12
+ }
13
+ function normalizeCornerPosition(position) {
14
+ return CORNER_POSITIONS.includes(position) ? position : "bottom-right";
15
+ }
16
+ function shuffleArray(arr) {
17
+ const shuffled = arr.slice();
18
+ for (let i = shuffled.length - 1; i > 0; i--) {
19
+ const j = Math.floor(Math.random() * (i + 1));
20
+ const tmp = shuffled[i];
21
+ shuffled[i] = shuffled[j];
22
+ shuffled[j] = tmp;
23
+ }
24
+ return shuffled;
25
+ }
26
+ function formatTime(seconds) {
27
+ if (!isFinite(seconds) || seconds < 0) return "0:00";
28
+ const m = Math.floor(seconds / 60);
29
+ const s = Math.floor(seconds % 60);
30
+ return m + ":" + (s < 10 ? "0" : "") + s;
31
+ }
32
+ function persistStorageKey(id) {
33
+ return "vanduo:music-player:" + (id && id.trim() ? id.trim() : "default") + ":pos";
34
+ }
35
+ function updateRangeFill(input) {
36
+ const min = parseFloat(input.min) || 0;
37
+ const max = parseFloat(input.max) || 1;
38
+ const val = parseFloat(input.value) || 0;
39
+ const pct = (val - min) / (max - min) * 100;
40
+ input.style.setProperty("--vd-fill", pct + "%");
41
+ input.style.backgroundImage = "linear-gradient(to right, var(--vd-music-player-track-fill, currentColor) 0%, var(--vd-music-player-track-fill, currentColor) " + pct + "%, var(--vd-music-player-track-bg, #ccc) " + pct + "%, var(--vd-music-player-track-bg, #ccc) 100%)";
42
+ }
43
+ function icon(name) {
44
+ const el = document.createElement("i");
45
+ el.className = "ph ph-" + name;
46
+ el.setAttribute("aria-hidden", "true");
47
+ return el;
48
+ }
49
+ var MusicPlayer = {
50
+ /** @type {Map<HTMLElement, Object>} */
51
+ instances: /* @__PURE__ */ new Map(),
52
+ /**
53
+ * Default options.
54
+ */
55
+ defaults: {
56
+ tracks: [],
57
+ volume: 0.5,
58
+ shuffle: false,
59
+ repeat: "off",
60
+ showProgress: false,
61
+ showPlaylist: false,
62
+ autoAdvance: true,
63
+ glass: false,
64
+ detachable: false,
65
+ /** @type {null|string} */
66
+ floatingPosition: null,
67
+ draggable: false,
68
+ minimizable: false,
69
+ startMinimized: false,
70
+ persistPosition: false,
71
+ persistKey: ""
72
+ },
73
+ /**
74
+ * Initialize a single player element.
75
+ * @param {HTMLElement} container
76
+ * @param {Object} [options]
77
+ */
78
+ initPlayer: function(container, options) {
79
+ const opts = Object.assign({}, this.defaults, options || {});
80
+ const rawTracks = Array.isArray(opts.tracks) ? opts.tracks : [];
81
+ const tracks = rawTracks.filter((t) => t && typeof t.url === "string" && t.url.trim());
82
+ const trackList = opts.shuffle ? shuffleArray(tracks) : tracks.slice();
83
+ const state = {
84
+ tracks: trackList,
85
+ originalTracks: tracks.slice(),
86
+ currentIndex: 0,
87
+ isPlaying: false,
88
+ volume: Math.max(0, Math.min(1, opts.volume)),
89
+ shuffle: opts.shuffle,
90
+ repeat: normalizeRepeat(opts.repeat),
91
+ showProgress: opts.showProgress,
92
+ showPlaylist: opts.showPlaylist,
93
+ autoAdvance: opts.autoAdvance,
94
+ audio: null,
95
+ glass: Boolean(opts.glass),
96
+ detachable: Boolean(opts.detachable),
97
+ floatingPosition: opts.floatingPosition || "bottom-right",
98
+ draggable: Boolean(opts.draggable) && Boolean(opts.detachable),
99
+ minimizable: Boolean(opts.minimizable),
100
+ startMinimized: Boolean(opts.startMinimized),
101
+ persistPosition: Boolean(opts.persistPosition),
102
+ persistKey: typeof opts.persistKey === "string" ? opts.persistKey : "",
103
+ isDetached: false,
104
+ isMinimized: false,
105
+ _startMinimizeApplied: false
106
+ };
107
+ const audio = new Audio();
108
+ audio.volume = state.volume;
109
+ audio.preload = "metadata";
110
+ state.audio = audio;
111
+ this._buildDOM(container, state);
112
+ const refs = {
113
+ btnPlay: container.querySelector(".vd-music-player-btn-play"),
114
+ btnPrev: container.querySelector(".vd-music-player-btn-prev"),
115
+ btnNext: container.querySelector(".vd-music-player-btn-next"),
116
+ btnRepeat: container.querySelector(".vd-music-player-btn-repeat"),
117
+ btnShuffle: container.querySelector(".vd-music-player-btn-shuffle"),
118
+ btnPlaylist: container.querySelector(".vd-music-player-btn-playlist"),
119
+ btnDetach: container.querySelector(".vd-music-player-btn-detach"),
120
+ btnAttach: container.querySelector(".vd-music-player-btn-attach"),
121
+ btnMinimize: container.querySelector(".vd-music-player-btn-minimize"),
122
+ dragHandle: container.querySelector(".vd-music-player-drag-handle"),
123
+ trackName: container.querySelector(".vd-music-player-track-name"),
124
+ volumeSlider: container.querySelector(".vd-music-player-volume-slider"),
125
+ volumeIcon: container.querySelector(".vd-music-player-volume-icon"),
126
+ progressBar: container.querySelector(".vd-music-player-progress-bar"),
127
+ timeElapsed: container.querySelector(".vd-music-player-time-elapsed"),
128
+ timeDuration: container.querySelector(".vd-music-player-time-duration"),
129
+ playlistPanel: container.querySelector(".vd-music-player-playlist")
130
+ };
131
+ const renderPlayIcon = () => {
132
+ const btn = refs.btnPlay;
133
+ if (!btn) return;
134
+ btn.innerHTML = "";
135
+ btn.appendChild(icon(state.isPlaying ? "pause" : "play"));
136
+ btn.setAttribute("aria-label", state.isPlaying ? "Pause" : "Play");
137
+ btn.classList.toggle("is-active", state.isPlaying);
138
+ };
139
+ const renderTrackName = () => {
140
+ const el = refs.trackName;
141
+ if (!el) return;
142
+ const track = state.tracks[state.currentIndex];
143
+ if (track) {
144
+ el.textContent = track.name || "Unknown Track";
145
+ el.classList.remove("is-idle");
146
+ } else {
147
+ el.textContent = "No tracks loaded";
148
+ el.classList.add("is-idle");
149
+ }
150
+ };
151
+ const renderVolumeIcon = () => {
152
+ const el = refs.volumeIcon;
153
+ if (!el) return;
154
+ el.innerHTML = "";
155
+ const v = state.volume;
156
+ const name = v === 0 ? "speaker-none" : v < 0.5 ? "speaker-low" : "speaker-high";
157
+ el.appendChild(icon(name));
158
+ };
159
+ const renderShuffleBtn = () => {
160
+ const btn = refs.btnShuffle;
161
+ if (!btn) return;
162
+ btn.classList.toggle("is-active", state.shuffle);
163
+ btn.setAttribute("aria-pressed", state.shuffle ? "true" : "false");
164
+ };
165
+ const renderRepeatBtn = () => {
166
+ const btn = refs.btnRepeat;
167
+ if (!btn) return;
168
+ btn.innerHTML = "";
169
+ btn.appendChild(icon("repeat"));
170
+ if (state.repeat === "one") {
171
+ const badge = document.createElement("span");
172
+ badge.className = "vd-music-player-repeat-badge";
173
+ badge.setAttribute("aria-hidden", "true");
174
+ badge.textContent = "1";
175
+ btn.appendChild(badge);
176
+ }
177
+ btn.classList.toggle("is-active", state.repeat !== "off");
178
+ btn.setAttribute("aria-pressed", state.repeat !== "off" ? "true" : "false");
179
+ const label = REPEAT_LABELS[state.repeat] || REPEAT_LABELS.off;
180
+ btn.setAttribute("aria-label", label);
181
+ btn.title = label;
182
+ };
183
+ const dispatchRepeatChange = () => {
184
+ container.dispatchEvent(
185
+ new CustomEvent("musicplayer:repeatchange", {
186
+ bubbles: true,
187
+ detail: { repeat: state.repeat }
188
+ })
189
+ );
190
+ };
191
+ const cycleRepeat = () => {
192
+ state.repeat = REPEAT_CYCLE[state.repeat] || "off";
193
+ renderRepeatBtn();
194
+ dispatchRepeatChange();
195
+ };
196
+ const setRepeatMode = (mode) => {
197
+ state.repeat = normalizeRepeat(mode);
198
+ renderRepeatBtn();
199
+ dispatchRepeatChange();
200
+ };
201
+ const renderPlaylistItems = () => {
202
+ const panel = refs.playlistPanel;
203
+ if (!panel) return;
204
+ panel.innerHTML = "";
205
+ state.tracks.forEach((track, i) => {
206
+ const item = document.createElement("button");
207
+ item.className = "vd-music-player-playlist-item" + (i === state.currentIndex ? " is-active" : "");
208
+ item.type = "button";
209
+ item.setAttribute("data-index", String(i));
210
+ item.setAttribute("aria-current", i === state.currentIndex ? "true" : "false");
211
+ const num = document.createElement("span");
212
+ num.className = "vd-music-player-playlist-num";
213
+ num.textContent = String(i + 1);
214
+ const name = document.createElement("span");
215
+ name.className = "vd-music-player-playlist-name";
216
+ name.textContent = track.name || "Track " + (i + 1);
217
+ item.appendChild(num);
218
+ item.appendChild(name);
219
+ panel.appendChild(item);
220
+ });
221
+ };
222
+ const renderProgress = () => {
223
+ const bar = refs.progressBar;
224
+ if (!bar || !audio.duration) return;
225
+ const pct = audio.currentTime / audio.duration * 100;
226
+ bar.value = String(pct);
227
+ updateRangeFill(bar);
228
+ if (refs.timeElapsed) refs.timeElapsed.textContent = formatTime(audio.currentTime);
229
+ if (refs.timeDuration) refs.timeDuration.textContent = formatTime(audio.duration);
230
+ };
231
+ const loadTrack = (index, autoPlay) => {
232
+ const track = state.tracks[index];
233
+ if (!track) return;
234
+ state.currentIndex = index;
235
+ audio.src = track.url;
236
+ renderTrackName();
237
+ renderPlaylistItems();
238
+ if (refs.progressBar) {
239
+ refs.progressBar.value = "0";
240
+ updateRangeFill(refs.progressBar);
241
+ }
242
+ if (refs.timeElapsed) refs.timeElapsed.textContent = "0:00";
243
+ if (refs.timeDuration) refs.timeDuration.textContent = "0:00";
244
+ container.dispatchEvent(
245
+ new CustomEvent("musicplayer:trackchange", {
246
+ bubbles: true,
247
+ detail: { index, name: track.name, url: track.url }
248
+ })
249
+ );
250
+ if (autoPlay) {
251
+ audio.play().catch(() => {
252
+ });
253
+ }
254
+ };
255
+ const cleanupFunctions = [];
256
+ const onPlay = () => {
257
+ state.isPlaying = true;
258
+ renderPlayIcon();
259
+ container.dispatchEvent(new CustomEvent("musicplayer:play", { bubbles: true }));
260
+ };
261
+ const onPause = () => {
262
+ state.isPlaying = false;
263
+ renderPlayIcon();
264
+ container.dispatchEvent(new CustomEvent("musicplayer:pause", { bubbles: true }));
265
+ };
266
+ const onEnded = () => {
267
+ if (state.repeat === "one") {
268
+ audio.currentTime = 0;
269
+ audio.play().catch(() => {
270
+ });
271
+ return;
272
+ }
273
+ if (state.repeat === "all") {
274
+ if (state.tracks.length > 0) {
275
+ const next = (state.currentIndex + 1) % state.tracks.length;
276
+ loadTrack(next, true);
277
+ }
278
+ return;
279
+ }
280
+ if (state.autoAdvance && state.tracks.length > 1) {
281
+ const next = (state.currentIndex + 1) % state.tracks.length;
282
+ loadTrack(next, true);
283
+ } else {
284
+ state.isPlaying = false;
285
+ renderPlayIcon();
286
+ container.dispatchEvent(new CustomEvent("musicplayer:ended", { bubbles: true }));
287
+ }
288
+ };
289
+ const onTimeUpdate = () => {
290
+ if (state.showProgress) renderProgress();
291
+ };
292
+ const onLoadedMetadata = () => {
293
+ if (refs.timeDuration) refs.timeDuration.textContent = formatTime(audio.duration);
294
+ if (refs.progressBar) {
295
+ refs.progressBar.max = "100";
296
+ updateRangeFill(refs.progressBar);
297
+ }
298
+ };
299
+ audio.addEventListener("play", onPlay);
300
+ audio.addEventListener("pause", onPause);
301
+ audio.addEventListener("ended", onEnded);
302
+ audio.addEventListener("timeupdate", onTimeUpdate);
303
+ audio.addEventListener("loadedmetadata", onLoadedMetadata);
304
+ cleanupFunctions.push(() => {
305
+ audio.removeEventListener("play", onPlay);
306
+ audio.removeEventListener("pause", onPause);
307
+ audio.removeEventListener("ended", onEnded);
308
+ audio.removeEventListener("timeupdate", onTimeUpdate);
309
+ audio.removeEventListener("loadedmetadata", onLoadedMetadata);
310
+ audio.pause();
311
+ audio.src = "";
312
+ });
313
+ if (refs.btnPlay) {
314
+ const handler = () => {
315
+ if (!audio.src && state.tracks.length) loadTrack(state.currentIndex, false);
316
+ if (state.isPlaying) {
317
+ audio.pause();
318
+ } else {
319
+ audio.play().catch(() => {
320
+ });
321
+ }
322
+ };
323
+ refs.btnPlay.addEventListener("click", handler);
324
+ cleanupFunctions.push(() => refs.btnPlay.removeEventListener("click", handler));
325
+ const keyHandler = (e) => {
326
+ if (e.key === " " || e.key === "Enter") {
327
+ e.preventDefault();
328
+ handler();
329
+ }
330
+ };
331
+ refs.btnPlay.addEventListener("keydown", keyHandler);
332
+ cleanupFunctions.push(() => refs.btnPlay.removeEventListener("keydown", keyHandler));
333
+ }
334
+ if (refs.btnPrev) {
335
+ const handler = () => {
336
+ if (!state.tracks.length) return;
337
+ if (audio.currentTime > 3) {
338
+ audio.currentTime = 0;
339
+ } else {
340
+ const prev = state.currentIndex === 0 ? state.tracks.length - 1 : state.currentIndex - 1;
341
+ loadTrack(prev, state.isPlaying);
342
+ }
343
+ };
344
+ refs.btnPrev.addEventListener("click", handler);
345
+ cleanupFunctions.push(() => refs.btnPrev.removeEventListener("click", handler));
346
+ }
347
+ if (refs.btnNext) {
348
+ const handler = () => {
349
+ if (!state.tracks.length) return;
350
+ const next = (state.currentIndex + 1) % state.tracks.length;
351
+ loadTrack(next, state.isPlaying);
352
+ };
353
+ refs.btnNext.addEventListener("click", handler);
354
+ cleanupFunctions.push(() => refs.btnNext.removeEventListener("click", handler));
355
+ }
356
+ if (refs.btnRepeat) {
357
+ const handler = () => {
358
+ cycleRepeat();
359
+ };
360
+ refs.btnRepeat.addEventListener("click", handler);
361
+ cleanupFunctions.push(() => refs.btnRepeat.removeEventListener("click", handler));
362
+ }
363
+ if (refs.btnShuffle) {
364
+ const handler = () => {
365
+ state.shuffle = !state.shuffle;
366
+ if (state.shuffle) {
367
+ const current = state.tracks[state.currentIndex];
368
+ state.tracks = shuffleArray(state.tracks);
369
+ const newIdx = state.tracks.findIndex((t) => t === current);
370
+ if (newIdx > 0) {
371
+ state.tracks.splice(newIdx, 1);
372
+ state.tracks.unshift(current);
373
+ }
374
+ state.currentIndex = 0;
375
+ } else {
376
+ const current = state.tracks[state.currentIndex];
377
+ state.tracks = state.originalTracks.slice();
378
+ state.currentIndex = state.tracks.findIndex((t) => t === current);
379
+ if (state.currentIndex < 0) state.currentIndex = 0;
380
+ }
381
+ renderShuffleBtn();
382
+ renderPlaylistItems();
383
+ };
384
+ refs.btnShuffle.addEventListener("click", handler);
385
+ cleanupFunctions.push(() => refs.btnShuffle.removeEventListener("click", handler));
386
+ }
387
+ if (refs.btnPlaylist) {
388
+ const handler = () => {
389
+ const panel = refs.playlistPanel;
390
+ if (!panel) return;
391
+ const isOpen = panel.classList.toggle("is-open");
392
+ refs.btnPlaylist.classList.toggle("is-active", isOpen);
393
+ refs.btnPlaylist.setAttribute("aria-expanded", isOpen ? "true" : "false");
394
+ };
395
+ refs.btnPlaylist.addEventListener("click", handler);
396
+ cleanupFunctions.push(() => refs.btnPlaylist.removeEventListener("click", handler));
397
+ }
398
+ if (refs.volumeSlider) {
399
+ const handler = (e) => {
400
+ const v = parseFloat(e.target.value);
401
+ state.volume = v;
402
+ audio.volume = v;
403
+ renderVolumeIcon();
404
+ updateRangeFill(refs.volumeSlider);
405
+ container.dispatchEvent(
406
+ new CustomEvent("musicplayer:volumechange", { bubbles: true, detail: { volume: v } })
407
+ );
408
+ };
409
+ refs.volumeSlider.addEventListener("input", handler);
410
+ cleanupFunctions.push(() => refs.volumeSlider.removeEventListener("input", handler));
411
+ updateRangeFill(refs.volumeSlider);
412
+ }
413
+ if (refs.progressBar) {
414
+ const handler = (e) => {
415
+ if (!audio.duration) return;
416
+ const pct = parseFloat(e.target.value);
417
+ audio.currentTime = pct / 100 * audio.duration;
418
+ updateRangeFill(refs.progressBar);
419
+ };
420
+ refs.progressBar.addEventListener("input", handler);
421
+ cleanupFunctions.push(() => refs.progressBar.removeEventListener("input", handler));
422
+ }
423
+ if (refs.playlistPanel) {
424
+ const panelHandler = (e) => {
425
+ const item = e.target.closest(".vd-music-player-playlist-item");
426
+ if (!item) return;
427
+ const idx = parseInt(item.getAttribute("data-index"), 10);
428
+ if (!isNaN(idx)) loadTrack(idx, true);
429
+ };
430
+ refs.playlistPanel.addEventListener("click", panelHandler);
431
+ cleanupFunctions.push(() => refs.playlistPanel.removeEventListener("click", panelHandler));
432
+ }
433
+ if (refs.btnDetach) {
434
+ const h2 = () => {
435
+ this.detach(container);
436
+ };
437
+ refs.btnDetach.addEventListener("click", h2);
438
+ cleanupFunctions.push(() => refs.btnDetach.removeEventListener("click", h2));
439
+ }
440
+ if (refs.btnAttach) {
441
+ const h2 = () => {
442
+ this.attach(container);
443
+ };
444
+ refs.btnAttach.addEventListener("click", h2);
445
+ cleanupFunctions.push(() => refs.btnAttach.removeEventListener("click", h2));
446
+ }
447
+ if (refs.btnMinimize) {
448
+ const h2 = () => {
449
+ this.toggleMinimize(container);
450
+ };
451
+ refs.btnMinimize.addEventListener("click", h2);
452
+ cleanupFunctions.push(() => refs.btnMinimize.removeEventListener("click", h2));
453
+ }
454
+ renderPlayIcon();
455
+ renderTrackName();
456
+ renderVolumeIcon();
457
+ renderRepeatBtn();
458
+ if (opts.showPlaylist) renderPlaylistItems();
459
+ this.instances.set(container, {
460
+ state,
461
+ audio,
462
+ refs,
463
+ cleanup: cleanupFunctions,
464
+ ui: { restore: null, unbindDrag: null },
465
+ cycleRepeat,
466
+ setRepeatMode
467
+ });
468
+ container.setAttribute("data-music-player-initialized", "true");
469
+ },
470
+ /* ─── DOM builder ─────────────────────────────────────── */
471
+ /**
472
+ * Build the inner DOM structure inside container.
473
+ * Pre-existing inner content is replaced only if it has no
474
+ * recognised child elements (allows server-rendered markup).
475
+ * @param {HTMLElement} container
476
+ * @param {Object} state
477
+ */
478
+ _buildDOM: function(container, state) {
479
+ if (container.querySelector(".vd-music-player-controls")) return;
480
+ container.setAttribute("role", "region");
481
+ container.setAttribute("aria-label", "Music Player");
482
+ if (state.showProgress) container.classList.add("has-progress");
483
+ if (state.showPlaylist) container.classList.add("has-playlist");
484
+ if (state.glass) container.classList.add("vd-music-player-glass");
485
+ if (state.draggable) container.classList.add("vd-music-player-draggable");
486
+ if (state.detachable || state.minimizable) {
487
+ const tb = document.createElement("div");
488
+ tb.className = "vd-music-player-toolbar";
489
+ tb.setAttribute("role", "toolbar");
490
+ tb.setAttribute("aria-label", "Player window");
491
+ if (state.draggable) {
492
+ const h2 = document.createElement("button");
493
+ h2.type = "button";
494
+ h2.className = "vd-music-player-drag-handle";
495
+ h2.setAttribute("aria-label", "Drag to move player");
496
+ h2.appendChild(icon("dots-six-vertical"));
497
+ tb.appendChild(h2);
498
+ }
499
+ const tSp = document.createElement("span");
500
+ tSp.className = "vd-music-player-toolbar-spacer";
501
+ tSp.setAttribute("aria-hidden", "true");
502
+ tb.appendChild(tSp);
503
+ if (state.minimizable) {
504
+ const bMin = document.createElement("button");
505
+ bMin.type = "button";
506
+ bMin.className = "vd-music-player-btn vd-music-player-btn-minimize";
507
+ bMin.setAttribute("aria-label", "Minimize player");
508
+ bMin.setAttribute("aria-expanded", "true");
509
+ bMin.appendChild(icon("minus"));
510
+ tb.appendChild(bMin);
511
+ }
512
+ if (state.detachable) {
513
+ const bOut = document.createElement("button");
514
+ bOut.type = "button";
515
+ bOut.className = "vd-music-player-btn vd-music-player-btn-detach";
516
+ bOut.setAttribute("aria-label", "Detach player");
517
+ bOut.appendChild(icon("arrows-out"));
518
+ tb.appendChild(bOut);
519
+ const bIn = document.createElement("button");
520
+ bIn.type = "button";
521
+ bIn.className = "vd-music-player-btn vd-music-player-btn-attach";
522
+ bIn.setAttribute("aria-label", "Attach player");
523
+ bIn.appendChild(icon("arrows-in"));
524
+ tb.appendChild(bIn);
525
+ }
526
+ container.classList.add("vd-music-player-has-chrome");
527
+ container.appendChild(tb);
528
+ }
529
+ const info = document.createElement("div");
530
+ info.className = "vd-music-player-info";
531
+ const iconWrap = document.createElement("span");
532
+ iconWrap.className = "vd-music-player-icon";
533
+ iconWrap.setAttribute("aria-hidden", "true");
534
+ iconWrap.appendChild(icon("music-note"));
535
+ const trackName = document.createElement("span");
536
+ trackName.className = "vd-music-player-track-name";
537
+ trackName.setAttribute("aria-live", "polite");
538
+ trackName.setAttribute("aria-atomic", "true");
539
+ info.appendChild(iconWrap);
540
+ info.appendChild(trackName);
541
+ container.appendChild(info);
542
+ const controls = document.createElement("div");
543
+ controls.className = "vd-music-player-controls";
544
+ controls.setAttribute("role", "group");
545
+ controls.setAttribute("aria-label", "Playback controls");
546
+ const btnPrev = document.createElement("button");
547
+ btnPrev.type = "button";
548
+ btnPrev.className = "vd-music-player-btn vd-music-player-btn-prev";
549
+ btnPrev.setAttribute("aria-label", "Previous track");
550
+ btnPrev.appendChild(icon("skip-back"));
551
+ const btnPlay = document.createElement("button");
552
+ btnPlay.type = "button";
553
+ btnPlay.className = "vd-music-player-btn vd-music-player-btn-play";
554
+ btnPlay.setAttribute("aria-label", "Play");
555
+ btnPlay.appendChild(icon("play"));
556
+ const btnNext = document.createElement("button");
557
+ btnNext.type = "button";
558
+ btnNext.className = "vd-music-player-btn vd-music-player-btn-next";
559
+ btnNext.setAttribute("aria-label", "Next track");
560
+ btnNext.appendChild(icon("skip-forward"));
561
+ controls.appendChild(btnPrev);
562
+ controls.appendChild(btnPlay);
563
+ controls.appendChild(btnNext);
564
+ const btnRepeat = document.createElement("button");
565
+ btnRepeat.type = "button";
566
+ btnRepeat.className = "vd-music-player-btn vd-music-player-btn-repeat";
567
+ btnRepeat.setAttribute("aria-label", REPEAT_LABELS[normalizeRepeat(state.repeat)]);
568
+ btnRepeat.setAttribute("aria-pressed", state.repeat !== "off" ? "true" : "false");
569
+ btnRepeat.appendChild(icon("repeat"));
570
+ controls.appendChild(btnRepeat);
571
+ if (state.showPlaylist || state.shuffle !== void 0) {
572
+ const btnShuffle = document.createElement("button");
573
+ btnShuffle.type = "button";
574
+ btnShuffle.className = "vd-music-player-btn vd-music-player-btn-shuffle";
575
+ btnShuffle.setAttribute("aria-label", "Shuffle");
576
+ btnShuffle.setAttribute("aria-pressed", state.shuffle ? "true" : "false");
577
+ btnShuffle.appendChild(icon("shuffle"));
578
+ controls.appendChild(btnShuffle);
579
+ }
580
+ const spacer = document.createElement("span");
581
+ spacer.className = "vd-music-player-spacer";
582
+ spacer.setAttribute("aria-hidden", "true");
583
+ controls.appendChild(spacer);
584
+ const volumeWrap = document.createElement("div");
585
+ volumeWrap.className = "vd-music-player-volume";
586
+ const volumeIcon = document.createElement("span");
587
+ volumeIcon.className = "vd-music-player-volume-icon";
588
+ volumeIcon.setAttribute("aria-hidden", "true");
589
+ const volumeSlider = document.createElement("input");
590
+ volumeSlider.type = "range";
591
+ volumeSlider.className = "vd-music-player-volume-slider";
592
+ volumeSlider.min = "0";
593
+ volumeSlider.max = "1";
594
+ volumeSlider.step = "0.01";
595
+ volumeSlider.value = String(state.volume);
596
+ volumeSlider.setAttribute("aria-label", "Volume");
597
+ volumeWrap.appendChild(volumeIcon);
598
+ volumeWrap.appendChild(volumeSlider);
599
+ controls.appendChild(volumeWrap);
600
+ if (state.showPlaylist) {
601
+ const btnPlaylist = document.createElement("button");
602
+ btnPlaylist.type = "button";
603
+ btnPlaylist.className = "vd-music-player-btn vd-music-player-btn-playlist";
604
+ btnPlaylist.setAttribute("aria-label", "Show playlist");
605
+ btnPlaylist.setAttribute("aria-expanded", "false");
606
+ btnPlaylist.appendChild(icon("playlist"));
607
+ controls.appendChild(btnPlaylist);
608
+ }
609
+ container.appendChild(controls);
610
+ if (state.showProgress) {
611
+ const progressRow = document.createElement("div");
612
+ progressRow.className = "vd-music-player-progress";
613
+ const timeElapsed = document.createElement("span");
614
+ timeElapsed.className = "vd-music-player-time vd-music-player-time-elapsed";
615
+ timeElapsed.textContent = "0:00";
616
+ timeElapsed.setAttribute("aria-hidden", "true");
617
+ const progressBar = document.createElement("input");
618
+ progressBar.type = "range";
619
+ progressBar.className = "vd-music-player-progress-bar";
620
+ progressBar.min = "0";
621
+ progressBar.max = "100";
622
+ progressBar.step = "0.1";
623
+ progressBar.value = "0";
624
+ progressBar.setAttribute("aria-label", "Seek");
625
+ const timeDuration = document.createElement("span");
626
+ timeDuration.className = "vd-music-player-time vd-music-player-time-duration";
627
+ timeDuration.textContent = "0:00";
628
+ timeDuration.setAttribute("aria-hidden", "true");
629
+ progressRow.appendChild(timeElapsed);
630
+ progressRow.appendChild(progressBar);
631
+ progressRow.appendChild(timeDuration);
632
+ container.appendChild(progressRow);
633
+ }
634
+ if (state.showPlaylist) {
635
+ const playlist = document.createElement("div");
636
+ playlist.className = "vd-music-player-playlist";
637
+ playlist.setAttribute("aria-label", "Playlist");
638
+ container.appendChild(playlist);
639
+ }
640
+ },
641
+ /* ─── Public API ──────────────────────────────────────── */
642
+ /**
643
+ * @param {HTMLElement} container
644
+ */
645
+ play: function(container) {
646
+ const inst = this.instances.get(container);
647
+ if (!inst) return;
648
+ if (!inst.audio.src && inst.state.tracks.length) {
649
+ inst.audio.src = inst.state.tracks[inst.state.currentIndex].url;
650
+ }
651
+ inst.audio.play().catch(() => {
652
+ });
653
+ },
654
+ /**
655
+ * @param {HTMLElement} container
656
+ */
657
+ pause: function(container) {
658
+ const inst = this.instances.get(container);
659
+ if (inst) inst.audio.pause();
660
+ },
661
+ /**
662
+ * @param {HTMLElement} container
663
+ */
664
+ toggle: function(container) {
665
+ const inst = this.instances.get(container);
666
+ if (!inst) return;
667
+ if (inst.state.isPlaying) {
668
+ this.pause(container);
669
+ } else {
670
+ this.play(container);
671
+ }
672
+ },
673
+ /**
674
+ * @param {HTMLElement} container
675
+ */
676
+ next: function(container) {
677
+ const inst = this.instances.get(container);
678
+ if (!inst || !inst.state.tracks.length) return;
679
+ const next = (inst.state.currentIndex + 1) % inst.state.tracks.length;
680
+ this._loadTrack(inst, next, inst.state.isPlaying);
681
+ },
682
+ /**
683
+ * @param {HTMLElement} container
684
+ */
685
+ previous: function(container) {
686
+ const inst = this.instances.get(container);
687
+ if (!inst || !inst.state.tracks.length) return;
688
+ const len = inst.state.tracks.length;
689
+ const prev = (inst.state.currentIndex - 1 + len) % len;
690
+ this._loadTrack(inst, prev, inst.state.isPlaying);
691
+ },
692
+ /**
693
+ * @param {HTMLElement} container
694
+ * @param {number} value - 0 to 1
695
+ */
696
+ setVolume: function(container, value) {
697
+ const inst = this.instances.get(container);
698
+ if (!inst) return;
699
+ const v = Math.max(0, Math.min(1, value));
700
+ inst.state.volume = v;
701
+ inst.audio.volume = v;
702
+ if (inst.refs.volumeSlider) {
703
+ inst.refs.volumeSlider.value = String(v);
704
+ updateRangeFill(inst.refs.volumeSlider);
705
+ }
706
+ container.dispatchEvent(
707
+ new CustomEvent("musicplayer:volumechange", { bubbles: true, detail: { volume: v } })
708
+ );
709
+ },
710
+ /**
711
+ * @param {HTMLElement} container
712
+ * @param {number} index - Track index
713
+ */
714
+ setTrack: function(container, index) {
715
+ const inst = this.instances.get(container);
716
+ if (!inst) return;
717
+ this._loadTrack(inst, index, inst.state.isPlaying);
718
+ },
719
+ /**
720
+ * Shuffle or un-shuffle the track list.
721
+ * @param {HTMLElement} container
722
+ */
723
+ shuffle: function(container) {
724
+ const inst = this.instances.get(container);
725
+ if (!inst || !inst.refs.btnShuffle) return;
726
+ inst.refs.btnShuffle.click();
727
+ },
728
+ /**
729
+ * Cycle repeat mode: off → one → all → off.
730
+ * @param {HTMLElement} container
731
+ */
732
+ repeat: function(container) {
733
+ const inst = this.instances.get(container);
734
+ if (!inst || typeof inst.cycleRepeat !== "function") return;
735
+ inst.cycleRepeat();
736
+ },
737
+ /**
738
+ * Set repeat mode explicitly.
739
+ * @param {HTMLElement} container
740
+ * @param {'off'|'one'|'all'} mode
741
+ */
742
+ setRepeat: function(container, mode) {
743
+ const inst = this.instances.get(container);
744
+ if (!inst || typeof inst.setRepeatMode !== "function") return;
745
+ inst.setRepeatMode(mode);
746
+ },
747
+ /**
748
+ * Float the player above the page. Requires { detachable: true } at init.
749
+ * @param {HTMLElement} container
750
+ * @param {string} [position] Corner preset or uses floatingPosition from init
751
+ */
752
+ detach: function(container, position) {
753
+ const inst = this.instances.get(container);
754
+ if (!inst || !inst.state.detachable || inst.state.isDetached) return;
755
+ const s = inst.state;
756
+ inst.ui = inst.ui || { restore: null, unbindDrag: null };
757
+ s.isDetached = true;
758
+ inst.ui.restore = {
759
+ parent: container.parentNode,
760
+ next: container.nextSibling
761
+ };
762
+ document.body.appendChild(container);
763
+ container.classList.add("vd-music-player-floating", "vd-music-player-detached");
764
+ const pos = position != null && position !== void 0 ? position : s.floatingPosition;
765
+ this._setCornerPosition(container, normalizeCornerPosition(pos));
766
+ this._loadPersistedPosition(container, inst);
767
+ if (s.startMinimized && !s._startMinimizeApplied) {
768
+ s._startMinimizeApplied = true;
769
+ this.minimize(container);
770
+ }
771
+ this._bindFloatingDrag(inst);
772
+ container.dispatchEvent(new CustomEvent("musicplayer:detach", { bubbles: true }));
773
+ },
774
+ /**
775
+ * Return a detached player to its original place in the document.
776
+ * @param {HTMLElement} container
777
+ */
778
+ attach: function(container) {
779
+ const inst = this.instances.get(container);
780
+ if (!inst || !inst.state.isDetached) return;
781
+ this._unbindFloatingDrag(inst);
782
+ inst.state.isDetached = false;
783
+ const r = inst.ui && inst.ui.restore;
784
+ container.classList.remove(
785
+ "vd-music-player-floating",
786
+ "vd-music-player-detached",
787
+ "vd-music-player-floating-bottom-left",
788
+ "vd-music-player-floating-bottom-right",
789
+ "vd-music-player-floating-top-left",
790
+ "vd-music-player-floating-top-right",
791
+ "is-position-custom"
792
+ );
793
+ container.style.removeProperty("--vd-music-player-floating-top");
794
+ container.style.removeProperty("--vd-music-player-floating-left");
795
+ if (r && r.parent && r.parent.isConnected) {
796
+ r.parent.insertBefore(container, r.next);
797
+ }
798
+ if (inst.ui) {
799
+ inst.ui.restore = null;
800
+ inst.ui.unbindDrag = null;
801
+ }
802
+ container.dispatchEvent(new CustomEvent("musicplayer:attach", { bubbles: true }));
803
+ },
804
+ /**
805
+ * Collapse to essential controls. Requires { minimizable: true } at init.
806
+ * @param {HTMLElement} container
807
+ */
808
+ minimize: function(container) {
809
+ const inst = this.instances.get(container);
810
+ if (!inst || !inst.state.minimizable || inst.state.isMinimized) return;
811
+ const s = inst.state;
812
+ s.isMinimized = true;
813
+ container.classList.add("vd-music-player-minimized");
814
+ this._setMinimizeButtonState(inst, true);
815
+ if (inst.refs.playlistPanel && inst.refs.playlistPanel.classList.contains("is-open") && inst.refs.btnPlaylist) {
816
+ inst.refs.playlistPanel.classList.remove("is-open");
817
+ inst.refs.btnPlaylist.classList.remove("is-active");
818
+ inst.refs.btnPlaylist.setAttribute("aria-expanded", "false");
819
+ }
820
+ container.dispatchEvent(new CustomEvent("musicplayer:minimize", { bubbles: true }));
821
+ },
822
+ /**
823
+ * Restore from minimized state.
824
+ * @param {HTMLElement} container
825
+ */
826
+ expand: function(container) {
827
+ const inst = this.instances.get(container);
828
+ if (!inst || !inst.state.minimizable || !inst.state.isMinimized) return;
829
+ inst.state.isMinimized = false;
830
+ container.classList.remove("vd-music-player-minimized");
831
+ this._setMinimizeButtonState(inst, false);
832
+ container.dispatchEvent(new CustomEvent("musicplayer:expand", { bubbles: true }));
833
+ },
834
+ /**
835
+ * Toggle minimize / expand.
836
+ * @param {HTMLElement} container
837
+ */
838
+ toggleMinimize: function(container) {
839
+ const inst = this.instances.get(container);
840
+ if (!inst || !inst.state.minimizable) return;
841
+ if (inst.state.isMinimized) {
842
+ this.expand(container);
843
+ } else {
844
+ this.minimize(container);
845
+ }
846
+ },
847
+ /**
848
+ * Set floating corner or pixel position (detached only).
849
+ * @param {HTMLElement} container
850
+ * @param {string|{x:number,y:number}} position Corner preset or { x, y } viewport pixels
851
+ */
852
+ setPosition: function(container, position) {
853
+ const inst = this.instances.get(container);
854
+ if (!inst || !inst.state.isDetached) return;
855
+ if (typeof position === "string") {
856
+ this._setCornerPosition(container, normalizeCornerPosition(position));
857
+ } else if (position && typeof position.x === "number" && typeof position.y === "number") {
858
+ this._setCustomPositionFromRect(container, position.x, position.y);
859
+ }
860
+ if (inst.state.persistPosition) {
861
+ const r = container.getBoundingClientRect();
862
+ this._savePositionPixels(inst, r.left, r.top);
863
+ }
864
+ },
865
+ /**
866
+ * @param {Object} inst
867
+ * @param {boolean} minimized
868
+ */
869
+ _setMinimizeButtonState: function(inst, minimized) {
870
+ const b = inst.refs && inst.refs.btnMinimize;
871
+ if (!b) return;
872
+ b.innerHTML = "";
873
+ b.appendChild(icon(minimized ? "plus" : "minus"));
874
+ b.setAttribute("aria-label", minimized ? "Expand player" : "Minimize player");
875
+ b.setAttribute("aria-expanded", minimized ? "false" : "true");
876
+ },
877
+ /**
878
+ * @param {HTMLElement} container
879
+ * @param {string} which Corner preset from CORNER_POSITIONS
880
+ */
881
+ _setCornerPosition: function(container, which) {
882
+ const corner = normalizeCornerPosition(which);
883
+ container.classList.remove(
884
+ "is-position-custom",
885
+ "vd-music-player-floating-bottom-left",
886
+ "vd-music-player-floating-bottom-right",
887
+ "vd-music-player-floating-top-left",
888
+ "vd-music-player-floating-top-right"
889
+ );
890
+ container.style.removeProperty("--vd-music-player-floating-top");
891
+ container.style.removeProperty("--vd-music-player-floating-left");
892
+ container.classList.add("vd-music-player-floating-" + corner);
893
+ },
894
+ /**
895
+ * @param {HTMLElement} container
896
+ * @param {number} left
897
+ * @param {number} top
898
+ */
899
+ _setCustomPositionFromRect: function(container, left, top) {
900
+ container.classList.remove(
901
+ "vd-music-player-floating-bottom-left",
902
+ "vd-music-player-floating-bottom-right",
903
+ "vd-music-player-floating-top-left",
904
+ "vd-music-player-floating-top-right"
905
+ );
906
+ container.classList.add("is-position-custom");
907
+ container.style.setProperty("--vd-music-player-floating-left", left + "px");
908
+ container.style.setProperty("--vd-music-player-floating-top", top + "px");
909
+ },
910
+ /**
911
+ * @param {HTMLElement} container
912
+ * @param {Object} inst
913
+ */
914
+ _loadPersistedPosition: function(container, inst) {
915
+ if (!inst.state.persistPosition) return;
916
+ const key = this._persistKeyForInstance(inst, container);
917
+ let raw = null;
918
+ try {
919
+ raw = localStorage.getItem(key);
920
+ } catch {
921
+ }
922
+ if (!raw) return;
923
+ try {
924
+ const o = JSON.parse(raw);
925
+ if (o && typeof o.x === "number" && typeof o.y === "number") {
926
+ this._setCustomPositionFromRect(container, o.x, o.y);
927
+ }
928
+ } catch {
929
+ }
930
+ },
931
+ /**
932
+ * @param {Object} inst
933
+ * @param {number} x
934
+ * @param {number} y
935
+ */
936
+ _savePositionPixels: function(inst, x, y) {
937
+ if (!inst.state.persistPosition) return;
938
+ const container = this._containerOf(inst);
939
+ if (!container) return;
940
+ const key = this._persistKeyForInstance(inst, container);
941
+ const val = JSON.stringify({ x, y });
942
+ try {
943
+ localStorage.setItem(key, val);
944
+ } catch {
945
+ }
946
+ },
947
+ /**
948
+ * @param {Object} inst
949
+ * @param {HTMLElement} container
950
+ * @returns {string}
951
+ */
952
+ _persistKeyForInstance: function(inst, container) {
953
+ const pk = inst.state.persistKey;
954
+ if (pk && String(pk).trim()) return persistStorageKey(String(pk).trim());
955
+ return persistStorageKey(container.id || "");
956
+ },
957
+ /**
958
+ * @param {Object} inst
959
+ */
960
+ _unbindFloatingDrag: function(inst) {
961
+ if (inst.ui && typeof inst.ui.unbindDrag === "function") {
962
+ inst.ui.unbindDrag();
963
+ inst.ui.unbindDrag = null;
964
+ }
965
+ },
966
+ /**
967
+ * Free-form pointer drag on the handle. Vanduo's `draggable` component uses HTML5
968
+ * drag/drop for list reordering; floating players use pointer events on the handle instead.
969
+ * @param {Object} inst
970
+ */
971
+ _bindFloatingDrag: function(inst) {
972
+ this._unbindFloatingDrag(inst);
973
+ const h2 = inst.refs && inst.refs.dragHandle;
974
+ if (!h2 || !inst.state || !inst.state.draggable) return;
975
+ const self = this;
976
+ const container = this._containerOf(inst);
977
+ if (!container) return;
978
+ let startX = 0;
979
+ let startY = 0;
980
+ let origL = 0;
981
+ let origT = 0;
982
+ let activeDrag = false;
983
+ const onDown = function(e) {
984
+ if (e.pointerType === "mouse" && e.button !== 0) return;
985
+ e.preventDefault();
986
+ activeDrag = true;
987
+ const r = container.getBoundingClientRect();
988
+ origL = r.left;
989
+ origT = r.top;
990
+ startX = e.clientX;
991
+ startY = e.clientY;
992
+ self._setCustomPositionFromRect(container, origL, origT);
993
+ try {
994
+ h2.setPointerCapture(e.pointerId);
995
+ } catch {
996
+ }
997
+ };
998
+ const onMove = function(e) {
999
+ if (!activeDrag) return;
1000
+ const dx = e.clientX - startX;
1001
+ const dy = e.clientY - startY;
1002
+ let nl = origL + dx;
1003
+ let nt = origT + dy;
1004
+ const r = container.getBoundingClientRect();
1005
+ const w = r.width;
1006
+ const ph = r.height;
1007
+ const vw = window.innerWidth;
1008
+ const vh = window.innerHeight;
1009
+ const pad = 8;
1010
+ nl = Math.max(pad, Math.min(nl, vw - w - pad));
1011
+ nt = Math.max(pad, Math.min(nt, vh - ph - pad));
1012
+ self._setCustomPositionFromRect(container, nl, nt);
1013
+ };
1014
+ const onUp = function(e) {
1015
+ if (!activeDrag) return;
1016
+ activeDrag = false;
1017
+ if (typeof h2.hasPointerCapture === "function" && h2.hasPointerCapture(e.pointerId)) {
1018
+ try {
1019
+ h2.releasePointerCapture(e.pointerId);
1020
+ } catch {
1021
+ }
1022
+ }
1023
+ if (inst.state.persistPosition) {
1024
+ const r = container.getBoundingClientRect();
1025
+ self._savePositionPixels(inst, r.left, r.top);
1026
+ }
1027
+ };
1028
+ h2.addEventListener("pointerdown", onDown);
1029
+ h2.addEventListener("pointermove", onMove);
1030
+ h2.addEventListener("pointerup", onUp);
1031
+ h2.addEventListener("pointercancel", onUp);
1032
+ inst.ui = inst.ui || { restore: null, unbindDrag: null };
1033
+ inst.ui.unbindDrag = function() {
1034
+ h2.removeEventListener("pointerdown", onDown);
1035
+ h2.removeEventListener("pointermove", onMove);
1036
+ h2.removeEventListener("pointerup", onUp);
1037
+ h2.removeEventListener("pointercancel", onUp);
1038
+ };
1039
+ },
1040
+ /**
1041
+ * Return a shallow copy of the current player state.
1042
+ * @param {HTMLElement} container
1043
+ * @returns {Object|null}
1044
+ */
1045
+ getState: function(container) {
1046
+ const inst = this.instances.get(container);
1047
+ if (!inst) return null;
1048
+ const s = inst.state;
1049
+ return {
1050
+ isPlaying: s.isPlaying,
1051
+ currentIndex: s.currentIndex,
1052
+ currentTrack: s.tracks[s.currentIndex] || null,
1053
+ volume: s.volume,
1054
+ shuffle: s.shuffle,
1055
+ repeat: s.repeat,
1056
+ tracks: s.tracks.slice(),
1057
+ isDetached: Boolean(s.isDetached),
1058
+ isMinimized: Boolean(s.isMinimized)
1059
+ };
1060
+ },
1061
+ /**
1062
+ * Stop playback, clean up listeners, remove instance.
1063
+ * @param {HTMLElement} container
1064
+ */
1065
+ destroy: function(container) {
1066
+ const inst = this.instances.get(container);
1067
+ if (!inst) return;
1068
+ this._unbindFloatingDrag(inst);
1069
+ if (inst.state && inst.state.isDetached) {
1070
+ try {
1071
+ this.attach(container);
1072
+ } catch {
1073
+ }
1074
+ }
1075
+ inst.cleanup.forEach((fn) => fn());
1076
+ this.instances.delete(container);
1077
+ container.removeAttribute("data-music-player-initialized");
1078
+ },
1079
+ /**
1080
+ * Destroy all instances.
1081
+ */
1082
+ destroyAll: function() {
1083
+ this.instances.forEach((_, container) => this.destroy(container));
1084
+ },
1085
+ /* ─── Internal helpers ────────────────────────────────── */
1086
+ /**
1087
+ * Load track by index on an already-initialised instance object.
1088
+ * @param {Object} inst
1089
+ * @param {number} index
1090
+ * @param {boolean} autoPlay
1091
+ */
1092
+ _loadTrack: function(inst, index, autoPlay) {
1093
+ const track = inst.state.tracks[index];
1094
+ if (!track) return;
1095
+ const container = this._containerOf(inst);
1096
+ inst.state.currentIndex = index;
1097
+ inst.audio.src = track.url;
1098
+ if (inst.refs.trackName) {
1099
+ inst.refs.trackName.textContent = track.name || "Unknown Track";
1100
+ inst.refs.trackName.classList.remove("is-idle");
1101
+ }
1102
+ if (inst.refs.playlistPanel) {
1103
+ inst.refs.playlistPanel.querySelectorAll(".vd-music-player-playlist-item").forEach((item, i) => {
1104
+ const active = i === index;
1105
+ item.classList.toggle("is-active", active);
1106
+ item.setAttribute("aria-current", active ? "true" : "false");
1107
+ });
1108
+ }
1109
+ if (inst.refs.progressBar) {
1110
+ inst.refs.progressBar.value = "0";
1111
+ updateRangeFill(inst.refs.progressBar);
1112
+ }
1113
+ if (inst.refs.timeElapsed) inst.refs.timeElapsed.textContent = "0:00";
1114
+ if (inst.refs.timeDuration) inst.refs.timeDuration.textContent = "0:00";
1115
+ if (container) {
1116
+ container.dispatchEvent(
1117
+ new CustomEvent("musicplayer:trackchange", {
1118
+ bubbles: true,
1119
+ detail: { index, name: track.name, url: track.url }
1120
+ })
1121
+ );
1122
+ }
1123
+ if (autoPlay) inst.audio.play().catch(() => {
1124
+ });
1125
+ },
1126
+ /**
1127
+ * Reverse-lookup the container element for a given instance object.
1128
+ * @param {Object} inst
1129
+ * @returns {HTMLElement|null}
1130
+ */
1131
+ _containerOf: function(inst) {
1132
+ for (const [container, i] of this.instances) {
1133
+ if (i === inst) return container;
1134
+ }
1135
+ return null;
1136
+ }
1137
+ };
1138
+ MusicPlayer.version = VD_MUSIC_PLAYER_VERSION;
1139
+
1140
+ // src/music-player/vue.js
1141
+ var PLAYER_EVENTS = [
1142
+ "play",
1143
+ "pause",
1144
+ "trackchange",
1145
+ "volumechange",
1146
+ "repeatchange",
1147
+ "ended",
1148
+ "detach",
1149
+ "attach",
1150
+ "minimize",
1151
+ "expand"
1152
+ ];
1153
+ var VdMusicPlayer = defineComponent({
1154
+ name: "VdMusicPlayer",
1155
+ props: {
1156
+ /** Playlist — `[{ name, url }]`. */
1157
+ tracks: { type: Array, default: () => [] },
1158
+ /** Player options (volume, shuffle, repeat, glass, detachable, …). */
1159
+ options: { type: Object, default: () => ({}) }
1160
+ },
1161
+ emits: [...PLAYER_EVENTS, "ready"],
1162
+ setup(props, { emit, expose }) {
1163
+ const el = ref(null);
1164
+ const bound = [];
1165
+ const create = () => {
1166
+ MusicPlayer.initPlayer(el.value, { tracks: props.tracks, ...props.options });
1167
+ PLAYER_EVENTS.forEach((name) => {
1168
+ const type = `musicplayer:${name}`;
1169
+ const handler = (e) => emit(name, e.detail);
1170
+ el.value.addEventListener(type, handler);
1171
+ bound.push([type, handler]);
1172
+ });
1173
+ emit("ready", el.value);
1174
+ };
1175
+ const teardown = () => {
1176
+ bound.forEach(([type, handler]) => {
1177
+ if (el.value) el.value.removeEventListener(type, handler);
1178
+ });
1179
+ bound.length = 0;
1180
+ if (el.value) MusicPlayer.destroy(el.value);
1181
+ };
1182
+ onMounted(() => {
1183
+ if (typeof window === "undefined" || !el.value) return;
1184
+ create();
1185
+ });
1186
+ watch(
1187
+ () => [props.tracks, props.options],
1188
+ () => {
1189
+ teardown();
1190
+ create();
1191
+ },
1192
+ { deep: true }
1193
+ );
1194
+ onBeforeUnmount(teardown);
1195
+ expose({ player: MusicPlayer, container: () => el.value });
1196
+ return () => h("div", { ref: el, class: "vd-music-player" });
1197
+ }
1198
+ });
1199
+ export {
1200
+ MusicPlayer,
1201
+ VD_MUSIC_PLAYER_VERSION,
1202
+ VdMusicPlayer
1203
+ };
1204
+ //# sourceMappingURL=index.js.map