Skip to content

Commit 7085729

Browse files
committed
resolved circular dependency by extracting the crawler
1 parent 57d0c13 commit 7085729

File tree

5 files changed

+67
-35
lines changed

5 files changed

+67
-35
lines changed

src/lib/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ var lib = module.exports = {};
1515

1616
lib.nestedProperty = require('./nested_property');
1717
lib.isPlainObject = require('./is_plain_object');
18+
lib.isValObject = require('./is_val_object');
1819
lib.isArray = require('./is_array');
1920

2021
var coerceModule = require('./coerce');

src/lib/is_val_object.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* Copyright 2012-2016, Plotly, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the MIT license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
9+
10+
'use strict';
11+
12+
// returns true for a valid value object and false for tree nodes in the attribute hierarchy
13+
module.exports = function(obj) {
14+
return obj && obj.valType !== undefined;
15+
};

src/plot_api/crawler.js

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* Copyright 2012-2016, Plotly, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the MIT license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
9+
10+
'use strict';
11+
12+
var Lib = require('../lib');
13+
14+
var crawler = module.exports = {};
15+
16+
crawler.IS_SUBPLOT_OBJ = '_isSubplotObj';
17+
crawler.IS_LINKED_TO_ARRAY = '_isLinkedToArray';
18+
crawler.DEPRECATED = '_deprecated';
19+
20+
// list of underscore attributes to keep in schema as is
21+
crawler.UNDERSCORE_ATTRS = [crawler.IS_SUBPLOT_OBJ, crawler.IS_LINKED_TO_ARRAY, crawler.DEPRECATED];
22+
23+
crawler.crawl = function(attrs, callback, specifiedLevel) {
24+
var level = specifiedLevel || 0;
25+
Object.keys(attrs).forEach(function(attrName) {
26+
var attr = attrs[attrName];
27+
28+
if(crawler.UNDERSCORE_ATTRS.indexOf(attrName) !== -1) return;
29+
30+
callback(attr, attrName, attrs, level);
31+
32+
if(Lib.isValObject(attr)) return;
33+
if(Lib.isPlainObject(attr)) crawler.crawl(attr, callback, level + 1);
34+
});
35+
};

src/plot_api/plot_schema.js

+13-32
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ var Plotly = require('../plotly');
1313
var Registry = require('../registry');
1414
var Plots = require('../plots/plots');
1515
var Lib = require('../lib');
16+
var crawler = require('./crawler');
1617

1718
// FIXME polar attribute are not part of Plotly yet
1819
var polarAreaAttrs = require('../plots/polar/area_attributes');
@@ -23,13 +24,7 @@ var extendDeep = Lib.extendDeep;
2324
var extendDeepAll = Lib.extendDeepAll;
2425

2526
var NESTED_MODULE = '_nestedModules',
26-
COMPOSED_MODULE = '_composedModules',
27-
IS_SUBPLOT_OBJ = '_isSubplotObj',
28-
IS_LINKED_TO_ARRAY = '_isLinkedToArray',
29-
DEPRECATED = '_deprecated';
30-
31-
// list of underscore attributes to keep in schema as is
32-
var UNDERSCORE_ATTRS = [IS_SUBPLOT_OBJ, IS_LINKED_TO_ARRAY, DEPRECATED];
27+
COMPOSED_MODULE = '_composedModules';
3328

3429
var plotSchema = {
3530
traces: {},
@@ -56,23 +51,9 @@ PlotSchema.get = function() {
5651
return plotSchema;
5752
};
5853

59-
PlotSchema.crawl = function (attrs, callback, specifiedLevel) {
60-
var level = specifiedLevel || 0;
61-
Object.keys(attrs).forEach(function(attrName) {
62-
var attr = attrs[attrName];
63-
64-
if(UNDERSCORE_ATTRS.indexOf(attrName) !== -1) return;
54+
PlotSchema.crawl = crawler.crawl;
6555

66-
callback(attr, attrName, attrs, level);
67-
68-
if(PlotSchema.isValObject(attr)) return;
69-
if(Lib.isPlainObject(attr)) PlotSchema.crawl(attr, callback, level + 1);
70-
});
71-
};
72-
73-
PlotSchema.isValObject = function(obj) {
74-
return obj && obj.valType !== undefined;
75-
};
56+
PlotSchema.isValObject = Lib.isValObject;
7657

7758
function getTraceAttributes(type) {
7859
var globalAttributes = Plots.attributes,
@@ -132,13 +113,13 @@ function getLayoutAttributes() {
132113
// FIXME polar layout attributes
133114
layoutAttributes = assignPolarLayoutAttrs(layoutAttributes);
134115

135-
// add IS_SUBPLOT_OBJ attribute
116+
// add crawler.IS_SUBPLOT_OBJ attribute
136117
layoutAttributes = handleSubplotObjs(layoutAttributes);
137118

138119
layoutAttributes = removeUnderscoreAttrs(layoutAttributes);
139120
mergeValTypeAndRole(layoutAttributes);
140121

141-
// generate IS_LINKED_TO_ARRAY structure
122+
// generate crawler.IS_LINKED_TO_ARRAY structure
142123
handleLinkedToArray(layoutAttributes);
143124

144125
plotSchema.layout = { layoutAttributes: layoutAttributes };
@@ -159,7 +140,7 @@ function getTransformAttributes(name) {
159140
function getDefs() {
160141
plotSchema.defs = {
161142
valObjects: Lib.valObjects,
162-
metaKeys: UNDERSCORE_ATTRS.concat(['description', 'role'])
143+
metaKeys: crawler.UNDERSCORE_ATTRS.concat(['description', 'role'])
163144
};
164145
}
165146

@@ -242,7 +223,7 @@ function mergeValTypeAndRole(attrs) {
242223
}
243224
}
244225

245-
PlotSchema.crawl(attrs, callback);
226+
crawler.crawl(attrs, callback);
246227
}
247228

248229
// helper methods
@@ -268,7 +249,7 @@ function getModule(arg) {
268249
function removeUnderscoreAttrs(attributes) {
269250
Object.keys(attributes).forEach(function(k) {
270251
if(k.charAt(0) === '_' &&
271-
UNDERSCORE_ATTRS.indexOf(k) === -1) delete attributes[k];
252+
crawler.UNDERSCORE_ATTRS.indexOf(k) === -1) delete attributes[k];
272253
});
273254
return attributes;
274255
}
@@ -322,7 +303,7 @@ function handleSubplotObjs(layoutAttributes) {
322303
isSubplotObj = subplotRegistry.attrRegex.test(k);
323304
}
324305

325-
if(isSubplotObj) layoutAttributes[k][IS_SUBPLOT_OBJ] = true;
306+
if(isSubplotObj) layoutAttributes[k][crawler.IS_SUBPLOT_OBJ] = true;
326307
});
327308
});
328309

@@ -332,17 +313,17 @@ function handleSubplotObjs(layoutAttributes) {
332313
function handleLinkedToArray(layoutAttributes) {
333314

334315
function callback(attr, attrName, attrs) {
335-
if(attr[IS_LINKED_TO_ARRAY] !== true) return;
316+
if(attr[crawler.IS_LINKED_TO_ARRAY] !== true) return;
336317

337318
// TODO more robust logic
338319
var itemName = attrName.substr(0, attrName.length - 1);
339320

340-
delete attr[IS_LINKED_TO_ARRAY];
321+
delete attr[crawler.IS_LINKED_TO_ARRAY];
341322

342323
attrs[attrName] = { items: {} };
343324
attrs[attrName].items[itemName] = attr;
344325
attrs[attrName].role = 'object';
345326
}
346327

347-
PlotSchema.crawl(layoutAttributes, callback);
328+
crawler.crawl(layoutAttributes, callback);
348329
}

src/plots/plots.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
'use strict';
1111

12-
var PlotSchema = require('./../plot_api/plot_schema')
12+
var crawler = require('./../plot_api/crawler');
1313
var d3 = require('d3');
1414
var isNumeric = require('fast-isnumeric');
1515

@@ -797,13 +797,13 @@ function applyTransforms(fullTrace, fullData, layout) {
797797

798798
stack = stack.slice(0, level).concat([attrName]);
799799

800-
var splittableAttr = attr.valType === 'data_array' || attr.arrayOk === true
800+
var splittableAttr = attr.valType === 'data_array' || attr.arrayOk === true;
801801
if(splittableAttr) {
802802
arraySplitAttributes.push(stack.slice());
803803
}
804804
}
805805

806-
PlotSchema.crawl(trace._module.attributes, callback);
806+
crawler.crawl(trace._module.attributes, callback);
807807

808808
return arraySplitAttributes;
809809
});

0 commit comments

Comments
 (0)