forked from plotly/plotly.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdraw.js
632 lines (502 loc) · 19.5 KB
/
draw.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
/**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var d3 = require('d3');
var Plots = require('../../plots/plots');
var Lib = require('../../lib');
var Color = require('../color');
var Drawing = require('../drawing');
var svgTextUtils = require('../../lib/svg_text_utils');
var anchorUtils = require('../legend/anchor_utils');
var constants = require('./constants');
var ScrollBox = require('./scrollbox');
module.exports = function draw(gd) {
var fullLayout = gd._fullLayout,
menuData = makeMenuData(fullLayout);
/* Update menu data is bound to the header-group.
* The items in the header group are always present.
*
* Upon clicking on a header its corresponding button
* data is bound to the button-group.
*
* We draw all headers in one group before all buttons
* so that the buttons *always* appear above the headers.
*
* Note that only one set of buttons are visible at once.
*
* <g container />
*
* <g header-group />
* <g item header />
* <text item header-arrow />
* <g header-group />
* <g item header />
* <text item header-arrow />
* ...
*
* <g button-group />
* <g item button />
* <g item button />
* ...
*/
// draw update menu container
var menus = fullLayout._infolayer
.selectAll('g.' + constants.containerClassName)
.data(menuData.length > 0 ? [0] : []);
menus.enter().append('g')
.classed(constants.containerClassName, true)
.style('cursor', 'pointer');
menus.exit().remove();
// remove push margin object(s)
if(menus.exit().size()) clearPushMargins(gd);
// return early if no update menus are visible
if(menuData.length === 0) return;
// join header group
var headerGroups = menus.selectAll('g.' + constants.headerGroupClassName)
.data(menuData, keyFunction);
headerGroups.enter().append('g')
.classed(constants.headerGroupClassName, true);
// draw dropdown button container
var gButton = menus.selectAll('g.' + constants.dropdownButtonGroupClassName)
.data([0]);
gButton.enter().append('g')
.classed(constants.dropdownButtonGroupClassName, true)
.style('pointer-events', 'all');
// whenever we add new menu, attach 'state' variable to node
// to keep track of the active menu ('-1' means no menu is active)
// and remove all dropped buttons (if any)
if(headerGroups.enter().size()) {
gButton
.call(removeAllButtons)
.attr(constants.menuIndexAttrName, '-1');
}
// remove exiting header, remove dropped buttons and reset margins
headerGroups.exit().each(function(menuOpts) {
d3.select(this).remove();
gButton
.call(removeAllButtons)
.attr(constants.menuIndexAttrName, '-1');
Plots.autoMargin(gd, constants.autoMarginIdRoot + menuOpts._index);
});
// find dimensions before plotting anything (this mutates menuOpts)
for(var i = 0; i < menuData.length; i++) {
var menuOpts = menuData[i];
findDimensions(gd, menuOpts);
}
// draw headers!
headerGroups.each(function(menuOpts) {
var gHeader = d3.select(this);
var _gButton = menuOpts.type === 'dropdown' ? gButton : null;
Plots.manageCommandObserver(gd, menuOpts, menuOpts.buttons, function(data) {
setActive(gd, menuOpts, menuOpts.buttons[data.index], gHeader, _gButton, data.index, true);
});
if(menuOpts.type === 'dropdown') {
drawHeader(gd, gHeader, gButton, menuOpts);
// update buttons if they are dropped
if(areMenuButtonsDropped(gButton, menuOpts)) {
drawButtons(gd, gHeader, gButton, menuOpts);
}
} else {
drawButtons(gd, gHeader, null, menuOpts);
}
});
};
function makeMenuData(fullLayout) {
var contOpts = fullLayout[constants.name],
menuData = [];
// Filter visible dropdowns and attach '_index' to each
// fullLayout options object to be used for 'object constancy'
// in the data join key function.
for(var i = 0; i < contOpts.length; i++) {
var item = contOpts[i];
if(item.visible) menuData.push(item);
}
return menuData;
}
// Note that '_index' is set at the default step,
// it corresponds to the menu index in the user layout update menu container.
// This is a more 'consistent' field than e.g. the index in the menuData.
function keyFunction(opts) {
return opts._index;
}
function areMenuButtonsDropped(gButton, menuOpts) {
var droppedIndex = +gButton.attr(constants.menuIndexAttrName);
return droppedIndex === menuOpts._index;
}
function drawHeader(gd, gHeader, gButton, menuOpts) {
var header = gHeader.selectAll('g.' + constants.headerClassName)
.data([0]);
header.enter().append('g')
.classed(constants.headerClassName, true)
.style('pointer-events', 'all');
var active = menuOpts.active,
headerOpts = menuOpts.buttons[active] || constants.blankHeaderOpts,
posOpts = { y: menuOpts.pad.t, yPad: 0, x: menuOpts.pad.l, xPad: 0, index: 0 },
positionOverrides = {
width: menuOpts.headerWidth,
height: menuOpts.headerHeight
};
header
.call(drawItem, menuOpts, headerOpts)
.call(setItemPosition, menuOpts, posOpts, positionOverrides);
// draw drop arrow at the right edge
var arrow = gHeader.selectAll('text.' + constants.headerArrowClassName)
.data([0]);
arrow.enter().append('text')
.classed(constants.headerArrowClassName, true)
.classed('user-select-none', true)
.attr('text-anchor', 'end')
.call(Drawing.font, menuOpts.font)
.text('▼');
arrow.attr({
x: menuOpts.headerWidth - constants.arrowOffsetX + menuOpts.pad.l,
y: menuOpts.headerHeight / 2 + constants.textOffsetY + menuOpts.pad.t
});
header.on('click', function() {
gButton.call(removeAllButtons);
// if clicked index is same as dropped index => fold
// otherwise => drop buttons associated with header
gButton.attr(
constants.menuIndexAttrName,
areMenuButtonsDropped(gButton, menuOpts) ? '-1' : String(menuOpts._index)
);
drawButtons(gd, gHeader, gButton, menuOpts);
});
header.on('mouseover', function() {
header.call(styleOnMouseOver);
});
header.on('mouseout', function() {
header.call(styleOnMouseOut, menuOpts);
});
// translate header group
Lib.setTranslate(gHeader, menuOpts.lx, menuOpts.ly);
}
function drawButtons(gd, gHeader, gButton, menuOpts) {
// If this is a set of buttons, set pointer events = all since we play
// some minor games with which container is which in order to simplify
// the drawing of *either* buttons or menus
if(!gButton) {
gButton = gHeader;
gButton.attr('pointer-events', 'all');
}
var buttonData = (gButton.attr(constants.menuIndexAttrName) !== '-1' || menuOpts.type === 'buttons') ?
menuOpts.buttons :
[];
var klass = menuOpts.type === 'dropdown' ? constants.dropdownButtonClassName : constants.buttonClassName;
var buttons = gButton.selectAll('g.' + klass)
.data(buttonData);
var enter = buttons.enter().append('g')
.classed(klass, true);
var exit = buttons.exit();
if(menuOpts.type === 'dropdown') {
enter.attr('opacity', '0')
.transition()
.attr('opacity', '1');
exit.transition()
.attr('opacity', '0')
.remove()
.each('end', function() {
// remove the scrollbox, if all the buttons have been removed
if(gButton.selectAll('g.' + klass).size() === 0) {
gButton.call(removeAllButtons);
}
});
} else {
exit.remove();
}
// if folding a dropdown menu, don't draw the buttons
if(!buttons.size()) return;
var x0 = 0;
var y0 = 0;
var isVertical = ['up', 'down'].indexOf(menuOpts.direction) !== -1;
if(menuOpts.type === 'dropdown') {
if(isVertical) {
y0 = menuOpts.headerHeight + constants.gapButtonHeader;
} else {
x0 = menuOpts.headerWidth + constants.gapButtonHeader;
}
}
if(menuOpts.type === 'dropdown' && menuOpts.direction === 'up') {
y0 = -constants.gapButtonHeader + constants.gapButton - menuOpts.openHeight;
}
if(menuOpts.type === 'dropdown' && menuOpts.direction === 'left') {
x0 = -constants.gapButtonHeader + constants.gapButton - menuOpts.openWidth;
}
var posOpts = {
x: menuOpts.lx + x0 + menuOpts.pad.l,
y: menuOpts.ly + y0 + menuOpts.pad.t,
yPad: constants.gapButton,
xPad: constants.gapButton,
index: 0,
};
var fullLayout = gd._fullLayout,
scrollBoxId = 'updatemenus' + fullLayout._uid + menuOpts._index,
scrollBoxPosition = {
l: menuOpts.lx + menuOpts.borderwidth + x0 + menuOpts.pad.l,
t: menuOpts.ly + menuOpts.borderwidth + y0 + menuOpts.pad.t,
w: Math.max(menuOpts.openWidth, menuOpts.headerWidth),
h: menuOpts.openHeight
},
scrollBox = new ScrollBox(gd, gButton, scrollBoxPosition, scrollBoxId);
buttons.each(function(buttonOpts, buttonIndex) {
var button = d3.select(this);
button
.call(drawItem, menuOpts, buttonOpts)
.call(setItemPosition, menuOpts, posOpts);
button.on('click', function() {
// skip `dragend` events
if(d3.event.defaultPrevented) return;
setActive(gd, menuOpts, buttonOpts, gHeader, gButton, buttonIndex);
Plots.executeAPICommand(gd, buttonOpts.method, buttonOpts.args);
gd.emit('plotly_buttonclicked', {menu: menuOpts, button: buttonOpts, active: menuOpts.active});
});
button.on('mouseover', function() {
button.call(styleOnMouseOver);
});
button.on('mouseout', function() {
button.call(styleOnMouseOut, menuOpts);
buttons.call(styleButtons, menuOpts);
});
});
buttons.call(styleButtons, menuOpts);
scrollBox.enable();
var active = menuOpts.active,
i;
if(isVertical) {
if(scrollBox._vbar) {
var translateY = 0;
for(i = 0; i < active; i++) {
translateY += menuOpts.heights[i] + constants.gapButton;
}
translateY -= constants.gapButton;
scrollBox.setTranslate(0, translateY);
}
}
else {
if(scrollBox._hbar) {
var translateX = 0;
for(i = 0; i < active; i++) {
translateX += menuOpts.widths[i] + constants.gapButton;
}
translateX -= constants.gapButton;
scrollBox.setTranslate(translateX, 0);
}
}
}
function setActive(gd, menuOpts, buttonOpts, gHeader, gButton, buttonIndex, isSilentUpdate) {
// update 'active' attribute in menuOpts
menuOpts._input.active = menuOpts.active = buttonIndex;
if(menuOpts.type === 'dropdown') {
// fold up buttons and redraw header
gButton.attr(constants.menuIndexAttrName, '-1');
drawHeader(gd, gHeader, gButton, menuOpts);
}
if(!isSilentUpdate || menuOpts.type === 'buttons') {
drawButtons(gd, gHeader, gButton, menuOpts);
}
}
function drawItem(item, menuOpts, itemOpts) {
item.call(drawItemRect, menuOpts)
.call(drawItemText, menuOpts, itemOpts);
}
function drawItemRect(item, menuOpts) {
var rect = item.selectAll('rect')
.data([0]);
rect.enter().append('rect')
.classed(constants.itemRectClassName, true)
.attr({
rx: constants.rx,
ry: constants.ry,
'shape-rendering': 'crispEdges'
});
rect.call(Color.stroke, menuOpts.bordercolor)
.call(Color.fill, menuOpts.bgcolor)
.style('stroke-width', menuOpts.borderwidth + 'px');
}
function drawItemText(item, menuOpts, itemOpts) {
var text = item.selectAll('text')
.data([0]);
text.enter().append('text')
.classed(constants.itemTextClassName, true)
.classed('user-select-none', true)
.attr('text-anchor', 'start');
text.call(Drawing.font, menuOpts.font)
.text(itemOpts.label)
.call(svgTextUtils.convertToTspans);
}
function styleButtons(buttons, menuOpts) {
var active = menuOpts.active;
buttons.each(function(buttonOpts, i) {
var button = d3.select(this);
if(i === active && menuOpts.showactive) {
button.select('rect.' + constants.itemRectClassName)
.call(Color.fill, constants.activeColor);
}
});
}
function styleOnMouseOver(item) {
item.select('rect.' + constants.itemRectClassName)
.call(Color.fill, constants.hoverColor);
}
function styleOnMouseOut(item, menuOpts) {
item.select('rect.' + constants.itemRectClassName)
.call(Color.fill, menuOpts.bgcolor);
}
// find item dimensions (this mutates menuOpts)
function findDimensions(gd, menuOpts) {
menuOpts.width1 = 0;
menuOpts.height1 = 0;
menuOpts.heights = [];
menuOpts.widths = [];
menuOpts.totalWidth = 0;
menuOpts.totalHeight = 0;
menuOpts.openWidth = 0;
menuOpts.openHeight = 0;
menuOpts.lx = 0;
menuOpts.ly = 0;
var fakeButtons = gd._tester.selectAll('g.' + constants.dropdownButtonClassName)
.data(menuOpts.buttons);
fakeButtons.enter().append('g')
.classed(constants.dropdownButtonClassName, true);
var isVertical = ['up', 'down'].indexOf(menuOpts.direction) !== -1;
// loop over fake buttons to find width / height
fakeButtons.each(function(buttonOpts, i) {
var button = d3.select(this);
button.call(drawItem, menuOpts, buttonOpts);
var text = button.select('.' + constants.itemTextClassName),
tspans = text.selectAll('tspan');
// width is given by max width of all buttons
var tWidth = text.node() && Drawing.bBox(text.node()).width,
wEff = Math.max(tWidth + constants.textPadX, constants.minWidth);
// height is determined by item text
var tHeight = menuOpts.font.size * constants.fontSizeToHeight,
tLines = tspans[0].length || 1,
hEff = Math.max(tHeight * tLines, constants.minHeight) + constants.textOffsetY;
hEff = Math.ceil(hEff);
wEff = Math.ceil(wEff);
// Store per-item sizes since a row of horizontal buttons, for example,
// don't all need to be the same width:
menuOpts.widths[i] = wEff;
menuOpts.heights[i] = hEff;
// Height and width of individual element:
menuOpts.height1 = Math.max(menuOpts.height1, hEff);
menuOpts.width1 = Math.max(menuOpts.width1, wEff);
if(isVertical) {
menuOpts.totalWidth = Math.max(menuOpts.totalWidth, wEff);
menuOpts.openWidth = menuOpts.totalWidth;
menuOpts.totalHeight += hEff + constants.gapButton;
menuOpts.openHeight += hEff + constants.gapButton;
} else {
menuOpts.totalWidth += wEff + constants.gapButton;
menuOpts.openWidth += wEff + constants.gapButton;
menuOpts.totalHeight = Math.max(menuOpts.totalHeight, hEff);
menuOpts.openHeight = menuOpts.totalHeight;
}
});
if(isVertical) {
menuOpts.totalHeight -= constants.gapButton;
} else {
menuOpts.totalWidth -= constants.gapButton;
}
menuOpts.headerWidth = menuOpts.width1 + constants.arrowPadX;
menuOpts.headerHeight = menuOpts.height1;
if(menuOpts.type === 'dropdown') {
if(isVertical) {
menuOpts.width1 += constants.arrowPadX;
menuOpts.totalHeight = menuOpts.height1;
} else {
menuOpts.totalWidth = menuOpts.width1;
}
menuOpts.totalWidth += constants.arrowPadX;
}
fakeButtons.remove();
var paddedWidth = menuOpts.totalWidth + menuOpts.pad.l + menuOpts.pad.r;
var paddedHeight = menuOpts.totalHeight + menuOpts.pad.t + menuOpts.pad.b;
var graphSize = gd._fullLayout._size;
menuOpts.lx = graphSize.l + graphSize.w * menuOpts.x;
menuOpts.ly = graphSize.t + graphSize.h * (1 - menuOpts.y);
var xanchor = 'left';
if(anchorUtils.isRightAnchor(menuOpts)) {
menuOpts.lx -= paddedWidth;
xanchor = 'right';
}
if(anchorUtils.isCenterAnchor(menuOpts)) {
menuOpts.lx -= paddedWidth / 2;
xanchor = 'center';
}
var yanchor = 'top';
if(anchorUtils.isBottomAnchor(menuOpts)) {
menuOpts.ly -= paddedHeight;
yanchor = 'bottom';
}
if(anchorUtils.isMiddleAnchor(menuOpts)) {
menuOpts.ly -= paddedHeight / 2;
yanchor = 'middle';
}
menuOpts.totalWidth = Math.ceil(menuOpts.totalWidth);
menuOpts.totalHeight = Math.ceil(menuOpts.totalHeight);
menuOpts.lx = Math.round(menuOpts.lx);
menuOpts.ly = Math.round(menuOpts.ly);
Plots.autoMargin(gd, constants.autoMarginIdRoot + menuOpts._index, {
x: menuOpts.x,
y: menuOpts.y,
l: paddedWidth * ({right: 1, center: 0.5}[xanchor] || 0),
r: paddedWidth * ({left: 1, center: 0.5}[xanchor] || 0),
b: paddedHeight * ({top: 1, middle: 0.5}[yanchor] || 0),
t: paddedHeight * ({bottom: 1, middle: 0.5}[yanchor] || 0)
});
}
// set item positions (mutates posOpts)
function setItemPosition(item, menuOpts, posOpts, overrideOpts) {
overrideOpts = overrideOpts || {};
var rect = item.select('.' + constants.itemRectClassName),
text = item.select('.' + constants.itemTextClassName),
tspans = text.selectAll('tspan'),
borderWidth = menuOpts.borderwidth,
index = posOpts.index;
Lib.setTranslate(item, borderWidth + posOpts.x, borderWidth + posOpts.y);
var isVertical = ['up', 'down'].indexOf(menuOpts.direction) !== -1;
rect.attr({
x: 0,
y: 0,
width: overrideOpts.width || (isVertical ? menuOpts.width1 : menuOpts.widths[index]),
height: overrideOpts.height || (isVertical ? menuOpts.heights[index] : menuOpts.height1)
});
var tHeight = menuOpts.font.size * constants.fontSizeToHeight,
tLines = tspans[0].length || 1,
spanOffset = ((tLines - 1) * tHeight / 4);
var textAttrs = {
x: constants.textOffsetX,
y: menuOpts.heights[index] / 2 - spanOffset + constants.textOffsetY
};
text.attr(textAttrs);
tspans.attr(textAttrs);
if(isVertical) {
posOpts.y += menuOpts.heights[index] + posOpts.yPad;
} else {
posOpts.x += menuOpts.widths[index] + posOpts.xPad;
}
posOpts.index++;
}
function removeAllButtons(gButton) {
gButton.selectAll('g.' + constants.dropdownButtonClassName).remove();
// remove scrollbox
gButton.selectAll('rect.scrollbar-horizontal').remove();
gButton.selectAll('rect.scrollbar-vertical').remove();
gButton.call(Drawing.setClipUrl, null);
}
function clearPushMargins(gd) {
var pushMargins = gd._fullLayout._pushmargin || {},
keys = Object.keys(pushMargins);
for(var i = 0; i < keys.length; i++) {
var k = keys[i];
if(k.indexOf(constants.autoMarginIdRoot) !== -1) {
Plots.autoMargin(gd, k);
}
}
}