瀏覽代碼

Strict null checks FTW!

Raanan Weber 7 年之前
父節點
當前提交
e66d77db97
共有 5 個文件被更改,包括 71 次插入25 次删除
  1. 32 11
      Viewer/dist/viewer.js
  2. 11 6
      Viewer/src/templateManager.ts
  3. 22 7
      Viewer/src/viewer/defaultViewer.ts
  4. 5 1
      Viewer/src/viewer/viewer.ts
  5. 1 0
      Viewer/tsconfig.json

+ 32 - 11
Viewer/dist/viewer.js

@@ -73996,7 +73996,8 @@ var AbstractViewer = (function () {
         this.prepareContainerElement();
         loader_1.default.loadConfiguration(initialConfiguration).then(function (configuration) {
             _this.configuration = configuration;
-            _this.templateManager.initTemplate(_this.configuration.template);
+            var templateConfiguration = _this.configuration.template || {};
+            _this.templateManager.initTemplate(templateConfiguration);
             _this.templateManager.onAllLoaded.add(function () {
                 _this.onTemplatesLoaded();
             });
@@ -74016,6 +74017,9 @@ var AbstractViewer = (function () {
     AbstractViewer.prototype.initEngine = function () {
         var _this = this;
         var canvasElement = this.templateManager.getCanvas();
+        if (!canvasElement) {
+            return Promise.reject('Canvas element not found!');
+        }
         var config = this.configuration.engine || {};
         this.engine = new babylonjs_1.Engine(canvasElement, !!config.antialiasing);
         window.addEventListener('resize', function () {
@@ -74280,7 +74284,7 @@ var TemplateManager = (function () {
         this.templates[name] = template;
         var childrenMap = configuration.children || {};
         var childrenTemplates = Object.keys(childrenMap).map(function (name) {
-            return _this.initTemplate(configuration.children[name], name, template);
+            return _this.initTemplate(childrenMap[name], name, template);
         });
         template.onLoaded.add(function () {
             var addToParent = function () {
@@ -74391,7 +74395,7 @@ var Template = (function () {
         var _this = this;
         if (this.configuration.events) {
             Object.keys(this.configuration.events).forEach(function (eventName) {
-                if (_this.configuration.events[eventName]) {
+                if (_this.configuration.events && _this.configuration.events[eventName]) {
                     var functionToFire_1 = function (selector, event) {
                         _this.onEventTriggered.notifyObservers({ event: event, template: _this, selector: selector });
                     };
@@ -74414,7 +74418,7 @@ var Template = (function () {
 exports.Template = Template;
 function getTemplateAsHtml(templateConfig) {
     if (!templateConfig) {
-        return Promise.resolve(undefined);
+        return Promise.reject('No templateConfig provided');
     }
     else if (templateConfig.html) {
         return Promise.resolve(templateConfig.html);
@@ -74426,7 +74430,13 @@ function getTemplateAsHtml(templateConfig) {
         }
         else {
             location_1 = location_1.replace('#', '');
-            document.getElementById('#' + location_1);
+            var element = document.getElementById('#' + location_1);
+            if (element) {
+                return Promise.resolve(element.innerHTML);
+            }
+            else {
+                return Promise.reject('Template ID not found');
+            }
         }
     }
 }
@@ -103292,12 +103302,18 @@ var DefaultViewer = (function (_super) {
     DefaultViewer.prototype.setModelMetaData = function () {
         var navbar = this.templateManager.getTemplate('navBar');
         var metadataContainer = navbar.parent.querySelector('#model-metadata');
-        if (typeof this.configuration.model === 'object') {
+        if (metadataContainer && typeof this.configuration.model === 'object') {
             if (this.configuration.model.title) {
-                metadataContainer.querySelector('span.model-title').innerHTML = this.configuration.model.title;
+                var element = metadataContainer.querySelector('span.model-title');
+                if (element) {
+                    element.innerHTML = this.configuration.model.title;
+                }
             }
             if (this.configuration.model.subtitle) {
-                metadataContainer.querySelector('span.model-subtitle').innerHTML = this.configuration.model.subtitle;
+                var element = metadataContainer.querySelector('span.model-subtitle');
+                if (element) {
+                    element.innerHTML = this.configuration.model.subtitle;
+                }
             }
             if (this.configuration.model.thumbnail) {
                 metadataContainer.querySelector('.thumbnail').style.backgroundImage = "url('" + this.configuration.model.thumbnail + "')";
@@ -103369,7 +103385,8 @@ var DefaultViewer = (function (_super) {
     DefaultViewer.prototype.setupLights = function (focusMeshes) {
         var _this = this;
         if (focusMeshes === void 0) { focusMeshes = []; }
-        if (!this.configuration.scene.defaultLight && (this.configuration.lights && this.configuration.lights.length)) {
+        var sceneConfig = this.configuration.scene || { defaultLight: true };
+        if (!sceneConfig.defaultLight && (this.configuration.lights && this.configuration.lights.length)) {
             this.scene.lights.forEach(function (l) {
                 l.dispose();
             });
@@ -103396,7 +103413,8 @@ var DefaultViewer = (function (_super) {
     DefaultViewer.prototype.setupCamera = function (focusMeshes) {
         var _this = this;
         if (focusMeshes === void 0) { focusMeshes = []; }
-        if (this.configuration.scene.defaultCamera) {
+        var sceneConfig = this.configuration.scene || { autoRotate: false, defaultCamera: true };
+        if (sceneConfig.defaultCamera) {
             return;
         }
         var cameraConfig = this.configuration.camera || {};
@@ -103414,7 +103432,7 @@ var DefaultViewer = (function (_super) {
             });
         }
         ;
-        if (this.configuration.scene.autoRotate) {
+        if (sceneConfig.autoRotate) {
             this.camera.useAutoRotationBehavior = true;
         }
     };
@@ -103432,6 +103450,9 @@ var DefaultViewer = (function (_super) {
             case 2:
                 behavior = new babylonjs_1.FramingBehavior();
                 break;
+            default:
+                behavior = null;
+                break;
         }
         if (behavior) {
             if (typeof behaviorConfig === "object") {

+ 11 - 6
Viewer/src/templateManager.ts

@@ -24,7 +24,7 @@ export interface TemplateConfiguration {
         dragstart?: boolean | Array<string>;
         drop?: boolean | Array<string>;
 
-        [key: string]: boolean | Array<string>;
+        [key: string]: boolean | Array<string> | undefined;
     }
     children?: { [name: string]: TemplateConfiguration };
 }
@@ -61,7 +61,7 @@ export class TemplateManager {
 
         let childrenMap = configuration.children || {};
         let childrenTemplates = Object.keys(childrenMap).map(name => {
-            return this.initTemplate(configuration.children[name], name, template);
+            return this.initTemplate(childrenMap[name], name, template);
         });
 
         // register the observers
@@ -85,7 +85,7 @@ export class TemplateManager {
     }
 
     // assumiung only ONE(!) canvas
-    public getCanvas(): HTMLCanvasElement {
+    public getCanvas(): HTMLCanvasElement | null {
         return this.containerElement.querySelector('canvas');
     }
 
@@ -201,7 +201,7 @@ export class Template {
     private registerEvents() {
         if (this.configuration.events) {
             Object.keys(this.configuration.events).forEach(eventName => {
-                if (this.configuration.events[eventName]) {
+                if (this.configuration.events && this.configuration.events[eventName]) {
                     let functionToFire = (selector, event) => {
                         this.onEventTriggered.notifyObservers({ event: event, template: this, selector: selector });
                     }
@@ -225,7 +225,7 @@ export class Template {
 
 export function getTemplateAsHtml(templateConfig: TemplateConfiguration): Promise<string> {
     if (!templateConfig) {
-        return Promise.resolve(undefined);
+        return Promise.reject('No templateConfig provided');
     } else if (templateConfig.html) {
         return Promise.resolve(templateConfig.html);
     } else {
@@ -234,7 +234,12 @@ export function getTemplateAsHtml(templateConfig: TemplateConfiguration): Promis
             return loadFile(location);
         } else {
             location = location.replace('#', '');
-            document.getElementById('#' + location);
+            let element = document.getElementById('#' + location);
+            if (element) {
+                return Promise.resolve(element.innerHTML);
+            } else {
+                return Promise.reject('Template ID not found');
+            }
         }
     }
 }

+ 22 - 7
Viewer/src/viewer/defaultViewer.ts

@@ -113,13 +113,19 @@ export class DefaultViewer extends AbstractViewer {
         let metadataContainer = navbar.parent.querySelector('#model-metadata');
 
         //title
-        if (typeof this.configuration.model === 'object') {
+        if (metadataContainer && typeof this.configuration.model === 'object') {
             if (this.configuration.model.title) {
-                metadataContainer.querySelector('span.model-title').innerHTML = this.configuration.model.title;
+                let element = metadataContainer.querySelector('span.model-title');
+                if (element) {
+                    element.innerHTML = this.configuration.model.title;
+                }
             }
 
             if (this.configuration.model.subtitle) {
-                metadataContainer.querySelector('span.model-subtitle').innerHTML = this.configuration.model.subtitle;
+                let element = metadataContainer.querySelector('span.model-subtitle');
+                if (element) {
+                    element.innerHTML = this.configuration.model.subtitle;
+                }
             }
 
             if (this.configuration.model.thumbnail) {
@@ -208,7 +214,10 @@ export class DefaultViewer extends AbstractViewer {
     }
 
     private setupLights(focusMeshes: Array<AbstractMesh> = []) {
-        if (!this.configuration.scene.defaultLight && (this.configuration.lights && this.configuration.lights.length)) {
+
+        let sceneConfig = this.configuration.scene || { defaultLight: true };
+
+        if (!sceneConfig.defaultLight && (this.configuration.lights && this.configuration.lights.length)) {
             // remove old lights
             this.scene.lights.forEach(l => {
                 l.dispose();
@@ -242,7 +251,10 @@ export class DefaultViewer extends AbstractViewer {
     }
 
     private setupCamera(focusMeshes: Array<AbstractMesh> = []) {
-        if (this.configuration.scene.defaultCamera) {
+
+        let sceneConfig = this.configuration.scene || { autoRotate: false, defaultCamera: true };
+
+        if (sceneConfig.defaultCamera) {
             return;
         }
 
@@ -265,7 +277,7 @@ export class DefaultViewer extends AbstractViewer {
             });
         };
 
-        if (this.configuration.scene.autoRotate) {
+        if (sceneConfig.autoRotate) {
             this.camera.useAutoRotationBehavior = true;
         }
     }
@@ -275,7 +287,7 @@ export class DefaultViewer extends AbstractViewer {
         [propName: string]: any;
     }, payload: any) {
 
-        let behavior: Behavior<ArcRotateCamera>;
+        let behavior: Behavior<ArcRotateCamera> | null;
         let type = (typeof behaviorConfig !== "object") ? behaviorConfig : behaviorConfig.type;
 
         let config: { [propName: string]: any } = (typeof behaviorConfig === "object") ? behaviorConfig : {};
@@ -291,6 +303,9 @@ export class DefaultViewer extends AbstractViewer {
             case CameraBehavior.FRAMING:
                 behavior = new FramingBehavior();
                 break;
+            default:
+                behavior = null;
+                break;
         }
 
         if (behavior) {

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

@@ -29,7 +29,8 @@ export abstract class AbstractViewer {
         configurationLoader.loadConfiguration(initialConfiguration).then((configuration) => {
             this.configuration = configuration;
             // initialize the templates
-            this.templateManager.initTemplate(this.configuration.template);
+            let templateConfiguration = this.configuration.template || {};
+            this.templateManager.initTemplate(templateConfiguration);
             // when done, execute onTemplatesLoaded()
             this.templateManager.onAllLoaded.add(() => {
                 this.onTemplatesLoaded();
@@ -69,6 +70,9 @@ export abstract class AbstractViewer {
      */
     protected initEngine(): Promise<Engine> {
         let canvasElement = this.templateManager.getCanvas();
+        if (!canvasElement) {
+            return Promise.reject('Canvas element not found!');
+        }
         let config = this.configuration.engine || {};
         // TDO enable further configuration
         this.engine = new Engine(canvasElement, !!config.antialiasing);

+ 1 - 0
Viewer/tsconfig.json

@@ -4,6 +4,7 @@
         "module": "commonjs",
         "noResolve": true,
         "noImplicitAny": false, //mainly due to usage of external libs without typings.
+        "strictNullChecks": true,
         "removeComments": true,
         "preserveConstEnums": true,
         "sourceMap": true,