Skip to content

Commit 9fc83a0

Browse files
committed
Calendar: Add change event
1 parent 6f9d266 commit 9fc83a0

File tree

6 files changed

+82
-1
lines changed

6 files changed

+82
-1
lines changed

tests/unit/calendar/common.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ common.testWidget( "calendar", {
3535
value: null,
3636

3737
// callbacks
38+
change: null,
3839
create: null,
3940
select: null
4041
}

tests/unit/calendar/events.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,42 @@ module( "calendar: events", {
1010
}
1111
} );
1212

13+
test( "change", function() {
14+
expect( 6 );
15+
16+
var shouldFire, eventType;
17+
18+
this.element.calendar( {
19+
change: function( event ) {
20+
ok( shouldFire, "change event fired" );
21+
equal(
22+
event.type,
23+
"calendarchange",
24+
"change event"
25+
);
26+
equal(
27+
event.originalEvent.type,
28+
eventType,
29+
"change originalEvent on calendar button " + eventType
30+
);
31+
}
32+
} );
33+
34+
shouldFire = true;
35+
eventType = "mousedown";
36+
this.element.find( "tbody button" ).last().simulate( eventType );
37+
38+
shouldFire = true;
39+
eventType = "keydown";
40+
testHelper.focusGrid( this.element )
41+
.simulate( eventType, { keyCode: $.ui.keyCode.HOME } )
42+
.simulate( eventType, { keyCode: $.ui.keyCode.ENTER } );
43+
44+
shouldFire = false;
45+
eventType = "mousedown";
46+
this.element.find( "tbody button" ).first().simulate( eventType );
47+
} );
48+
1349
asyncTest( "select", function() {
1450
expect( 6 );
1551

tests/unit/datepicker/common.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ common.testWidget( "datepicker", {
4242

4343
// callbacks
4444
beforeOpen: null,
45+
change: null,
4546
close: null,
4647
create: null,
4748
open: null,

tests/unit/datepicker/events.js

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,35 @@ test( "beforeOpen", function() {
4242
.datepicker( "open" );
4343
} );
4444

45+
test( "change", function() {
46+
expect( 4 );
47+
48+
var shouldFire;
49+
50+
this.element.datepicker( {
51+
change: function( event ) {
52+
ok( shouldFire, "change event fired" );
53+
equal(
54+
event.type,
55+
"datepickerchange",
56+
"change event"
57+
);
58+
}
59+
} );
60+
61+
shouldFire = true;
62+
this.element.datepicker( "open" );
63+
this.widget.find( "tbody button" ).eq( 1 ).simulate( "mousedown" );
64+
65+
shouldFire = false;
66+
this.element.datepicker( "open" );
67+
this.widget.find( "tbody button" ).eq( 1 ).simulate( "mousedown" );
68+
69+
shouldFire = true;
70+
this.element.datepicker( "open" );
71+
this.widget.find( "tbody button" ).eq( 2 ).simulate( "mousedown" );
72+
} );
73+
4574
test( "close", function() {
4675
expect( 4 );
4776

@@ -71,7 +100,7 @@ test( "close", function() {
71100
shouldFire = false;
72101
this.element.datepicker( "open" );
73102
shouldFire = true;
74-
this.element.datepicker( "widget" ).find( "tbody tr:first button:first" ).simulate( "mousedown" );
103+
this.widget.find( "tbody tr:first button:first" ).simulate( "mousedown" );
75104
} );
76105

77106
test( "open", function() {

ui/widgets/calendar.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ return $.widget( "ui.calendar", {
7272
value: null,
7373

7474
// callbacks
75+
change: null,
7576
select: null
7677
},
7778

@@ -125,6 +126,8 @@ return $.widget( "ui.calendar", {
125126
},
126127

127128
_select: function( event ) {
129+
var oldValue = this.options.value ? this.options.value.getTime() : "";
130+
128131
this._setOption( "value", new Date( $( event.currentTarget ).data( "timestamp" ) ) );
129132
this._updateDayElement( "ui-state-active" );
130133

@@ -133,6 +136,10 @@ return $.widget( "ui.calendar", {
133136
this.activeDescendant.closest( this.grid ).focus();
134137
event.preventDefault();
135138
}
139+
140+
if ( oldValue !== this.options.value.getTime() ) {
141+
this._trigger( "change", event );
142+
}
136143
},
137144

138145
_handleKeydown: function( event ) {

ui/widgets/datepicker.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ var widget = $.widget( "ui.datepicker", {
5151

5252
// callbacks
5353
beforeOpen: null,
54+
change: null,
5455
close: null,
5556
open: null,
5657
select: null
@@ -104,6 +105,9 @@ var widget = $.widget( "ui.datepicker", {
104105
this.calendarInstance = this.calendar
105106
.calendar( $.extend( {}, this.options, {
106107
value: this._parse( this.element.val() ),
108+
change: function( event ) {
109+
that._trigger( "change", event );
110+
},
107111
select: function( event ) {
108112
that.element.val( that.calendarInstance.value() );
109113
that.close();
@@ -172,6 +176,9 @@ var widget = $.widget( "ui.datepicker", {
172176
},
173177
blur: function() {
174178
this.suppressExpandOnFocus = false;
179+
},
180+
change: function( event ) {
181+
this._trigger( "change", event );
175182
}
176183
},
177184

0 commit comments

Comments
 (0)