Skip to content

Commit 2e6c030

Browse files
committed
break up new circular deps
1 parent d55568c commit 2e6c030

File tree

10 files changed

+162
-107
lines changed

10 files changed

+162
-107
lines changed

src/lib/identity.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* Copyright 2012-2017, 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+
'use strict';
10+
11+
// Simple helper functions
12+
// none of these need any external deps
13+
14+
module.exports = function identity(d) { return d; };

src/lib/index.js

Lines changed: 4 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,13 @@ lib.notifier = require('./notifier');
8181

8282
lib.filterUnique = require('./filter_unique');
8383
lib.filterVisible = require('./filter_visible');
84-
84+
lib.pushUnique = require('./push_unique');
8585

8686
lib.cleanNumber = require('./clean_number');
8787

88+
lib.noop = require('./noop');
89+
lib.identity = require('./identity');
90+
8891
/**
8992
* swap x and y of the same attribute in container cont
9093
* specify attr with a ? in place of x/y
@@ -136,12 +139,6 @@ lib.bBoxIntersect = function(a, b, pad) {
136139
b.top <= a.bottom + pad);
137140
};
138141

139-
// minor convenience/performance booster for d3...
140-
lib.identity = function(d) { return d; };
141-
142-
// minor convenience helper
143-
lib.noop = function() {};
144-
145142
/*
146143
* simpleMap: alternative to Array.map that only
147144
* passes on the element and up to 2 extra args you
@@ -338,33 +335,6 @@ lib.noneOrAll = function(containerIn, containerOut, attrList) {
338335
}
339336
};
340337

341-
/**
342-
* Push array with unique items
343-
*
344-
* @param {array} array
345-
* array to be filled
346-
* @param {any} item
347-
* item to be or not to be inserted
348-
* @return {array}
349-
* ref to array (now possibly containing one more item)
350-
*
351-
*/
352-
lib.pushUnique = function(array, item) {
353-
if(item instanceof RegExp) {
354-
var itemStr = item.toString(),
355-
i;
356-
for(i = 0; i < array.length; i++) {
357-
if(array[i] instanceof RegExp && array[i].toString() === itemStr) {
358-
return array;
359-
}
360-
}
361-
array.push(item);
362-
}
363-
else if(item && array.indexOf(item) === -1) array.push(item);
364-
365-
return array;
366-
};
367-
368338
lib.mergeArray = function(traceAttr, cd, cdAttr) {
369339
if(Array.isArray(traceAttr)) {
370340
var imax = Math.min(traceAttr.length, cd.length);

src/lib/nested_property.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
var isNumeric = require('fast-isnumeric');
1313
var isArray = require('./is_array');
1414
var isPlainObject = require('./is_plain_object');
15-
var containerArrayMatch = require('../plot_api/manage_arrays').containerArrayMatch;
15+
var containerArrayMatch = require('../plot_api/container_array_match');
1616

1717
/**
1818
* convert a string s (such as 'xaxis.range[0]')

src/lib/noop.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* Copyright 2012-2017, 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+
'use strict';
10+
11+
// Simple helper functions
12+
// none of these need any external deps
13+
14+
module.exports = function noop() {};

src/lib/push_unique.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Copyright 2012-2017, 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+
'use strict';
10+
11+
/**
12+
* Push array with unique items
13+
*
14+
* @param {array} array
15+
* array to be filled
16+
* @param {any} item
17+
* item to be or not to be inserted
18+
* @return {array}
19+
* ref to array (now possibly containing one more item)
20+
*
21+
*/
22+
module.exports = function pushUnique(array, item) {
23+
if(item instanceof RegExp) {
24+
var itemStr = item.toString(),
25+
i;
26+
for(i = 0; i < array.length; i++) {
27+
if(array[i] instanceof RegExp && array[i].toString() === itemStr) {
28+
return array;
29+
}
30+
}
31+
array.push(item);
32+
}
33+
else if(item && array.indexOf(item) === -1) array.push(item);
34+
35+
return array;
36+
};

src/plot_api/container_array_match.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
* Copyright 2012-2017, 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 Registry = require('../registry');
13+
14+
/*
15+
* containerArrayMatch: does this attribute string point into a
16+
* layout container array?
17+
*
18+
* @param {String} astr: an attribute string, like *annotations[2].text*
19+
*
20+
* @returns {Object | false} Returns false if `astr` doesn't match a container
21+
* array. If it does, returns:
22+
* {array: {String}, index: {Number}, property: {String}}
23+
* ie the attribute string for the array, the index within the array (or ''
24+
* if the whole array) and the property within that (or '' if the whole array
25+
* or the whole object)
26+
*/
27+
module.exports = function containerArrayMatch(astr) {
28+
var rootContainers = Registry.layoutArrayContainers,
29+
regexpContainers = Registry.layoutArrayRegexes,
30+
rootPart = astr.split('[')[0],
31+
arrayStr,
32+
match;
33+
34+
// look for regexp matches first, because they may be nested inside root matches
35+
// eg updatemenus[i].buttons is nested inside updatemenus
36+
for(var i = 0; i < regexpContainers.length; i++) {
37+
match = astr.match(regexpContainers[i]);
38+
if(match && match.index === 0) {
39+
arrayStr = match[0];
40+
break;
41+
}
42+
}
43+
44+
// now look for root matches
45+
if(!arrayStr) arrayStr = rootContainers[rootContainers.indexOf(rootPart)];
46+
47+
if(!arrayStr) return false;
48+
49+
var tail = astr.substr(arrayStr.length);
50+
if(!tail) return {array: arrayStr, index: '', property: ''};
51+
52+
match = tail.match(/^\[(0|[1-9][0-9]*)\](\.(.+))?$/);
53+
if(!match) return false;
54+
55+
return {array: arrayStr, index: Number(match[1]), property: match[3] || ''};
56+
};

src/plot_api/manage_arrays.js

Lines changed: 16 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -9,62 +9,23 @@
99

1010
'use strict';
1111

12-
var Lib = require('../lib');
12+
var nestedProperty = require('../lib/nested_property');
13+
var isPlainObject = require('../lib/is_plain_object');
14+
var noop = require('../lib/noop');
15+
var Loggers = require('../lib/loggers');
1316
var Registry = require('../registry');
1417

1518

19+
exports.containerArrayMatch = require('./container_array_match');
20+
1621
var isAddVal = exports.isAddVal = function isAddVal(val) {
17-
return val === 'add' || Lib.isPlainObject(val);
22+
return val === 'add' || isPlainObject(val);
1823
};
1924

2025
var isRemoveVal = exports.isRemoveVal = function isRemoveVal(val) {
2126
return val === null || val === 'remove';
2227
};
2328

24-
/*
25-
* containerArrayMatch: does this attribute string point into a
26-
* layout container array?
27-
*
28-
* @param {String} astr: an attribute string, like *annotations[2].text*
29-
*
30-
* @returns {Object | false} Returns false if `astr` doesn't match a container
31-
* array. If it does, returns:
32-
* {array: {String}, index: {Number}, property: {String}}
33-
* ie the attribute string for the array, the index within the array (or ''
34-
* if the whole array) and the property within that (or '' if the whole array
35-
* or the whole object)
36-
*/
37-
exports.containerArrayMatch = function containerArrayMatch(astr) {
38-
var rootContainers = Registry.layoutArrayContainers,
39-
regexpContainers = Registry.layoutArrayRegexes,
40-
rootPart = astr.split('[')[0],
41-
arrayStr,
42-
match;
43-
44-
// look for regexp matches first, because they may be nested inside root matches
45-
// eg updatemenus[i].buttons is nested inside updatemenus
46-
for(var i = 0; i < regexpContainers.length; i++) {
47-
match = astr.match(regexpContainers[i]);
48-
if(match && match.index === 0) {
49-
arrayStr = match[0];
50-
break;
51-
}
52-
}
53-
54-
// now look for root matches
55-
if(!arrayStr) arrayStr = rootContainers[rootContainers.indexOf(rootPart)];
56-
57-
if(!arrayStr) return false;
58-
59-
var tail = astr.substr(arrayStr.length);
60-
if(!tail) return {array: arrayStr, index: '', property: ''};
61-
62-
match = tail.match(/^\[(0|[1-9][0-9]*)\](\.(.+))?$/);
63-
if(!match) return false;
64-
65-
return {array: arrayStr, index: Number(match[1]), property: match[3] || ''};
66-
};
67-
6829
/*
6930
* editContainerArray: for managing arrays of layout components in relayout
7031
* handles them all with a consistent interface.
@@ -113,14 +74,14 @@ exports.editContainerArray = function editContainerArray(gd, np, edits, flags) {
11374
supplyComponentDefaults = Registry.getComponentMethod(componentType, 'supplyLayoutDefaults'),
11475
draw = Registry.getComponentMethod(componentType, 'draw'),
11576
drawOne = Registry.getComponentMethod(componentType, 'drawOne'),
116-
replotLater = flags.replot || flags.recalc || (supplyComponentDefaults === Lib.noop) ||
117-
(draw === Lib.noop),
77+
replotLater = flags.replot || flags.recalc || (supplyComponentDefaults === noop) ||
78+
(draw === noop),
11879
layout = gd.layout,
11980
fullLayout = gd._fullLayout;
12081

12182
if(edits['']) {
12283
if(Object.keys(edits).length > 1) {
123-
Lib.warn('Full array edits are incompatible with other edits',
84+
Loggers.warn('Full array edits are incompatible with other edits',
12485
componentType);
12586
}
12687

@@ -129,7 +90,7 @@ exports.editContainerArray = function editContainerArray(gd, np, edits, flags) {
12990
if(isRemoveVal(fullVal)) np.set(null);
13091
else if(Array.isArray(fullVal)) np.set(fullVal);
13192
else {
132-
Lib.warn('Unrecognized full array edit value', componentType, fullVal);
93+
Loggers.warn('Unrecognized full array edit value', componentType, fullVal);
13394
return true;
13495
}
13596

@@ -164,13 +125,13 @@ exports.editContainerArray = function editContainerArray(gd, np, edits, flags) {
164125
adding = isAddVal(objVal);
165126

166127
if(componentNum < 0 || componentNum > componentArray.length - (adding ? 0 : 1)) {
167-
Lib.warn('index out of range', componentType, componentNum);
128+
Loggers.warn('index out of range', componentType, componentNum);
168129
continue;
169130
}
170131

171132
if(objVal !== undefined) {
172133
if(objKeys.length > 1) {
173-
Lib.warn(
134+
Loggers.warn(
174135
'Insertion & removal are incompatible with edits to the same index.',
175136
componentType, componentNum);
176137
}
@@ -183,15 +144,15 @@ exports.editContainerArray = function editContainerArray(gd, np, edits, flags) {
183144
componentArray.splice(componentNum, 0, objVal);
184145
}
185146
else {
186-
Lib.warn('Unrecognized full object edit value',
147+
Loggers.warn('Unrecognized full object edit value',
187148
componentType, componentNum, objVal);
188149
}
189150

190151
if(firstIndexChange === -1) firstIndexChange = componentNum;
191152
}
192153
else {
193154
for(j = 0; j < objKeys.length; j++) {
194-
Lib.nestedProperty(componentArray[componentNum], objKeys[j]).set(objEdits[objKeys[j]]);
155+
nestedProperty(componentArray[componentNum], objKeys[j]).set(objEdits[objKeys[j]]);
195156
}
196157
}
197158
}
@@ -210,7 +171,7 @@ exports.editContainerArray = function editContainerArray(gd, np, edits, flags) {
210171

211172
// finally draw all the components we need to
212173
// if we added or removed any, redraw all after it
213-
if(drawOne !== Lib.noop) {
174+
if(drawOne !== noop) {
214175
var indicesToDraw;
215176
if(firstIndexChange === -1) {
216177
// there's no re-indexing to do, so only redraw components that changed

0 commit comments

Comments
 (0)