()
| 38 | * @doc {heading: 'Visor & Surfaces'} |
| 39 | */ |
| 40 | export function visor(): Visor { |
| 41 | if (typeof document === 'undefined') { |
| 42 | throw new Error( |
| 43 | 'No document defined. This library needs a browser/dom to work'); |
| 44 | } |
| 45 | |
| 46 | if (document.getElementById(VISOR_CONTAINER_ID) && visorSingleton != null) { |
| 47 | return visorSingleton; |
| 48 | } |
| 49 | |
| 50 | // Create the container |
| 51 | let visorEl = document.getElementById(VISOR_CONTAINER_ID); |
| 52 | |
| 53 | if (visorEl == null) { |
| 54 | visorEl = document.createElement('div'); |
| 55 | visorEl.id = VISOR_CONTAINER_ID; |
| 56 | document.body.appendChild(visorEl); |
| 57 | } |
| 58 | |
| 59 | let renderRoot: Element; |
| 60 | function renderVisor( |
| 61 | domNode: HTMLElement, |
| 62 | surfaceList: Map<string, SurfaceInfoStrict>): VisorComponent { |
| 63 | let visorInstance: VisorComponent = null; |
| 64 | renderRoot = VisorComponent.render(domNode, renderRoot, { |
| 65 | ref: (r: VisorComponent) => visorInstance = r, |
| 66 | surfaceList: Array.from(surfaceList.values()), |
| 67 | }); |
| 68 | // Side effect of VisorComponent.render() is to assign visorInstance |
| 69 | return visorInstance; |
| 70 | } |
| 71 | |
| 72 | // TODO: consider changing this type. Possibly lift into top level state |
| 73 | // object |
| 74 | const surfaceList: Map<string, SurfaceInfoStrict> = new Map(); |
| 75 | const visorComponentInstance: VisorComponent = |
| 76 | renderVisor(visorEl, surfaceList); |
| 77 | |
| 78 | visorSingleton = |
| 79 | new Visor(visorComponentInstance, visorEl, surfaceList, renderVisor); |
| 80 | |
| 81 | return visorSingleton; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * An instance of the visor. An instance of this class is created using the |
no test coverage detected
searching dependent graphs…