aboutsummaryrefslogtreecommitdiffstats
path: root/src/js/listeners.js
blob: 5fe20695b69a1fe49e3d2b3bd5853062d2f1edab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
// ==========================================================================
// Plyr Event Listeners
// ==========================================================================

import controls from './controls';
import ui from './ui';
import { repaint } from './utils/animation';
import browser from './utils/browser';
import { getElement, getElements, hasClass, matches, toggleClass, toggleHidden } from './utils/elements';
import { on, once, toggleListener, triggerEvent } from './utils/events';
import is from './utils/is';

class Listeners {
    constructor(player) {
        this.player = player;
        this.lastKey = null;
        this.focusTimer = null;
        this.lastKeyDown = null;

        this.handleKey = this.handleKey.bind(this);
        this.toggleMenu = this.toggleMenu.bind(this);
        this.setTabFocus = this.setTabFocus.bind(this);
        this.firstTouch = this.firstTouch.bind(this);
    }

    // Handle key presses
    handleKey(event) {
        const { player } = this;
        const { elements } = player;
        const code = event.keyCode ? event.keyCode : event.which;
        const pressed = event.type === 'keydown';
        const repeat = pressed && code === this.lastKey;

        // Bail if a modifier key is set
        if (event.altKey || event.ctrlKey || event.metaKey || event.shiftKey) {
            return;
        }

        // If the event is bubbled from the media element
        // Firefox doesn't get the keycode for whatever reason
        if (!is.number(code)) {
            return;
        }

        // Seek by the number keys
        const seekByKey = () => {
            // Divide the max duration into 10th's and times by the number value
            player.currentTime = player.duration / 10 * (code - 48);
        };

        // Handle the key on keydown
        // Reset on keyup
        if (pressed) {
            // Check focused element
            // and if the focused element is not editable (e.g. text input)
            // and any that accept key input http://webaim.org/techniques/keyboard/
            const focused = document.activeElement;
            if (is.element(focused)) {
                const { editable } = player.config.selectors;
                const { seek } = elements.inputs;

                if (focused !== seek && matches(focused, editable)) {
                    return;
                }

                if (event.which === 32 && matches(focused, 'button, [role^="menuitem"]')) {
                    return;
                }
            }

            // Which keycodes should we prevent default
            const preventDefault = [32, 37, 38, 39, 40, 48, 49, 50, 51, 52, 53, 54, 56, 57, 67, 70, 73, 75, 76, 77, 79];

            // If the code is found prevent default (e.g. prevent scrolling for arrows)
            if (preventDefault.includes(code)) {
                event.preventDefault();
                event.stopPropagation();
            }

            switch (code) {
                case 48:
                case 49:
                case 50:
                case 51:
                case 52:
                case 53:
                case 54:
                case 55:
                case 56:
                case 57:
                    // 0-9
                    if (!repeat) {
                        seekByKey();
                    }
                    break;

                case 32:
                case 75:
                    // Space and K key
                    if (!repeat) {
                        player.togglePlay();
                    }
                    break;

                case 38:
                    // Arrow up
                    player.increaseVolume(0.1);
                    break;

                case 40:
                    // Arrow down
                    player.decreaseVolume(0.1);
                    break;

                case 77:
                    // M key
                    if (!repeat) {
                        player.muted = !player.muted;
                    }
                    break;

                case 39:
                    // Arrow forward
                    player.forward();
                    break;

                case 37:
                    // Arrow back
                    player.rewind();
                    break;

                case 70:
                    // F key
                    player.fullscreen.toggle();
                    break;

                case 67:
                    // C key
                    if (!repeat) {
                        player.toggleCaptions();
                    }
                    break;

                case 76:
                    // L key
                    player.loop = !player.loop;
                    break;

                /* case 73:
                    this.setLoop('start');
                    break;

                case 76:
                    this.setLoop();
                    break;

                case 79:
                    this.setLoop('end');
                    break; */

                default:
                    break;
            }

            // Escape is handle natively when in full screen
            // So we only need to worry about non native
            if (!player.fullscreen.enabled && player.fullscreen.active && code === 27) {
                player.fullscreen.toggle();
            }

            // Store last code for next cycle
            this.lastKey = code;
        } else {
            this.lastKey = null;
        }
    }

    // Toggle menu
    toggleMenu(event) {
        controls.toggleMenu.call(this.player, event);
    }

    // Device is touch enabled
    firstTouch() {
        const { player } = this;
        const { elements } = player;

        player.touch = true;

        // Add touch class
        toggleClass(elements.container, player.config.classNames.isTouch, true);
    }

    setTabFocus(event) {
        const { player } = this;
        const { elements } = player;

        clearTimeout(this.focusTimer);

        // Ignore any key other than tab
        if (event.type === 'keydown' && event.which !== 9) {
            return;
        }

        // Store reference to event timeStamp
        if (event.type === 'keydown') {
            this.lastKeyDown = event.timeStamp;
        }

        // Remove current classes
        const removeCurrent = () => {
            const className = player.config.classNames.tabFocus;
            const current = getElements.call(player, `.${className}`);
            toggleClass(current, className, false);
        };

        // Determine if a key was pressed to trigger this event
        const wasKeyDown = event.timeStamp - this.lastKeyDown <= 20;

        // Ignore focus events if a key was pressed prior
        if (event.type === 'focus' && !wasKeyDown) {
            return;
        }

        // Remove all current
        removeCurrent();

        // Delay the adding of classname until the focus has changed
        // This event fires before the focusin event
        this.focusTimer = setTimeout(() => {
            const focused = document.activeElement;

            // Ignore if current focus element isn't inside the player
            if (!elements.container.contains(focused)) {
                return;
            }

            toggleClass(document.activeElement, player.config.classNames.tabFocus, true);
        }, 10);
    }

    // Global window & document listeners
    global(toggle = true) {
        const { player } = this;

        // Keyboard shortcuts
        if (player.config.keyboard.global) {
            toggleListener.call(player, window, 'keydown keyup', this.handleKey, toggle, false);
        }

        // Click anywhere closes menu
        toggleListener.call(player, document.body, 'click', this.toggleMenu, toggle);

        // Detect touch by events
        once.call(player, document.body, 'touchstart', this.firstTouch);

        // Tab focus detection
        toggleListener.call(player, document.body, 'keydown focus blur', this.setTabFocus, toggle, false, true);
    }

    // Container listeners
    container() {
        const { player } = this;
        const { elements } = player;

        // Keyboard shortcuts
        if (!player.config.keyboard.global && player.config.keyboard.focused) {
            on.call(player, elements.container, 'keydown keyup', this.handleKey, false);
        }

        // Toggle controls on mouse events and entering fullscreen
        on.call(
            player,
            elements.container,
            'mousemove mouseleave touchstart touchmove enterfullscreen exitfullscreen',
            event => {
                const { controls } = elements;

                // Remove button states for fullscreen
                if (controls && event.type === 'enterfullscreen') {
                    controls.pressed = false;
                    controls.hover = false;
                }

                // Show, then hide after a timeout unless another control event occurs
                const show = ['touchstart', 'touchmove', 'mousemove'].includes(event.type);

                let delay = 0;

                if (show) {
                    ui.toggleControls.call(player, true);
                    // Use longer timeout for touch devices
                    delay = player.touch ? 3000 : 2000;
                }

                // Clear timer
                clearTimeout(player.timers.controls);

                // Set new timer to prevent flicker when seeking
                player.timers.controls = setTimeout(() => ui.toggleControls.call(player, false), delay);
            },
        );
    }

    // Listen for media events
    media() {
        const { player } = this;
        const { elements } = player;

        // Time change on media
        on.call(player, player.media, 'timeupdate seeking seeked', event => controls.timeUpdate.call(player, event));

        // Display duration
        on.call(player, player.media, 'durationchange loadeddata loadedmetadata', event =>
            controls.durationUpdate.call(player, event),
        );

        // Check for audio tracks on load
        // We can't use `loadedmetadata` as it doesn't seem to have audio tracks at that point
        on.call(player, player.media, 'canplay', () => {
            toggleHidden(elements.volume, !player.hasAudio);
            toggleHidden(elements.buttons.mute, !player.hasAudio);
        });

        // Handle the media finishing
        on.call(player, player.media, 'ended', () => {
            // Show poster on end
            if (player.isHTML5 && player.isVideo && player.config.resetOnEnd) {
                // Restart
                player.restart();
            }
        });

        // Check for buffer progress
        on.call(player, player.media, 'progress playing seeking seeked', event =>
            controls.updateProgress.call(player, event),
        );

        // Handle volume changes
        on.call(player, player.media, 'volumechange', event => controls.updateVolume.call(player, event));

        // Handle play/pause
        on.call(player, player.media, 'playing play pause ended emptied timeupdate', event =>
            ui.checkPlaying.call(player, event),
        );

        // Loading state
        on.call(player, player.media, 'waiting canplay seeked playing', event => ui.checkLoading.call(player, event));

        // If autoplay, then load advertisement if required
        // TODO: Show some sort of loading state while the ad manager loads else there's a delay before ad shows
        on.call(player, player.media, 'playing', () => {
            if (!player.ads) {
                return;
            }

            // If ads are enabled, wait for them first
            if (player.ads.enabled && !player.ads.initialized) {
                // Wait for manager response
                player.ads.managerPromise.then(() => player.ads.play()).catch(() => player.play());
            }
        });

        // Click video
        if (player.supported.ui && player.config.clickToPlay && !player.isAudio) {
            // Re-fetch the wrapper
            const wrapper = getElement.call(player, `.${player.config.classNames.video}`);

            // Bail if there's no wrapper (this should never happen)
            if (!is.element(wrapper)) {
                return;
            }

            // On click play, pause ore restart
            on.call(player, elements.container, 'click touchstart', event => {
                const targets = [elements.container, wrapper];

                // Ignore if click if not container or in video wrapper
                if (!targets.includes(event.target) && !wrapper.contains(event.target)) {
                    return;
                }

                // First touch on touch devices will just show controls (if we're hiding controls)
                // If controls are shown then it'll toggle like a pointer device
                if (
                    player.config.hideControls &&
                    player.touch &&
                    hasClass(elements.container, player.config.classNames.hideControls)
                ) {
                    return;
                }

                if (player.ended) {
                    player.restart();
                    player.play();
                } else {
                    player.togglePlay();
                }
            });
        }

        // Disable right click
        if (player.supported.ui && player.config.disableContextMenu) {
            on.call(
                player,
                elements.wrapper,
                'contextmenu',
                event => {
                    event.preventDefault();
                },
                false,
            );
        }

        // Volume change
        on.call(player, player.media, 'volumechange', () => {
            // Save to storage
            player.storage.set({
                volume: player.volume,
                muted: player.muted,
            });
        });

        // Speed change
        on.call(player, player.media, 'ratechange', () => {
            // Update UI
            controls.updateSetting.call(player, 'speed');

            // Save to storage
            player.storage.set({ speed: player.speed });
        });

        // Quality change
        on.call(player, player.media, 'qualitychange', event => {
            // Update UI
            controls.updateSetting.call(player, 'quality', null, event.detail.quality);
        });

        // Proxy events to container
        // Bubble up key events for Edge
        const proxyEvents = player.config.events.concat(['keyup', 'keydown']).join(' ');

        on.call(player, player.media, proxyEvents, event => {
            let { detail = {} } = event;

            // Get error details from media
            if (event.type === 'error') {
                detail = player.media.error;
            }

            triggerEvent.call(player, elements.container, event.type, true, detail);
        });
    }

    // Run default and custom handlers
    proxy(event, defaultHandler, customHandlerKey) {
        const { player } = this;
        const customHandler = player.config.listeners[customHandlerKey];
        const hasCustomHandler = is.function(customHandler);
        let returned = true;

        // Execute custom handler
        if (hasCustomHandler) {
            returned = customHandler.call(player, event);
        }

        // Only call default handler if not prevented in custom handler
        if (returned && is.function(defaultHandler)) {
            defaultHandler.call(player, event);
        }
    }

    // Trigger custom and default handlers
    bind(element, type, defaultHandler, customHandlerKey, passive = true) {
        const { player } = this;
        const customHandler = player.config.listeners[customHandlerKey];
        const hasCustomHandler = is.function(customHandler);

        on.call(
            player,
            element,
            type,
            event => this.proxy(event, defaultHandler, customHandlerKey),
            passive && !hasCustomHandler,
        );
    }

    // Listen for control events
    controls() {
        const { player } = this;
        const { elements } = player;

        // IE doesn't support input event, so we fallback to change
        const inputEvent = browser.isIE ? 'change' : 'input';

        // Play/pause toggle
        if (elements.buttons.play) {
            Array.from(elements.buttons.play).forEach(button => {
                this.bind(button, 'click', player.togglePlay, 'play');
            });
        }

        // Pause
        this.bind(elements.buttons.restart, 'click', player.restart, 'restart');

        // Rewind
        this.bind(elements.buttons.rewind, 'click', player.rewind, 'rewind');

        // Rewind
        this.bind(elements.buttons.fastForward, 'click', player.forward, 'fastForward');

        // Mute toggle
        this.bind(
            elements.buttons.mute,
            'click',
            () => {
                player.muted = !player.muted;
            },
            'mute',
        );

        // Captions toggle
        this.bind(elements.buttons.captions, 'click', () => player.toggleCaptions());

        // Fullscreen toggle
        this.bind(
            elements.buttons.fullscreen,
            'click',
            () => {
                player.fullscreen.toggle();
            },
            'fullscreen',
        );

        // Picture-in-Picture
        this.bind(
            elements.buttons.pip,
            'click',
            () => {
                player.pip = 'toggle';
            },
            'pip',
        );

        // Airplay
        this.bind(elements.buttons.airplay, 'click', player.airplay, 'airplay');

        // Settings menu - click toggle
        this.bind(elements.buttons.settings, 'click', event => {
            // Prevent the document click listener closing the menu
            event.stopPropagation();

            controls.toggleMenu.call(player, event);
        });

        // Settings menu - keyboard toggle
        // We have to bind to keyup otherwise Firefox triggers a click when a keydown event handler shifts focus
        // https://bugzilla.mozilla.org/show_bug.cgi?id=1220143
        this.bind(
            elements.buttons.settings,
            'keyup',
            event => {
                const code = event.which;

                // We only care about space and return
                if (![13, 32].includes(code)) {
                    return;
                }

                // Because return triggers a click anyway, all we need to do is set focus
                if (code === 13) {
                    controls.focusFirstMenuItem.call(player, null, true);
                    return;
                }

                // Prevent scroll
                event.preventDefault();

                // Prevent playing video (Firefox)
                event.stopPropagation();

                // Toggle menu
                controls.toggleMenu.call(player, event);
            },
            null,
            false, // Can't be passive as we're preventing default
        );

        // Escape closes menu
        this.bind(elements.settings.menu, 'keydown', event => {
            if (event.which === 27) {
                controls.toggleMenu.call(player, event);
            }
        });

        // Set range input alternative "value", which matches the tooltip time (#954)
        this.bind(elements.inputs.seek, 'mousedown mousemove', event => {
            const rect = elements.progress.getBoundingClientRect();
            const percent = 100 / rect.width * (event.pageX - rect.left);
            event.currentTarget.setAttribute('seek-value', percent);
        });

        // Pause while seeking
        this.bind(elements.inputs.seek, 'mousedown mouseup keydown keyup touchstart touchend', event => {
            const seek = event.currentTarget;
            const code = event.keyCode ? event.keyCode : event.which;
            const attribute = 'play-on-seeked';

            if (is.keyboardEvent(event) && (code !== 39 && code !== 37)) {
                return;
            }

            // Was playing before?
            const play = seek.hasAttribute(attribute);

            // Done seeking
            const done = ['mouseup', 'touchend', 'keyup'].includes(event.type);

            // If we're done seeking and it was playing, resume playback
            if (play && done) {
                seek.removeAttribute(attribute);
                player.play();
            } else if (!done && player.playing) {
                seek.setAttribute(attribute, '');
                player.pause();
            }
        });

        // Fix range inputs on iOS
        // Super weird iOS bug where after you interact with an <input type="range">,
        // it takes over further interactions on the page. This is a hack
        if (browser.isIos) {
            const inputs = getElements.call(player, 'input[type="range"]');
            Array.from(inputs).forEach(input => this.bind(input, inputEvent, event => repaint(event.target)));
        }

        // Seek
        this.bind(
            elements.inputs.seek,
            inputEvent,
            event => {
                const seek = event.currentTarget;

                // If it exists, use seek-value instead of "value" for consistency with tooltip time (#954)
                let seekTo = seek.getAttribute('seek-value');

                if (is.empty(seekTo)) {
                    seekTo = seek.value;
                }

                seek.removeAttribute('seek-value');

                player.currentTime = seekTo / seek.max * player.duration;
            },
            'seek',
        );

        // Seek tooltip
        this.bind(elements.progress, 'mouseenter mouseleave mousemove', event =>
            controls.updateSeekTooltip.call(player, event),
        );

        // Polyfill for lower fill in <input type="range"> for webkit
        if (browser.isWebkit) {
            Array.from(getElements.call(player, 'input[type="range"]')).forEach(element => {
                this.bind(element, 'input', event => controls.updateRangeFill.call(player, event.target));
            });
        }

        // Current time invert
        // Only if one time element is used for both currentTime and duration
        if (player.config.toggleInvert && !is.element(elements.display.duration)) {
            this.bind(elements.display.currentTime, 'click', () => {
                // Do nothing if we're at the start
                if (player.currentTime === 0) {
                    return;
                }

                player.config.invertTime = !player.config.invertTime;

                controls.timeUpdate.call(player);
            });
        }

        // Volume
        this.bind(
            elements.inputs.volume,
            inputEvent,
            event => {
                player.volume = event.target.value;
            },
            'volume',
        );

        // Update controls.hover state (used for ui.toggleControls to avoid hiding when interacting)
        this.bind(elements.controls, 'mouseenter mouseleave', event => {
            elements.controls.hover = !player.touch && event.type === 'mouseenter';
        });

        // Update controls.pressed state (used for ui.toggleControls to avoid hiding when interacting)
        this.bind(elements.controls, 'mousedown mouseup touchstart touchend touchcancel', event => {
            elements.controls.pressed = ['mousedown', 'touchstart'].includes(event.type);
        });

        // Focus in/out on controls
        this.bind(elements.controls, 'focusin focusout', event => {
            const { config, elements, timers } = player;
            const isFocusIn = event.type === 'focusin';

            // Skip transition to prevent focus from scrolling the parent element
            toggleClass(elements.controls, config.classNames.noTransition, isFocusIn);

            // Toggle
            ui.toggleControls.call(player, isFocusIn);

            // If focusin, hide again after delay
            if (isFocusIn) {
                // Restore transition
                setTimeout(() => {
                    toggleClass(elements.controls, config.classNames.noTransition, false);
                }, 0);

                // Delay a little more for keyboard users
                const delay = this.touch ? 3000 : 4000;

                // Clear timer
                clearTimeout(timers.controls);

                // Hide
                timers.controls = setTimeout(() => ui.toggleControls.call(player, false), delay);
            }
        });

        // Mouse wheel for volume
        this.bind(
            elements.inputs.volume,
            'wheel',
            event => {
                // Detect "natural" scroll - suppored on OS X Safari only
                // Other browsers on OS X will be inverted until support improves
                const inverted = event.webkitDirectionInvertedFromDevice;

                // Get delta from event. Invert if `inverted` is true
                const [x, y] = [event.deltaX, -event.deltaY].map(value => (inverted ? -value : value));

                // Using the biggest delta, normalize to 1 or -1 (or 0 if no delta)
                const direction = Math.sign(Math.abs(x) > Math.abs(y) ? x : y);

                // Change the volume by 2%
                player.increaseVolume(direction / 50);

                // Don't break page scrolling at max and min
                const { volume } = player.media;
                if ((direction === 1 && volume < 1) || (direction === -1 && volume > 0)) {
                    event.preventDefault();
                }
            },
            'volume',
            false,
        );
    }
}

export default Listeners;