Skip to content

Commit 166da3b

Browse files
committed
modif modebar click handler call signature:
- modebar click handler are now functions of (gd, ev) the graph div and the event object only. - remove all traces of modebar methods inside click handler - call updateActiveButtons after click handler - N.B. updateActiveButtons is also called in Plotly.relayout
1 parent 543fe14 commit 166da3b

File tree

2 files changed

+38
-61
lines changed

2 files changed

+38
-61
lines changed

src/components/modebar/buttons.js

Lines changed: 36 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ var modebarButtons = module.exports = {};
2828
* @param {string} [gravity]
2929
* icon positioning
3030
* @param {function} click
31-
* click handler associated with the button
31+
* click handler associated with the button, a function of
32+
* 'gd' (the main graph object) and
33+
* 'ev' (the event object)
3234
* @param {string} [attr]
3335
* attribute associated with button,
3436
* use this with 'val' to keep track of the state
@@ -42,7 +44,7 @@ modebarButtons.toImage = {
4244
name: 'toImage',
4345
title: 'Download plot as a png',
4446
icon: 'camera',
45-
click: function(modebar) {
47+
click: function(gd) {
4648
var format = 'png';
4749

4850
if (Lib.isIE()) {
@@ -51,22 +53,22 @@ modebarButtons.toImage = {
5153
return;
5254
}
5355

54-
if (modebar._snapshotInProgress) {
56+
if (gd._snapshotInProgress) {
5557
Lib.notifier('Snapshotting is still in progress - please hold', 'long');
5658
return;
5759
}
5860

59-
modebar._snapshotInProgress = true;
61+
gd._snapshotInProgress = true;
6062
Lib.notifier('Taking snapshot - this may take a few seconds', 'long');
6163

62-
var ev = Snapshot.toImage(modebar.graphInfo, {format: format});
64+
var ev = Snapshot.toImage(gd, {format: format});
6365

64-
var filename = modebar.graphInfo.fn || 'newplot';
66+
var filename = gd.fn || 'newplot';
6567
filename += '.' + format;
6668

6769
ev.once('success', function(result) {
6870

69-
modebar._snapshotInProgress = false;
71+
gd._snapshotInProgress = false;
7072

7173
var downloadLink = document.createElement('a');
7274
downloadLink.href = result;
@@ -80,7 +82,7 @@ modebarButtons.toImage = {
8082
});
8183

8284
ev.once('error', function (err) {
83-
modebar._snapshotInProgress = false;
85+
gd._snapshotInProgress = false;
8486

8587
Lib.notifier('Sorry there was a problem downloading your ' + format, 'long');
8688
console.error(err);
@@ -94,8 +96,7 @@ modebarButtons.sendDataToCloud = {
9496
name: 'sendDataToCloud',
9597
title: 'Save and edit plot in cloud',
9698
icon: 'disk',
97-
click: function(modebar) {
98-
var gd = modebar.graphInfo;
99+
click: function(gd) {
99100
Plotly.Plots.sendDataToCloud(gd);
100101
}
101102
};
@@ -176,19 +177,18 @@ modebarButtons.hoverCompareCartesian = {
176177
click: handleCartesian
177178
};
178179

179-
function handleCartesian(modebar, ev) {
180+
function handleCartesian(gd, ev) {
180181
var button = ev.currentTarget,
181182
astr = button.getAttribute('data-attr'),
182183
val = button.getAttribute('data-val') || true,
183-
graphInfo = modebar.graphInfo,
184-
fullLayout = graphInfo._fullLayout,
184+
fullLayout = gd._fullLayout,
185185
aobj = {};
186186

187187
if(astr === 'zoom') {
188188
var mag = (val === 'in') ? 0.5 : 2,
189189
r0 = (1 + mag) / 2,
190190
r1 = (1 - mag) / 2,
191-
axList = Plotly.Axes.list(graphInfo, null, true);
191+
axList = Plotly.Axes.list(gd, null, true);
192192

193193
var ax, axName, initialRange;
194194

@@ -212,7 +212,8 @@ function handleCartesian(modebar, ev) {
212212
}
213213
}
214214
}
215-
} else {
215+
}
216+
else {
216217
// if ALL traces have orientation 'h', 'hovermode': 'x' otherwise: 'y'
217218
if (astr==='hovermode' && (val==='x' || val==='y')) {
218219
val = fullLayout._isHoriz ? 'y' : 'x';
@@ -222,17 +223,15 @@ function handleCartesian(modebar, ev) {
222223
aobj[astr] = val;
223224
}
224225

225-
Plotly.relayout(graphInfo, aobj).then( function() {
226-
modebar.updateActiveButton();
226+
Plotly.relayout(gd, aobj).then( function() {
227227
if(astr === 'dragmode') {
228228
if(fullLayout._hasCartesian) {
229229
Plotly.Fx.setCursor(
230230
fullLayout._paper.select('.nsewdrag'),
231231
{pan:'move', zoom:'crosshair'}[val]
232232
);
233233
}
234-
Plotly.Fx.supplyLayoutDefaults(graphInfo.layout, fullLayout,
235-
graphInfo._fullData);
234+
Plotly.Fx.supplyLayoutDefaults(gd.layout, fullLayout, gd._fullData);
236235
}
237236
});
238237
}
@@ -273,11 +272,10 @@ modebarButtons.tableRotation = {
273272
click: handleDrag3d
274273
};
275274

276-
function handleDrag3d(modebar, ev) {
275+
function handleDrag3d(gd, ev) {
277276
var button = ev.currentTarget,
278277
attr = button.getAttribute('data-attr'),
279278
val = button.getAttribute('data-val') || true,
280-
graphInfo = modebar.graphInfo,
281279
layoutUpdate = {};
282280

283281
layoutUpdate[attr] = val;
@@ -286,9 +284,7 @@ function handleDrag3d(modebar, ev) {
286284
* Dragmode will go through the relayout -> doplot -> scene.plot()
287285
* routine where the dragmode will be set in scene.plot()
288286
*/
289-
Plotly.relayout(graphInfo, layoutUpdate).then( function() {
290-
modebar.updateActiveButton();
291-
});
287+
Plotly.relayout(gd, layoutUpdate);
292288
}
293289

294290
modebarButtons.resetCameraDefault3d = {
@@ -307,11 +303,11 @@ modebarButtons.resetCameraLastSave3d = {
307303
click: handleCamera3d
308304
};
309305

310-
function handleCamera3d(modebar, ev) {
306+
function handleCamera3d(gd, ev) {
311307
var button = ev.currentTarget,
312308
attr = button.getAttribute('data-attr'),
313-
layout = modebar.graphInfo.layout,
314-
fullLayout = modebar.graphInfo._fullLayout,
309+
layout = gd.layout,
310+
fullLayout = gd._fullLayout,
315311
sceneIds = Plotly.Plots.getSubplotIds(fullLayout, 'gl3d');
316312

317313
for(var i = 0; i < sceneIds.length; i++) {
@@ -343,11 +339,10 @@ modebarButtons.hoverClosest3d = {
343339
toggle: true,
344340
icon: 'tooltip_basic',
345341
gravity: 'ne',
346-
click: function(modebar, ev) {
342+
click: function(gd, ev) {
347343
var button = ev.currentTarget,
348344
val = JSON.parse(button.getAttribute('data-val')) || false,
349-
graphInfo = modebar.graphInfo,
350-
fullLayout = graphInfo._fullLayout,
345+
fullLayout = gd._fullLayout,
351346
sceneIds = Plotly.Plots.getSubplotIds(fullLayout, 'gl3d');
352347

353348
var axes = ['xaxis', 'yaxis', 'zaxis'],
@@ -385,9 +380,7 @@ modebarButtons.hoverClosest3d = {
385380
button.setAttribute('data-val', JSON.stringify(currentSpikes));
386381
}
387382

388-
Plotly.relayout(graphInfo, layoutUpdate).then(function() {
389-
modebar.updateActiveButton(button);
390-
});
383+
Plotly.relayout(gd, layoutUpdate);
391384
}
392385
};
393386

@@ -429,11 +422,11 @@ modebarButtons.hoverClosestGeo = {
429422
click: handleGeo
430423
};
431424

432-
function handleGeo(modebar, ev) {
425+
function handleGeo(gd, ev) {
433426
var button = ev.currentTarget,
434427
attr = button.getAttribute('data-attr'),
435428
val = button.getAttribute('data-val') || true,
436-
fullLayout = modebar.graphInfo._fullLayout,
429+
fullLayout = gd._fullLayout,
437430
geoIds = Plotly.Plots.getSubplotIds(fullLayout, 'geo');
438431

439432
for(var i = 0; i < geoIds.length; i++) {
@@ -449,8 +442,6 @@ function handleGeo(modebar, ev) {
449442
else if(attr === 'reset') geo.zoomReset();
450443
else if(attr === 'hovermode') geo.showHover = !geo.showHover;
451444
}
452-
453-
modebar.updateActiveButton(button);
454445
}
455446

456447
modebarButtons.hoverClosestGl2d = {
@@ -461,17 +452,7 @@ modebarButtons.hoverClosestGl2d = {
461452
toggle: true,
462453
icon: 'tooltip_basic',
463454
gravity: 'ne',
464-
click: function(modebar, ev) {
465-
var button = ev.currentTarget,
466-
graphInfo = modebar.graphInfo,
467-
newHover = graphInfo._fullLayout.hovermode ?
468-
false :
469-
'closest';
470-
471-
Plotly.relayout(graphInfo, 'hovermode', newHover).then(function() {
472-
modebar.updateActiveButton(button);
473-
});
474-
}
455+
click: toggleHover
475456
};
476457

477458
modebarButtons.hoverClosestPie = {
@@ -481,15 +462,11 @@ modebarButtons.hoverClosestPie = {
481462
val: 'closest',
482463
icon: 'tooltip_basic',
483464
gravity: 'ne',
484-
click: function(modebar) {
485-
var graphInfo = modebar.graphInfo,
486-
newHover = graphInfo._fullLayout.hovermode ?
487-
false :
488-
'closest';
489-
490-
Plotly.relayout(graphInfo, 'hovermode', newHover).then(function() {
491-
modebar.updateActiveButton();
492-
});
493-
494-
}
465+
click: toggleHover
495466
};
467+
468+
function toggleHover(gd) {
469+
var newHover = gd._fullLayout.hovermode ? false : 'closest';
470+
471+
Plotly.relayout(gd, 'hovermode', newHover);
472+
}

src/components/modebar/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ var Icons = require('../../../build/ploticon');
2424
* @Param {object} opts.graphInfo primary plot object containing data and layout
2525
*/
2626
function ModeBar(opts) {
27-
this._snapshotInProgress = false;
2827
this.container = opts.container;
2928
this.element = document.createElement('div');
3029

@@ -139,7 +138,8 @@ proto.createButton = function (config) {
139138
}
140139
else {
141140
button.addEventListener('click', function(ev) {
142-
config.click(_this, ev);
141+
config.click(_this.graphInfo, ev);
142+
_this.updateActiveButton(ev.currentTarget);
143143
});
144144
}
145145

0 commit comments

Comments
 (0)