From 0dcd8ac78012d88bd3b85852ee5c89a9043f3ea9 Mon Sep 17 00:00:00 2001 From: Alexander Goryushkin Date: Sat, 24 Jun 2023 23:19:01 -0400 Subject: [PATCH] fix repeatedly component init issue --- index.html | 40 +++++++++++++++++++++------------------- src/json-utils.js | 6 ++++++ 2 files changed, 27 insertions(+), 19 deletions(-) diff --git a/index.html b/index.html index 0950ea1eb..ab0417961 100644 --- a/index.html +++ b/index.html @@ -87,41 +87,43 @@ schema: { defaultURL: { type: 'string' } }, - init: async function () { + init: function () { // get hash from window const streetURL = window.location.hash.substring(1); if (!streetURL) { return; } - if (streetURL.includes('streetmix')) { + if (streetURL.includes('//streetmix.net')) { console.log('[set-loader-from-hash]','Using URL from hash', streetURL) this.el.setAttribute('streetmix-loader', 'streetmixStreetURL', streetURL); } else { // try to load JSON file from remote resource - const jsonData = await this.fetchJSON(streetURL); - createElementsFromJSON(jsonData); + this.fetchJSON(streetURL); } // else { // console.log('[set-loader-from-hash]','Using default URL', this.data.defaultURL) // this.el.setAttribute('streetmix-loader', 'streetmixStreetURL', this.data.defaultURL); // } }, - fetchJSON: async function (request) { - try { - const response = await fetch(request, { - mode: "no-cors", - credentials: "omit" - }); - const contentType = response.headers.get("content-type"); - if (!contentType || !contentType.includes("application/json")) { - console.log("Oops, we haven't got JSON!"); + fetchJSON: function (requestURL) { + const request = new XMLHttpRequest(); + request.open('GET', requestURL, true); + request.onload = function () { + if (this.status >= 200 && this.status < 400) { + // Connection success + // remove 'set-loader-from-hash' component from json data + const jsonData = JSON.parse(this.response, + (key, value) => (key === 'set-loader-from-hash') ? undefined : value ); + + console.log("loading 3D-Street from JSON file"); + createElementsFromJSON(jsonData); } - const jsonData = await response.json(); - console.log("Loading scene from JSON file"); - return jsonData; - } catch (error) { - console.error("Error:", error); - } + }; + request.onerror = function () { + // There was a connection error of some sort + console.log('Loading Error: There was a connection error during JSON loading'); + }; + request.send(); } }); diff --git a/src/json-utils.js b/src/json-utils.js index fe17a6e22..5407767c6 100644 --- a/src/json-utils.js +++ b/src/json-utils.js @@ -276,6 +276,12 @@ function getModifiedProperty (entity, componentName) { function createEntities (entitiesData, parentEl) { for (const entityData of entitiesData) { + if (entityData.id === 'street-container' && + entityData.children && + entityData.children[0].id === 'default-street' && + entityData.children[0].components.hasOwnProperty('set-loader-from-hash')) { + delete entityData.children[0].components['set-loader-from-hash']; + } createEntityFromObj(entityData, parentEl); } }