@@ -4,6 +4,7 @@ import Logger from '../logger/logger';
44import DataSync from '../data-sync/data-sync' ;
55import ScheduleService from './scheduleService' ;
66import ConfigLoader from '../config-loader' ;
7+ import PullStrategy from '../data-sync/pull-strategy' ;
78
89/**
910 * ContentService.
@@ -176,6 +177,7 @@ class ContentService {
176177 document . addEventListener ( 'content' , this . contentHandler ) ;
177178 document . addEventListener ( 'regionReady' , this . regionReadyHandler ) ;
178179 document . addEventListener ( 'regionRemoved' , this . regionRemovedHandler ) ;
180+ document . addEventListener ( 'startPreview' , this . startPreview ) ;
179181 }
180182
181183 /**
@@ -189,6 +191,96 @@ class ContentService {
189191 document . removeEventListener ( 'content' , this . contentHandler ) ;
190192 document . removeEventListener ( 'regionReady' , this . regionReadyHandler ) ;
191193 document . removeEventListener ( 'regionRemoved' , this . regionRemovedHandler ) ;
194+ document . removeEventListener ( 'startPreview' , this . startPreview ) ;
195+ }
196+
197+ /**
198+ * Start preview.
199+ *
200+ * @param event
201+ */
202+ async startPreview ( event ) {
203+ const data = event . detail ;
204+ const { mode, id } = data ;
205+ Logger . log ( 'info' , `Starting preview. Mode: ${ mode } , ID: ${ id } ` ) ;
206+
207+ const config = await ConfigLoader . loadConfig ( ) ;
208+
209+
210+ if ( mode === 'screen' ) {
211+ this . startSyncing ( `/v2/screen/${ id } ` ) ;
212+ } else if ( mode === 'playlist' ) {
213+ const pullStrategy = new PullStrategy ( {
214+ endpoint : config . apiEndpoint ,
215+ } ) ;
216+
217+ const playlist = await pullStrategy . getPath (
218+ `/v2/playlists/${ id } `
219+ ) ;
220+
221+ const playlistSlidesResponse = await pullStrategy . getPath (
222+ playlist . slides
223+ ) ;
224+
225+ playlist . slidesData = playlistSlidesResponse [ 'hydra:member' ] . map (
226+ ( playlistSlide ) => playlistSlide . slide
227+ ) ;
228+
229+ for ( const item of playlist . slidesData ) {
230+ item . templateData = await pullStrategy . addTemplateData ( item ) ;
231+ }
232+
233+ const screen = {
234+ '@id' : '/v2/screens/SCREEN01234567890123456789' ,
235+ '@type' : 'Screen' ,
236+ title : 'Preview' ,
237+ description : 'Screen for preview.' ,
238+ layout : '/v2/layouts/LAYOUT01234567890123456789' ,
239+ regions : [
240+ '/v2/screens/SCREEN01234567890123456789/regions/REGION01234567890123456789/playlists' ,
241+ ] ,
242+ regionData : {
243+ REGION01234567890123456789 : [ playlist ] ,
244+ } ,
245+ layoutData : {
246+ '@id' : '/v2/layouts/LAYOUT01234567890123456789' ,
247+ '@type' : 'ScreenLayout' ,
248+ title : 'Full screen' ,
249+ grid : {
250+ rows : 1 ,
251+ columns : 1 ,
252+ } ,
253+ regions : [
254+ {
255+ '@type' : 'ScreenLayoutRegions' ,
256+ '@id' : '/v2/layouts/regions/REGION01234567890123456789' ,
257+ title : 'full' ,
258+ gridArea : [ 'a' ] ,
259+ screenLayout : '/v2/layouts/LAYOUT01234567890123456789' ,
260+ } ,
261+ ] ,
262+ } ,
263+ } ;
264+
265+ document . dispatchEvent (
266+ new CustomEvent ( 'content' , {
267+ detail : {
268+ screen,
269+ } ,
270+ } )
271+ ) ;
272+ } else if ( mode === 'slide' ) {
273+ const pullStrategy = new PullStrategy ( { } ) ;
274+ // const slide = pullStrategy.getSlide(id);
275+
276+ const screen = {
277+ // TODO: Build fake screen.
278+ } ;
279+
280+ ContentService . emitScreen ( screen ) ;
281+ } else {
282+ Logger . error ( `Unsupported preview mode: ${ mode } .` ) ;
283+ }
192284 }
193285
194286 /**
0 commit comments