Skip to content

Commit

Permalink
added swapping dates when range is true, fixes t1m0n#46
Browse files Browse the repository at this point in the history
  • Loading branch information
t1m0n committed May 16, 2016
1 parent f5c1ae4 commit 5aeb04c
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 2 deletions.
5 changes: 5 additions & 0 deletions dist/js/datepicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,11 @@
} else {
_this.minRange = date;
}
// Swap dates if they were selected via dp.selectDate() and second date was smaller then first
if (datepicker.bigger(_this.maxRange, _this.minRange)) {
_this.maxRange = _this.minRange;
_this.minRange = date;
}
_this.selectedDates = [_this.minRange, _this.maxRange]

} else {
Expand Down
4 changes: 2 additions & 2 deletions dist/js/datepicker.min.js

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions src/js/datepicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,11 @@
} else {
_this.minRange = date;
}
// Swap dates if they were selected via dp.selectDate() and second date was smaller then first
if (datepicker.bigger(_this.maxRange, _this.minRange)) {
_this.maxRange = _this.minRange;
_this.minRange = date;
}
_this.selectedDates = [_this.minRange, _this.maxRange]

} else {
Expand Down
1 change: 1 addition & 0 deletions tests/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<script type="text/javascript" src="specs/options.js"></script>
<script type="text/javascript" src="specs/events.js"></script>
<script type="text/javascript" src="specs/static-methods.js"></script>
<script type="text/javascript" src="specs/api.js"></script>
<style type="text/css">
/* Remove transitions to test position options*/
.datepicker {
Expand Down
65 changes: 65 additions & 0 deletions tests/specs/api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
describe('API', function () {

var assert = chai.assert,
expect = chai.expect,
destroy = true,
$altInput,
$input, dp;

before(function () {
$input = $('<input>').appendTo('#container');
$altInput = $('<input class="alt-field">').appendTo('#container');
});

afterEach(function () {
if (dp && destroy) {
dp.destroy();
}

destroy = true;
});

after(function () {
$input.remove();
$altInput.remove();
});

describe('selectDate', function () {
it('should select passed date', function () {
dp = $input.datepicker().data('datepicker');

var date = new Date();

dp.selectDate(date);

expect(dp.selectedDates).to.have.length(1);
expect(dp.selectedDates[0]).to.be.equal(date)
});
it('should select multiple dates if {multipleDates: true}', function () {
dp = $input.datepicker({
multipleDates: true
}).data('datepicker');

var date = new Date(2016, 4, 16),
date2 = new Date(2016, 4, 18);

dp.selectDate(date);
dp.selectDate(date2);

expect(dp.selectedDates).to.have.length(2);
});
it('should swap dates if {range: true} and second date is smaller then first', function () {
dp = $input.datepicker({
range: true
}).data('datepicker');

var date = new Date(2016, 4, 18),
date2 = new Date(2016, 4, 16);

dp.selectDate(date);
dp.selectDate(date2);

expect(dp.selectedDates[0]).to.be.equal(date2);
})
});
});

0 comments on commit 5aeb04c

Please sign in to comment.