11import sha256 from 'crypto-js/sha256' ;
22import Base64 from 'crypto-js/enc-base64' ;
3+ import cloneDeep from 'lodash.clonedeep' ;
4+ import { object , string } from 'prop-types' ;
35import Logger from '../logger/logger' ;
46import DataSync from '../data-sync/data-sync' ;
57import ScheduleService from './scheduleService' ;
68import ConfigLoader from '../config-loader' ;
79import PullStrategy from '../data-sync/pull-strategy' ;
10+ import {
11+ screenForPlaylistPreview ,
12+ screenForSlidePreview ,
13+ } from '../util/preview' ;
814
915/**
1016 * ContentService.
@@ -197,7 +203,7 @@ class ContentService {
197203 /**
198204 * Start preview.
199205 *
200- * @param event
206+ * @param { CustomEvent } event The event.
201207 */
202208 async startPreview ( event ) {
203209 const data = event . detail ;
@@ -206,17 +212,14 @@ class ContentService {
206212
207213 const config = await ConfigLoader . loadConfig ( ) ;
208214
209-
210215 if ( mode === 'screen' ) {
211216 this . startSyncing ( `/v2/screen/${ id } ` ) ;
212217 } else if ( mode === 'playlist' ) {
213218 const pullStrategy = new PullStrategy ( {
214219 endpoint : config . apiEndpoint ,
215220 } ) ;
216221
217- const playlist = await pullStrategy . getPath (
218- `/v2/playlists/${ id } `
219- ) ;
222+ const playlist = await pullStrategy . getPath ( `/v2/playlists/${ id } ` ) ;
220223
221224 const playlistSlidesResponse = await pullStrategy . getPath (
222225 playlist . slides
@@ -226,41 +229,13 @@ class ContentService {
226229 ( playlistSlide ) => playlistSlide . slide
227230 ) ;
228231
229- for ( const item of playlist . slidesData ) {
230- item . templateData = await pullStrategy . addTemplateData ( item ) ;
232+ // eslint-disable-next-line no-restricted-syntax
233+ for ( const slide of playlist . slidesData ) {
234+ // eslint-disable-next-line no-await-in-loop
235+ await ContentService . attachReferencesToSlide ( pullStrategy , slide ) ;
231236 }
232237
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- } ;
238+ const screen = screenForPlaylistPreview ( playlist ) ;
264239
265240 document . dispatchEvent (
266241 new CustomEvent ( 'content' , {
@@ -270,19 +245,47 @@ class ContentService {
270245 } )
271246 ) ;
272247 } else if ( mode === 'slide' ) {
273- const pullStrategy = new PullStrategy ( { } ) ;
274- // const slide = pullStrategy.getSlide(id);
248+ const pullStrategy = new PullStrategy ( {
249+ endpoint : config . apiEndpoint ,
250+ } ) ;
275251
276- const screen = {
277- // TODO: Build fake screen.
278- } ;
252+ const slide = await pullStrategy . getPath ( `/v2/slides/${ id } ` ) ;
279253
280- ContentService . emitScreen ( screen ) ;
254+ // eslint-disable-next-line no-await-in-loop
255+ await ContentService . attachReferencesToSlide ( pullStrategy , slide ) ;
256+
257+ const screen = screenForSlidePreview ( slide ) ;
258+
259+ document . dispatchEvent (
260+ new CustomEvent ( 'content' , {
261+ detail : {
262+ screen,
263+ } ,
264+ } )
265+ ) ;
281266 } else {
282267 Logger . error ( `Unsupported preview mode: ${ mode } .` ) ;
283268 }
284269 }
285270
271+ static async attachReferencesToSlide ( strategy , slide ) {
272+ /* eslint-disable no-param-reassign */
273+ slide . templateData = await strategy . getTemplateData ( slide ) ;
274+ slide . feedData = await strategy . getFeedData ( slide ) ;
275+
276+ slide . mediaData = { } ;
277+ // eslint-disable-next-line no-restricted-syntax
278+ for ( const media of slide . media ) {
279+ // eslint-disable-next-line no-await-in-loop
280+ slide . mediaData [ media ] = await strategy . getMediaData ( media ) ;
281+ }
282+
283+ if ( typeof slide . theme === 'string' || slide . theme instanceof String ) {
284+ slide . theme = await strategy . getPath ( slide . theme ) ;
285+ }
286+ /* eslint-enable no-param-reassign */
287+ }
288+
286289 /**
287290 * Emit screen.
288291 *
0 commit comments