Skip to content
This repository was archived by the owner on Jul 29, 2019. It is now read-only.

Commit 674813d

Browse files
wimrijndersmojoaxel
authored andcommitted
Uniform handling of common user options (#2168)
1 parent 5740c13 commit 674813d

File tree

1 file changed

+64
-28
lines changed

1 file changed

+64
-28
lines changed

lib/graph3d/Graph3d.js

Lines changed: 64 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,65 @@ var Filter = require('./Filter');
88
var Slider = require('./Slider');
99
var StepNumber = require('./StepNumber');
1010

11+
// -----------------------------------------------------------------------------
12+
// Definitions private to module
13+
// -----------------------------------------------------------------------------
14+
15+
/**
16+
* Field names in the options hash which are of relevance to the user.
17+
*
18+
* Specifically, these are the fields which require no special handling,
19+
* and can be directly copied over.
20+
*/
21+
var OPTIONKEYS = [
22+
'width',
23+
'height',
24+
'filterLabel',
25+
'legendLabel',
26+
'xLabel',
27+
'yLabel',
28+
'zLabel',
29+
'xValueLabel',
30+
'yValueLabel',
31+
'zValueLabel',
32+
'showGrid',
33+
'showPerspective',
34+
'showShadow',
35+
'showAnimationControls',
36+
'keepAspectRatio',
37+
'verticalRatio',
38+
'animationInterval',
39+
'animationPreload',
40+
'animationAutoStart',
41+
'axisColor',
42+
'gridColor'
43+
];
44+
45+
46+
/**
47+
* Copy fields from src to dst in a controlled manner.
48+
*
49+
* Only the fields mentioned in array 'fields' will be copied over,
50+
* and only if these are actually defined.
51+
*
52+
* @param fields array with names of fields to copy
53+
*/
54+
function safeCopy(src, dst, fields) {
55+
for (var i in fields) {
56+
var field = fields[i];
57+
58+
if (src[field] !== undefined) {
59+
dst[field] = src[field];
60+
}
61+
}
62+
}
63+
64+
65+
66+
// -----------------------------------------------------------------------------
67+
// Class Graph3d
68+
// -----------------------------------------------------------------------------
69+
1170
/**
1271
* @constructor Graph3d
1372
* Graph3d displays data in 3d.
@@ -867,46 +926,25 @@ Graph3d.prototype.setOptions = function (options) {
867926

868927
if (options !== undefined) {
869928
// retrieve parameter values
870-
if (options.width !== undefined) this.width = options.width;
871-
if (options.height !== undefined) this.height = options.height;
872929

930+
// Handle the parameters which can be simply copied over
931+
safeCopy(options, this, OPTIONKEYS);
932+
933+
// Handle the rest of the parameters
873934
if (options.xCenter !== undefined) this.defaultXCenter = options.xCenter;
874935
if (options.yCenter !== undefined) this.defaultYCenter = options.yCenter;
875-
876-
if (options.filterLabel !== undefined) this.filterLabel = options.filterLabel;
877-
if (options.legendLabel !== undefined) this.legendLabel = options.legendLabel;
878936
if (options.showLegend !== undefined) this.defaultShowLegend = options.showLegend;
879-
if (options.xLabel !== undefined) this.xLabel = options.xLabel;
880-
if (options.yLabel !== undefined) this.yLabel = options.yLabel;
881-
if (options.zLabel !== undefined) this.zLabel = options.zLabel;
882-
883-
if (options.xValueLabel !== undefined) this.xValueLabel = options.xValueLabel;
884-
if (options.yValueLabel !== undefined) this.yValueLabel = options.yValueLabel;
885-
if (options.zValueLabel !== undefined) this.zValueLabel = options.zValueLabel;
886-
887-
if (options.dotSizeRatio !== undefined) this.dotSizeRatio = options.dotSizeRatio;
888937

889938
if (options.style !== undefined) {
890939
var styleNumber = this._getStyleNumber(options.style);
891940
if (styleNumber !== -1) {
892941
this.style = styleNumber;
893942
}
894943
}
895-
if (options.showGrid !== undefined) this.showGrid = options.showGrid;
896-
if (options.showPerspective !== undefined) this.showPerspective = options.showPerspective;
897-
if (options.showShadow !== undefined) this.showShadow = options.showShadow;
898-
if (options.tooltip !== undefined) this.showTooltip = options.tooltip;
899-
if (options.showAnimationControls !== undefined) this.showAnimationControls = options.showAnimationControls;
900-
if (options.keepAspectRatio !== undefined) this.keepAspectRatio = options.keepAspectRatio;
901-
if (options.verticalRatio !== undefined) this.verticalRatio = options.verticalRatio;
902-
903-
if (options.animationInterval !== undefined) this.animationInterval = options.animationInterval;
904-
if (options.animationPreload !== undefined) this.animationPreload = options.animationPreload;
905-
if (options.animationAutoStart !== undefined)this.animationAutoStart = options.animationAutoStart;
944+
if (options.tooltip !== undefined) this.showTooltip = options.tooltip;
906945

907946
if (options.xBarWidth !== undefined) this.defaultXBarWidth = options.xBarWidth;
908947
if (options.yBarWidth !== undefined) this.defaultYBarWidth = options.yBarWidth;
909-
910948
if (options.xMin !== undefined) this.defaultXMin = options.xMin;
911949
if (options.xStep !== undefined) this.defaultXStep = options.xStep;
912950
if (options.xMax !== undefined) this.defaultXMax = options.xMax;
@@ -928,8 +966,6 @@ Graph3d.prototype.setOptions = function (options) {
928966
}
929967

930968
// colors
931-
if (options.axisColor !== undefined) this.axisColor = options.axisColor;
932-
if (options.gridColor !== undefined) this.gridColor = options.gridColor;
933969
if (options.dataColor) {
934970
if (typeof options.dataColor === 'string') {
935971
this.dataColor.fill = options.dataColor;

0 commit comments

Comments
 (0)