Skip to content

Commit 57fcf96

Browse files
committed
Add mock for bindings
1 parent 7db7f03 commit 57fcf96

File tree

6 files changed

+149
-70
lines changed

6 files changed

+149
-70
lines changed

src/components/sliders/draw.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
var d3 = require('d3');
1313

14-
var Plotly = require('../../plotly');
1514
var Plots = require('../../plots/plots');
1615
var Lib = require('../../lib');
1716
var Color = require('../color');
@@ -381,14 +380,13 @@ function setActive(gd, sliderGroup, sliderOpts, index, doCallback, doTransition)
381380
sliderGroup._nextMethod = {step: step, doCallback: doCallback, doTransition: doTransition};
382381
sliderGroup._nextMethodRaf = window.requestAnimationFrame(function() {
383382
var _step = sliderGroup._nextMethod.step;
384-
var args = _step.args;
385383
if(!_step.method) return;
386384

387385
sliderOpts._invokingCommand = true;
388386

389387
Plots.executeAPICommand(gd, _step.method, _step.args).then(function() {
390388
sliderOpts._invokingCommand = false;
391-
}, function () {
389+
}, function() {
392390
sliderOpts._invokingCommand = false;
393391
});
394392

@@ -476,7 +474,7 @@ function setGripPosition(sliderGroup, sliderOpts, position, doTransition) {
476474

477475
// If this is true, then *this component* is already invoking its own command
478476
// and has triggered its own animation.
479-
if (sliderOpts._invokingCommand) return;
477+
if(sliderOpts._invokingCommand) return;
480478

481479
var el = grip;
482480
if(doTransition && sliderOpts.transition.duration > 0) {

src/components/updatemenus/draw.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
var d3 = require('d3');
1313

14-
var Plotly = require('../../plotly');
1514
var Plots = require('../../plots/plots');
1615
var Lib = require('../../lib');
1716
var Color = require('../color');

src/plots/command.js

Lines changed: 8 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
var Plotly = require('../plotly');
1313
var Lib = require('../lib');
1414

15-
var attrPrefixRegex = /^(data|layout)(\[(-?[0-9]*)\])?\.(.*)$/;
16-
1715
/*
1816
* This function checks to see if an array of objects containing
1917
* method and args properties is compatible with automatic two-way
@@ -23,7 +21,7 @@ var attrPrefixRegex = /^(data|layout)(\[(-?[0-9]*)\])?\.(.*)$/;
2321
* 2. only one property may be affected
2422
* 3. the same property must be affected by all commands
2523
*/
26-
exports.hasSimpleBindings = function(gd, commandList, bindingsByValue) {
24+
exports.hasSimpleAPICommandBindings = function(gd, commandList, bindingsByValue) {
2725
var n = commandList.length;
2826

2927
var refBinding;
@@ -81,7 +79,9 @@ exports.hasSimpleBindings = function(gd, commandList, bindingsByValue) {
8179
if(Array.isArray(value)) {
8280
value = value[0];
8381
}
84-
bindingsByValue[value] = i;
82+
if(bindingsByValue) {
83+
bindingsByValue[value] = i;
84+
}
8585
}
8686

8787
return refBinding;
@@ -95,8 +95,8 @@ exports.createCommandObserver = function(gd, commandList, onchange) {
9595

9696
// Determine whether there's anything to do for this binding:
9797
var binding;
98-
if((binding = exports.hasSimpleBindings(gd, commandList, lookupTable))) {
99-
exports.bindingValueHasChanged(gd, binding, cache);
98+
if((binding = exports.hasSimpleAPICommandBindings(gd, commandList, lookupTable))) {
99+
bindingValueHasChanged(gd, binding, cache);
100100

101101
check = function check() {
102102
if(!enabled) return;
@@ -170,7 +170,7 @@ exports.createCommandObserver = function(gd, commandList, onchange) {
170170
};
171171
};
172172

173-
exports.bindingValueHasChanged = function(gd, binding, cache) {
173+
function bindingValueHasChanged(gd, binding, cache) {
174174
var container, value, obj;
175175
var changed = false;
176176

@@ -198,32 +198,7 @@ exports.bindingValueHasChanged = function(gd, binding, cache) {
198198
obj[binding.prop] = value;
199199

200200
return changed;
201-
};
202-
203-
exports.evaluateAPICommandBinding = function(gd, attrName) {
204-
var match = attrName.match(attrPrefixRegex);
205-
206-
if(!match) {
207-
return null;
208-
}
209-
210-
var group = match[1];
211-
var propStr = match[4];
212-
var container;
213-
214-
switch(group) {
215-
case 'data':
216-
container = gd._fullData[parseInt(match[3])];
217-
break;
218-
case 'layout':
219-
container = gd._fullLayout;
220-
break;
221-
default:
222-
return null;
223-
}
224-
225-
return Lib.nestedProperty(container, propStr).get();
226-
};
201+
}
227202

228203
exports.executeAPICommand = function(gd, method, args) {
229204
var apiMethod = Plotly[method];

src/plots/plots.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,8 @@ var ErrorBars = require('../components/errorbars');
4141
var commandModule = require('./command');
4242
plots.executeAPICommand = commandModule.executeAPICommand;
4343
plots.computeAPICommandBindings = commandModule.computeAPICommandBindings;
44-
plots.evaluateAPICommandBinding = commandModule.evaluateAPICommandBinding;
45-
plots.hasSimpleBindings = commandModule.hasSimpleBindings;
46-
plots.bindingValueHasChanged = commandModule.bindingValueHasChanged;
4744
plots.createCommandObserver = commandModule.createCommandObserver;
45+
plots.hasSimpleAPICommandBindings = commandModule.hasSimpleAPICommandBindings;
4846

4947
/**
5048
* Find subplot ids in data.

test/image/mocks/binding.json

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
{
2+
"data": [
3+
{
4+
"x": [0, 1, 2],
5+
"y": [0.5, 1, 2.5]
6+
}
7+
],
8+
"layout": {
9+
"sliders": [{
10+
"active": 0,
11+
"steps": [{
12+
"label": "red",
13+
"method": "restyle",
14+
"args": [{"marker.color": "red"}]
15+
}, {
16+
"label": "orange",
17+
"method": "restyle",
18+
"args": [{"marker.color": "orange"}]
19+
}, {
20+
"label": "yellow",
21+
"method": "restyle",
22+
"args": [{"marker.color": "yellow"}]
23+
}, {
24+
"label": "green",
25+
"method": "restyle",
26+
"args": [{"marker.color": "green"}]
27+
}, {
28+
"label": "blue",
29+
"method": "restyle",
30+
"args": [{"marker.color": "blue"}]
31+
}, {
32+
"label": "purple",
33+
"method": "restyle",
34+
"args": [{"marker.color": "purple"}]
35+
}],
36+
"visible": true,
37+
"x": 0,
38+
"len": 1.0,
39+
"xanchor": "left",
40+
"y": 0,
41+
"yanchor": "top",
42+
"currentvalue": {
43+
"visible": false
44+
},
45+
46+
"transition": {
47+
"duration": 150,
48+
"easing": "cubic-in-out"
49+
},
50+
51+
"pad": {
52+
"r": 20,
53+
"t": 40
54+
},
55+
56+
"font": {}
57+
}],
58+
"updatemenus": [{
59+
"active": 0,
60+
"type": "buttons",
61+
"buttons": [{
62+
"label": "red",
63+
"method": "restyle",
64+
"args": [{"marker.color": "red"}]
65+
}, {
66+
"label": "orange",
67+
"method": "restyle",
68+
"args": [{"marker.color": "orange"}]
69+
}, {
70+
"label": "yellow",
71+
"method": "restyle",
72+
"args": [{"marker.color": "yellow"}]
73+
}, {
74+
"label": "green",
75+
"method": "restyle",
76+
"args": [{"marker.color": "green"}]
77+
}, {
78+
"label": "blue",
79+
"method": "restyle",
80+
"args": [{"marker.color": "blue"}]
81+
}, {
82+
"label": "purple",
83+
"method": "restyle",
84+
"args": [{"marker.color": "purple"}]
85+
}],
86+
"visible": true,
87+
"direction": "right",
88+
"x": 0,
89+
"xanchor": "left",
90+
"y": 1.05,
91+
"yanchor": "bottom",
92+
"pad": {
93+
"l": 20,
94+
"t": 20
95+
}
96+
}],
97+
"xaxis": {
98+
"range": [0, 2],
99+
"autorange": true
100+
},
101+
"yaxis": {
102+
"type": "linear",
103+
"range": [0, 3],
104+
"autorange": true
105+
},
106+
"height": 450,
107+
"width": 1100,
108+
"autosize": true
109+
}
110+
}

test/jasmine/tests/command_test.js

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,6 @@ var createGraphDiv = require('../assets/create_graph_div');
55
var destroyGraphDiv = require('../assets/destroy_graph_div');
66
var fail = require('../assets/fail_test');
77

8-
describe('Plots.evaluateAPICommandBinding', function() {
9-
it('evaluates a data binding', function() {
10-
var gd = {_fullData: [null, {line: {width: 7}}]};
11-
var astr = 'data[1].line.width';
12-
13-
expect(Plots.evaluateAPICommandBinding(gd, astr)).toEqual(7);
14-
});
15-
16-
it('evaluates a layout binding', function() {
17-
var gd = {_fullLayout: {margin: {t: 100}}};
18-
var astr = 'layout.margin.t';
19-
20-
expect(Plots.evaluateAPICommandBinding(gd, astr)).toEqual(100);
21-
});
22-
});
23-
248
describe('Plots.executeAPICommand', function() {
259
'use strict';
2610

@@ -75,7 +59,7 @@ describe('Plots.executeAPICommand', function() {
7559
});
7660
});
7761

78-
describe('Plots.hasSimpleBindings', function() {
62+
describe('Plots.hasSimpleAPICommandBindings', function() {
7963
'use strict';
8064
var gd;
8165
beforeEach(function() {
@@ -91,20 +75,25 @@ describe('Plots.hasSimpleBindings', function() {
9175
destroyGraphDiv(gd);
9276
});
9377

94-
it('return true when bindings are simple', function() {
95-
var isSimple = Plots.hasSimpleBindings(gd, [{
78+
it('return the binding when bindings are simple', function() {
79+
var isSimple = Plots.hasSimpleAPICommandBindings(gd, [{
9680
method: 'restyle',
9781
args: [{'marker.size': 10}]
9882
}, {
9983
method: 'restyle',
10084
args: [{'marker.size': 20}]
10185
}]);
10286

103-
expect(isSimple).toBe(true);
87+
expect(isSimple).toEqual({
88+
type: 'data',
89+
prop: 'marker.size',
90+
traces: null,
91+
value: 10
92+
});
10493
});
10594

10695
it('return false when properties are not the same', function() {
107-
var isSimple = Plots.hasSimpleBindings(gd, [{
96+
var isSimple = Plots.hasSimpleAPICommandBindings(gd, [{
10897
method: 'restyle',
10998
args: [{'marker.size': 10}]
11099
}, {
@@ -116,7 +105,7 @@ describe('Plots.hasSimpleBindings', function() {
116105
});
117106

118107
it('return false when a command binds to more than one property', function() {
119-
var isSimple = Plots.hasSimpleBindings(gd, [{
108+
var isSimple = Plots.hasSimpleAPICommandBindings(gd, [{
120109
method: 'restyle',
121110
args: [{'marker.color': 10, 'marker.size': 12}]
122111
}, {
@@ -128,7 +117,7 @@ describe('Plots.hasSimpleBindings', function() {
128117
});
129118

130119
it('return false when commands affect different traces', function() {
131-
var isSimple = Plots.hasSimpleBindings(gd, [{
120+
var isSimple = Plots.hasSimpleAPICommandBindings(gd, [{
132121
method: 'restyle',
133122
args: [{'marker.color': 10}, [0]]
134123
}, {
@@ -139,28 +128,38 @@ describe('Plots.hasSimpleBindings', function() {
139128
expect(isSimple).toBe(false);
140129
});
141130

142-
it('return true when commands affect the same traces', function() {
143-
var isSimple = Plots.hasSimpleBindings(gd, [{
131+
it('return the binding when commands affect the same traces', function() {
132+
var isSimple = Plots.hasSimpleAPICommandBindings(gd, [{
144133
method: 'restyle',
145134
args: [{'marker.color': 10}, [1]]
146135
}, {
147136
method: 'restyle',
148137
args: [{'marker.color': 20}, [1]]
149138
}]);
150139

151-
expect(isSimple).toBe(true);
140+
expect(isSimple).toEqual({
141+
type: 'data',
142+
prop: 'marker.color',
143+
traces: [ 1 ],
144+
value: [ 10 ]
145+
});
152146
});
153147

154-
it('return true when commands affect the same traces in different order', function() {
155-
var isSimple = Plots.hasSimpleBindings(gd, [{
148+
it('return the binding when commands affect the same traces in different order', function() {
149+
var isSimple = Plots.hasSimpleAPICommandBindings(gd, [{
156150
method: 'restyle',
157151
args: [{'marker.color': 10}, [1, 2]]
158152
}, {
159153
method: 'restyle',
160154
args: [{'marker.color': 20}, [2, 1]]
161155
}]);
162156

163-
expect(isSimple).toBe(true);
157+
expect(isSimple).toEqual({
158+
type: 'data',
159+
prop: 'marker.color',
160+
traces: [ 1, 2 ],
161+
value: [ 10, 10 ]
162+
});
164163
});
165164
});
166165

0 commit comments

Comments
 (0)