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