Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add first version to load JSON from URL #304

Merged
merged 5 commits into from
Jun 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/json/crash-scene.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions examples/json/intersection-demo.json

Large diffs are not rendered by default.

48 changes: 41 additions & 7 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,41 @@
init: function () {
// get hash from window
const streetURL = window.location.hash.substring(1);
if (streetURL !== undefined && streetURL.length > 0) {
console.log('[set-loader-from-hash]','Using URL from hash', streetURL)
if (!streetURL) {
return;
}
if (streetURL.includes('//streetmix.net')) {
console.log('[set-loader-from-hash]','Set streetmix-loader streetmixStreetURL to', streetURL)
this.el.setAttribute('streetmix-loader', 'streetmixStreetURL', streetURL);
}
} else {
// try to load JSON file from remote resource
console.log('[set-loader-from-hash]','Load 3DStreet scene with fetchJSON from', streetURL)
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: 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('[set-loader-from-hash]', '200 response received and JSON parsed, now createElementsFromJSON');
createElementsFromJSON(jsonData);
}
};
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();
}
});

Expand Down Expand Up @@ -134,14 +161,21 @@
.replace(/[\u0000-\u0019]+/g,"");
}

function createElementsFromJSON(streetJSONString) {
const validJSONString = getValidJSON(streetJSONString);
function createElementsFromJSON(streetJSON) {
let streetObject = {};
if (typeof streetJSON == 'string') {
const validJSONString = getValidJSON(streetJSON);
streetObject = JSON.parse(validJSONString);
} else if (typeof streetJSON == 'object') {
streetObject = streetJSON;
}

streetContainerEl = document.getElementById('street-container');
while (streetContainerEl.firstChild) {
streetContainerEl.removeChild(streetContainerEl.lastChild);
}
var streetObject = JSON.parse(validJSONString);
createEntities(streetObject.data[0].children, streetContainerEl);

createEntities(streetObject.data, streetContainerEl);
}

function inputJSON() {
Expand Down
9 changes: 8 additions & 1 deletion src/json-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ function isEmpty (object) {
const removeProps = {
src: {},
normalMap: {},
'set-loader-from-hash': '*',
'create-from-json': '*',
street: { JSON: '*' }
};
Expand Down Expand Up @@ -275,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);
}
}
Expand Down Expand Up @@ -323,7 +330,7 @@ function createEntityFromObj (entityData, parentEl) {
entity.setAttribute('mixin', entityData.mixin);
}
// Ensure the components are loaded before update the UI
entity.emit('entitycreated', { element: entityData.element, components: entity.components }, false);
entity.emit('entitycreated', {}, false);
});

if (entityData.children) {
Expand Down