Skip to content

Commit b964305

Browse files
Juzar10KMchaudhary
andauthored
GoDAM Player: Introduce Developer API with Player Class for video management (#1018)
* feat: add GoDAM API and Player class for video management * feat: enhance GoDAM API to include player instance retrieval and dispatch custom event on player readiness * feat: enhance Player class with input validation, error handling, and layer management improvements * feat: update GoDAM API to improve player retrieval and add functionality for getting all players and ready players * doc: add comprehensive GoDAM Player API documentation with usage examples and error handling * feat: enhance fullscreen functionality by supporting ios devices * feat: Add replay functionality and layer management methods to Player and FormLayerManager for the native layers management --------- Co-authored-by: KMchaudhary <[email protected]>
1 parent ce5440b commit b964305

File tree

8 files changed

+1572
-1
lines changed

8 files changed

+1572
-1
lines changed

assets/src/js/godam-player/api/README.md

Lines changed: 616 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/**
2+
* External dependencies
3+
*/
4+
import videojs from 'video.js';
5+
6+
/**
7+
* Internal dependencies
8+
*/
9+
import Player from './player.js';
10+
11+
window.GoDAMAPI = {
12+
/**
13+
* Get Player Instance
14+
*
15+
* @param {string} attachmentID - The attachment ID of the video
16+
* @param {HTMLElement} videoElement - (optional) an actual video tag element
17+
* @param {Object} player - (optional) the video.js player instance
18+
* @return {Player} Player instance
19+
*/
20+
getPlayer( attachmentID, videoElement = null, player = null ) {
21+
let video, videoJs;
22+
23+
if ( videoElement && player ) {
24+
// Both provided, use them directly
25+
videoJs = player;
26+
27+
// Check if videoElement is the actual <video> tag or the container div
28+
if ( videoElement.tagName.toLowerCase() === 'video' ) {
29+
// It's the <video> tag, find the parent container div
30+
video = videoElement.closest( `[data-id="${ attachmentID }"]` );
31+
} else {
32+
// It's the container div, use it directly
33+
video = videoElement;
34+
}
35+
} else if ( videoElement ) {
36+
// Only videoElement provided
37+
if ( videoElement.tagName.toLowerCase() === 'video' ) {
38+
// It's the <video> tag, find the parent container and get VideoJS instance
39+
video = videoElement.closest( `[data-id="${ attachmentID }"]` );
40+
videoJs = videojs( videoElement );
41+
} else {
42+
// It's the container div, find the video tag inside
43+
video = videoElement;
44+
videoJs = videojs( video.querySelector( 'video' ) );
45+
}
46+
} else {
47+
// Nothing provided, search by attachment ID
48+
video = document.querySelector( `[data-id="${ attachmentID }"]` );
49+
videoJs = videojs( video.querySelector( 'video' ) );
50+
}
51+
52+
return new Player( videoJs, video );
53+
},
54+
55+
/**
56+
* Get all players on the page
57+
*
58+
* @return {Array} Array of player objects with attachment ID and Player instance
59+
*/
60+
getAllPlayers() {
61+
const players = [];
62+
const videoContainers = document.querySelectorAll( '[data-id]' );
63+
64+
videoContainers.forEach( ( container ) => {
65+
const attachmentId = container.dataset.id;
66+
const videoElement = container.querySelector( 'video' );
67+
68+
if ( videoElement ) {
69+
try {
70+
// Check if VideoJS instance exists
71+
const videoJsInstance = videojs.getPlayer( videoElement );
72+
if ( videoJsInstance ) {
73+
const playerInstance = new Player( videoJsInstance, container );
74+
players.push( {
75+
attachmentId,
76+
player: playerInstance,
77+
container,
78+
videoElement,
79+
isReady: videoJsInstance.readyState() > 0,
80+
} );
81+
}
82+
} catch ( error ) {
83+
// VideoJS instance might not exist yet
84+
}
85+
}
86+
} );
87+
88+
return players;
89+
},
90+
91+
/**
92+
* Get all ready players on the page
93+
*
94+
* @return {Array} Array of ready player objects
95+
*/
96+
getAllReadyPlayers() {
97+
return this.getAllPlayers().filter( ( playerObj ) => playerObj.isReady );
98+
},
99+
100+
};
101+

0 commit comments

Comments
 (0)