Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 86e09bb

Browse files
committed
add visible attribute to lots of container array items:
rangeselector.buttons updatemenus.buttons slider.steps axes.tickformatstops mapbox.layers
1 parent b5ccfbe commit 86e09bb

21 files changed

+426
-221
lines changed

src/components/rangeselector/button_attributes.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@
1010

1111

1212
module.exports = {
13+
visible: {
14+
valType: 'boolean',
15+
role: 'info',
16+
editType: 'plot',
17+
description: 'Determines whether or not this button is visible.'
18+
},
1319
step: {
1420
valType: 'enumerated',
1521
role: 'info',

src/components/rangeselector/defaults.js

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,16 @@
1010

1111
var Lib = require('../../lib');
1212
var Color = require('../color');
13+
var Template = require('../../plot_api/plot_template');
1314

1415
var attributes = require('./attributes');
1516
var buttonAttrs = require('./button_attributes');
1617
var constants = require('./constants');
1718

1819

1920
module.exports = function handleDefaults(containerIn, containerOut, layout, counterAxes, calendar) {
20-
var selectorIn = containerIn.rangeselector || {},
21-
selectorOut = containerOut.rangeselector = {};
21+
var selectorIn = containerIn.rangeselector || {};
22+
var selectorOut = Template.newContainer(containerOut, 'rangeselector');
2223

2324
function coerce(attr, dflt) {
2425
return Lib.coerce(selectorIn, selectorOut, attributes, attr, dflt);
@@ -59,22 +60,24 @@ function buttonsDefaults(containerIn, containerOut, calendar) {
5960
buttonIn = buttonsIn[i];
6061
buttonOut = {};
6162

62-
if(!Lib.isPlainObject(buttonIn)) continue;
63+
var visible = coerce('visible', Lib.isPlainObject(buttonIn));
6364

64-
var step = coerce('step');
65-
if(step !== 'all') {
66-
if(calendar && calendar !== 'gregorian' && (step === 'month' || step === 'year')) {
67-
buttonOut.stepmode = 'backward';
68-
}
69-
else {
70-
coerce('stepmode');
65+
if(visible) {
66+
var step = coerce('step');
67+
if(step !== 'all') {
68+
if(calendar && calendar !== 'gregorian' && (step === 'month' || step === 'year')) {
69+
buttonOut.stepmode = 'backward';
70+
}
71+
else {
72+
coerce('stepmode');
73+
}
74+
75+
coerce('count');
7176
}
7277

73-
coerce('count');
78+
coerce('label');
7479
}
7580

76-
coerce('label');
77-
7881
buttonOut._index = i;
7982
buttonsOut.push(buttonOut);
8083
}

src/components/rangeselector/draw.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ module.exports = function draw(gd) {
5050
selectorLayout = axisLayout.rangeselector;
5151

5252
var buttons = selector.selectAll('g.button')
53-
.data(selectorLayout.buttons);
53+
.data(Lib.filterVisible(selectorLayout.buttons));
5454

5555
buttons.enter().append('g')
5656
.classed('button', true);

src/components/sliders/attributes.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@ var constants = require('./constants');
1818
var stepsAttrs = {
1919
_isLinkedToArray: 'step',
2020

21+
visible: {
22+
valType: 'boolean',
23+
role: 'info',
24+
dflt: true,
25+
description: [
26+
'Determines whether or not this step is included in the slider.'
27+
].join(' ')
28+
},
2129
method: {
2230
valType: 'enumerated',
2331
values: ['restyle', 'relayout', 'animate', 'update', 'skip'],

src/components/sliders/defaults.js

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,10 @@ var stepAttrs = attributes.steps;
1919

2020

2121
module.exports = function slidersDefaults(layoutIn, layoutOut) {
22-
var opts = {
22+
handleArrayContainerDefaults(layoutIn, layoutOut, {
2323
name: name,
2424
handleItemDefaults: sliderDefaults
25-
};
26-
27-
handleArrayContainerDefaults(layoutIn, layoutOut, opts);
25+
});
2826
};
2927

3028
function sliderDefaults(sliderIn, sliderOut, layoutOut) {
@@ -33,12 +31,27 @@ function sliderDefaults(sliderIn, sliderOut, layoutOut) {
3331
return Lib.coerce(sliderIn, sliderOut, attributes, attr, dflt);
3432
}
3533

36-
var steps = stepsDefaults(sliderIn, sliderOut);
34+
var steps = handleArrayContainerDefaults(sliderIn, sliderOut, {
35+
name: 'steps',
36+
handleItemDefaults: stepDefaults
37+
});
38+
39+
var stepCount = 0;
40+
for(var i = 0; i < steps.length; i++) {
41+
if(steps[i].visible) stepCount++;
42+
}
3743

38-
var visible = coerce('visible', steps.length > 0);
44+
var visible;
45+
// If it has fewer than two options, it's not really a slider
46+
if(stepCount < 2) visible = sliderOut.visible = false;
47+
else visible = coerce('visible');
3948
if(!visible) return;
4049

41-
coerce('active');
50+
sliderOut._stepCount = stepCount;
51+
var visSteps = sliderOut._visibleSteps = Lib.filterVisible(steps);
52+
53+
var active = coerce('active');
54+
if(!(steps[active] || {}).visible) sliderOut.active = visSteps[0]._index;
4255

4356
coerce('x');
4457
coerce('y');
@@ -81,33 +94,21 @@ function sliderDefaults(sliderIn, sliderOut, layoutOut) {
8194
coerce('minorticklen');
8295
}
8396

84-
function stepsDefaults(sliderIn, sliderOut) {
85-
var valuesIn = sliderIn.steps || [],
86-
valuesOut = sliderOut.steps = [];
87-
88-
var valueIn, valueOut;
89-
97+
function stepDefaults(valueIn, valueOut, sliderOut, opts, itemOpts) {
9098
function coerce(attr, dflt) {
9199
return Lib.coerce(valueIn, valueOut, stepAttrs, attr, dflt);
92100
}
93101

94-
for(var i = 0; i < valuesIn.length; i++) {
95-
valueIn = valuesIn[i];
96-
valueOut = {};
97-
98-
coerce('method');
99-
100-
if(!Lib.isPlainObject(valueIn) || (valueOut.method !== 'skip' && !Array.isArray(valueIn.args))) {
101-
continue;
102-
}
103-
104-
coerce('args');
105-
coerce('label', 'step-' + i);
106-
coerce('value', valueOut.label);
107-
coerce('execute');
108-
109-
valuesOut.push(valueOut);
102+
var visible;
103+
if(itemOpts.itemIsNotPlainObject || (valueIn.method !== 'skip' && !Array.isArray(valueIn.args))) {
104+
visible = valueOut.visible = false;
110105
}
106+
else visible = coerce('visible');
107+
if(!visible) return;
111108

112-
return valuesOut;
109+
coerce('method');
110+
coerce('args');
111+
var label = coerce('label', 'step-' + itemOpts.index);
112+
coerce('value', label);
113+
coerce('execute');
113114
}

src/components/sliders/draw.js

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,11 @@ module.exports = function draw(gd) {
7474
}
7575

7676
sliderGroups.each(function(sliderOpts) {
77-
// If it has fewer than two options, it's not really a slider:
78-
if(sliderOpts.steps.length < 2) return;
79-
8077
var gSlider = d3.select(this);
8178

8279
computeLabelSteps(sliderOpts);
8380

84-
Plots.manageCommandObserver(gd, sliderOpts, sliderOpts.steps, function(data) {
81+
Plots.manageCommandObserver(gd, sliderOpts, sliderOpts._visibleSteps, function(data) {
8582
// NB: Same as below. This is *not* always the same as sliderOpts since
8683
// if a new set of steps comes in, the reference in this callback would
8784
// be invalid. We need to refetch it from the slider group, which is
@@ -111,7 +108,7 @@ function makeSliderData(fullLayout, gd) {
111108

112109
for(var i = 0; i < contOpts.length; i++) {
113110
var item = contOpts[i];
114-
if(!item.visible || !item.steps.length) continue;
111+
if(!item.visible) continue;
115112
item._gd = gd;
116113
sliderData.push(item);
117114
}
@@ -127,7 +124,7 @@ function keyFunction(opts) {
127124
// Compute the dimensions (mutates sliderOpts):
128125
function findDimensions(gd, sliderOpts) {
129126
var sliderLabels = Drawing.tester.selectAll('g.' + constants.labelGroupClass)
130-
.data(sliderOpts.steps);
127+
.data(sliderOpts._visibleSteps);
131128

132129
sliderLabels.enter().append('g')
133130
.classed(constants.labelGroupClass, true);
@@ -176,7 +173,7 @@ function findDimensions(gd, sliderOpts) {
176173
dims.inputAreaLength = Math.round(dims.outerLength - sliderOpts.pad.l - sliderOpts.pad.r);
177174

178175
var textableInputLength = dims.inputAreaLength - 2 * constants.stepInset;
179-
var availableSpacePerLabel = textableInputLength / (sliderOpts.steps.length - 1);
176+
var availableSpacePerLabel = textableInputLength / (sliderOpts._stepCount - 1);
180177
var computedSpacePerLabel = maxLabelWidth + constants.labelPadding;
181178
dims.labelStride = Math.max(1, Math.ceil(computedSpacePerLabel / availableSpacePerLabel));
182179
dims.labelHeight = labelHeight;
@@ -260,8 +257,8 @@ function drawSlider(gd, sliderGroup, sliderOpts) {
260257
// the *current* slider step is removed. The drawing functions will error out
261258
// when they fail to find it, so the fix for now is that it will just draw the
262259
// slider in the first position but will not execute the command.
263-
if(sliderOpts.active >= sliderOpts.steps.length) {
264-
sliderOpts.active = 0;
260+
if(!((sliderOpts.steps[sliderOpts.active] || {}).visible)) {
261+
sliderOpts.active = sliderOpts._visibleSteps[0]._index;
265262
}
266263

267264
// These are carefully ordered for proper z-ordering:
@@ -278,7 +275,7 @@ function drawSlider(gd, sliderGroup, sliderOpts) {
278275
// Position the rectangle:
279276
Drawing.setTranslate(sliderGroup, dims.lx + sliderOpts.pad.l, dims.ly + sliderOpts.pad.t);
280277

281-
sliderGroup.call(setGripPosition, sliderOpts, sliderOpts.active / (sliderOpts.steps.length - 1), false);
278+
sliderGroup.call(setGripPosition, sliderOpts, false);
282279
sliderGroup.call(drawCurrentValue, sliderOpts);
283280

284281
}
@@ -406,10 +403,11 @@ function drawLabelGroup(sliderGroup, sliderOpts) {
406403
}
407404

408405
function handleInput(gd, sliderGroup, sliderOpts, normalizedPosition, doTransition) {
409-
var quantizedPosition = Math.round(normalizedPosition * (sliderOpts.steps.length - 1));
406+
var quantizedPosition = Math.round(normalizedPosition * (sliderOpts._stepCount - 1));
407+
var quantizedIndex = sliderOpts._visibleSteps[quantizedPosition]._index;
410408

411-
if(quantizedPosition !== sliderOpts.active) {
412-
setActive(gd, sliderGroup, sliderOpts, quantizedPosition, true, doTransition);
409+
if(quantizedIndex !== sliderOpts.active) {
410+
setActive(gd, sliderGroup, sliderOpts, quantizedIndex, true, doTransition);
413411
}
414412
}
415413

@@ -419,7 +417,7 @@ function setActive(gd, sliderGroup, sliderOpts, index, doCallback, doTransition)
419417

420418
var step = sliderOpts.steps[sliderOpts.active];
421419

422-
sliderGroup.call(setGripPosition, sliderOpts, sliderOpts.active / (sliderOpts.steps.length - 1), doTransition);
420+
sliderGroup.call(setGripPosition, sliderOpts, doTransition);
423421
sliderGroup.call(drawCurrentValue, sliderOpts);
424422

425423
gd.emit('plotly_sliderchange', {
@@ -502,7 +500,7 @@ function attachGripEvents(item, gd, sliderGroup) {
502500

503501
function drawTicks(sliderGroup, sliderOpts) {
504502
var tick = sliderGroup.selectAll('rect.' + constants.tickRectClass)
505-
.data(sliderOpts.steps);
503+
.data(sliderOpts._visibleSteps);
506504
var dims = sliderOpts._dims;
507505

508506
tick.enter().append('rect')
@@ -524,7 +522,7 @@ function drawTicks(sliderGroup, sliderOpts) {
524522
.call(Color.fill, isMajor ? sliderOpts.tickcolor : sliderOpts.tickcolor);
525523

526524
Drawing.setTranslate(item,
527-
normalizedValueToPosition(sliderOpts, i / (sliderOpts.steps.length - 1)) - 0.5 * sliderOpts.tickwidth,
525+
normalizedValueToPosition(sliderOpts, i / (sliderOpts._stepCount - 1)) - 0.5 * sliderOpts.tickwidth,
528526
(isMajor ? constants.tickOffset : constants.minorTickOffset) + dims.currentValueTotalHeight
529527
);
530528
});
@@ -534,21 +532,28 @@ function drawTicks(sliderGroup, sliderOpts) {
534532
function computeLabelSteps(sliderOpts) {
535533
var dims = sliderOpts._dims;
536534
dims.labelSteps = [];
537-
var i0 = 0;
538-
var nsteps = sliderOpts.steps.length;
535+
var nsteps = sliderOpts._stepCount;
539536

540-
for(var i = i0; i < nsteps; i += dims.labelStride) {
537+
for(var i = 0; i < nsteps; i += dims.labelStride) {
541538
dims.labelSteps.push({
542539
fraction: i / (nsteps - 1),
543-
step: sliderOpts.steps[i]
540+
step: sliderOpts._visibleSteps[i]
544541
});
545542
}
546543
}
547544

548-
function setGripPosition(sliderGroup, sliderOpts, position, doTransition) {
545+
function setGripPosition(sliderGroup, sliderOpts, doTransition) {
549546
var grip = sliderGroup.select('rect.' + constants.gripRectClass);
550547

551-
var x = normalizedValueToPosition(sliderOpts, position);
548+
var quantizedIndex = 0;
549+
for(var i = 0; i < sliderOpts._stepCount; i++) {
550+
if(sliderOpts._visibleSteps[i]._index === sliderOpts.active) {
551+
quantizedIndex = i;
552+
break;
553+
}
554+
}
555+
556+
var x = normalizedValueToPosition(sliderOpts, quantizedIndex / (sliderOpts._stepCount - 1));
552557

553558
// If this is true, then *this component* is already invoking its own command
554559
// and has triggered its own animation.

src/components/updatemenus/attributes.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ var padAttrs = require('../../plots/pad_attributes');
1717
var buttonsAttrs = {
1818
_isLinkedToArray: 'button',
1919

20+
visible: {
21+
valType: 'boolean',
22+
role: 'info',
23+
description: 'Determines whether or not this button is visible.'
24+
},
2025
method: {
2126
valType: 'enumerated',
2227
values: ['restyle', 'relayout', 'animate', 'update', 'skip'],

src/components/updatemenus/defaults.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,16 +76,15 @@ function buttonsDefaults(menuIn, menuOut) {
7676
buttonIn = buttonsIn[i];
7777
buttonOut = {};
7878

79-
coerce('method');
80-
81-
if(!Lib.isPlainObject(buttonIn) || (buttonOut.method !== 'skip' && !Array.isArray(buttonIn.args))) {
82-
continue;
79+
var visible = coerce('visible', Lib.isPlainObject(buttonIn) &&
80+
(buttonIn.method === 'skip' || Array.isArray(buttonIn.args)));
81+
if(visible) {
82+
coerce('method');
83+
coerce('args');
84+
coerce('label');
85+
coerce('execute');
8386
}
8487

85-
coerce('args');
86-
coerce('label');
87-
coerce('execute');
88-
8988
buttonOut._index = i;
9089
buttonsOut.push(buttonOut);
9190
}

0 commit comments

Comments
 (0)