Skip to content

Commit

Permalink
add possibility to change time when clicking on selected date
Browse files Browse the repository at this point in the history
  • Loading branch information
t1m0n committed May 6, 2016
1 parent 5453ad8 commit f5c1ae4
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 30 deletions.
36 changes: 22 additions & 14 deletions dist/js/datepicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -434,8 +434,7 @@

// Set new time values from Date
if (this.timepicker) {
this.timepicker.hours = date.getHours();
this.timepicker.minutes = date.getMinutes();
this.timepicker._setTime(date);
}

// On this step timepicker will set valid values in it's instance
Expand All @@ -445,7 +444,7 @@
// Prevent from setting hours or minutes which values are lesser then `min` value or
// greater then `max` value
if (this.timepicker) {
date.setHours(this.timepicker.hours)
date.setHours(this.timepicker.hours);
date.setMinutes(this.timepicker.minutes)
}

Expand Down Expand Up @@ -626,9 +625,14 @@
},

_isSelected: function (checkDate, cellType) {
return this.selectedDates.some(function (date) {
return datepicker.isSame(date, checkDate, cellType)
})
var res = false;
this.selectedDates.some(function (date) {
if (datepicker.isSame(date, checkDate, cellType)) {
res = date;
return true;
}
});
return res;
},

_setInputValue: function () {
Expand Down Expand Up @@ -1691,6 +1695,12 @@
this.d._trigger('clickCell', selectedDate);
} else if (alreadySelected && this.opts.toggleSelected){
this.d.removeDate(selectedDate);
} else if (alreadySelected && !this.opts.toggleSelected) {
this.d.lastSelectedDate = alreadySelected;
if (this.d.opts.timepicker) {
this.d.timepicker._setTime(alreadySelected);
this.d.timepicker.update();
}
}

},
Expand Down Expand Up @@ -1877,7 +1887,7 @@
datepicker.Timepicker.prototype = {
init: function () {
var input = 'input';
this._setInitialTime(this.d.date);
this._setTime(this.d.date);
this._buildHTML();

if (navigator.userAgent.match(/trident/gi)) {
Expand All @@ -1891,7 +1901,7 @@
this.$ranges.on('mouseout blur', this._onMouseOutRange.bind(this));
},

_setInitialTime: function (date, parse) {
_setTime: function (date) {
var _date = dp.getParsedDate(date);

this._handleDate(date);
Expand Down Expand Up @@ -1984,15 +1994,13 @@
_updateRanges: function () {
this.$hours.attr({
min: this.minHours,
max: this.maxHours,
value: this.hours
});
max: this.maxHours
}).val(this.hours);

this.$minutes.attr({
min: this.minMinutes,
max: this.maxMinutes,
value: this.minutes
});
max: this.maxMinutes
}).val(this.minutes)
},

/**
Expand Down
4 changes: 2 additions & 2 deletions dist/js/datepicker.min.js

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions src/js/body.js
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,12 @@
this.d._trigger('clickCell', selectedDate);
} else if (alreadySelected && this.opts.toggleSelected){
this.d.removeDate(selectedDate);
} else if (alreadySelected && !this.opts.toggleSelected) {
this.d.lastSelectedDate = alreadySelected;
if (this.d.opts.timepicker) {
this.d.timepicker._setTime(alreadySelected);
this.d.timepicker.update();
}
}

},
Expand Down
16 changes: 10 additions & 6 deletions src/js/datepicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -434,8 +434,7 @@

// Set new time values from Date
if (this.timepicker) {
this.timepicker.hours = date.getHours();
this.timepicker.minutes = date.getMinutes();
this.timepicker._setTime(date);
}

// On this step timepicker will set valid values in it's instance
Expand All @@ -445,7 +444,7 @@
// Prevent from setting hours or minutes which values are lesser then `min` value or
// greater then `max` value
if (this.timepicker) {
date.setHours(this.timepicker.hours)
date.setHours(this.timepicker.hours);
date.setMinutes(this.timepicker.minutes)
}

Expand Down Expand Up @@ -626,9 +625,14 @@
},

_isSelected: function (checkDate, cellType) {
return this.selectedDates.some(function (date) {
return datepicker.isSame(date, checkDate, cellType)
})
var res = false;
this.selectedDates.some(function (date) {
if (datepicker.isSame(date, checkDate, cellType)) {
res = date;
return true;
}
});
return res;
},

_setInputValue: function () {
Expand Down
14 changes: 6 additions & 8 deletions src/js/timepicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
datepicker.Timepicker.prototype = {
init: function () {
var input = 'input';
this._setInitialTime(this.d.date);
this._setTime(this.d.date);
this._buildHTML();

if (navigator.userAgent.match(/trident/gi)) {
Expand All @@ -41,7 +41,7 @@
this.$ranges.on('mouseout blur', this._onMouseOutRange.bind(this));
},

_setInitialTime: function (date, parse) {
_setTime: function (date) {
var _date = dp.getParsedDate(date);

this._handleDate(date);
Expand Down Expand Up @@ -134,15 +134,13 @@
_updateRanges: function () {
this.$hours.attr({
min: this.minHours,
max: this.maxHours,
value: this.hours
});
max: this.maxHours
}).val(this.hours);

this.$minutes.attr({
min: this.minMinutes,
max: this.maxMinutes,
value: this.minutes
});
max: this.maxMinutes
}).val(this.minutes)
},

/**
Expand Down
14 changes: 14 additions & 0 deletions visual-tests.html
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,20 @@ <h1>Timepicker</h1>
<h2>{timepicker: true}</h2>
<div class="datepicker-here" data-timepicker="true"></div>
</div>
<div class="vt-tile">
<h2>{onChange: ..., toggleSelected: false, range: true}</h2>
<input id="dp-5" class="datepicker-here" data-timepicker="true" />
<script type="text/javascript">
$('#dp-5').datepicker({
toggleSelected: false,
inline: true,
range: true,
onSelect: function (fd) {
log(fd)
}
})
</script>
</div>
<div class="vt-tile">
<h2>{timepicker: true}</h2>
<input class="datepicker-here" data-timepicker="true" />
Expand Down

0 comments on commit f5c1ae4

Please sign in to comment.