Skip to content

Commit 7bd501f

Browse files
committed
prevent non-gregorian month/year todate range selectors
1 parent 509f287 commit 7bd501f

File tree

4 files changed

+66
-10
lines changed

4 files changed

+66
-10
lines changed

src/components/rangeselector/button_attributes.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ module.exports = {
3333
'*step* milliseconds back.',
3434
'For example, with `step` set to *year* and `count` set to *1*',
3535
'the range update shifts the start of the range back to',
36-
'January 01 of the current year.'
36+
'January 01 of the current year.',
37+
'Month and year *todate* are currently available only',
38+
'for the built-in (Gregorian) calendar.'
3739
].join(' ')
3840
},
3941
count: {

src/components/rangeselector/defaults.js

+10-4
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@ var buttonAttrs = require('./button_attributes');
1616
var constants = require('./constants');
1717

1818

19-
module.exports = function handleDefaults(containerIn, containerOut, layout, counterAxes) {
19+
module.exports = function handleDefaults(containerIn, containerOut, layout, counterAxes, calendar) {
2020
var selectorIn = containerIn.rangeselector || {},
2121
selectorOut = containerOut.rangeselector = {};
2222

2323
function coerce(attr, dflt) {
2424
return Lib.coerce(selectorIn, selectorOut, attributes, attr, dflt);
2525
}
2626

27-
var buttons = buttonsDefaults(selectorIn, selectorOut);
27+
var buttons = buttonsDefaults(selectorIn, selectorOut, calendar);
2828

2929
var visible = coerce('visible', buttons.length > 0);
3030
if(!visible) return;
@@ -45,7 +45,7 @@ module.exports = function handleDefaults(containerIn, containerOut, layout, coun
4545
coerce('borderwidth');
4646
};
4747

48-
function buttonsDefaults(containerIn, containerOut) {
48+
function buttonsDefaults(containerIn, containerOut, calendar) {
4949
var buttonsIn = containerIn.buttons || [],
5050
buttonsOut = containerOut.buttons = [];
5151

@@ -63,7 +63,13 @@ function buttonsDefaults(containerIn, containerOut) {
6363

6464
var step = coerce('step');
6565
if(step !== 'all') {
66-
coerce('stepmode');
66+
if(calendar && calendar !== 'gregorian' && (step === 'month' || step === 'year')) {
67+
buttonOut.stepmode = 'backward';
68+
}
69+
else {
70+
coerce('stepmode');
71+
}
72+
6773
coerce('count');
6874
}
6975

src/plots/cartesian/layout_defaults.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,8 @@ module.exports = function supplyLayoutDefaults(layoutIn, layoutOut, fullData) {
167167
rangeSliderDefaults(layoutIn, layoutOut, axName, counterAxes);
168168

169169
if(axLetter === 'x' && axLayoutOut.type === 'date') {
170-
rangeSelectorDefaults(axLayoutIn, axLayoutOut, layoutOut, counterAxes);
170+
rangeSelectorDefaults(axLayoutIn, axLayoutOut, layoutOut, counterAxes,
171+
axLayoutOut.calendar);
171172
}
172173
});
173174
};

test/jasmine/tests/range_selector_test.js

+51-4
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ describe('range selector defaults:', function() {
1717

1818
var handleDefaults = RangeSelector.handleDefaults;
1919

20-
function supply(containerIn, containerOut) {
20+
function supply(containerIn, containerOut, calendar) {
2121
containerOut.domain = [0, 1];
2222

2323
var layout = {
@@ -26,7 +26,7 @@ describe('range selector defaults:', function() {
2626

2727
var counterAxes = ['yaxis'];
2828

29-
handleDefaults(containerIn, containerOut, layout, counterAxes);
29+
handleDefaults(containerIn, containerOut, layout, counterAxes, calendar);
3030
}
3131

3232
it('should set \'visible\' to false when no buttons are present', function() {
@@ -94,7 +94,7 @@ describe('range selector defaults:', function() {
9494
};
9595
var containerOut = {};
9696

97-
supply(containerIn, containerOut, {}, []);
97+
supply(containerIn, containerOut);
9898

9999
expect(containerOut.rangeselector.visible).toBe(true);
100100
expect(containerOut.rangeselector.buttons).toEqual([
@@ -114,7 +114,7 @@ describe('range selector defaults:', function() {
114114
};
115115
var containerOut = {};
116116

117-
supply(containerIn, containerOut, {}, []);
117+
supply(containerIn, containerOut);
118118

119119
expect(containerOut.rangeselector.buttons).toEqual([{
120120
step: 'all',
@@ -176,6 +176,53 @@ describe('range selector defaults:', function() {
176176
expect(containerOut.rangeselector.x).toEqual(0.5);
177177
expect(containerOut.rangeselector.y).toBeCloseTo(0.87);
178178
});
179+
180+
it('should not allow month/year todate with calendars other than Gregorian', function() {
181+
var containerIn = {
182+
rangeselector: {
183+
buttons: [{
184+
step: 'year',
185+
count: 1,
186+
stepmode: 'todate'
187+
}, {
188+
step: 'month',
189+
count: 6,
190+
stepmode: 'todate'
191+
}, {
192+
step: 'day',
193+
count: 1,
194+
stepmode: 'todate'
195+
}, {
196+
step: 'hour',
197+
count: 1,
198+
stepmode: 'todate'
199+
}]
200+
}
201+
};
202+
var containerOut;
203+
function getStepmode(button) { return button.stepmode; }
204+
205+
containerOut = {};
206+
supply(containerIn, containerOut);
207+
208+
expect(containerOut.rangeselector.buttons.map(getStepmode)).toEqual([
209+
'todate', 'todate', 'todate', 'todate'
210+
]);
211+
212+
containerOut = {};
213+
supply(containerIn, containerOut, 'gregorian');
214+
215+
expect(containerOut.rangeselector.buttons.map(getStepmode)).toEqual([
216+
'todate', 'todate', 'todate', 'todate'
217+
]);
218+
219+
containerOut = {};
220+
supply(containerIn, containerOut, 'chinese');
221+
222+
expect(containerOut.rangeselector.buttons.map(getStepmode)).toEqual([
223+
'backward', 'backward', 'todate', 'todate'
224+
]);
225+
});
179226
});
180227

181228
describe('range selector getUpdateObject:', function() {

0 commit comments

Comments
 (0)