Skip to content
This repository was archived by the owner on Sep 12, 2024. It is now read-only.

Commit 881fa6a

Browse files
authored
Merge pull request #25 from sensortower/add-custom-period-range
Add custom period range
2 parents c89fd9f + adda8bf commit 881fa6a

File tree

8 files changed

+77
-21
lines changed

8 files changed

+77
-21
lines changed

docs/index.md

+21-5
Original file line numberDiff line numberDiff line change
@@ -195,18 +195,18 @@ ranges: Object
195195
Default:
196196
```javascript
197197
{
198-
'Last 30 days': [moment().subtract(29, 'days'), moment()]
199-
'Last 90 days': [moment().subtract(89, 'days'), moment()]
200-
'Last Year': [moment().subtract(1, 'year').add(1,'day'), moment()]
201-
'All Time': 'all-time' // [minDate, maxDate]
198+
'Last 30 days': [moment().subtract(29, 'days'), moment()],
199+
'Last 90 days': [moment().subtract(89, 'days'), moment()],
200+
'Last Year': [moment().subtract(1, 'year').add(1,'day'), moment()],
201+
'All Time': 'all-time' // [minDate, maxDate],
202202
'Custom Range': 'custom'
203203
}
204204
```
205205

206206
Examples:
207207
```javascript
208208
ranges: {
209-
'Last 245 Days': [moment().subtract(244, 'days'), moment()]
209+
'Last 245 Days': [moment().subtract(244, 'days'), moment()],
210210
'Last 3 Years': [moment().subtract(3, 'years').add(1, 'day'), moment()]
211211
}
212212
```
@@ -235,6 +235,22 @@ Array of available _periods_. Period selector disappears if only one period spec
235235
periods: String[]
236236
```
237237

238+
### customPeriodRanges
239+
240+
Similar to ranges except they are appended to the period row across the top of the picker.
241+
242+
```javascript
243+
ranges: Object
244+
```
245+
246+
Examples:
247+
```javascript
248+
customPeriodRanges: {
249+
'Last Year': [moment().subtract(1, 'year').add(1,'day'), moment()],
250+
'All Time': 'all-time' // [minDate, maxDate]
251+
}
252+
```
253+
238254
### single
239255
```javascript
240256
single: Boolean

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"gulp-gh-pages": "^0.5.4",
2222
"gulp-github-release": "^1.0.3",
2323
"gulp-header": "^1.7.1",
24-
"gulp-if": "^1.2.5",
24+
"gulp-if": "^2.0.2",
2525
"gulp-include": "^2.0.3",
2626
"gulp-load-plugins": "^0.10.0",
2727
"gulp-minify-css": "^1.1.1",

src/scripts/daterangepicker/calendar-view.coffee

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ class CalendarView
66
@locale = mainView.locale
77
@startDate = mainView.startDate
88
@endDate = mainView.endDate
9+
@isCustomPeriodRangeActive = mainView.isCustomPeriodRangeActive
910

1011
@type = type
1112
@label = mainView.locale["#{type}Label"] || ''
@@ -104,8 +105,8 @@ class CalendarView
104105
{
105106
"in-range": !@single() && (inRange || onRangeEnd)
106107
"#{@type}-date": onRangeEnd
107-
"clickable": withinBoundaries
108-
"out-of-boundaries": !withinBoundaries
108+
"clickable": withinBoundaries && !@isCustomPeriodRangeActive()
109+
"out-of-boundaries": !withinBoundaries || @isCustomPeriodRangeActive()
109110
"unavailable": (periodIsDay && differentMonth)
110111
}
111112

src/scripts/daterangepicker/config.coffee

+19-9
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ class Config
33
@firstDayOfWeek = @_firstDayOfWeek(options.firstDayOfWeek)
44
@timeZone = @_timeZone(options.timeZone)
55
@periods = @_periods(options.periods)
6+
@customPeriodRanges = @_customPeriodRanges(options.customPeriodRanges)
67
@period = @_period(options.period)
78
@single = @_single(options.single)
89
@opened = @_opened(options.opened)
@@ -19,6 +20,7 @@ class Config
1920
@endDate = @_endDate(options.endDate)
2021

2122
@ranges = @_ranges(options.ranges)
23+
@isCustomPeriodRangeActive = ko.observable(false)
2224

2325
@anchorElement = @_anchorElement(options.anchorElement)
2426
@parentElement = @_parentElement(options.parentElement)
@@ -40,6 +42,11 @@ class Config
4042
_periods: (val) ->
4143
ko.observableArray(val || Period.allPeriods)
4244

45+
_customPeriodRanges: (obj) ->
46+
obj ||= {}
47+
for title, value of obj
48+
@parseRange(value, title)
49+
4350
_period: (val) ->
4451
val ||= @periods()[0]
4552
throw new Error('Invalid period') unless val in ['day', 'week', 'month', 'quarter', 'year']
@@ -88,15 +95,18 @@ class Config
8895
when 'custom'
8996
new CustomDateRange(title)
9097
else
91-
throw new Error('Value should be an array') unless $.isArray(value)
92-
[startDate, endDate] = value
93-
throw new Error('Missing start date') unless startDate
94-
throw new Error('Missing end date') unless endDate
95-
from = MomentUtil.tz(startDate, @timeZone())
96-
to = MomentUtil.tz(endDate, @timeZone())
97-
throw new Error('Invalid start date') unless from.isValid()
98-
throw new Error('Invalid end date') unless to.isValid()
99-
new DateRange(title, from, to)
98+
@parseRange(value, title)
99+
100+
parseRange: (value, title) ->
101+
throw new Error('Value should be an array') unless $.isArray(value)
102+
[startDate, endDate] = value
103+
throw new Error('Missing start date') unless startDate
104+
throw new Error('Missing end date') unless endDate
105+
from = MomentUtil.tz(startDate, @timeZone())
106+
to = MomentUtil.tz(endDate, @timeZone())
107+
throw new Error('Invalid start date') unless from.isValid()
108+
throw new Error('Invalid end date') unless to.isValid()
109+
new DateRange(title, from, to)
100110

101111
_locale: (val) ->
102112
$.extend({

src/scripts/daterangepicker/date-range-picker-view.coffee

+9-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class DateRangePickerView
6565
expanded: @standalone() || @single() || @expanded()
6666
standalone: @standalone()
6767
'hide-weekdays': @hideWeekdays()
68-
'hide-periods': @periods().length == 1
68+
'hide-periods': (@periods().length + @customPeriodRanges.length) == 1
6969
'orientation-left': @orientation() == 'left'
7070
'orientation-right': @orientation() == 'right'
7171
}
@@ -85,10 +85,14 @@ class DateRangePickerView
8585
else
8686
@startDate().isSame(dateRange.startDate, 'day') && @endDate().isSame(dateRange.endDate, 'day')
8787

88+
isActiveCustomPeriodRange: (customPeriodRange) ->
89+
@isActiveDateRange(customPeriodRange) && @isCustomPeriodRangeActive()
90+
8891
inputFocus: () ->
8992
@expanded(true)
9093

9194
setPeriod: (period) ->
95+
@isCustomPeriodRangeActive(false)
9296
@period(period)
9397
@expanded(true)
9498

@@ -103,6 +107,10 @@ class DateRangePickerView
103107
@endDate(dateRange.endDate)
104108
@updateDateRange()
105109

110+
setCustomPeriodRange: (customPeriodRange) =>
111+
@isCustomPeriodRangeActive(true)
112+
@setDateRange(customPeriodRange)
113+
106114
applyChanges: () ->
107115
@close()
108116
@updateDateRange()

src/templates/daterangepicker.html

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
<div class="daterangepicker" data-bind="css: $data.cssClasses(), style: $data.style()">
22
<div class="controls">
3-
<ul class="periods" data-bind="foreach: $data.periods">
4-
<li class="period" data-bind="css: {active: $parent.isActivePeriod($data)}, text: $parent.periodProxy.title($data), click: function(){ $parent.setPeriod($data); }"></li>
3+
<ul class="periods">
4+
<!-- ko foreach: $data.periods -->
5+
<li class="period" data-bind="css: {active: $parent.isActivePeriod($data) && !$parent.isCustomPeriodRangeActive()}, text: $parent.periodProxy.title($data), click: function(){ $parent.setPeriod($data); }"></li>
6+
<!-- /ko -->
7+
<!-- ko foreach: $data.customPeriodRanges -->
8+
<li class="period" data-bind="css: {active: $parent.isActiveCustomPeriodRange($data)}, text: $data.title, click: function(){ $parent.setCustomPeriodRange($data); }"></li>
9+
<!-- /ko -->
510
</ul>
611
<ul class="ranges" data-bind="foreach: $data.ranges">
712
<li class="range" data-bind="css: {active: $parent.isActiveDateRange($data)}, text: $data.title, click: function(){ $parent.setDateRange($data); }"></li>

tasks/build.babel.js

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import browserSync from 'browser-sync';
66
const $ = gulpLoadPlugins();
77
const reload = browserSync.reload;
88

9-
109
function readJson(path) {
1110
return JSON.parse(fs.readFileSync(path, 'utf8'));
1211
}

test/config.coffee

+17
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,23 @@ describe 'Config', ->
104104
})
105105
, /Missing end date/)
106106

107+
describe 'customPeriodRanges', ->
108+
it 'works with a valid object', () ->
109+
new Config({
110+
customPeriodRanges: {
111+
"Test Range": ['2015-05-14', moment()]
112+
}
113+
})
114+
115+
it 'fails with a object value that is not an array', () ->
116+
assert.throw( ->
117+
new Config({
118+
ranges: {
119+
'Test Range': '2015-05-14'
120+
}
121+
})
122+
, /Value should be an array/)
123+
107124
describe '_dateObservable', ->
108125
describe '#fit()', ->
109126
describe 'minDate = 2015-05-14, period = month', ->

0 commit comments

Comments
 (0)