Browse Source

Merge pull request #3080 from RaananW/viewer-changes

Viewer public API extension
Raanan Weber 7 years ago
parent
commit
55734ee8a4

+ 3 - 1
Viewer/src/index.ts

@@ -1,3 +1,5 @@
+import { viewerManager } from './viewer/viewerManager';
+import { DefaultViewer } from './viewer/defaultViewer';
 import { AbstractViewer } from './viewer/viewer';
 
 /**
@@ -25,4 +27,4 @@ setTimeout(() => {
 });
 
 // public API for initialization
-export { InitTags };
+export { InitTags, DefaultViewer, AbstractViewer, viewerManager };

+ 1 - 1
Viewer/src/viewer/defaultViewer.ts

@@ -9,7 +9,7 @@ window['BABYLON'] = BABYLON;
 
 export class DefaultViewer extends AbstractViewer {
 
-    private camera: ArcRotateCamera;
+    public camera: ArcRotateCamera;
 
     public initScene(): Promise<Scene> {
         return super.initScene().then(() => {

+ 5 - 0
Viewer/src/viewer/viewer.ts

@@ -1,3 +1,4 @@
+import { viewerManager } from './viewerManager';
 import { TemplateManager } from './../templateManager';
 import configurationLoader from './../configuration/loader';
 import { Observable, Engine, Scene, ArcRotateCamera, Vector3, SceneLoader, AbstractMesh, Mesh, HemisphericLight } from 'babylonjs';
@@ -21,6 +22,10 @@ export abstract class AbstractViewer {
             this.baseId = containerElement.id = 'bjs' + Math.random().toString(32).substr(2, 8);
         }
 
+        // add this viewer to the viewer manager
+        viewerManager.addViewer(this);
+
+        // create a new template manager. TODO - singleton?
         this.templateManager = new TemplateManager(containerElement);
 
         this.prepareContainerElement();

+ 28 - 0
Viewer/src/viewer/viewerManager.ts

@@ -0,0 +1,28 @@
+import { AbstractViewer } from './viewer';
+
+class ViewerManager {
+
+    private viewers: { [key: string]: AbstractViewer };
+
+    constructor() {
+        this.viewers = {};
+    }
+
+    public addViewer(viewer: AbstractViewer) {
+        this.viewers[viewer.getBaseId()] = viewer;
+    }
+
+    public getViewerById(id: string): AbstractViewer {
+        return this.viewers[id];
+    }
+
+    public getViewerByHTMLElement(element: HTMLElement) {
+        for (let id in this.viewers) {
+            if (this.viewers[id].containerElement === element) {
+                return this.getViewerById(id);
+            }
+        }
+    }
+}
+
+export let viewerManager = new ViewerManager();