Skip to content

Commit

Permalink
Merge pull request #11 from spongessuck/select-range
Browse files Browse the repository at this point in the history
Add 'selectRange' functionality.
  • Loading branch information
spongessuck committed Feb 26, 2015
2 parents 6ab9cb7 + abc0447 commit ba8b689
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 8 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ NOTE: Selected dates are stored as an array of time values, not date objects. An

selectedDates.push(new Date().setHours(0, 0, 0, 0));

##Date Ranges

You can change the selection mode from individual dates to a date range by setting the select-range attribute to a truthy value or expression (that isn't a string equal to "false"):

<datepicker ng-model='activeDate' multi-select='selectedDates' select-range='true'><datepicker>

After selecting a range, clicking a new date will reset the selection.

##Demo

<a href='http://plnkr.co/iVSdXt' target='_blank'>View demo on Plunker</a>

Screenshot:
Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gm.datepickerMultiSelect",
"version": "1.0.4",
"version": "1.0.5",
"homepage": "https://github.com/spongessuck/gm.datepickerMultiSelect",
"authors": [
"Greg McGee <[email protected]>"
Expand Down
4 changes: 2 additions & 2 deletions dist/gm.datepickerMultiSelect.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gm.datepickerMultiSelect",
"version": "1.0.4",
"version": "1.0.5",
"description": "AngularJS module to extend UI Bootstrap's Datepicker directive to allow for multiple date selections.",
"main": "dist/gm.datepickerMultiSelect.min.js",
"scripts": {
Expand Down
33 changes: 29 additions & 4 deletions src/gm.datepickerMultiSelect.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,40 @@ SOFTWARE.
scope.$broadcast('update', selectedDates);
});

attrs.$observe('selectRange', function(newVal) {
selectRange = !!newVal && newVal !== "false";
});

scope.$watch(attrs.ngModel, function(newVal, oldVal) {
if(!newVal) return;

var dateVal = newVal.getTime();

if(selectedDates.indexOf(dateVal) < 0) {

if(selectRange) {
/* reset range */
if(!selectedDates.length || selectedDates.length > 1)
return selectedDates.splice(0, selectedDates.length, dateVal);

selectedDates.push(dateVal);
} else {
selectedDates.splice(selectedDates.indexOf(dateVal), 1);

var tempVal = Math.min.apply(null, selectedDates);
var maxVal = Math.max.apply(null, selectedDates);

/* Start on the next day to prevent duplicating the
first date */
tempVal = new Date(tempVal).setHours(24);
while(tempVal < maxVal) {
selectedDates.push(tempVal);
/* Set a day ahead after pushing to prevent
duplicating last date */
tempVal = new Date(tempVal).setHours(24);
}
} else {
if(selectedDates.indexOf(dateVal) < 0) {
selectedDates.push(dateVal);
} else {
selectedDates.splice(selectedDates.indexOf(dateVal), 1);
}
}
});
}
Expand Down

0 comments on commit ba8b689

Please sign in to comment.