Skip to content

Commit

Permalink
changed handling of selection of already selected dates, closes t1m0n…
Browse files Browse the repository at this point in the history
…#105

Now for selecting single date in range mode, `toggleSelected` must be `false`
  • Loading branch information
t1m0n committed Aug 28, 2016
1 parent e8facd2 commit 701df87
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 42 deletions.
48 changes: 28 additions & 20 deletions dist/js/datepicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -1083,6 +1083,30 @@
}
},

_handleAlreadySelectedDates: function (alreadySelected, selectedDate) {
if (this.opts.range) {
if (!this.opts.toggleSelected) {
// Add possibility to select same date when range is true
if (this.selectedDates.length != 2) {
this._trigger('clickCell', selectedDate);
}
} else {
this.removeDate(selectedDate);
}
} else if (this.opts.toggleSelected){
this.removeDate(selectedDate);
}

// Change last selected date to be able to change time when clicking on this cell
if (!this.opts.toggleSelected) {
this.lastSelectedDate = alreadySelected;
if (this.opts.timepicker) {
this.timepicker._setTime(alreadySelected);
this.timepicker.update();
}
}
},

_onShowEvent: function (e) {
if (!this.visible) {
this.show();
Expand Down Expand Up @@ -1157,9 +1181,9 @@
this.focused.setMinutes(this.timepicker.minutes);
}
this.selectDate(this.focused);
} else if (alreadySelected && this.opts.toggleSelected){
this.removeDate(this.focused);
return;
}
this._handleAlreadySelectedDates(alreadySelected, this.focused)
}
}
}
Expand Down Expand Up @@ -1755,27 +1779,11 @@

if (!alreadySelected) {
dp._trigger('clickCell', selectedDate);
return;
}

if (alreadySelected && this.opts.range) {
// Add possibility to select same date when range is true
if (dp.selectedDates.length != 2 && !this.opts.toggleSelected || this.opts.toggleSelected) {
dp._trigger('clickCell', selectedDate);
// Change last selected date to be able to change time on last date
dp.lastSelectedDate = alreadySelected;
}
} else if (alreadySelected && this.opts.toggleSelected){
dp.removeDate(selectedDate);
}
dp._handleAlreadySelectedDates.bind(dp, alreadySelected, selectedDate)();

// Change last selected date to be able to change time when clicking on this cell
if (alreadySelected && !this.opts.toggleSelected) {
dp.lastSelectedDate = alreadySelected;
if (dp.opts.timepicker) {
dp.timepicker._setTime(alreadySelected);
dp.timepicker.update();
}
}
},

_onClickCell: function (e) {
Expand Down
4 changes: 2 additions & 2 deletions dist/js/datepicker.min.js

Large diffs are not rendered by default.

20 changes: 2 additions & 18 deletions src/js/body.js
Original file line number Diff line number Diff line change
Expand Up @@ -293,27 +293,11 @@

if (!alreadySelected) {
dp._trigger('clickCell', selectedDate);
return;
}

if (alreadySelected && this.opts.range) {
// Add possibility to select same date when range is true
if (dp.selectedDates.length != 2 && !this.opts.toggleSelected || this.opts.toggleSelected) {
dp._trigger('clickCell', selectedDate);
// Change last selected date to be able to change time on last date
dp.lastSelectedDate = alreadySelected;
}
} else if (alreadySelected && this.opts.toggleSelected){
dp.removeDate(selectedDate);
}
dp._handleAlreadySelectedDates.bind(dp, alreadySelected, selectedDate)();

// Change last selected date to be able to change time when clicking on this cell
if (alreadySelected && !this.opts.toggleSelected) {
dp.lastSelectedDate = alreadySelected;
if (dp.opts.timepicker) {
dp.timepicker._setTime(alreadySelected);
dp.timepicker.update();
}
}
},

_onClickCell: function (e) {
Expand Down
28 changes: 26 additions & 2 deletions src/js/datepicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -1083,6 +1083,30 @@
}
},

_handleAlreadySelectedDates: function (alreadySelected, selectedDate) {
if (this.opts.range) {
if (!this.opts.toggleSelected) {
// Add possibility to select same date when range is true
if (this.selectedDates.length != 2) {
this._trigger('clickCell', selectedDate);
}
} else {
this.removeDate(selectedDate);
}
} else if (this.opts.toggleSelected){
this.removeDate(selectedDate);
}

// Change last selected date to be able to change time when clicking on this cell
if (!this.opts.toggleSelected) {
this.lastSelectedDate = alreadySelected;
if (this.opts.timepicker) {
this.timepicker._setTime(alreadySelected);
this.timepicker.update();
}
}
},

_onShowEvent: function (e) {
if (!this.visible) {
this.show();
Expand Down Expand Up @@ -1157,9 +1181,9 @@
this.focused.setMinutes(this.timepicker.minutes);
}
this.selectDate(this.focused);
} else if (alreadySelected && this.opts.toggleSelected){
this.removeDate(this.focused);
return;
}
this._handleAlreadySelectedDates(alreadySelected, this.focused)
}
}
}
Expand Down

0 comments on commit 701df87

Please sign in to comment.