Skip to content
This repository was archived by the owner on Dec 26, 2019. It is now read-only.

Commit b810a20

Browse files
committed
Merge pull request #71 from octronic/support-clearDate-hide-and-show-events
Support clearDate, hide and show events
2 parents 0334d96 + e013322 commit b810a20

File tree

3 files changed

+128
-57
lines changed

3 files changed

+128
-57
lines changed

README.md

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,13 +217,32 @@ The changeDate action is triggered when the selected date changes. It can be spe
217217
The action can be handled by a parent component, controller or route:
218218

219219
```javascript
220-
actions {
220+
actions: {
221221
changeDateAction(date) {
222222
// do sth with the new date
223223
}
224224
}
225225
```
226226

227+
#### clearDate
228+
229+
The clearDate action is triggered when the date is cleared (e.g. when the "clear" button is clicked).
230+
231+
232+
```handlebars
233+
{{bootstrap-datepicker clearDate="clearDateAction"}}
234+
```
235+
236+
The action can be handled by a parent component, controller or route:
237+
238+
```javascript
239+
actions: {
240+
clearDateAction() {
241+
// do sth
242+
}
243+
}
244+
```
245+
227246
#### focus-in & focus-out
228247

229248
The focus-in and focus-out actions are triggered when the respective focus events occur on the input field.
@@ -235,7 +254,7 @@ The focus-in and focus-out actions are triggered when the respective focus event
235254
The actions can be handled by a parent component, controller or route:
236255

237256
```javascript
238-
actions {
257+
actions: {
239258
focusInAction(component, event) {
240259
// handle event
241260
},
@@ -245,6 +264,26 @@ actions {
245264
}
246265
```
247266

267+
#### hide & show
268+
269+
The hide and show actions are triggered when the datepicker is either hidden or displayed.
270+
271+
```handlebars
272+
{{bootstrap-datepicker hide="hideAction" show="showAction"}}
273+
```
274+
275+
The actions can be handled by a parent component, controller or route:
276+
277+
```javascript
278+
actions: {
279+
hideAction() {
280+
// datepicker is hidden
281+
},
282+
showAction() {
283+
// datepicker is shown
284+
}
285+
}
286+
```
248287

249288

250289
## Contributing

addon/components/datepicker-support.js

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ export default Ember.Mixin.create({
1111
endDate: undefined,
1212

1313
setupBootstrapDatepicker: Ember.on('didInsertElement', function() {
14-
var self = this;
1514

1615
this.$().
1716
datepicker({
@@ -43,21 +42,27 @@ export default Ember.Mixin.create({
4342
weekStart: this.get('weekStart'),
4443
datesDisabled: this.get('datesDisabled')
4544
}).
46-
on('changeDate', function(event) {
47-
Ember.run(function() {
48-
self._didChangeDate(event);
45+
on('changeDate', event => {
46+
Ember.run(() => {
47+
this._didChangeDate(event);
4948
});
5049
}).
51-
on('input', function() {
52-
if (!self.$().val()) {
53-
self._applyDateValue(null);
54-
}
50+
on('focusout', event => {
51+
this.sendAction('focus-out', this, event);
5552
}).
56-
on('focusout', function(event) {
57-
self.sendAction('focus-out', self, event);
53+
on('focusin', event => {
54+
this.sendAction('focus-in', this, event);
5855
}).
59-
on('focusin', function(event) {
60-
self.sendAction('focus-in', self, event);
56+
on('clearDate', event => {
57+
Ember.run(() => {
58+
this._didChangeDate(event);
59+
});
60+
}).
61+
on('show', () => {
62+
this.sendAction('show');
63+
}).
64+
on('hide', () => {
65+
this.sendAction('hide');
6166
});
6267

6368
this._updateDatepicker();
@@ -83,12 +88,12 @@ export default Ember.Mixin.create({
8388
}
8489

8590
this.set('mustUpdateInput', false);
86-
this._applyDateValue(value);
87-
},
88-
89-
_applyDateValue: function(value) {
9091
this.set('value', value);
91-
this.sendAction('changeDate', value);
92+
if (event.type === 'clearDate') {
93+
this.sendAction('clearDate');
94+
} else {
95+
this.sendAction('changeDate', value);
96+
}
9297
},
9398

9499
_addObservers: Ember.on('didInsertElement', function() {
@@ -127,8 +132,7 @@ export default Ember.Mixin.create({
127132
}),
128133

129134
_updateDatepicker: function() {
130-
var self = this,
131-
element = this.$(),
135+
var element = this.$(),
132136
value = this.get('value'),
133137
dates = [];
134138

@@ -147,8 +151,8 @@ export default Ember.Mixin.create({
147151
default:
148152
dates = [null];
149153
}
150-
dates = dates.map(function(date) {
151-
return (Ember.isNone(date)) ? null : self._getDateCloneWithNoTime(date);
154+
dates = dates.map(date => {
155+
return (Ember.isNone(date)) ? null : this._getDateCloneWithNoTime(date);
152156
});
153157

154158
element.datepicker
Lines changed: 62 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,48 @@
1-
import Ember from 'ember';
21
import hbs from 'htmlbars-inline-precompile';
32
import { moduleForComponent, test } from 'ember-qunit';
43

54
moduleForComponent('bootstrap-datepicker', 'BootstrapDatepickerComponent', {
65
integration: true
76
});
87

9-
test('resets date when input is cleared', function (assert) {
10-
assert.expect(2);
8+
test('triggers specified action on focusout event', function (assert) {
9+
assert.expect(1);
1110

12-
this.set('myDate', new Date());
11+
this.render(hbs`
12+
{{bootstrap-datepicker focus-out="focusOutAction"}}
13+
`);
14+
15+
var actionIsTriggered = false;
16+
this.on('focusOutAction', () => {
17+
actionIsTriggered = true;
18+
});
19+
20+
this.$('input.ember-text-field').trigger('focusout');
21+
22+
assert.ok(actionIsTriggered, 'action is triggered on focusout');
23+
});
24+
25+
test('triggers specified action on focusin event', function (assert) {
26+
assert.expect(1);
1327

1428
this.render(hbs`
15-
{{bootstrap-datepicker value=myDate}}
29+
{{bootstrap-datepicker focus-in="focusInAction"}}
1630
`);
17-
18-
var datepicker = this.$('input.ember-text-field').datepicker();
19-
20-
datepicker.val('');
21-
Ember.run(() => {
22-
datepicker.trigger('input');
31+
32+
var actionIsTriggered = false;
33+
this.on('focusInAction', () => {
34+
actionIsTriggered = true;
2335
});
2436

25-
assert.equal(this.get('myDate'), null, 'value is reset');
26-
assert.equal(this.$('input.ember-text-field').datepicker('getDate'), null, 'datepicker is updated');
37+
this.$('input.ember-text-field').trigger('focusin');
38+
39+
assert.ok(actionIsTriggered, 'action is triggered on focusin');
2740
});
2841

29-
test('triggers changeDate action when input field is cleared', function (assert) {
42+
test('triggers changeDate action when date selection changes', function(assert) {
3043
assert.expect(1);
3144

32-
this.set('myDate', new Date());
45+
this.set('myDate', null);
3346

3447
var actionIsTriggered = false;
3548
this.on('myAction', () => {
@@ -40,47 +53,62 @@ test('triggers changeDate action when input field is cleared', function (assert)
4053
{{bootstrap-datepicker value=myDate changeDate="myAction"}}
4154
`);
4255

43-
var datepicker = this.$('input.ember-text-field').datepicker();
44-
45-
datepicker.val('');
46-
Ember.run(() => {
47-
datepicker.trigger('input');
48-
});
56+
var input = this.$('input.ember-text-field');
57+
input.datepicker('setDate', new Date());
4958

5059
assert.ok(actionIsTriggered, 'action is triggered');
5160
});
5261

53-
test('triggers specified action on focusout event', function (assert) {
62+
test('triggers clearDate action when date selection is cleared', function(assert) {
5463
assert.expect(1);
5564

56-
this.render(hbs`
57-
{{bootstrap-datepicker focus-out="focusOutAction"}}
58-
`);
65+
this.set('myDate', new Date());
5966

6067
var actionIsTriggered = false;
61-
this.on('focusOutAction', () => {
68+
this.on('myAction', () => {
6269
actionIsTriggered = true;
6370
});
6471

65-
this.$('input.ember-text-field').trigger('focusout');
72+
this.render(hbs`
73+
{{bootstrap-datepicker value=myDate clearDate="myAction"}}
74+
`);
6675

67-
assert.ok(actionIsTriggered, 'action is triggered on focusout');
68-
});
76+
var input = this.$('input.ember-text-field');
77+
input.datepicker('setDate', null);
6978

79+
assert.ok(actionIsTriggered, 'action is triggered');
80+
});
7081

71-
test('triggers specified action on focusin event', function (assert) {
82+
test('triggers show action when date datepicker is displayed', function(assert) {
7283
assert.expect(1);
7384

85+
var actionIsTriggered = false;
86+
this.on('myAction', () => {
87+
actionIsTriggered = true;
88+
});
89+
7490
this.render(hbs`
75-
{{bootstrap-datepicker focus-in="focusInAction"}}
91+
{{bootstrap-datepicker show="myAction"}}
7692
`);
7793

94+
this.$('input.ember-text-field').trigger('show');
95+
96+
assert.ok(actionIsTriggered, 'action is triggered');
97+
});
98+
99+
test('triggers hide action when date datepicker is hidden', function(assert) {
100+
assert.expect(1);
101+
78102
var actionIsTriggered = false;
79-
this.on('focusInAction', () => {
103+
this.on('myAction', () => {
80104
actionIsTriggered = true;
81105
});
82106

83-
this.$('input.ember-text-field').trigger('focusin');
107+
this.render(hbs`
108+
{{bootstrap-datepicker hide="myAction"}}
109+
`);
84110

85-
assert.ok(actionIsTriggered, 'action is triggered on focusin');
111+
this.$('input.ember-text-field').trigger('hide');
112+
113+
assert.ok(actionIsTriggered, 'action is triggered');
86114
});

0 commit comments

Comments
 (0)