improve selectDate method by adding time validation, add some timepicker tests

This commit is contained in:
t1m0n 2016-03-09 12:39:52 +03:00
parent 8c4b94547a
commit 2711ba3c04
6 changed files with 94 additions and 18 deletions

38
dist/js/datepicker.js vendored
View File

@ -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) {
@ -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') {
@ -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);
@ -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);
}
@ -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) {
@ -1946,7 +1964,12 @@ var Datepicker;
}
}
this._validateHoursMinutes();
this._validateHoursMinutes(date);
},
update: function () {
this._updateRanges();
this._updateCurrentTime();
},
/**
@ -2004,8 +2027,7 @@ var Datepicker;
_onSelectDate: function (e, data) {
this._handleDate(data);
this._updateRanges();
this._updateCurrentTime();
this.update();
},
set hours (val) {

File diff suppressed because one or more lines are too long

View File

@ -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);
}

View File

@ -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) {
@ -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') {
@ -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);

View File

@ -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) {
@ -154,7 +154,12 @@
}
}
this._validateHoursMinutes();
this._validateHoursMinutes(date);
},
update: function () {
this._updateRanges();
this._updateCurrentTime();
},
/**
@ -212,8 +217,7 @@
_onSelectDate: function (e, data) {
this._handleDate(data);
this._updateRanges();
this._updateCurrentTime();
this.update();
},
set hours (val) {

View File

@ -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);
})
})
});