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