From 24bef7b329fd8d2fce2450926b2b8b376ae5d48c Mon Sep 17 00:00:00 2001 From: Janne Cederberg Date: Wed, 15 Jun 2016 09:05:46 +0300 Subject: [PATCH] Prevent empty values in URL config from stopping panorama rendering Should the user use URL configuration for their panorama and then specify an option/key but leave the value empty, previously this would stop the panorama from being displayed due to the generated JSON config from src/standalone/standalone.js being invalid. This commit omits creating an on-the-fly JSON string and instead uses a JavaScript object (which the generated JSON string was converted to anyway). The background of wanting to enable URL options with possibly empty values relates to using Pannellum with static site generators. Depending on the static site generator, specifying default values for undefined template variables can result in lots of template notation whereas outputting a value for a variable that isn't defined usuallly simply outputs an empty string. It would hence be desirable to treat the empty-string value of an option/key in URL config as if that option/key were not user-supplied at all and hence the Pannellum default value for that option/key be used. --- src/standalone/standalone.js | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/src/standalone/standalone.js b/src/standalone/standalone.js index a04b66b..5e33cfc 100644 --- a/src/standalone/standalone.js +++ b/src/standalone/standalone.js @@ -14,35 +14,29 @@ function parseURLParameters() { return; } URL = URL[0].split('&'); - var json = '{'; + var configFromURL = {}; for (var i = 0; i < URL.length; i++) { var option = URL[i].split('=')[0]; var value = URL[i].split('=')[1]; - json += '"' + option + '":'; + if ( value == '' ) continue; // skip options with empty values in URL config switch(option) { - case 'hfov': case 'pitch': case 'yaw': case 'haov': case 'vaov': + case 'hfov': case 'pitch': case 'yaw': case 'haov': case 'vaov': case 'vOffset': case 'minHfov': case 'maxHfov': case 'minPitch': case 'maxPitch': case 'minYaw': case 'maxYaw': - case 'vOffset': case 'autoRotate': - json += value; + configFromURL[option] = value; break; - case 'autoLoad': case 'ignoreGPanoXMP': - json += JSON.parse(value); + case 'autoLoad': case 'autoRotate': case 'ignoreGPanoXMP': + configFromURL[option] = JSON.parse(value); break; case 'tour': console.log('The `tour` parameter is deprecated and will be removed. Use the `config` parameter instead.') case 'author': case 'title': case 'firstScene': case 'fallback': case 'preview': case 'panorama': case 'config': - json += '"' + decodeURIComponent(value) + '"'; + configFromURL[option] = decodeURIComponent(value); break; default: anError('An invalid configuration parameter was specified: ' + option); } - if (i < URL.length - 1) { - json += ','; - } } - json += '}'; - var configFromURL = JSON.parse(json); var request;