@frameset/plex-player 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.
@@ -0,0 +1,879 @@
1
+ /*!
2
+ * @frameset/plex-player v1.0.0
3
+ * Professional video player with VAST ads, Chromecast, PiP, subtitles, playlists and more. Built by FRAMESET Studio.
4
+ * (c) 2026 FRAMESET Studio
5
+ * Released under the MIT License
6
+ * https://frameset.dev/plex-player
7
+ */
8
+ 'use strict';
9
+
10
+ Object.defineProperty(exports, '__esModule', { value: true });
11
+
12
+ /**
13
+ * @frameset/plex-player - Core Entry Point
14
+ * Professional Video Player by FRAMESET Studio
15
+ * https://frameset.dev
16
+ */
17
+
18
+ /**
19
+ * Utility Functions
20
+ */
21
+ const Utils = {
22
+ formatTime(seconds) {
23
+ if (isNaN(seconds) || !isFinite(seconds)) return '0:00';
24
+ seconds = Math.max(0, Math.floor(seconds));
25
+ const hours = Math.floor(seconds / 3600);
26
+ const minutes = Math.floor(seconds % 3600 / 60);
27
+ const secs = seconds % 60;
28
+ if (hours > 0) {
29
+ return `${hours}:${minutes.toString().padStart(2, '0')}:${secs.toString().padStart(2, '0')}`;
30
+ }
31
+ return `${minutes}:${secs.toString().padStart(2, '0')}`;
32
+ },
33
+ clamp(num, min, max) {
34
+ return Math.min(Math.max(num, min), max);
35
+ },
36
+ throttle(fn, delay) {
37
+ let lastCall = 0;
38
+ return function (...args) {
39
+ const now = Date.now();
40
+ if (now - lastCall >= delay) {
41
+ lastCall = now;
42
+ return fn.apply(this, args);
43
+ }
44
+ };
45
+ },
46
+ debounce(fn, delay) {
47
+ let timeoutId;
48
+ return function (...args) {
49
+ clearTimeout(timeoutId);
50
+ timeoutId = setTimeout(() => fn.apply(this, args), delay);
51
+ };
52
+ }
53
+ };
54
+
55
+ /**
56
+ * PlexPlayer - Main Player Class
57
+ * A complete, standalone video player
58
+ */
59
+ class PlexPlayer {
60
+ constructor(options = {}) {
61
+ // Validate container
62
+ if (!options.container) {
63
+ throw new Error('PlexPlayer: container option is required');
64
+ }
65
+
66
+ // Get container element
67
+ this.container = typeof options.container === 'string' ? document.querySelector(options.container) : options.container;
68
+ if (!this.container) {
69
+ throw new Error('PlexPlayer: container element not found');
70
+ }
71
+
72
+ // Store options with defaults
73
+ this.options = {
74
+ autoplay: false,
75
+ muted: false,
76
+ loop: false,
77
+ volume: 1,
78
+ poster: '',
79
+ preload: 'metadata',
80
+ keyboard: true,
81
+ touch: true,
82
+ pip: true,
83
+ cast: true,
84
+ fullscreen: true,
85
+ controlsHideDelay: 3000,
86
+ theme: {},
87
+ subtitles: {},
88
+ ads: {
89
+ enabled: false
90
+ },
91
+ i18n: {},
92
+ ...options
93
+ };
94
+
95
+ // Internal state
96
+ this._eventListeners = new Map();
97
+ this._state = {
98
+ playing: false,
99
+ paused: true,
100
+ muted: this.options.muted,
101
+ volume: this.options.volume,
102
+ currentTime: 0,
103
+ duration: 0,
104
+ buffered: 0,
105
+ fullscreen: false,
106
+ pip: false,
107
+ playbackRate: 1
108
+ };
109
+
110
+ // Playlist state
111
+ this._playlist = [];
112
+ this._currentIndex = 0;
113
+
114
+ // Controls timeout
115
+ this._controlsTimeout = null;
116
+
117
+ // Initialize player
118
+ this._init();
119
+ }
120
+ _init() {
121
+ // Add player class
122
+ this.container.classList.add('plex-player');
123
+
124
+ // Apply theme
125
+ this._applyTheme();
126
+
127
+ // Create DOM structure
128
+ this._createDOM();
129
+
130
+ // Bind events
131
+ this._bindEvents();
132
+
133
+ // Initialize features
134
+ if (this.options.keyboard) this._initKeyboard();
135
+ if (this.options.touch) this._initTouch();
136
+ if (this.options.pip) this._initPiP();
137
+ if (this.options.cast) this._initCast();
138
+
139
+ // Emit ready event
140
+ setTimeout(() => this._emit('ready'), 0);
141
+ }
142
+ _applyTheme() {
143
+ const {
144
+ theme
145
+ } = this.options;
146
+ if (theme.primary) this.container.style.setProperty('--plex-primary', theme.primary);
147
+ if (theme.background) this.container.style.setProperty('--plex-bg', theme.background);
148
+ if (theme.text) this.container.style.setProperty('--plex-text', theme.text);
149
+ if (theme.borderRadius) this.container.style.setProperty('--plex-border-radius', theme.borderRadius);
150
+ }
151
+ _createDOM() {
152
+ // Video element
153
+ this.video = document.createElement('video');
154
+ this.video.className = 'plex-video';
155
+ this.video.setAttribute('playsinline', '');
156
+ this.video.setAttribute('webkit-playsinline', '');
157
+ this.video.preload = this.options.preload;
158
+ this.video.muted = this.options.muted;
159
+ this.video.volume = this.options.volume;
160
+ this.video.loop = this.options.loop;
161
+ if (this.options.poster) this.video.poster = this.options.poster;
162
+ this.container.appendChild(this.video);
163
+
164
+ // Controls overlay
165
+ this.controls = document.createElement('div');
166
+ this.controls.className = 'plex-controls';
167
+ this.controls.innerHTML = this._getControlsHTML();
168
+ this.container.appendChild(this.controls);
169
+
170
+ // Cache control elements
171
+ this._cacheElements();
172
+
173
+ // Big play button
174
+ this.bigPlayBtn = document.createElement('div');
175
+ this.bigPlayBtn.className = 'plex-big-play';
176
+ this.bigPlayBtn.innerHTML = `<svg viewBox="0 0 24 24"><path d="M8 5v14l11-7z"/></svg>`;
177
+ this.container.appendChild(this.bigPlayBtn);
178
+
179
+ // Loading spinner
180
+ this.loader = document.createElement('div');
181
+ this.loader.className = 'plex-loader';
182
+ this.loader.innerHTML = '<div class="plex-spinner"></div>';
183
+ this.container.appendChild(this.loader);
184
+ }
185
+ _getControlsHTML() {
186
+ const {
187
+ i18n
188
+ } = this.options;
189
+ return `
190
+ <div class="plex-progress-container">
191
+ <div class="plex-progress-bar">
192
+ <div class="plex-progress-buffered"></div>
193
+ <div class="plex-progress-played"></div>
194
+ <div class="plex-progress-handle"></div>
195
+ </div>
196
+ <div class="plex-progress-preview">
197
+ <div class="plex-preview-time">0:00</div>
198
+ </div>
199
+ </div>
200
+ <div class="plex-controls-bar">
201
+ <div class="plex-controls-left">
202
+ <button class="plex-btn plex-play-btn" title="${i18n.play || 'Play'}">
203
+ <svg class="plex-icon-play" viewBox="0 0 24 24"><path d="M8 5v14l11-7z"/></svg>
204
+ <svg class="plex-icon-pause" viewBox="0 0 24 24"><path d="M6 19h4V5H6v14zm8-14v14h4V5h-4z"/></svg>
205
+ </button>
206
+ <button class="plex-btn plex-prev-btn" title="${i18n.previous || 'Previous'}">
207
+ <svg viewBox="0 0 24 24"><path d="M6 6h2v12H6zm3.5 6l8.5 6V6z"/></svg>
208
+ </button>
209
+ <button class="plex-btn plex-next-btn" title="${i18n.next || 'Next'}">
210
+ <svg viewBox="0 0 24 24"><path d="M6 18l8.5-6L6 6v12zM16 6v12h2V6h-2z"/></svg>
211
+ </button>
212
+ <div class="plex-volume-container">
213
+ <button class="plex-btn plex-volume-btn" title="${i18n.mute || 'Mute'}">
214
+ <svg class="plex-icon-volume-high" viewBox="0 0 24 24"><path d="M3 9v6h4l5 5V4L7 9H3zm13.5 3c0-1.77-1.02-3.29-2.5-4.03v8.05c1.48-.73 2.5-2.25 2.5-4.02zM14 3.23v2.06c2.89.86 5 3.54 5 6.71s-2.11 5.85-5 6.71v2.06c4.01-.91 7-4.49 7-8.77s-2.99-7.86-7-8.77z"/></svg>
215
+ <svg class="plex-icon-volume-low" viewBox="0 0 24 24"><path d="M18.5 12c0-1.77-1.02-3.29-2.5-4.03v8.05c1.48-.73 2.5-2.25 2.5-4.02zM5 9v6h4l5 5V4L9 9H5z"/></svg>
216
+ <svg class="plex-icon-volume-mute" viewBox="0 0 24 24"><path d="M16.5 12c0-1.77-1.02-3.29-2.5-4.03v2.21l2.45 2.45c.03-.2.05-.41.05-.63zm2.5 0c0 .94-.2 1.82-.54 2.64l1.51 1.51C20.63 14.91 21 13.5 21 12c0-4.28-2.99-7.86-7-8.77v2.06c2.89.86 5 3.54 5 6.71zM4.27 3L3 4.27 7.73 9H3v6h4l5 5v-6.73l4.25 4.25c-.67.52-1.42.93-2.25 1.18v2.06c1.38-.31 2.63-.95 3.69-1.81L19.73 21 21 19.73l-9-9L4.27 3zM12 4L9.91 6.09 12 8.18V4z"/></svg>
217
+ </button>
218
+ <div class="plex-volume-slider">
219
+ <div class="plex-volume-track">
220
+ <div class="plex-volume-level"></div>
221
+ <div class="plex-volume-handle"></div>
222
+ </div>
223
+ </div>
224
+ </div>
225
+ <div class="plex-time">
226
+ <span class="plex-time-current">0:00</span>
227
+ <span class="plex-time-separator">/</span>
228
+ <span class="plex-time-duration">0:00</span>
229
+ </div>
230
+ </div>
231
+ <div class="plex-controls-right">
232
+ <button class="plex-btn plex-settings-btn" title="${i18n.settings || 'Settings'}">
233
+ <svg viewBox="0 0 24 24"><path d="M19.14,12.94c0.04-0.3,0.06-0.61,0.06-0.94c0-0.32-0.02-0.64-0.07-0.94l2.03-1.58c0.18-0.14,0.23-0.41,0.12-0.61 l-1.92-3.32c-0.12-0.22-0.37-0.29-0.59-0.22l-2.39,0.96c-0.5-0.38-1.03-0.7-1.62-0.94L14.4,2.81c-0.04-0.24-0.24-0.41-0.48-0.41 h-3.84c-0.24,0-0.43,0.17-0.47,0.41L9.25,5.35C8.66,5.59,8.12,5.92,7.63,6.29L5.24,5.33c-0.22-0.08-0.47,0-0.59,0.22L2.74,8.87 C2.62,9.08,2.66,9.34,2.86,9.48l2.03,1.58C4.84,11.36,4.8,11.69,4.8,12s0.02,0.64,0.07,0.94l-2.03,1.58 c-0.18,0.14-0.23,0.41-0.12,0.61l1.92,3.32c0.12,0.22,0.37,0.29,0.59,0.22l2.39-0.96c0.5,0.38,1.03,0.7,1.62,0.94l0.36,2.54 c0.05,0.24,0.24,0.41,0.48,0.41h3.84c0.24,0,0.44-0.17,0.47-0.41l0.36-2.54c0.59-0.24,1.13-0.56,1.62-0.94l2.39,0.96 c0.22,0.08,0.47,0,0.59-0.22l1.92-3.32c0.12-0.22,0.07-0.47-0.12-0.61L19.14,12.94z M12,15.6c-1.98,0-3.6-1.62-3.6-3.6 s1.62-3.6,3.6-3.6s3.6,1.62,3.6,3.6S13.98,15.6,12,15.6z"/></svg>
234
+ </button>
235
+ ${this.options.pip ? `
236
+ <button class="plex-btn plex-pip-btn" title="${i18n.pip || 'Picture-in-Picture'}">
237
+ <svg viewBox="0 0 24 24"><path d="M19 7h-8v6h8V7zm2-4H3c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h18c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2zm0 16H3V5h18v14z"/></svg>
238
+ </button>
239
+ ` : ''}
240
+ ${this.options.cast ? `
241
+ <button class="plex-btn plex-cast-btn" title="${i18n.cast || 'Cast'}">
242
+ <svg viewBox="0 0 24 24"><path d="M21 3H3c-1.1 0-2 .9-2 2v3h2V5h18v14h-7v2h7c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2zM1 18v3h3c0-1.66-1.34-3-3-3zm0-4v2c2.76 0 5 2.24 5 5h2c0-3.87-3.13-7-7-7zm0-4v2c4.97 0 9 4.03 9 9h2c0-6.08-4.93-11-11-11z"/></svg>
243
+ </button>
244
+ ` : ''}
245
+ ${this.options.fullscreen ? `
246
+ <button class="plex-btn plex-fullscreen-btn" title="${i18n.fullscreen || 'Fullscreen'}">
247
+ <svg class="plex-icon-fullscreen" viewBox="0 0 24 24"><path d="M7 14H5v5h5v-2H7v-3zm-2-4h2V7h3V5H5v5zm12 7h-3v2h5v-5h-2v3zM14 5v2h3v3h2V5h-5z"/></svg>
248
+ <svg class="plex-icon-fullscreen-exit" viewBox="0 0 24 24"><path d="M5 16h3v3h2v-5H5v2zm3-8H5v2h5V5H8v3zm6 11h2v-3h3v-2h-5v5zm2-11V5h-2v5h5V8h-3z"/></svg>
249
+ </button>
250
+ ` : ''}
251
+ </div>
252
+ </div>
253
+ `;
254
+ }
255
+ _cacheElements() {
256
+ const $ = sel => this.controls.querySelector(sel);
257
+ this.els = {
258
+ progressContainer: $('.plex-progress-container'),
259
+ progressBar: $('.plex-progress-bar'),
260
+ progressBuffered: $('.plex-progress-buffered'),
261
+ progressPlayed: $('.plex-progress-played'),
262
+ progressHandle: $('.plex-progress-handle'),
263
+ progressPreview: $('.plex-progress-preview'),
264
+ previewTime: $('.plex-preview-time'),
265
+ playBtn: $('.plex-play-btn'),
266
+ prevBtn: $('.plex-prev-btn'),
267
+ nextBtn: $('.plex-next-btn'),
268
+ volumeBtn: $('.plex-volume-btn'),
269
+ volumeSlider: $('.plex-volume-slider'),
270
+ volumeTrack: $('.plex-volume-track'),
271
+ volumeLevel: $('.plex-volume-level'),
272
+ volumeHandle: $('.plex-volume-handle'),
273
+ timeCurrent: $('.plex-time-current'),
274
+ timeDuration: $('.plex-time-duration'),
275
+ settingsBtn: $('.plex-settings-btn'),
276
+ pipBtn: $('.plex-pip-btn'),
277
+ castBtn: $('.plex-cast-btn'),
278
+ fullscreenBtn: $('.plex-fullscreen-btn')
279
+ };
280
+ }
281
+ _bindEvents() {
282
+ // Video events
283
+ this.video.addEventListener('play', () => this._onPlay());
284
+ this.video.addEventListener('pause', () => this._onPause());
285
+ this.video.addEventListener('ended', () => this._onEnded());
286
+ this.video.addEventListener('timeupdate', () => this._onTimeUpdate());
287
+ this.video.addEventListener('progress', () => this._onProgress());
288
+ this.video.addEventListener('loadedmetadata', () => this._onLoadedMetadata());
289
+ this.video.addEventListener('volumechange', () => this._onVolumeChange());
290
+ this.video.addEventListener('waiting', () => this._onWaiting());
291
+ this.video.addEventListener('canplay', () => this._onCanPlay());
292
+ this.video.addEventListener('error', e => this._onError(e));
293
+
294
+ // Control events
295
+ this.bigPlayBtn.addEventListener('click', () => this.togglePlay());
296
+ this.video.addEventListener('click', () => this.togglePlay());
297
+ this.els.playBtn?.addEventListener('click', () => this.togglePlay());
298
+ this.els.prevBtn?.addEventListener('click', () => this.previous());
299
+ this.els.nextBtn?.addEventListener('click', () => this.next());
300
+ this.els.volumeBtn?.addEventListener('click', () => this.toggleMute());
301
+ this.els.pipBtn?.addEventListener('click', () => this.togglePiP());
302
+ this.els.fullscreenBtn?.addEventListener('click', () => this.toggleFullscreen());
303
+
304
+ // Progress bar
305
+ this._bindProgressEvents();
306
+
307
+ // Volume slider
308
+ this._bindVolumeEvents();
309
+
310
+ // Controls visibility
311
+ this._bindControlsVisibility();
312
+
313
+ // Fullscreen change
314
+ document.addEventListener('fullscreenchange', () => this._onFullscreenChange());
315
+ document.addEventListener('webkitfullscreenchange', () => this._onFullscreenChange());
316
+ }
317
+ _bindProgressEvents() {
318
+ const progress = this.els.progressContainer;
319
+ if (!progress) return;
320
+ let isDragging = false;
321
+ const seek = e => {
322
+ const rect = this.els.progressBar.getBoundingClientRect();
323
+ const percent = Utils.clamp((e.clientX - rect.left) / rect.width, 0, 1);
324
+ this.seekPercent(percent * 100);
325
+ };
326
+ const updatePreview = e => {
327
+ const rect = this.els.progressBar.getBoundingClientRect();
328
+ const percent = Utils.clamp((e.clientX - rect.left) / rect.width, 0, 1);
329
+ const time = percent * this.video.duration;
330
+ if (this.els.previewTime) {
331
+ this.els.previewTime.textContent = Utils.formatTime(time);
332
+ }
333
+ if (this.els.progressPreview) {
334
+ const left = Utils.clamp(e.clientX - rect.left, 30, rect.width - 30);
335
+ this.els.progressPreview.style.left = `${left}px`;
336
+ }
337
+ };
338
+ progress.addEventListener('mousedown', e => {
339
+ isDragging = true;
340
+ seek(e);
341
+ });
342
+ progress.addEventListener('mousemove', e => {
343
+ updatePreview(e);
344
+ if (isDragging) seek(e);
345
+ });
346
+ document.addEventListener('mouseup', () => {
347
+ isDragging = false;
348
+ });
349
+ document.addEventListener('mousemove', e => {
350
+ if (isDragging) seek(e);
351
+ });
352
+ }
353
+ _bindVolumeEvents() {
354
+ const volumeTrack = this.els.volumeTrack;
355
+ if (!volumeTrack) return;
356
+ let isDragging = false;
357
+ const setVolume = e => {
358
+ const rect = volumeTrack.getBoundingClientRect();
359
+ const percent = Utils.clamp((e.clientX - rect.left) / rect.width, 0, 1);
360
+ this.setVolume(percent);
361
+ };
362
+ volumeTrack.addEventListener('mousedown', e => {
363
+ isDragging = true;
364
+ setVolume(e);
365
+ });
366
+ document.addEventListener('mousemove', e => {
367
+ if (isDragging) setVolume(e);
368
+ });
369
+ document.addEventListener('mouseup', () => {
370
+ isDragging = false;
371
+ });
372
+ }
373
+ _bindControlsVisibility() {
374
+ const showControls = () => {
375
+ this.container.classList.add('plex-controls-visible');
376
+ clearTimeout(this._controlsTimeout);
377
+ if (this._state.playing) {
378
+ this._controlsTimeout = setTimeout(() => {
379
+ this.container.classList.remove('plex-controls-visible');
380
+ }, this.options.controlsHideDelay);
381
+ }
382
+ };
383
+ this.container.addEventListener('mousemove', showControls);
384
+ this.container.addEventListener('mouseenter', showControls);
385
+ this.container.addEventListener('mouseleave', () => {
386
+ if (this._state.playing) {
387
+ this.container.classList.remove('plex-controls-visible');
388
+ }
389
+ });
390
+
391
+ // Always show when paused
392
+ this.video.addEventListener('pause', showControls);
393
+ }
394
+
395
+ // Event handlers
396
+ _onPlay() {
397
+ this._state.playing = true;
398
+ this._state.paused = false;
399
+ this.container.classList.add('plex-playing');
400
+ this.container.classList.remove('plex-paused');
401
+ this.bigPlayBtn.style.display = 'none';
402
+ this._emit('play');
403
+ }
404
+ _onPause() {
405
+ this._state.playing = false;
406
+ this._state.paused = true;
407
+ this.container.classList.remove('plex-playing');
408
+ this.container.classList.add('plex-paused');
409
+ this.bigPlayBtn.style.display = '';
410
+ this._emit('pause');
411
+ }
412
+ _onEnded() {
413
+ this._state.playing = false;
414
+ this.container.classList.remove('plex-playing');
415
+
416
+ // Auto-play next if playlist
417
+ if (this._playlist.length > 0 && this._currentIndex < this._playlist.length - 1) {
418
+ this.next();
419
+ } else {
420
+ this._emit('ended');
421
+ }
422
+ }
423
+ _onTimeUpdate() {
424
+ this._state.currentTime = this.video.currentTime;
425
+ const percent = this.video.currentTime / this.video.duration * 100;
426
+ if (this.els.progressPlayed) {
427
+ this.els.progressPlayed.style.width = `${percent}%`;
428
+ }
429
+ if (this.els.progressHandle) {
430
+ this.els.progressHandle.style.left = `${percent}%`;
431
+ }
432
+ if (this.els.timeCurrent) {
433
+ this.els.timeCurrent.textContent = Utils.formatTime(this.video.currentTime);
434
+ }
435
+ this._emit('timeupdate', {
436
+ currentTime: this.video.currentTime,
437
+ duration: this.video.duration
438
+ });
439
+ }
440
+ _onProgress() {
441
+ const buffered = this.video.buffered;
442
+ if (buffered.length > 0) {
443
+ const percent = buffered.end(buffered.length - 1) / this.video.duration * 100;
444
+ this._state.buffered = percent;
445
+ if (this.els.progressBuffered) {
446
+ this.els.progressBuffered.style.width = `${percent}%`;
447
+ }
448
+ this._emit('progress', {
449
+ buffered: percent
450
+ });
451
+ }
452
+ }
453
+ _onLoadedMetadata() {
454
+ this._state.duration = this.video.duration;
455
+ if (this.els.timeDuration) {
456
+ this.els.timeDuration.textContent = Utils.formatTime(this.video.duration);
457
+ }
458
+ this._emit('loadedmetadata', {
459
+ duration: this.video.duration
460
+ });
461
+ }
462
+ _onVolumeChange() {
463
+ this._state.volume = this.video.volume;
464
+ this._state.muted = this.video.muted;
465
+ const volume = this.video.muted ? 0 : this.video.volume;
466
+
467
+ // Update volume UI
468
+ if (this.els.volumeLevel) {
469
+ this.els.volumeLevel.style.width = `${volume * 100}%`;
470
+ }
471
+ if (this.els.volumeHandle) {
472
+ this.els.volumeHandle.style.left = `${volume * 100}%`;
473
+ }
474
+
475
+ // Update icon
476
+ this.container.classList.remove('plex-muted', 'plex-volume-low');
477
+ if (this.video.muted || volume === 0) {
478
+ this.container.classList.add('plex-muted');
479
+ } else if (volume < 0.5) {
480
+ this.container.classList.add('plex-volume-low');
481
+ }
482
+ this._emit('volumechange', {
483
+ volume: this.video.volume,
484
+ muted: this.video.muted
485
+ });
486
+ }
487
+ _onWaiting() {
488
+ this.container.classList.add('plex-loading');
489
+ this._emit('waiting');
490
+ }
491
+ _onCanPlay() {
492
+ this.container.classList.remove('plex-loading');
493
+ this._emit('canplay');
494
+ }
495
+ _onError(e) {
496
+ this._emit('error', {
497
+ code: this.video.error?.code,
498
+ message: this.video.error?.message
499
+ });
500
+ }
501
+ _onFullscreenChange() {
502
+ const isFullscreen = !!(document.fullscreenElement || document.webkitFullscreenElement);
503
+ this._state.fullscreen = isFullscreen;
504
+ if (isFullscreen) {
505
+ this.container.classList.add('plex-fullscreen');
506
+ } else {
507
+ this.container.classList.remove('plex-fullscreen');
508
+ }
509
+ this._emit('fullscreenchange', {
510
+ isFullscreen
511
+ });
512
+ }
513
+
514
+ // Keyboard support
515
+ _initKeyboard() {
516
+ this._keyHandler = e => {
517
+ if (!this.container.contains(document.activeElement) && document.activeElement !== document.body) return;
518
+ switch (e.key) {
519
+ case ' ':
520
+ case 'k':
521
+ e.preventDefault();
522
+ this.togglePlay();
523
+ break;
524
+ case 'ArrowLeft':
525
+ e.preventDefault();
526
+ this.seek(this.video.currentTime - 10);
527
+ break;
528
+ case 'ArrowRight':
529
+ e.preventDefault();
530
+ this.seek(this.video.currentTime + 10);
531
+ break;
532
+ case 'ArrowUp':
533
+ e.preventDefault();
534
+ this.setVolume(Math.min(1, this.video.volume + 0.1));
535
+ break;
536
+ case 'ArrowDown':
537
+ e.preventDefault();
538
+ this.setVolume(Math.max(0, this.video.volume - 0.1));
539
+ break;
540
+ case 'm':
541
+ this.toggleMute();
542
+ break;
543
+ case 'f':
544
+ this.toggleFullscreen();
545
+ break;
546
+ case 'p':
547
+ this.togglePiP();
548
+ break;
549
+ case 'n':
550
+ if (e.shiftKey) {
551
+ this.previous();
552
+ } else {
553
+ this.next();
554
+ }
555
+ break;
556
+ }
557
+ };
558
+ document.addEventListener('keydown', this._keyHandler);
559
+ }
560
+
561
+ // Touch support
562
+ _initTouch() {
563
+ let touchStartX = 0;
564
+ let touchStartY = 0;
565
+ let touchStartTime = 0;
566
+ this.video.addEventListener('touchstart', e => {
567
+ touchStartX = e.touches[0].clientX;
568
+ touchStartY = e.touches[0].clientY;
569
+ touchStartTime = Date.now();
570
+ });
571
+ this.video.addEventListener('touchend', e => {
572
+ const touchEndX = e.changedTouches[0].clientX;
573
+ const touchEndY = e.changedTouches[0].clientY;
574
+ const touchDuration = Date.now() - touchStartTime;
575
+ const deltaX = touchEndX - touchStartX;
576
+ const deltaY = touchEndY - touchStartY;
577
+
578
+ // Tap to toggle play (if quick tap)
579
+ if (Math.abs(deltaX) < 30 && Math.abs(deltaY) < 30 && touchDuration < 300) {
580
+ this.togglePlay();
581
+ }
582
+ // Swipe to seek
583
+ else if (Math.abs(deltaX) > 50 && Math.abs(deltaY) < 30) {
584
+ if (deltaX > 0) {
585
+ this.seek(this.video.currentTime + 10);
586
+ } else {
587
+ this.seek(this.video.currentTime - 10);
588
+ }
589
+ }
590
+ });
591
+ }
592
+
593
+ // PiP support
594
+ _initPiP() {
595
+ if (!document.pictureInPictureEnabled) {
596
+ if (this.els.pipBtn) {
597
+ this.els.pipBtn.style.display = 'none';
598
+ }
599
+ return;
600
+ }
601
+ this.video.addEventListener('enterpictureinpicture', () => {
602
+ this._state.pip = true;
603
+ this.container.classList.add('plex-pip');
604
+ this._emit('enterpip');
605
+ });
606
+ this.video.addEventListener('leavepictureinpicture', () => {
607
+ this._state.pip = false;
608
+ this.container.classList.remove('plex-pip');
609
+ this._emit('leavepip');
610
+ });
611
+ }
612
+
613
+ // Cast support
614
+ _initCast() {
615
+ // Chromecast requires HTTPS and specific browser
616
+ const isChrome = /Chrome/.test(navigator.userAgent) && !/Edge/.test(navigator.userAgent);
617
+ const isHTTPS = location.protocol === 'https:' || location.hostname === 'localhost';
618
+ if (!isChrome || !isHTTPS) {
619
+ if (this.els.castBtn) {
620
+ this.els.castBtn.style.display = 'none';
621
+ }
622
+ return;
623
+ }
624
+
625
+ // Load Cast SDK if available
626
+ if (window.chrome && window.chrome.cast) {
627
+ this._initCastApi();
628
+ } else {
629
+ window['__onGCastApiAvailable'] = isAvailable => {
630
+ if (isAvailable) this._initCastApi();
631
+ };
632
+ }
633
+ }
634
+ _initCastApi() {
635
+ if (this.els.castBtn) {
636
+ this.els.castBtn.addEventListener('click', () => {
637
+ if (window.cast && window.cast.framework) {
638
+ const context = cast.framework.CastContext.getInstance();
639
+ context.requestSession();
640
+ }
641
+ });
642
+ }
643
+ }
644
+
645
+ // Event system
646
+ on(event, callback) {
647
+ if (!this._eventListeners.has(event)) {
648
+ this._eventListeners.set(event, new Set());
649
+ }
650
+ this._eventListeners.get(event).add(callback);
651
+ return this;
652
+ }
653
+ off(event, callback) {
654
+ if (this._eventListeners.has(event)) {
655
+ this._eventListeners.get(event).delete(callback);
656
+ }
657
+ return this;
658
+ }
659
+ _emit(event, data = {}) {
660
+ if (this._eventListeners.has(event)) {
661
+ this._eventListeners.get(event).forEach(callback => {
662
+ try {
663
+ callback(data);
664
+ } catch (e) {
665
+ console.error(`PlexPlayer: Error in ${event} handler:`, e);
666
+ }
667
+ });
668
+ }
669
+ }
670
+
671
+ // Public API
672
+ load(src, poster) {
673
+ this.video.src = src;
674
+ if (poster) this.video.poster = poster;
675
+ this.video.load();
676
+ if (this.options.autoplay) {
677
+ this.play();
678
+ }
679
+ return this;
680
+ }
681
+ loadPlaylist(items) {
682
+ this._playlist = items.map((item, index) => ({
683
+ ...item,
684
+ index
685
+ }));
686
+ this._currentIndex = 0;
687
+ if (this._playlist.length > 0) {
688
+ const first = this._playlist[0];
689
+ this.load(first.src, first.poster);
690
+ this._emit('playlistload', {
691
+ playlist: this._playlist
692
+ });
693
+ }
694
+ return this;
695
+ }
696
+ play() {
697
+ return this.video.play();
698
+ }
699
+ pause() {
700
+ this.video.pause();
701
+ return this;
702
+ }
703
+ togglePlay() {
704
+ if (this.video.paused) {
705
+ this.play();
706
+ } else {
707
+ this.pause();
708
+ }
709
+ return this;
710
+ }
711
+ seek(time) {
712
+ this.video.currentTime = Utils.clamp(time, 0, this.video.duration || 0);
713
+ return this;
714
+ }
715
+ seekPercent(percent) {
716
+ const time = percent / 100 * this.video.duration;
717
+ return this.seek(time);
718
+ }
719
+ setVolume(level) {
720
+ this.video.volume = Utils.clamp(level, 0, 1);
721
+ if (this.video.muted && level > 0) {
722
+ this.video.muted = false;
723
+ }
724
+ return this;
725
+ }
726
+ getVolume() {
727
+ return this.video.volume;
728
+ }
729
+ mute() {
730
+ this.video.muted = true;
731
+ return this;
732
+ }
733
+ unmute() {
734
+ this.video.muted = false;
735
+ return this;
736
+ }
737
+ toggleMute() {
738
+ this.video.muted = !this.video.muted;
739
+ return this;
740
+ }
741
+ setPlaybackRate(rate) {
742
+ this.video.playbackRate = rate;
743
+ this._state.playbackRate = rate;
744
+ this._emit('ratechange', {
745
+ rate
746
+ });
747
+ return this;
748
+ }
749
+ getPlaybackRate() {
750
+ return this.video.playbackRate;
751
+ }
752
+ enterFullscreen() {
753
+ const el = this.container;
754
+ if (el.requestFullscreen) {
755
+ return el.requestFullscreen();
756
+ } else if (el.webkitRequestFullscreen) {
757
+ return el.webkitRequestFullscreen();
758
+ }
759
+ }
760
+ exitFullscreen() {
761
+ if (document.exitFullscreen) {
762
+ return document.exitFullscreen();
763
+ } else if (document.webkitExitFullscreen) {
764
+ return document.webkitExitFullscreen();
765
+ }
766
+ }
767
+ toggleFullscreen() {
768
+ if (this._state.fullscreen) {
769
+ this.exitFullscreen();
770
+ } else {
771
+ this.enterFullscreen();
772
+ }
773
+ return this;
774
+ }
775
+ enterPiP() {
776
+ if (document.pictureInPictureEnabled && this.video !== document.pictureInPictureElement) {
777
+ return this.video.requestPictureInPicture();
778
+ }
779
+ }
780
+ exitPiP() {
781
+ if (document.pictureInPictureElement) {
782
+ return document.exitPictureInPicture();
783
+ }
784
+ }
785
+ togglePiP() {
786
+ if (this._state.pip) {
787
+ this.exitPiP();
788
+ } else {
789
+ this.enterPiP();
790
+ }
791
+ return this;
792
+ }
793
+ next() {
794
+ if (this._playlist.length > 0 && this._currentIndex < this._playlist.length - 1) {
795
+ this._currentIndex++;
796
+ const item = this._playlist[this._currentIndex];
797
+ this.load(item.src, item.poster);
798
+ this.play();
799
+ this._emit('trackchange', {
800
+ index: this._currentIndex,
801
+ item
802
+ });
803
+ }
804
+ return this;
805
+ }
806
+ previous() {
807
+ if (this._playlist.length > 0 && this._currentIndex > 0) {
808
+ this._currentIndex--;
809
+ const item = this._playlist[this._currentIndex];
810
+ this.load(item.src, item.poster);
811
+ this.play();
812
+ this._emit('trackchange', {
813
+ index: this._currentIndex,
814
+ item
815
+ });
816
+ }
817
+ return this;
818
+ }
819
+ playAt(index) {
820
+ if (this._playlist.length > 0 && index >= 0 && index < this._playlist.length) {
821
+ this._currentIndex = index;
822
+ const item = this._playlist[this._currentIndex];
823
+ this.load(item.src, item.poster);
824
+ this.play();
825
+ this._emit('trackchange', {
826
+ index: this._currentIndex,
827
+ item
828
+ });
829
+ }
830
+ return this;
831
+ }
832
+ getState() {
833
+ return {
834
+ currentTime: this.video.currentTime,
835
+ duration: this.video.duration || 0,
836
+ volume: this.video.volume,
837
+ muted: this.video.muted,
838
+ isPlaying: !this.video.paused && !this.video.ended,
839
+ isPaused: this.video.paused,
840
+ isFullscreen: this._state.fullscreen,
841
+ isPiP: this._state.pip,
842
+ playbackRate: this.video.playbackRate,
843
+ currentTrack: this._currentIndex,
844
+ totalTracks: this._playlist.length
845
+ };
846
+ }
847
+ getVideo() {
848
+ return this.video;
849
+ }
850
+ destroy() {
851
+ // Clear timeouts
852
+ clearTimeout(this._controlsTimeout);
853
+
854
+ // Remove keyboard listener
855
+ if (this._keyHandler) {
856
+ document.removeEventListener('keydown', this._keyHandler);
857
+ }
858
+
859
+ // Remove event listeners
860
+ this._eventListeners.clear();
861
+
862
+ // Clear container
863
+ this.container.innerHTML = '';
864
+ this.container.classList.remove('plex-player');
865
+
866
+ // Emit destroy event
867
+ this._emit('destroy');
868
+ }
869
+ }
870
+
871
+ // Attach to window for UMD builds
872
+ if (typeof window !== 'undefined') {
873
+ window.PlexPlayer = PlexPlayer;
874
+ }
875
+
876
+ exports.PlexPlayer = PlexPlayer;
877
+ exports.Utils = Utils;
878
+ exports.default = PlexPlayer;
879
+ //# sourceMappingURL=plex-player.cjs.js.map