added onShow & onHide events, resolves #95

This commit is contained in:
t1m0n 2016-08-11 14:01:18 +03:00
parent 42f6d4c76d
commit a71611bde3
6 changed files with 77 additions and 4 deletions

20
dist/js/datepicker.js vendored
View File

@ -75,6 +75,8 @@
// events
onSelect: '',
onShow: '',
onHide: '',
onChangeMonth: '',
onChangeYear: '',
onChangeDecade: '',
@ -785,12 +787,20 @@
},
show: function () {
var onShow = this.opts.onShow;
this.setPosition(this.opts.position);
this.$datepicker.addClass('active');
this.visible = true;
if (onShow) {
this._bindVisionEvents(onShow)
}
},
hide: function () {
var onHide = this.opts.onHide;
this.$datepicker
.removeClass('active')
.css({
@ -803,6 +813,10 @@
this.inFocus = false;
this.visible = false;
this.$el.blur();
if (onHide) {
this._bindVisionEvents(onHide)
}
},
down: function (date) {
@ -813,6 +827,12 @@
this._changeView(date, 'up');
},
_bindVisionEvents: function (event) {
this.$datepicker.off('transitionend.dp');
event(this, false);
this.$datepicker.one('transitionend.dp', event.bind(this, this, true))
},
_changeView: function (date, dir) {
date = date || this.focused || this.date;

File diff suppressed because one or more lines are too long

View File

@ -75,6 +75,8 @@
// events
onSelect: '',
onShow: '',
onHide: '',
onChangeMonth: '',
onChangeYear: '',
onChangeDecade: '',
@ -785,12 +787,20 @@
},
show: function () {
var onShow = this.opts.onShow;
this.setPosition(this.opts.position);
this.$datepicker.addClass('active');
this.visible = true;
if (onShow) {
this._bindVisionEvents(onShow)
}
},
hide: function () {
var onHide = this.opts.onHide;
this.$datepicker
.removeClass('active')
.css({
@ -803,6 +813,10 @@
this.inFocus = false;
this.visible = false;
this.$el.blur();
if (onHide) {
this._bindVisionEvents(onHide)
}
},
down: function (date) {
@ -813,6 +827,12 @@
this._changeView(date, 'up');
},
_bindVisionEvents: function (event) {
this.$datepicker.off('transitionend.dp');
event(this, false);
this.$datepicker.one('transitionend.dp', event.bind(this, this, true))
},
_changeView: function (date, dir) {
date = date || this.focused || this.date;

View File

@ -23,7 +23,7 @@
<style type="text/css">
/* Remove transitions to test position options*/
.datepicker {
transition: none !important;
transition-duration: 0s !important;
}
</style>
</head>

View File

@ -15,6 +15,7 @@ var assert = chai.assert,
afterEach(function () {
if (dp && destroy) {
dp.destroy();
dp = '';
}
destroy = true;
@ -78,6 +79,39 @@ var assert = chai.assert,
expect(dates).to.have.length(2)
})
});
describe('onShow', function () {
it('should add callback when datepicker is showing', function () {
var test = '';
dp = $input.datepicker({
onShow: function (dp, completed) {
if (!completed) {
test = dp;
}
}
}).data('datepicker');
dp.show();
expect(test).to.be.equal(dp);
})
});
describe('onHide', function () {
it('should add callback when datepicker is hiding (after transition completed)', function () {
var test = '';
dp = $input.datepicker({
onHide: function (dp, completed) {
if (!completed) {
test = dp;
}
}
}).data('datepicker');
dp.show();
dp.hide();
expect(test).to.be.equal(dp);
});
});
describe('onRenderCell', function () {
it('should add callback when cell is rendered', function () {

View File

@ -5,7 +5,6 @@ var assert = chai.assert,
describe('Datepicker', function () {
describe('getDaysCount', function () {
it('should return 31 days in December', function () {
console.log(plugin.getDaysCount);
assert.equal(plugin.getDaysCount(new Date(2015, 11)), 31)
});
it('should return 30 days in September', function () {