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 2 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.

45 changes: 38 additions & 7 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -87,17 +87,41 @@
schema: {
defaultURL: { type: 'string' }
},
init: function () {
init: async function () {
kfarr marked this conversation as resolved.
Show resolved Hide resolved
// get hash from window
const streetURL = window.location.hash.substring(1);
if (streetURL !== undefined && streetURL.length > 0) {
if (!streetURL) {
return;
}
if (streetURL.includes('streetmix')) {
kfarr marked this conversation as resolved.
Show resolved Hide resolved
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);
}
// 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!");
}
const jsonData = await response.json();
console.log("Loading scene from JSON file");
return jsonData;
} catch (error) {
console.error("Error:", error);
}
}
});

Expand Down Expand Up @@ -134,14 +158,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
3 changes: 2 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 @@ -323,7 +324,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