From 43879e08f44a5bb902b4dc0f8ccd1d3d6283b47d Mon Sep 17 00:00:00 2001 From: Albin Larsson Date: Tue, 19 Jun 2018 16:25:37 +0200 Subject: Make (increase/decrease)Volume methods ignore invalid input instead of raising / lowering to the min / max --- src/js/plyr.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/js') diff --git a/src/js/plyr.js b/src/js/plyr.js index c2bb6a4d..9e00e836 100644 --- a/src/js/plyr.js +++ b/src/js/plyr.js @@ -553,7 +553,7 @@ class Plyr { */ increaseVolume(step) { const volume = this.media.muted ? 0 : this.volume; - this.volume = volume + (is.number(step) ? step : 1); + this.volume = volume + (is.number(step) ? step : 0); } /** @@ -562,7 +562,7 @@ class Plyr { */ decreaseVolume(step) { const volume = this.media.muted ? 0 : this.volume; - this.volume = volume - (is.number(step) ? step : 1); + this.volume = volume - (is.number(step) ? step : 0); } /** -- cgit v1.2.3 From 39c7bd40c205700e7b1df677b8aaacc6cb8100db Mon Sep 17 00:00:00 2001 From: Albin Larsson Date: Tue, 19 Jun 2018 16:29:52 +0200 Subject: Make decreaseVolume wrap increaseVolume for code reuse --- src/js/plyr.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'src/js') diff --git a/src/js/plyr.js b/src/js/plyr.js index 9e00e836..374251e6 100644 --- a/src/js/plyr.js +++ b/src/js/plyr.js @@ -561,8 +561,7 @@ class Plyr { * @param {boolean} step - How much to decrease by (between 0 and 1) */ decreaseVolume(step) { - const volume = this.media.muted ? 0 : this.volume; - this.volume = volume - (is.number(step) ? step : 0); + this.increaseVolume(-step); } /** -- cgit v1.2.3 From 004528a65c334603bfd0e79222687a7fec02abcb Mon Sep 17 00:00:00 2001 From: Albin Larsson Date: Tue, 19 Jun 2018 17:16:30 +0200 Subject: Avoid conditions in volume scroll event listener --- src/js/listeners.js | 38 +++++++++++--------------------------- 1 file changed, 11 insertions(+), 27 deletions(-) (limited to 'src/js') diff --git a/src/js/listeners.js b/src/js/listeners.js index 9d987508..d9811dd1 100644 --- a/src/js/listeners.js +++ b/src/js/listeners.js @@ -665,36 +665,20 @@ class Listeners { // Detect "natural" scroll - suppored on OS X Safari only // Other browsers on OS X will be inverted until support improves const inverted = event.webkitDirectionInvertedFromDevice; - const step = 1 / 50; - let direction = 0; - - // Scroll down (or up on natural) to decrease - if (event.deltaY < 0 || event.deltaX > 0) { - if (inverted) { - this.player.decreaseVolume(step); - direction = -1; - } else { - this.player.increaseVolume(step); - direction = 1; - } - } - // Scroll up (or down on natural) to increase - if (event.deltaY > 0 || event.deltaX < 0) { - if (inverted) { - this.player.increaseVolume(step); - direction = 1; - } else { - this.player.decreaseVolume(step); - direction = -1; - } - } + // 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% + this.player.increaseVolume(direction / 50); // Don't break page scrolling at max and min - if ( - (direction === 1 && this.player.media.volume < 1) || - (direction === -1 && this.player.media.volume > 0) - ) { + const { volume } = this.player.media; + if ((direction === 1 && volume < 1) || (direction === -1 && volume > 0)) { event.preventDefault(); } }, -- cgit v1.2.3 From e04b90c9c030bf5629e034f616a636245770a8d1 Mon Sep 17 00:00:00 2001 From: Sam Potts Date: Thu, 21 Jun 2018 09:06:28 +1000 Subject: Ads only on HTML5 and .is cleanup --- src/js/plugins/ads.js | 4 +- src/js/utils/is.js | 112 ++++++++++++++++++++++++-------------------------- 2 files changed, 57 insertions(+), 59 deletions(-) (limited to 'src/js') diff --git a/src/js/plugins/ads.js b/src/js/plugins/ads.js index e0d49265..856772d5 100644 --- a/src/js/plugins/ads.js +++ b/src/js/plugins/ads.js @@ -49,7 +49,9 @@ class Ads { } get enabled() { - return this.player.isVideo && this.player.config.ads.enabled && !is.empty(this.publisherId); + return ( + this.player.isHTML5 && this.player.isVideo && this.player.config.ads.enabled && !is.empty(this.publisherId) + ); } /** diff --git a/src/js/utils/is.js b/src/js/utils/is.js index cb2c07c6..b4760da4 100644 --- a/src/js/utils/is.js +++ b/src/js/utils/is.js @@ -3,65 +3,61 @@ // ========================================================================== const getConstructor = input => (input !== null && typeof input !== 'undefined' ? input.constructor : null); - const instanceOf = (input, constructor) => Boolean(input && constructor && input instanceof constructor); +const isNullOrUndefined = input => input === null || typeof input === 'undefined'; +const isObject = input => getConstructor(input) === Object; +const isNumber = input => getConstructor(input) === Number && !Number.isNaN(input); +const isString = input => getConstructor(input) === String; +const isBoolean = input => getConstructor(input) === Boolean; +const isFunction = input => getConstructor(input) === Function; +const isArray = input => Array.isArray(input); +const isWeakMap = input => instanceOf(input, WeakMap); +const isNodeList = input => instanceOf(input, NodeList); +const isElement = input => instanceOf(input, Element); +const isTextNode = input => getConstructor(input) === Text; +const isEvent = input => instanceOf(input, Event); +const isCue = input => instanceOf(input, window.TextTrackCue) || instanceOf(input, window.VTTCue); +const isTrack = input => instanceOf(input, TextTrack) || (!isNullOrUndefined(input) && isString(input.kind)); + +const isEmpty = input => + isNullOrUndefined(input) || + ((isString(input) || isArray(input) || isNodeList(input)) && !input.length) || + (isObject(input) && !Object.keys(input).length); + +const isUrl = input => { + // Accept a URL object + if (instanceOf(input, window.URL)) { + return true; + } -const is = { - object(input) { - return getConstructor(input) === Object; - }, - number(input) { - return getConstructor(input) === Number && !Number.isNaN(input); - }, - string(input) { - return getConstructor(input) === String; - }, - boolean(input) { - return getConstructor(input) === Boolean; - }, - function(input) { - return getConstructor(input) === Function; - }, - array(input) { - return !is.nullOrUndefined(input) && Array.isArray(input); - }, - weakMap(input) { - return instanceOf(input, WeakMap); - }, - nodeList(input) { - return instanceOf(input, NodeList); - }, - element(input) { - return instanceOf(input, Element); - }, - textNode(input) { - return getConstructor(input) === Text; - }, - event(input) { - return instanceOf(input, Event); - }, - cue(input) { - return instanceOf(input, window.TextTrackCue) || instanceOf(input, window.VTTCue); - }, - track(input) { - return instanceOf(input, TextTrack) || (!is.nullOrUndefined(input) && is.string(input.kind)); - }, - url(input) { - return ( - !is.nullOrUndefined(input) && - /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-/]))?/.test(input) - ); - }, - nullOrUndefined(input) { - return input === null || typeof input === 'undefined'; - }, - empty(input) { - return ( - is.nullOrUndefined(input) || - ((is.string(input) || is.array(input) || is.nodeList(input)) && !input.length) || - (is.object(input) && !Object.keys(input).length) - ); - }, + // Add the protocol if required + let string = input; + if (!input.startsWith('http://') || !input.startsWith('https://')) { + string = `http://${input}`; + } + + try { + return !isEmpty(new URL(string).hostname); + } catch (e) { + return false; + } }; -export default is; +export default { + nullOrUndefined: isNullOrUndefined, + object: isObject, + number: isNumber, + string: isString, + boolean: isBoolean, + function: isFunction, + array: isArray, + weakMap: isWeakMap, + nodeList: isNodeList, + element: isElement, + textNode: isTextNode, + event: isEvent, + cue: isCue, + track: isTrack, + url: isUrl, + empty: isEmpty, +}; -- cgit v1.2.3 From 81c5477f1d9ac2539e4ec50ede7886abe50fa75a Mon Sep 17 00:00:00 2001 From: Albin Larsson Date: Thu, 21 Jun 2018 15:22:30 +0200 Subject: Fix captions.toggle() if there is no toggle button --- src/js/captions.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'src/js') diff --git a/src/js/captions.js b/src/js/captions.js index 732b2e38..9dca5505 100644 --- a/src/js/captions.js +++ b/src/js/captions.js @@ -191,8 +191,10 @@ const captions = { return; } - // Toggle state - this.elements.buttons.captions.pressed = active; + // Toggle button if it's enabled + if (this.elements.buttons.captions) { + this.elements.buttons.captions.pressed = active; + } // Add class hook toggleClass(this.elements.container, activeClass, active); -- cgit v1.2.3 From b6ddf144f4b6d784da68a322de6e44b6fe724409 Mon Sep 17 00:00:00 2001 From: Michael DePetrillo Date: Mon, 25 Jun 2018 12:00:02 +0200 Subject: handle undefined player.elements.buttons.play --- src/js/listeners.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'src/js') diff --git a/src/js/listeners.js b/src/js/listeners.js index d9811dd1..7615e365 100644 --- a/src/js/listeners.js +++ b/src/js/listeners.js @@ -431,9 +431,11 @@ class Listeners { }; // Play/pause toggle - Array.from(this.player.elements.buttons.play).forEach(button => { - bind(button, 'click', this.player.togglePlay, 'play'); - }); + if (this.player.elements.buttons.play) { + Array.from(this.player.elements.buttons.play).forEach(button => { + bind(button, 'click', this.player.togglePlay, 'play'); + }); + } // Pause bind(this.player.elements.buttons.restart, 'click', this.player.restart, 'restart'); -- cgit v1.2.3