diff --git a/src/json-utils.js b/src/json-utils.js index 7fd7381f4..1b48bb698 100644 --- a/src/json-utils.js +++ b/src/json-utils.js @@ -4,7 +4,7 @@ Takes one or more elements (from a DOM queryselector call) and returns a Javascript object */ -function convertDOMElToObject (entity) { +function convertDOMElToObject(entity) { const data = []; const environmentElement = document.querySelector('#environment'); const referenceEntities = document.querySelector('#reference-layers'); @@ -23,7 +23,7 @@ function convertDOMElToObject (entity) { }; } -function getElementData (entity) { +function getElementData(entity) { if (!entity.isEntity) { return; } @@ -42,7 +42,7 @@ function getElementData (entity) { return elementTree; } -function getAttributes (entity) { +function getAttributes(entity) { const elemObj = {}; elemObj['element'] = entity.tagName.toLowerCase(); @@ -83,7 +83,7 @@ function getAttributes (entity) { return elemObj; } -function toPropString (propData) { +function toPropString(propData) { if ( typeof propData === 'string' || typeof propData === 'number' || @@ -117,11 +117,11 @@ function toPropString (propData) { } } -function isSingleProperty (schema) { +function isSingleProperty(schema) { return AFRAME.schema.isSingleProperty(schema); } -function isEmpty (object) { +function isEmpty(object) { return Object.keys(object).length === 0; } @@ -142,15 +142,15 @@ const renameProps = { intersection: 'not-intersection' }; -function filterJSONstreet (removeProps, renameProps, streetJSON) { - function removeValueCheck (removeVal, value) { +function filterJSONstreet(removeProps, renameProps, streetJSON) { + function removeValueCheck(removeVal, value) { if (AFRAME.utils.deepEqual(removeVal, value) || removeVal === '*') { return true; } return undefined; } - let stringJSON = JSON.stringify(streetJSON, function replacer (key, value) { + let stringJSON = JSON.stringify(streetJSON, function replacer(key, value) { for (var removeKey in removeProps) { // check for removing components if (key === removeKey) { @@ -203,7 +203,7 @@ function filterJSONstreet (removeProps, renameProps, streetJSON) { * @return The value of the component or components' * property coming from mixins of the source. */ -function getMixedValue (component, propertyName, source) { +function getMixedValue(component, propertyName, source) { var value; var reversedMixins = source.mixinEls.reverse(); for (var i = 0; value === undefined && i < reversedMixins.length; i++) { @@ -220,7 +220,7 @@ function getMixedValue (component, propertyName, source) { return [component.name, value]; } -function shallowEqual (object1, object2) { +function shallowEqual(object1, object2) { if ( (typeof object1 === 'string' && typeof object2 === 'string') || (typeof object1 === 'number' && typeof object2 === 'number') @@ -243,7 +243,7 @@ function shallowEqual (object1, object2) { return true; } -function getModifiedProperty (entity, componentName) { +function getModifiedProperty(entity, componentName) { const data = AFRAME.utils.entity.getComponentProperty(entity, componentName); // if it is element's attribute @@ -305,7 +305,7 @@ function getModifiedProperty (entity, componentName) { return diff; } -function createEntities (entitiesData, parentEl) { +function createEntities(entitiesData, parentEl) { const sceneElement = document.querySelector('a-scene'); const removeEntities = ['environment', 'reference-layers']; for (const entityData of entitiesData) { @@ -347,7 +347,7 @@ Add a new entity with a list of components and children (if exists) * @param {Element} parentEl the parent element to which the Entity will be added * @return {Element} Entity created */ -function createEntityFromObj (entityData, parentEl) { +function createEntityFromObj(entityData, parentEl) { const entity = entityData.entityElement || document.createElement(entityData.element); @@ -449,11 +449,6 @@ AFRAME.registerComponent('set-loader-from-hash', { } }, fetchJSON: function (requestURL) { - const sceneId = getUUIDFromPath(requestURL); - if (sceneId) { - console.log('sceneId from fetchJSON from url hash loader', sceneId); - AFRAME.scenes[0].setAttribute('metadata', 'sceneId', sceneId); - } const request = new XMLHttpRequest(); request.open('GET', requestURL, true); request.onload = function () { @@ -469,24 +464,36 @@ AFRAME.registerComponent('set-loader-from-hash', { '200 response received and JSON parsed, now createElementsFromJSON' ); createElementsFromJSON(jsonData); - const sceneId = getUUIDFromPath(requestURL); + let sceneId = getUUIDFromPath(requestURL); if (sceneId) { console.log('sceneId from fetchJSON from url hash loader', sceneId); AFRAME.scenes[0].setAttribute('metadata', 'sceneId', sceneId); } + } else if (this.status === 404) { + console.error( + '[set-loader-from-hash] Error trying to load scene: Resource not found.' + ); + AFRAME.scenes[0].components['notify'].message( + 'Error trying to load scene: Resource not found.', + 'error' + ); } }; request.onerror = function () { // There was a connection error of some sort - console.log( + console.error( 'Loading Error: There was a connection error during JSON loading' ); + AFRAME.scenes[0].components['notify'].message( + 'Could not fetch scene.', + 'error' + ); }; request.send(); } }); -function getUUIDFromPath (path) { +function getUUIDFromPath(path) { // UUID regex pattern: [0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12} const uuidPattern = /[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/; @@ -500,7 +507,7 @@ function getUUIDFromPath (path) { } // this use os text input prompt, delete current scene, then load streetmix file -function inputStreetmix () { +function inputStreetmix() { streetmixURL = prompt( 'Please enter a Streetmix URL', 'https://streetmix.net/kfarr/3/example-street' @@ -521,7 +528,7 @@ function inputStreetmix () { } // JSON loading starts here -function getValidJSON (stringJSON) { +function getValidJSON(stringJSON) { // Preserve newlines, etc. - use valid JSON // Remove non-printable and other non-valid JSON characters return stringJSON @@ -530,16 +537,16 @@ function getValidJSON (stringJSON) { .replace(/[\u0000-\u0019]+/g, ''); } -function createElementsFromJSON (streetJSON) { +function createElementsFromJSON(streetJSON) { let streetObject = {}; - if (typeof streetJSON === 'string') { + if (typeof streetJSON == 'string') { const validJSONString = getValidJSON(streetJSON); streetObject = JSON.parse(validJSONString); - } else if (typeof streetJSON === 'object') { + } else if (typeof streetJSON == 'object') { streetObject = streetJSON; } - const sceneTitle = streetObject.title; + let sceneTitle = streetObject.title; if (sceneTitle) { console.log('sceneTitle from createElementsFromJSON', sceneTitle); AFRAME.scenes[0].setAttribute('metadata', 'sceneTitle', sceneTitle); @@ -558,7 +565,7 @@ function createElementsFromJSON (streetJSON) { } // viewer widget click to paste json string of 3dstreet scene -function inputJSON () { +function inputJSON() { const stringJSON = prompt('Please paste 3DStreet JSON string'); if (stringJSON) { createElementsFromJSON(stringJSON); @@ -566,8 +573,8 @@ function inputJSON () { } // handle viewer widget click to open 3dstreet json scene -function fileJSON () { - const reader = new FileReader(); +function fileJSON() { + let reader = new FileReader(); reader.onload = function () { AFRAME.scenes[0].setAttribute('metadata', 'sceneId', ''); AFRAME.scenes[0].setAttribute('metadata', 'sceneTitle', '');