Skip to content

Commit 74d195e

Browse files
adambaratzscottgonzalez
authored andcommitted
Datepicker: bind hover events using delegate. Fixed #7256 - minimize event binding in Datepicker initialization.
1 parent 94b7e6b commit 74d195e

File tree

1 file changed

+27
-20
lines changed

1 file changed

+27
-20
lines changed

ui/jquery.ui.datepicker.js

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ $.extend($.ui, { datepicker: { version: "@VERSION" } });
1616

1717
var PROP_NAME = 'datepicker';
1818
var dpuuid = new Date().getTime();
19+
var instActive;
1920

2021
/* Date picker manager.
2122
Use the singleton instance of this class, $.datepicker, to interact with the date picker.
@@ -107,7 +108,7 @@ function Datepicker() {
107108
autoSize: false // True to size the input for the date format, false to leave as is
108109
};
109110
$.extend(this._defaults, this.regional['']);
110-
this.dpDiv = $('<div id="' + this._mainDivId + '" class="ui-datepicker ui-widget ui-widget-content ui-helper-clearfix ui-corner-all"></div>');
111+
this.dpDiv = bindHover($('<div id="' + this._mainDivId + '" class="ui-datepicker ui-widget ui-widget-content ui-helper-clearfix ui-corner-all"></div>'));
111112
}
112113

113114
$.extend(Datepicker.prototype, {
@@ -173,7 +174,7 @@ $.extend(Datepicker.prototype, {
173174
drawMonth: 0, drawYear: 0, // month being drawn
174175
inline: inline, // is datepicker inline or not
175176
dpDiv: (!inline ? this.dpDiv : // presentation div
176-
$('<div class="' + this._inlineClass + ' ui-datepicker ui-widget ui-widget-content ui-helper-clearfix ui-corner-all"></div>'))};
177+
bindHover($('<div class="' + this._inlineClass + ' ui-datepicker ui-widget ui-widget-content ui-helper-clearfix ui-corner-all"></div>')))};
177178
},
178179

179180
/* Attach the date picker to an input field. */
@@ -674,29 +675,13 @@ $.extend(Datepicker.prototype, {
674675
_updateDatepicker: function(inst) {
675676
var self = this;
676677
var borders = $.datepicker._getBorders(inst.dpDiv);
678+
instActive = inst; // for delegate hover events
677679
inst.dpDiv.empty().append(this._generateHTML(inst));
678680
var cover = inst.dpDiv.find('iframe.ui-datepicker-cover'); // IE6- only
679681
if( !!cover.length ){ //avoid call to outerXXXX() when not in IE6
680682
cover.css({left: -borders[0], top: -borders[1], width: inst.dpDiv.outerWidth(), height: inst.dpDiv.outerHeight()})
681683
}
682-
inst.dpDiv.find('button, .ui-datepicker-prev, .ui-datepicker-next, .ui-datepicker-calendar td a')
683-
.bind('mouseout', function(){
684-
$(this).removeClass('ui-state-hover');
685-
if(this.className.indexOf('ui-datepicker-prev') != -1) $(this).removeClass('ui-datepicker-prev-hover');
686-
if(this.className.indexOf('ui-datepicker-next') != -1) $(this).removeClass('ui-datepicker-next-hover');
687-
})
688-
.bind('mouseover', function(){
689-
if (!self._isDisabledDatepicker( inst.inline ? inst.dpDiv.parent()[0] : inst.input[0])) {
690-
$(this).parents('.ui-datepicker-calendar').find('a').removeClass('ui-state-hover');
691-
$(this).addClass('ui-state-hover');
692-
if(this.className.indexOf('ui-datepicker-prev') != -1) $(this).addClass('ui-datepicker-prev-hover');
693-
if(this.className.indexOf('ui-datepicker-next') != -1) $(this).addClass('ui-datepicker-next-hover');
694-
}
695-
})
696-
.end()
697-
.find('.' + this._dayOverClass + ' a')
698-
.trigger('mouseover')
699-
.end();
684+
inst.dpDiv.find('.' + this._dayOverClass + ' a').mouseover();
700685
var numMonths = this._getNumberOfMonths(inst);
701686
var cols = numMonths[1];
702687
var width = 17;
@@ -1719,6 +1704,28 @@ $.extend(Datepicker.prototype, {
17191704
}
17201705
});
17211706

1707+
/*
1708+
* Bind hover events for datepicker elements.
1709+
* Done via delegate so the binding only occurs once in the lifetime of the parent div.
1710+
* Global instActive, set by _updateDatepicker allows the handlers to find their way back to the active picker.
1711+
*/
1712+
function bindHover(dpDiv) {
1713+
var selector = 'button, .ui-datepicker-prev, .ui-datepicker-next, .ui-datepicker-calendar td a';
1714+
return dpDiv.delegate(selector, 'mouseout', function() {
1715+
$(this).removeClass('ui-state-hover');
1716+
if (this.className.indexOf('ui-datepicker-prev') != -1) $(this).removeClass('ui-datepicker-prev-hover');
1717+
if (this.className.indexOf('ui-datepicker-next') != -1) $(this).removeClass('ui-datepicker-next-hover');
1718+
})
1719+
.delegate(selector, 'mouseover', function(){
1720+
if (!$.datepicker._isDisabledDatepicker( instActive.inline ? dpDiv.parent()[0] : instActive.input[0])) {
1721+
$(this).parents('.ui-datepicker-calendar').find('a').removeClass('ui-state-hover');
1722+
$(this).addClass('ui-state-hover');
1723+
if (this.className.indexOf('ui-datepicker-prev') != -1) $(this).addClass('ui-datepicker-prev-hover');
1724+
if (this.className.indexOf('ui-datepicker-next') != -1) $(this).addClass('ui-datepicker-next-hover');
1725+
}
1726+
});
1727+
}
1728+
17221729
/* jQuery extend now ignores nulls! */
17231730
function extendRemove(target, props) {
17241731
$.extend(target, props);

0 commit comments

Comments
 (0)