From baed96ee7ce2dd0344f57f61bde099894bb0473b Mon Sep 17 00:00:00 2001 From: Wim Rijnders Date: Fri, 21 Oct 2016 09:58:51 +0200 Subject: [PATCH] Fixes and hardening for Settings.js --- lib/graph3d/Graph3d.js | 3 +-- lib/graph3d/Settings.js | 60 +++++++++++++++++++++++++++++++++++++---- 2 files changed, 56 insertions(+), 7 deletions(-) diff --git a/lib/graph3d/Graph3d.js b/lib/graph3d/Graph3d.js index 1d8515d6..fa8e2b37 100644 --- a/lib/graph3d/Graph3d.js +++ b/lib/graph3d/Graph3d.js @@ -94,7 +94,6 @@ var DEFAULTS = { }; - // ----------------------------------------------------------------------------- // Class Graph3d // ----------------------------------------------------------------------------- @@ -125,7 +124,7 @@ function Graph3d(container, data, options) { // create a frame and canvas this.create(); - Settings.setDefaults(this); + Settings.setDefaults(DEFAULTS, this); // the column indexes this.colX = undefined; diff --git a/lib/graph3d/Settings.js b/lib/graph3d/Settings.js index c8663a18..f1b7da6a 100644 --- a/lib/graph3d/Settings.js +++ b/lib/graph3d/Settings.js @@ -2,6 +2,9 @@ // This modules handles the options for Graph3d. // //////////////////////////////////////////////////////////////////////////////// +var Camera = require('./Camera'); +var Point3d = require('./Point3d'); + // enumerate the available styles var STYLE = { @@ -89,6 +92,26 @@ var PREFIXEDOPTIONKEYS = [ ]; +// Placeholder for DEFAULTS reference +var DEFAULTS = undefined; + + +/** + * Check if given hash is empty. + * + * Source: http://stackoverflow.com/a/679937 + */ +function isEmpty(obj) { + for(var prop in obj) { + if (obj.hasOwnProperty(prop)) + return false; + } + + return true; +} + + + /** * Make first letter of parameter upper case. * @@ -166,13 +189,32 @@ function safeCopy(src, dst, fields, prefix) { } -function setDefaults(dst) { +/** + * Initialize dst with the values in src. + * + * src is the hash with the default values. + * A reference DEFAULTS to this hash is stored locally for + * further handling. + * + * For now, dst is assumed to be a Graph3d instance. + */ +function setDefaults(src, dst) { + if (src === undefined || isEmpty(src)) { + throw new Error('No DEFAULTS passed'); + } + if (dst === undefined) { + throw new Error('No dst passed'); + } + + // Remember defaults for future reference + DEFAULTS = src; + // Handle the defaults which can be simply copied over - forceCopy(DEFAULTS, dst, OPTIONKEYS); - forceCopy(DEFAULTS, dst, PREFIXEDOPTIONKEYS, 'default'); + forceCopy(src, dst, OPTIONKEYS); + forceCopy(src, dst, PREFIXEDOPTIONKEYS, 'default'); // Handle the more complex ('special') fields - setSpecialSettings(DEFAULTS, dst); + setSpecialSettings(src, dst); // Following are internal fields, not part of the user settings dst.margin = 10; // px @@ -187,6 +229,14 @@ function setOptions(options, dst) { if (options === undefined) { return; } + if (dst === undefined) { + throw new Error('No dst passed'); + } + + if (DEFAULTS === undefined || isEmpty(DEFAULTS)) { + throw new Error('DEFAULTS not set for module Settings'); + } + // Handle the parameters which can be simply copied over safeCopy(options, dst, OPTIONKEYS); @@ -253,7 +303,7 @@ function setShowLegend(showLegend, dst) { * when not found */ function getStyleNumberByName(styleName) { - var number = STYLENAME[stylename]; + var number = STYLENAME[styleName]; if (number === undefined) { return -1;