// eslint-disable-next-line @typescript-eslint/no-unused-vars import { h } from 'preact'; import { UnisphereWorkspaceType } from '@unisphere/runtime'; import { FakeEvent } from '@playkit-js/playkit-js'; import { core, ui, KalturaPlayer } from '@playkit-js/kaltura-player-js'; import { SidePanelsManager, UpperBarManager } from '@playkit-js/ui-managers'; import { UnisphereEvent, UnisphereStorageKeys, WidgetConfig, PlayerEvent } from './types'; import { UpperBarIcon } from './components'; import { ContainerManager, UnisphereElementService, UnisphereEventService, UnisphereStorageService, UnisphereWorkspaceService } from './services'; const { reducers: {shell}} = ui; class UnispherePluginManager { private iconIds = new Map(); private panelIds = new Set(); private persistentIconIds = new Map(); private containerManager: ContainerManager; private currentIsSmallSize?: boolean | undefined; private unsubscribeStore?: () => void; constructor(private player: KalturaPlayer, private eventManager: core.EventManager, private logger: any) { this.containerManager = new ContainerManager(player); this.currentIsSmallSize = undefined; } private get upperBarManager(): UpperBarManager | undefined { return this.player.getService('upperBarManager') as UpperBarManager; } private get sidePanelsManager(): SidePanelsManager | undefined { return this.player.getService('sidePanelsManager') as SidePanelsManager; } private get timelineManager(): | { disableChapters: () => void; addKalturaCuePoint: ( startTime: number, type: string, index: number, title: string, options: { onClick: ({ cuePoint }: { cuePoint: { id: string } }) => void } ) => void; } | undefined { return this.player.getService('timeline'); } private get cuePointManager(): | { CuepointType: { [key: string]: string }; registerTypes: (arr: string[]) => void; } | undefined { return this.player.getService('kalturaCuepoints'); } private addEventHandlers(): void { const store = ui.redux.useStore(); this.unsubscribeStore = store.subscribe(() => { const state = store.getState(); const newIsSmallSize = state.shell.isSmallSize; if (newIsSmallSize !== this.currentIsSmallSize) { this.currentIsSmallSize = newIsSmallSize; UnisphereStorageService.update(UnisphereStorageKeys.IS_SMALL_SIZE, newIsSmallSize); } }); // listen to events from runtime side UnisphereEventService.subscribe(UnisphereEvent.UPPER_BAR_ICON_SHOW, ({ payload }) => { const { id, label, order, iconPath, panelId, isPersistent } = payload; this.addUpperBarIcon(id as string, label as string, order as number, iconPath as string, panelId as number, isPersistent as boolean); }); UnisphereEventService.subscribe(UnisphereEvent.SIDE_PANEL_OPEN, ({ payload }) => { const { panelId } = payload; this.sidePanelsManager?.activateItem(panelId as number); UnisphereEventService.emit(UnisphereEvent.SIDE_PANEL_OPENED, payload); }); UnisphereEventService.subscribe(UnisphereEvent.SIDE_PANEL_CLOSE, ({ payload }) => { const { panelId } = payload; this.sidePanelsManager?.deactivateItem(panelId as number); UnisphereEventService.emit(UnisphereEvent.SIDE_PANEL_CLOSED, payload); }); UnisphereEventService.subscribe(UnisphereEvent.SIDE_PANEL_TOGGLE, ({ payload }) => { const { panelId } = payload; if (this.sidePanelsManager?.isItemActive(panelId as number)) { this.sidePanelsManager?.deactivateItem(panelId as number); UnisphereEventService.emit(UnisphereEvent.SIDE_PANEL_CLOSED, payload); } else { this.sidePanelsManager?.activateItem(panelId as number); UnisphereEventService.emit(UnisphereEvent.SIDE_PANEL_OPENED, payload); } }); UnisphereEventService.subscribe(UnisphereEvent.SET_VALUE_CURRENT_TIME, ({ payload }) => { const currentTime = payload.currentTime as number; if ((currentTime as number) >= 0) { this.player.currentTime = currentTime as number; } }); UnisphereEventService.subscribe(UnisphereEvent.CHAPTERS_ADD, ({ payload }) => { const chapters = payload.chapters as { title: string; time: string }[]; if (chapters && chapters.length) { this.timelineManager?.disableChapters(); for (let i = 0; i < chapters.length; ++i) { const { title, time } = chapters[i]; const startTime = +time; this.timelineManager?.addKalturaCuePoint(startTime, 'SummaryAndChapters', i, title, { onClick: ({ cuePoint }: { cuePoint: { id: string } }) => { UnisphereStorageService.update(UnisphereStorageKeys.SELECTED_CHAPTER, cuePoint.id); } }); } //@ts-ignore this.player.ui.store.dispatch(shell.actions.updateTimeDisplayIsMinimized(true)); // event for playkit-js-seo this.player.dispatchEvent(new FakeEvent(PlayerEvent.UNISPHERE_CHAPTERS_ADDED, payload)); } }); UnisphereEventService.subscribe(UnisphereEvent.DISPATCH_EVENT, ({ payload: { eventName, data } }) => { this.player.dispatchEvent(new FakeEvent(eventName as string, data)); }); UnisphereEventService.subscribe(UnisphereEvent.UPPER_BAR_ICON_SHOW, () => { this.player.dispatchEvent(new FakeEvent(UnisphereEvent.UPPER_BAR_ICON_SHOW as string)); }); // emit events to runtime side this.eventManager.listen(this.player, core.EventType.TIME_UPDATE, () => { UnisphereStorageService.update(UnisphereStorageKeys.CURRENT_TIME, `${this.player.currentTime}`); }); this.eventManager.listen(this.player, ui.EventType.GUI_RESIZE, () => { UnisphereEventService.emit(UnisphereEvent.PLAYER_RESIZE, {}); setTimeout(() => { UnisphereEventService.emit(UnisphereEvent.PLAYER_RESIZE, {}); }, 1000); }); this.eventManager.listen(this.player, core.EventType.PAUSE, () => { UnisphereStorageService.update(UnisphereStorageKeys.IS_PAUSED, true); }); this.eventManager.listen(this.player, core.EventType.PLAY, () => { UnisphereStorageService.update(UnisphereStorageKeys.IS_PAUSED, false); }); this.eventManager.listen(this.player, 'download_summary_chapters_download', ({payload}: {payload: any}) => { UnisphereStorageService.update(UnisphereStorageKeys.DOWNLOAD_CLICKED, true); }); } private addUpperBarIcon(id: string, label: string, order: number, iconPath: string, panelId: number, isPersistent: boolean): void { if (this.iconIds.has(id) || this.persistentIconIds.has(id)) return; this.panelIds.add(panelId); const iconId = this.upperBarManager?.add({ displayName: label, ariaLabel: label, order, component: () => () as preact.JSX.Element, svgIcon: { path: iconPath }, onClick: () => { UnisphereEventService.emit(UnisphereEvent.UPPER_BAR_ICON_CLICK, { id, panelId }); } }) as number; if (isPersistent) { this.persistentIconIds.set(id, iconId); } else { this.iconIds.set(id, iconId); } } private isVideoClipped(): boolean { const { seekFrom, clipTo, duration } = this.player.config.sources; return (typeof seekFrom === 'number' && seekFrom > 0) || (typeof clipTo === 'number' && clipTo < duration!); } private shouldLoadWidget(widget: WidgetConfig): boolean { const { widgetId, context } = widget; return !(widgetId.includes('video-summary') && context === 'kaltura-player-list' && this.isVideoClipped()); } public loadWidgets(widgets: WidgetConfig[]): void { for (const widget of widgets) { const { widgetId, context } = widget; if (!widgetId || !context || !this.shouldLoadWidget(widget)) continue; for (const containerConfig of widget.containers) { if (!containerConfig.unisphereArea) continue; const containerId = this.containerManager.createContainer(containerConfig); if (containerId) { UnisphereElementService.get(widget) .then((element) => { element?.assignAreaByName(containerConfig.unisphereArea, containerId); }) .catch((e) => { this.logger.warn(`Failed to load unisphere widget ${widgetId} with context ${context} `, e); }); } } } } public init(workspaceInstance: UnisphereWorkspaceType, widgets: WidgetConfig[]): void { UnisphereWorkspaceService.set(workspaceInstance); this.addEventHandlers(); this.loadWidgets(widgets); } public updateSessionData(): void { UnisphereStorageService.update(UnisphereStorageKeys.PLAYER_SESSION, { entryId: this.player.sources.id, ks: this.player.config.session?.ks, entryName: this.player.sources.metadata?.name }); if (this.player.plugins?.share) { UnisphereStorageService.update(UnisphereStorageKeys.IS_SHARE, true); } } public reset(): void { for (const panelId of this.panelIds) { if (this.sidePanelsManager?.isItemActive(panelId)) { this.sidePanelsManager.deactivateItem(panelId); break; } } for (const v of this.iconIds.values()) { this.upperBarManager?.remove(v); } this.iconIds.clear(); this.containerManager.destroy(); //@ts-ignore this.player.ui.store.dispatch(shell.actions.updateTimeDisplayIsMinimized(false)); } public destroy(): void { this.reset(); this.containerManager.destroy(); for (const v of this.persistentIconIds.values()) { this.upperBarManager?.remove(v); } UnisphereEventService.destroy(); UnisphereStorageService.destroy(); UnisphereWorkspaceService.destroy(); if (this.unsubscribeStore) { this.unsubscribeStore(); } } } export { UnispherePluginManager };