Skip to content

Commit

Permalink
improve selectDate method by adding time validation, add some timep…
Browse files Browse the repository at this point in the history
…icker tests
  • Loading branch information
t1m0n committed Mar 9, 2016
1 parent 8c4b945 commit 2711ba3
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 18 deletions.
38 changes: 30 additions & 8 deletions dist/js/datepicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ var Datepicker;
}
this.$datepicker.on('mousedown', this._onMouseDownDatepicker.bind(this));
this.$datepicker.on('mouseup', this._onMouseUpDatepicker.bind(this));
this.$el.on('clickCell.adp', this._onClickCell.bind(this));
}

if (this.opts.classes) {
Expand Down Expand Up @@ -416,12 +417,21 @@ var Datepicker;

this.lastSelectedDate = date;

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

// On this step timepicker will set valid values in it's instance
_this._trigger('selectDate', date);

//TODO стоит убрать в timepicker.js
// Set correct time values after timepicker's validation
// 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.setMinutes(this.timepicker.minutes);
date.setHours(this.timepicker.hours)
date.setMinutes(this.timepicker.minutes)
}

if (_this.view == 'days') {
Expand Down Expand Up @@ -1140,6 +1150,14 @@ var Datepicker;
}
},

_onClickCell: function (e, date) {
if (this.timepicker) {
date.setHours(this.timepicker.hours);
date.setMinutes(this.timepicker.minutes);
}
this.selectDate(date);
},

set focused(val) {
if (!val && this.focused) {
var $cell = this._getCell(this.focused);
Expand Down Expand Up @@ -1632,7 +1650,7 @@ var Datepicker;
alreadySelected = this.d._isSelected(selectedDate, this.d.cellType);

if (!alreadySelected) {
this.d.selectDate(selectedDate);
this.d._trigger('clickCell', selectedDate);
} else if (alreadySelected && this.opts.toggleSelected){
this.d.removeDate(selectedDate);
}
Expand Down Expand Up @@ -1862,7 +1880,7 @@ var Datepicker;
* are out of range sets valid values.
* @private
*/
_validateHoursMinutes: function () {
_validateHoursMinutes: function (date) {
if (this.hours < this.minHours) {
this.hours = this.minHours;
} else if (this.hours > this.maxHours) {
Expand Down Expand Up @@ -1946,7 +1964,12 @@ var Datepicker;
}
}

this._validateHoursMinutes();
this._validateHoursMinutes(date);
},

update: function () {
this._updateRanges();
this._updateCurrentTime();
},

/**
Expand Down Expand Up @@ -2004,8 +2027,7 @@ var Datepicker;

_onSelectDate: function (e, data) {
this._handleDate(data);
this._updateRanges();
this._updateCurrentTime();
this.update();
},

set hours (val) {
Expand Down
4 changes: 2 additions & 2 deletions dist/js/datepicker.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/js/body.js
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@
alreadySelected = this.d._isSelected(selectedDate, this.d.cellType);

if (!alreadySelected) {
this.d.selectDate(selectedDate);
this.d._trigger('clickCell', selectedDate);
} else if (alreadySelected && this.opts.toggleSelected){
this.d.removeDate(selectedDate);
}
Expand Down
24 changes: 21 additions & 3 deletions src/js/datepicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ var Datepicker;
}
this.$datepicker.on('mousedown', this._onMouseDownDatepicker.bind(this));
this.$datepicker.on('mouseup', this._onMouseUpDatepicker.bind(this));
this.$el.on('clickCell.adp', this._onClickCell.bind(this));
}

if (this.opts.classes) {
Expand Down Expand Up @@ -416,12 +417,21 @@ var Datepicker;

this.lastSelectedDate = date;

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

// On this step timepicker will set valid values in it's instance
_this._trigger('selectDate', date);

//TODO стоит убрать в timepicker.js
// Set correct time values after timepicker's validation
// 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.setMinutes(this.timepicker.minutes);
date.setHours(this.timepicker.hours)
date.setMinutes(this.timepicker.minutes)
}

if (_this.view == 'days') {
Expand Down Expand Up @@ -1140,6 +1150,14 @@ var Datepicker;
}
},

_onClickCell: function (e, date) {
if (this.timepicker) {
date.setHours(this.timepicker.hours);
date.setMinutes(this.timepicker.minutes);
}
this.selectDate(date);
},

set focused(val) {
if (!val && this.focused) {
var $cell = this._getCell(this.focused);
Expand Down
12 changes: 8 additions & 4 deletions src/js/timepicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
* are out of range sets valid values.
* @private
*/
_validateHoursMinutes: function () {
_validateHoursMinutes: function (date) {
if (this.hours < this.minHours) {
this.hours = this.minHours;
} else if (this.hours > this.maxHours) {
Expand Down Expand Up @@ -154,7 +154,12 @@
}
}

this._validateHoursMinutes();
this._validateHoursMinutes(date);
},

update: function () {
this._updateRanges();
this._updateCurrentTime();
},

/**
Expand Down Expand Up @@ -212,8 +217,7 @@

_onSelectDate: function (e, data) {
this._handleDate(data);
this._updateRanges();
this._updateCurrentTime();
this.update();
},

set hours (val) {
Expand Down
32 changes: 32 additions & 0 deletions tests/specs/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -823,6 +823,38 @@ describe('Options', function () {
var $time = $('.datepicker--time', dp.$datepicker);
expect($time).to.have.length(1)

})
});

describe('dateTimeSeparator', function () {
it('should define separator between date string and time', function () {
var date = new Date(2016,2,9,11,24);
dp = $input.datepicker({
timepicker: true,
onSelect: function (fd, d) {
expect(fd).to.be.equal('09.03.2016 time separator 11:24')
},
dateTimeSeparator: ' time separator '
}).data('datepicker');

dp.selectDate(date);

})
});

describe('timeFormat', function () {
it('should define time format', function () {
var date = new Date(2016,2,9,9,4);
dp = $input.datepicker({
timepicker: true,
timeFormat: 'h - ii',
onSelect: function (fd, d) {
expect(fd).to.be.equal('09.03.2016 9 - 04')
}
}).data('datepicker');

dp.selectDate(date);

})
})
});

0 comments on commit 2711ba3

Please sign in to comment.