Skip to content

Commit 96086aa

Browse files
committed
Datepicker: Code clean up for events
1 parent 29e0098 commit 96086aa

File tree

1 file changed

+102
-102
lines changed

1 file changed

+102
-102
lines changed

ui/datepicker.js

Lines changed: 102 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ $.widget( "ui.datepicker", {
5858

5959
_create: function() {
6060
this._createCalendar();
61+
62+
this._on( this._inputEvents );
63+
this._on( this.calendar, this._calendarEvents );
64+
this._on( this.document, this._documentEvents );
6165
},
6266

6367
_getCreateOptions: function() {
@@ -104,120 +108,116 @@ $.widget( "ui.datepicker", {
104108
this.element
105109
.attr( "aria-haspopup", "true" )
106110
.attr( "aria-owns", this.calendar.attr( "id" ) );
111+
},
107112

108-
this._on({
109-
keydown: function( event ) {
110-
switch ( event.keyCode ) {
111-
case $.ui.keyCode.TAB:
112-
113-
// Waiting for close() will make popup hide too late, which breaks tab key behavior
114-
this.calendar.hide();
113+
_inputEvents: {
114+
keydown: function( event ) {
115+
switch ( event.keyCode ) {
116+
case $.ui.keyCode.TAB:
117+
// Waiting for close() will make popup hide too late, which breaks tab key behavior
118+
this.calendar.hide();
119+
this.close( event );
120+
break;
121+
case $.ui.keyCode.ESCAPE:
122+
if ( this.isOpen ) {
115123
this.close( event );
116-
break;
117-
case $.ui.keyCode.ESCAPE:
124+
}
125+
break;
126+
case $.ui.keyCode.ENTER:
127+
this.calendarInstance._handleKeydown( event );
128+
break;
129+
case $.ui.keyCode.DOWN:
130+
case $.ui.keyCode.UP:
131+
clearTimeout( this.closeTimer );
132+
this._delay( function() {
133+
this.open( event );
134+
this.calendarInstance.grid.focus( 1 );
135+
}, 1 );
136+
break;
137+
case $.ui.keyCode.HOME:
138+
if ( event.ctrlKey ) {
139+
this.valueAsDate( new Date() );
140+
event.preventDefault();
118141
if ( this.isOpen ) {
119-
this.close( event );
120-
}
121-
break;
122-
case $.ui.keyCode.ENTER:
123-
this.calendarInstance._handleKeydown( event );
124-
break;
125-
case $.ui.keyCode.DOWN:
126-
case $.ui.keyCode.UP:
127-
clearTimeout( this.closeTimer );
128-
this._delay( function() {
129-
this.open( event );
130-
this.calendarInstance.grid.focus( 1 );
131-
}, 1 );
132-
break;
133-
case $.ui.keyCode.HOME:
134-
if ( event.ctrlKey ) {
135-
this.valueAsDate( new Date() );
136-
event.preventDefault();
137-
if ( this.isOpen ) {
138142
this.calendarInstance.refresh();
139-
} else {
140-
this.open( event );
141-
}
142-
}
143-
break;
144-
145-
// TODO This is not in specs, keep?
146-
case $.ui.keyCode.END:
147-
if ( event.ctrlKey ) {
148-
this.element.val( "" );
149-
event.preventDefault();
150-
if ( this.isOpen ) {
151-
this.close( event );
152-
}
153-
}
154-
break;
155-
}
156-
},
157-
keyup: function() {
158-
if ( this.isValid() ) {
159-
this.valueAsDate( this._getParsedValue() );
160-
}
161-
},
162-
mousedown: function( event ) {
163-
if ( this.isOpen ) {
164-
suppressExpandOnFocus = true;
165-
this.close();
166-
return;
167-
}
168-
this.open( event );
169-
clearTimeout( this.closeTimer );
170-
},
171-
focus: function( event ) {
172-
if ( !suppressExpandOnFocus ) {
173-
this._delay( function() {
174-
if ( !this.isOpen ) {
143+
} else {
175144
this.open( event );
176145
}
177-
}, 1);
178-
}
146+
}
147+
break;
148+
// TODO This is not in specs, keep?
149+
case $.ui.keyCode.END:
150+
if ( event.ctrlKey ) {
151+
this.element.val( "" );
152+
event.preventDefault();
153+
if ( this.isOpen ) {
154+
this.close( event );
155+
}
156+
}
157+
break;
158+
}
159+
},
160+
keyup: function() {
161+
if ( this.isValid() ) {
162+
this.valueAsDate( this._getParsedValue() );
163+
}
164+
},
165+
mousedown: function( event ) {
166+
if ( this.isOpen ) {
167+
suppressExpandOnFocus = true;
168+
this.close();
169+
return;
170+
}
171+
this.open( event );
172+
clearTimeout( this.closeTimer );
173+
},
174+
focus: function( event ) {
175+
if ( !suppressExpandOnFocus ) {
179176
this._delay( function() {
180-
suppressExpandOnFocus = false;
181-
}, 100 );
182-
},
183-
blur: function() {
184-
suppressExpandOnFocus = false;
177+
if ( !this.isOpen ) {
178+
this.open( event );
179+
}
180+
}, 1);
185181
}
186-
});
187-
188-
this._on( this.calendar, {
189-
focusout: function( event ) {
182+
this._delay( function() {
183+
suppressExpandOnFocus = false;
184+
}, 100 );
185+
},
186+
blur: function() {
187+
suppressExpandOnFocus = false;
188+
}
189+
},
190190

191-
// Use a timer to allow click to clear it and letting that
192-
// handle the closing instead of opening again
193-
// also allows tabbing inside the calendar without it closing
194-
this.closeTimer = this._delay( function() {
195-
this.close( event );
196-
}, 150 );
197-
},
198-
focusin: function() {
199-
clearTimeout( this.closeTimer );
200-
},
201-
mouseup: function() {
202-
clearTimeout( this.closeTimer );
203-
},
204-
205-
// TODO On TAB (or shift TAB), make sure it ends up on something useful in DOM order
206-
keyup: function( event ) {
207-
if ( event.keyCode === $.ui.keyCode.ESCAPE && this.calendar.is( ":visible" ) ) {
208-
this.close( event );
209-
this._focusTrigger();
210-
}
191+
_calendarEvents: {
192+
focusout: function( event ) {
193+
// use a timer to allow click to clear it and letting that
194+
// handle the closing instead of opening again
195+
// also allows tabbing inside the calendar without it closing
196+
this.closeTimer = this._delay( function() {
197+
this.close( event );
198+
}, 150 );
199+
},
200+
focusin: function() {
201+
clearTimeout( this.closeTimer );
202+
},
203+
mouseup: function() {
204+
clearTimeout( this.closeTimer );
205+
},
206+
// TODO on TAB (or shift TAB), make sure it ends up on something useful in DOM order
207+
keyup: function( event ) {
208+
if ( event.keyCode === $.ui.keyCode.ESCAPE && this.calendar.is( ":visible" ) ) {
209+
this.close( event );
210+
this._focusTrigger();
211211
}
212-
});
212+
}
213+
},
213214

214-
this._on( this.document, {
215-
click: function( event ) {
216-
if ( this.isOpen && !$( event.target ).closest( this.element.add( this.calendar ) ).length ) {
217-
this.close( event );
218-
}
215+
_documentEvents: {
216+
click: function( event ) {
217+
if ( this.isOpen && !$( event.target ).closest( this.element.add( this.calendar ) ).length ) {
218+
this.close( event );
219219
}
220-
});
220+
}
221221
},
222222

223223
_appendTo: function() {

0 commit comments

Comments
 (0)