Skip to content

Commit ab6c109

Browse files
committed
first pass range selector defaults
1 parent 36968ad commit ab6c109

File tree

4 files changed

+125
-0
lines changed

4 files changed

+125
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/**
2+
* Copyright 2012-2016, Plotly, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the MIT license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
9+
'use strict';
10+
11+
var Lib = require('../../lib');
12+
13+
var attributes = require('./attributes');
14+
var buttonAttrs = require('./button_attributes');
15+
16+
17+
module.exports = function rangeSelectorDefaults(containerIn, containerOut, layout) {
18+
var selectorIn = containerIn.rangeselector || {},
19+
selectorOut = containerOut.rangeselector = {};
20+
21+
function coerce(attr, dflt) {
22+
return Lib.coerce(selectorIn, selectorOut, attributes, attr, dflt);
23+
}
24+
25+
var buttons = buttonsDefaults(selectorIn, selectorOut);
26+
27+
var visible = coerce('visible', !!buttons.length);
28+
if(!visible) return;
29+
30+
coerce('x');
31+
coerce('xanchor');
32+
coerce('y');
33+
coerce('yanchor');
34+
35+
// coerce('width');
36+
// coerce('height');
37+
38+
Lib.coerceFont(coerce, 'font', layout.font);
39+
40+
coerce('bgcolor');
41+
coerce('bordercolor');
42+
coerce('borderwidth');
43+
};
44+
45+
function buttonsDefaults(containerIn, containerOut) {
46+
var buttonsIn = containerIn.buttons || [],
47+
buttonsOut = containerOut.buttons = [];
48+
49+
var buttonIn, buttonOut;
50+
51+
function coerce(attr, dflt) {
52+
return Lib.coerce(buttonIn, buttonOut, buttonAttrs, attr, dflt);
53+
}
54+
55+
for(var i = 0; i < buttonsIn.length; i++) {
56+
buttonIn = buttonsIn[i];
57+
buttonOut = {};
58+
59+
coerce('step');
60+
coerce('stepmode');
61+
coerce('count');
62+
coerce('label');
63+
64+
buttonsOut.push(buttonOut);
65+
}
66+
67+
return buttonsOut;
68+
}

src/plots/cartesian/layout_attributes.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88

99
'use strict';
10+
1011
var Cartesian = require('./index');
1112
var fontAttrs = require('../font_attributes');
1213
var colorAttrs = require('../../components/color/attributes');

src/plots/cartesian/layout_defaults.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ var Lib = require('../../lib');
1313
var Plots = require('../plots');
1414

1515
var RangeSlider = require('../../components/rangeslider');
16+
var rangeSelectorDefaults = require('../../components/range_selector/defaults');
1617

1718
var constants = require('./constants');
1819
var layoutAttributes = require('./layout_attributes');
@@ -132,6 +133,12 @@ module.exports = function supplyLayoutDefaults(layoutIn, layoutOut, fullData) {
132133

133134
handleAxisDefaults(axLayoutIn, axLayoutOut, coerce, defaultOptions);
134135
handlePositionDefaults(axLayoutIn, axLayoutOut, coerce, positioningOptions);
136+
137+
if(axLetter === 'x') {
138+
// TODO add check on axis type 'date'
139+
rangeSelectorDefaults(axLayoutIn, axLayoutOut, layoutOut);
140+
}
141+
135142
layoutOut[axName] = axLayoutOut;
136143

137144
// so we don't have to repeat autotype unnecessarily,
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
var RangeSelector = require('@src/components/range_selector');
2+
3+
4+
describe('range selector', function() {
5+
'use strict';
6+
7+
describe('defaults', function() {
8+
var supplyDefaults = RangeSelector.supplyDefaults;
9+
10+
it('should set \'visible\' to false when no buttons are present', function() {
11+
var containerIn = {};
12+
var containerOut = {};
13+
14+
supplyDefaults(containerIn, containerOut, {});
15+
16+
expect(containerOut.rangeselector)
17+
.toEqual({
18+
visible: false,
19+
buttons: []
20+
});
21+
});
22+
23+
it('should coerce all buttons present', function() {
24+
var containerIn = {
25+
rangeselector: {
26+
buttons: [{
27+
step: 'year',
28+
count: 10
29+
},{
30+
count: 6
31+
}]
32+
}
33+
};
34+
var containerOut = {};
35+
36+
supplyDefaults(containerIn, containerOut, {});
37+
38+
expect(containerOut.rangeselector.visible).toBe(true);
39+
expect(containerOut.rangeselector.buttons).toEqual([
40+
{ step: 'year', stepmode: 'backward', count: 10 },
41+
{ step: 'month', stepmode: 'backward', count: 6 }
42+
]);
43+
});
44+
45+
46+
});
47+
48+
49+
});

0 commit comments

Comments
 (0)