Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 2k
Handle weekday strings in rangebreaks#4661
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
a03d64dd52a80305ab23cdb2c3a3c48f551550a375e481a1384c2a6f9a0416aFile filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -8,6 +8,8 @@ | ||
| 'use strict'; | ||
| var isNumeric = require('fast-isnumeric'); | ||
| var Registry = require('../../registry'); | ||
| var Lib = require('../../lib'); | ||
| @@ -21,6 +23,9 @@ var handleCategoryOrderDefaults = require('./category_order_defaults'); | ||
| var handleLineGridDefaults = require('./line_grid_defaults'); | ||
| var setConvert = require('./set_convert'); | ||
| var DAY_OF_WEEK = require('./constants').WEEKDAY_PATTERN; | ||
| var HOUR = require('./constants').HOUR_PATTERN; | ||
| /** | ||
| * options: object containing: | ||
| * | ||
| @@ -155,10 +160,67 @@ function rangebreaksDefaults(itemIn, itemOut, containerOut) { | ||
| if(enabled) { | ||
| var bnds = coerce('bounds'); | ||
| if(bnds && bnds.length >= 2) { | ||
| if(bnds.length > 2) { | ||
| itemOut.bounds = itemOut.bounds.slice(0, 2); | ||
| var dfltPattern = ''; | ||
| var i, q; | ||
| if(bnds.length === 2) { | ||
| for(i = 0; i < 2; i++) { | ||
| q = indexOfDay(bnds[i]); | ||
| if(q) { | ||
| dfltPattern = DAY_OF_WEEK; | ||
| break; | ||
| } | ||
| } | ||
| } | ||
| var pattern = coerce('pattern', dfltPattern); | ||
| if(pattern === DAY_OF_WEEK) { | ||
| for(i = 0; i < 2; i++) { | ||
| q = indexOfDay(bnds[i]); | ||
| if(q) { | ||
| // convert to integers i.e 'Sunday' --> 0 | ||
| itemOut.bounds[i] = bnds[i] = q - 1; | ||
| } | ||
| } | ||
| } | ||
| if(pattern) { | ||
| // ensure types and ranges | ||
| for(i = 0; i < 2; i++) { | ||
| q = bnds[i]; | ||
| switch(pattern) { | ||
| case DAY_OF_WEEK : | ||
| if(!isNumeric(q)) { | ||
| itemOut.enabled = false; | ||
| return; | ||
| } | ||
| q = +q; | ||
| if( | ||
| q !== Math.floor(q) || // don't accept fractional days for mow | ||
| q < 0 || q >= 7 | ||
| ) { | ||
| itemOut.enabled = false; | ||
| return; | ||
| } | ||
| // use number | ||
| itemOut.bounds[i] = bnds[i] = q; | ||
| break; | ||
| case HOUR : | ||
| if(!isNumeric(q)) { | ||
| itemOut.enabled = false; | ||
| return; | ||
| } | ||
| q = +q; | ||
| if(q < 0 || q > 24) { // accept 24 | ||
| itemOut.enabled = false; | ||
| return; | ||
| } | ||
| // use number | ||
| itemOut.bounds[i] = bnds[i] = q; | ||
| break; | ||
| } | ||
| } | ||
| } | ||
| if(containerOut.autorange === false) { | ||
| @@ -175,8 +237,6 @@ function rangebreaksDefaults(itemIn, itemOut, containerOut) { | ||
| return; | ||
| } | ||
| } | ||
| coerce('pattern'); | ||
| } else { | ||
| var values = coerce('values'); | ||
| @@ -189,3 +249,21 @@ function rangebreaksDefaults(itemIn, itemOut, containerOut) { | ||
| } | ||
| } | ||
| } | ||
| // these numbers are one more than what bounds would be mapped to | ||
Collaborator There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. clever way to avoid having to distinguish 0 from undefined :) | ||
| var dayStrToNum = { | ||
| sun: 1, | ||
| mon: 2, | ||
| tue: 3, | ||
| wed: 4, | ||
| thu: 5, | ||
| fri: 6, | ||
| sat: 7 | ||
| }; | ||
| function indexOfDay(v) { | ||
| if(typeof v !== 'string') return; | ||
| return dayStrToNum[ | ||
| v.substr(0, 3).toLowerCase() | ||
| ]; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -280,16 +280,18 @@ module.exports = { | ||
| pattern: { | ||
| valType: 'enumerated', | ||
| values: [DAY_OF_WEEK, HOUR, ''], | ||
| dflt: '', | ||
| role: 'info', | ||
| editType: 'calc', | ||
| description: [ | ||
| 'Determines a pattern on the time line that generates breaks.', | ||
| 'If *' + DAY_OF_WEEK + '* - Sunday-based weekday as a decimal number [0, 6].', | ||
| 'If *' + DAY_OF_WEEK + '* - days of the week in English e.g. \'Sunday\' or `\sun\`', | ||
alexcjohnson marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| '(matching is case-insensitive and considers only the first three characters),', | ||
| 'as well as Sunday-based integers between 0 and 6.', | ||
| 'If *' + HOUR + '* - hour (24-hour clock) as decimal numbers between 0 and 24.', | ||
| 'for more info.', | ||
| 'Examples:', | ||
| '- { pattern: \'' + DAY_OF_WEEK + '\', bounds: [6, 0] }', | ||
| '- { pattern: \'' + DAY_OF_WEEK + '\', bounds: [6, 1] }', | ||
| ' or simply { bounds: [\'sat\', \'mon\'] }', | ||
| ' breaks from Saturday to Monday (i.e. skips the weekends).', | ||
| '- { pattern: \'' + HOUR + '\', bounds: [17, 8] }', | ||
| ' breaks from 5pm to 8am (i.e. skips non-work hours).' | ||
Uh oh!
There was an error while loading. Please reload this page.