@@ -29,8 +29,47 @@ function sepiaFW_build_ui_cards_embed(){
2929 }
3030 var mediaPlayerLastId = 0 ;
3131
32+ //Iframe message listener
33+ window . addEventListener ( 'message' , function ( message ) {
34+ if ( message . data && message . data . type ) {
35+ if ( message . data . type == "sepia-embedded-player-event" && message . data . ev ) {
36+ var mp = Object . values ( activeMediaPlayers ) . find ( function ( amp ) {
37+ return amp . iframe . contentWindow == message . source ;
38+ } ) ;
39+ if ( mp && mp . eventHandler ) {
40+ mp . eventHandler ( message . data . ev ) ;
41+ }
42+ }
43+ }
44+ } ) ;
45+
46+ //Create media player DOM element
47+ function createMediaPlayerDomElement ( id , contentUrl , onLoadHandler ) {
48+ //card
49+ var mediaPlayerDiv = document . createElement ( 'DIV' ) ;
50+ mediaPlayerDiv . id = id ;
51+ mediaPlayerDiv . className = "embeddedWebPlayer cardBodyItem fullWidthItem" ;
52+ //iframe
53+ var iframe = document . createElement ( "iframe" ) ;
54+ iframe . src = contentUrl ;
55+ iframe . width = "100%" ;
56+ iframe . height = 50 ; //can be set via postMessage interface
57+ iframe . allowtransparency = true ;
58+ iframe . onload = onLoadHandler ;
59+ //iframe.allow = ...;
60+ //iframe.sandbox = ...;
61+ mediaPlayerDiv . appendChild ( iframe ) ;
62+ return {
63+ card : mediaPlayerDiv , iframe : iframe
64+ }
65+ }
66+
67+ //MediaPlayer interface
3268 Embed . MediaPlayer = function ( options ) {
33- if ( ! options ) options = { } ;
69+ if ( ! options || ! options . parentElement || ! options . widgetUrl ) {
70+ SepiaFW . debug . error ( "Embedded MediaPlayer - Invalid options!" ) ;
71+ return ;
72+ }
3473 var thisPlayer = this ;
3574
3675 //ID
@@ -39,30 +78,55 @@ function sepiaFW_build_ui_cards_embed(){
3978 return playerId ;
4079 }
4180
42- //States
81+ //Widget URL
82+ options . widgetUrl = SepiaFW . config . replacePathTagWithActualPath ( options . widgetUrl ) ;
83+ console . error ( "URL" , options . widgetUrl ) ; //DEBUG
84+ //<custom_data>/embedded-player.html
85+
86+ //Create card (DOM element)
87+ var mpObj = createMediaPlayerDomElement ( playerId , options . widgetUrl , function ( ) {
88+ //on-load
89+ console . error ( "on-load" , playerId ) ; //DEBUG
90+ sendEvent ( { text : "World Hello" } ) ;
91+ } ) ;
92+ thisPlayer . iframe = mpObj . iframe ;
93+ options . parentElement . appendChild ( mpObj . card ) ;
94+
95+ //SEPIA postMessage interface
96+ function sendEvent ( ev ) {
97+ thisPlayer . iframe . contentWindow . postMessage ( {
98+ type : "sepia-embedded-player-event" ,
99+ ev : ev
100+ } , "*" ) ;
101+ }
102+ thisPlayer . eventHandler = function ( ev ) {
103+ //TODO: use
104+ console . error ( "ev" , playerId , ev ) ; //DEBUG
105+ }
43106
107+ //States
44108
45109 //Controls - TODO: implement
46110 thisPlayer . start = function ( doneCallback , errorCallback ) {
47-
111+ sendEvent ( { controls : "start" } ) ;
48112 }
49113 thisPlayer . pause = function ( doneCallback , errorCallback ) {
50-
51- }
52- thisPlayer . fadeOut = function ( doneCallback , errorCallback ) {
53-
114+ sendEvent ( { controls : "pause" } ) ;
54115 }
55116 thisPlayer . resume = function ( doneCallback , errorCallback ) {
56-
117+ sendEvent ( { controls : "resume" } ) ;
118+ }
119+ thisPlayer . fadeOut = function ( doneCallback , errorCallback ) {
120+ sendEvent ( { controls : "fadeOut" } ) ;
57121 }
58122 thisPlayer . fadeIn = function ( doneCallback , errorCallback ) {
59-
123+ sendEvent ( { controls : "fadeIn" } ) ;
60124 }
61125 thisPlayer . next = function ( doneCallback , errorCallback ) {
62-
126+ sendEvent ( { controls : "next" } ) ;
63127 }
64128 thisPlayer . previous = function ( doneCallback , errorCallback ) {
65-
129+ sendEvent ( { controls : "previous" } ) ;
66130 }
67131 //stop, release resources and remove handle
68132 thisPlayer . release = function ( doneCallback , errorCallback ) {
0 commit comments