Skip to content

Commit 9179b24

Browse files
committed
Generalize src handling function
1 parent 93b6614 commit 9179b24

File tree

7 files changed

+37
-43
lines changed

7 files changed

+37
-43
lines changed

src/EditorControls.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,10 @@ EditorControls.propTypes = {
281281
beforeUpdateTraces: PropTypes.func,
282282
children: PropTypes.node,
283283
className: PropTypes.string,
284-
customSrcHandling: PropTypes.object,
284+
customSrcHandling: PropTypes.shape({
285+
toSrc: PropTypes.func,
286+
fromSrc: PropTypes.func,
287+
}),
285288
dataSourceOptionRenderer: PropTypes.func,
286289
dataSourceOptions: PropTypes.array,
287290
dataSources: PropTypes.object,
@@ -308,7 +311,10 @@ EditorControls.defaultProps = {
308311
EditorControls.childContextTypes = {
309312
advancedTraceTypeSelector: PropTypes.bool,
310313
config: PropTypes.object,
311-
customSrcHandling: PropTypes.object,
314+
customSrcHandling: PropTypes.shape({
315+
toSrc: PropTypes.func,
316+
fromSrc: PropTypes.func,
317+
}),
312318
data: PropTypes.array,
313319
dataSourceOptionRenderer: PropTypes.func,
314320
dataSourceOptions: PropTypes.array,

src/PlotlyEditor.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,10 @@ PlotlyEditor.propTypes = {
7171
divId: PropTypes.string,
7272
hideControls: PropTypes.bool,
7373
showFieldTooltips: PropTypes.bool,
74-
customSrcHandling: PropTypes.object,
74+
customSrcHandling: PropTypes.shape({
75+
toSrc: PropTypes.func,
76+
fromSrc: PropTypes.func,
77+
}),
7578
};
7679

7780
PlotlyEditor.defaultProps = {

src/components/fields/DataSelector.js

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,13 @@ export class UnconnectedDataSelector extends Component {
2626
this.dataSourceOptions = context.dataSourceOptions || [];
2727

2828
this.srcAttr = props.attr + 'src';
29-
this.srcProperty = nestedProperty(props.container, this.srcAttr);
30-
this.fullValue = this.srcProperty.get();
29+
this.srcProperty = nestedProperty(props.container, this.srcAttr).get();
30+
this.fullValue = this.context.customSrcHandling
31+
? this.context.customSrcHandling.toSrc(
32+
this.srcProperty,
33+
props.container.type
34+
)
35+
: this.srcProperty;
3136

3237
this.is2D = false;
3338
if (props.container) {
@@ -44,15 +49,6 @@ export class UnconnectedDataSelector extends Component {
4449
].includes(props.container.type)) ||
4550
(props.container.type === 'table' && props.attr !== 'columnorder');
4651
}
47-
48-
if (
49-
this.is2D &&
50-
this.fullValue &&
51-
this.fullValue.length &&
52-
this.context.customSrcHandling
53-
) {
54-
this.fullValue = this.context.customSrcHandling.splitSrcs(this.fullValue);
55-
}
5652
}
5753

5854
updatePlot(value) {
@@ -81,8 +77,8 @@ export class UnconnectedDataSelector extends Component {
8177
this.srcAttr,
8278
this.props.container.type,
8379
{
84-
joinSrcs: this.context.customSrcHandling
85-
? this.context.customSrcHandling.joinSrcs
80+
fromSrc: this.context.customSrcHandling
81+
? this.context.customSrcHandling.fromSrc
8682
: null,
8783
}
8884
);
@@ -131,7 +127,10 @@ UnconnectedDataSelector.contextTypes = {
131127
dataSourceOptions: PropTypes.array,
132128
dataSourceValueRenderer: PropTypes.func,
133129
dataSourceOptionRenderer: PropTypes.func,
134-
customSrcHandling: PropTypes.object,
130+
customSrcHandling: PropTypes.shape({
131+
toSrc: PropTypes.func,
132+
fromSrc: PropTypes.func,
133+
}),
135134
};
136135

137136
function modifyPlotProps(props, context, plotProps) {

src/lib/__tests__/dereference-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ describe('dereference', () => {
5454
dereference(
5555
container,
5656
{z1: [1, 2, 3], z2: [2, 2, 2]},
57-
{splitSrcs: customParsing}
57+
{toSrc: customParsing}
5858
);
5959

6060
// contents should have been transposed

src/lib/__tests__/maybeAdjustSrc-test.js

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,14 @@
11
import {maybeAdjustSrc} from '../index';
22
/* eslint-disable no-magic-numbers */
33
describe('maybeAdjustSrc', () => {
4-
it('uses custom joinSrcs function if one is provided', () => {
5-
const customJoin = srcs => srcs.join('$');
4+
it('uses custom parsing function if one is provided', () => {
5+
const custom = srcs => srcs.join('$');
66
const adjusted = maybeAdjustSrc(['z1', 'z2'], 'zsrc', 'heatmap', {
7-
joinSrcs: customJoin,
7+
fromSrc: custom,
88
});
99
expect(adjusted).toBe('z1$z2');
1010
});
1111

12-
it('reduces src to string when just one src element in array', () => {
13-
const adjusted = maybeAdjustSrc(['z1'], 'zsrc', 'heatmap');
14-
expect(adjusted).toBe('z1');
15-
});
16-
1712
it('reduces src to string for special table case', () => {
1813
const adjusted = maybeAdjustSrc(['z1'], 'header.valuessrc', 'table');
1914
expect(adjusted).toBe('z1');

src/lib/dereference.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ export default function dereference(
1616
const dataKey = key.replace(SRC_ATTR_PATTERN, '');
1717
const traceType = parent.type;
1818

19+
let srcRef = config.toSrc ? config.toSrc(parent[key]) : parent[key];
20+
1921
// making this into an array to more easily lookup 1d and 2d srcs in dataSourceOptions
20-
const srcRef = config.splitSrcs
21-
? config.splitSrcs(parent[key])
22-
: Array.isArray(parent[key])
23-
? parent[key]
24-
: [parent[key]];
22+
if (!Array.isArray(srcRef)) {
23+
srcRef = [srcRef];
24+
}
2525

2626
let data = srcRef.map(ref => {
2727
if (config.deleteKeys && !(ref in dataSources)) {

src/lib/index.js

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -164,20 +164,11 @@ function maybeAdjustSrc(src, srcAttributePath, traceType, config) {
164164
return null;
165165
}
166166

167-
if (Array.isArray(src)) {
168-
if (src.length > 1 && config && config.joinSrcs) {
169-
return config.joinSrcs(src, traceType);
170-
}
171-
172-
if (
173-
(specialTableCase(traceType, srcAttributePath) && src.length === 1) ||
174-
src.length === 1
175-
) {
176-
return src[0];
177-
}
167+
if (specialTableCase(traceType, srcAttributePath) && src.length === 1) {
168+
return src[0];
178169
}
179170

180-
return src;
171+
return config && config.fromSrc ? config.fromSrc(src, traceType) : src;
181172
}
182173

183174
export {

0 commit comments

Comments
 (0)