From 41c9f52e8a8182a67ad2b638488ccbfb5ba7c8a0 Mon Sep 17 00:00:00 2001 From: Roy Bregman <48884909+RoyBregman@users.noreply.github.com> Date: Wed, 22 Jul 2020 20:26:46 +0300 Subject: [PATCH] feat(FEC-10081): as a front end developer I would like to be able to be notified via events when floating player is toggled on/off (#10) --- src/event-type.js | 17 +++++++++++++++++ src/index.js | 1 + src/visibility.js | 5 +++++ 3 files changed, 23 insertions(+) create mode 100644 src/event-type.js diff --git a/src/event-type.js b/src/event-type.js new file mode 100644 index 0000000..a07fae9 --- /dev/null +++ b/src/event-type.js @@ -0,0 +1,17 @@ +// @flow +const EventType: PKEventTypes = { + /** + * Fired when the player got in or out of visibility (according to the minimum visible percentage threshold) + */ + PLAYER_VISIBILITY_CHANGED: 'playervisibilitychanged', + /** + * Fired when the player started / stopped floating due to visibility change + */ + FLOATING_PLAYER_STATE_CHANGED: 'floatingplayerstatechanged', + /** + * Fired when the player stopped floating due to end user dismissal + */ + FLOATING_PLAYER_DISMISSED: 'floatingplayerdismissed' +}; + +export {EventType}; diff --git a/src/index.js b/src/index.js index b415e76..6c38f02 100644 --- a/src/index.js +++ b/src/index.js @@ -7,6 +7,7 @@ declare var __NAME__: string; export {Visibility as Plugin}; export {__VERSION__ as VERSION, __NAME__ as NAME}; +export {EventType} from './event-type'; const pluginName: string = 'visibility'; registerPlugin(pluginName, Visibility); diff --git a/src/visibility.js b/src/visibility.js index d558002..794b6c0 100644 --- a/src/visibility.js +++ b/src/visibility.js @@ -3,6 +3,7 @@ import {BasePlugin, Utils} from '@playkit-js/playkit-js'; import './style.css'; import {DismissibleFloatingButtonComponent} from './components/dismissible/dismissible'; import 'intersection-observer'; +import {EventType} from './event-type'; const FLOATING_ACTIVE_CLASS: string = 'playkit-floating-active'; const FLOATING_CONTAINER_CLASS: string = 'playkit-floating-container'; @@ -113,11 +114,13 @@ class Visibility extends BasePlugin { this._dismissed = true; this.player.pause(); this._stopFloating(); + this.dispatchEvent(EventType.FLOATING_PLAYER_DISMISSED); } _stopFloating() { Utils.Dom.removeClassName(this._floatingContainer, FLOATING_ACTIVE_CLASS); Utils.Dom.removeAttribute(this._floatingContainer, 'style'); + this.dispatchEvent(EventType.FLOATING_PLAYER_STATE_CHANGED, {active: false}); } _startFloating() { @@ -125,6 +128,7 @@ class Visibility extends BasePlugin { Utils.Dom.setStyle(this._floatingContainer, 'height', this.config.floating.height + 'px'); Utils.Dom.setStyle(this._floatingContainer, 'width', this.config.floating.width + 'px'); Utils.Dom.setStyle(this._floatingContainer, 'margin', `${this.config.floating.marginY}px ${this.config.floating.marginX}px`); + this.dispatchEvent(EventType.FLOATING_PLAYER_STATE_CHANGED, {active: true}); } _handleVisibilityChange(entries: Array) { @@ -132,6 +136,7 @@ class Visibility extends BasePlugin { if (this.config.floating && this._playbackStartOccurred && !this._dismissed && !this._isInPIP) { this._handleFloatingChange(playerIsOutOfVisibility); } + this.dispatchEvent(EventType.PLAYER_VISIBILITY_CHANGED, {visible: !playerIsOutOfVisibility}); } _handleFloatingChange(playerIsOutOfVisibility: boolean) {