Browse Source

Fixes and hardening for Settings.js

codeClimate
Wim Rijnders 8 years ago
parent
commit
baed96ee7c
2 changed files with 56 additions and 7 deletions
  1. +1
    -2
      lib/graph3d/Graph3d.js
  2. +55
    -5
      lib/graph3d/Settings.js

+ 1
- 2
lib/graph3d/Graph3d.js View File

@ -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;

+ 55
- 5
lib/graph3d/Settings.js View File

@ -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;

Loading…
Cancel
Save