Skip to content

Commit

Permalink
added onShow & onHide events, resolves t1m0n#95
Browse files Browse the repository at this point in the history
  • Loading branch information
t1m0n committed Aug 11, 2016
1 parent 42f6d4c commit a71611b
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 4 deletions.
20 changes: 20 additions & 0 deletions dist/js/datepicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@

// events
onSelect: '',
onShow: '',
onHide: '',
onChangeMonth: '',
onChangeYear: '',
onChangeDecade: '',
Expand Down Expand Up @@ -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({
Expand All @@ -803,6 +813,10 @@
this.inFocus = false;
this.visible = false;
this.$el.blur();

if (onHide) {
this._bindVisionEvents(onHide)
}
},

down: function (date) {
Expand All @@ -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;

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

Large diffs are not rendered by default.

20 changes: 20 additions & 0 deletions src/js/datepicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@

// events
onSelect: '',
onShow: '',
onHide: '',
onChangeMonth: '',
onChangeYear: '',
onChangeDecade: '',
Expand Down Expand Up @@ -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({
Expand All @@ -803,6 +813,10 @@
this.inFocus = false;
this.visible = false;
this.$el.blur();

if (onHide) {
this._bindVisionEvents(onHide)
}
},

down: function (date) {
Expand All @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion tests/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<style type="text/css">
/* Remove transitions to test position options*/
.datepicker {
transition: none !important;
transition-duration: 0s !important;
}
</style>
</head>
Expand Down
34 changes: 34 additions & 0 deletions tests/specs/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ var assert = chai.assert,
afterEach(function () {
if (dp && destroy) {
dp.destroy();
dp = '';
}

destroy = true;
Expand Down Expand Up @@ -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 () {
Expand Down
1 change: 0 additions & 1 deletion tests/specs/static-methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down

0 comments on commit a71611b

Please sign in to comment.