Skip to content

Commit 0da176b

Browse files
adambaratzscottgonzalez
authored andcommitted
Datepicker: bind hover events using delegate. Fixed #7256 - minimize event binding in Datepicker initialization.
(cherry picked from commit 74d195e)
1 parent 96f6b1a commit 0da176b

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. */
@@ -672,29 +673,13 @@ $.extend(Datepicker.prototype, {
672673
_updateDatepicker: function(inst) {
673674
var self = this;
674675
var borders = $.datepicker._getBorders(inst.dpDiv);
676+
instActive = inst; // for delegate hover events
675677
inst.dpDiv.empty().append(this._generateHTML(inst));
676678
var cover = inst.dpDiv.find('iframe.ui-datepicker-cover'); // IE6- only
677679
if( !!cover.length ){ //avoid call to outerXXXX() when not in IE6
678680
cover.css({left: -borders[0], top: -borders[1], width: inst.dpDiv.outerWidth(), height: inst.dpDiv.outerHeight()})
679681
}
680-
inst.dpDiv.find('button, .ui-datepicker-prev, .ui-datepicker-next, .ui-datepicker-calendar td a')
681-
.bind('mouseout', function(){
682-
$(this).removeClass('ui-state-hover');
683-
if(this.className.indexOf('ui-datepicker-prev') != -1) $(this).removeClass('ui-datepicker-prev-hover');
684-
if(this.className.indexOf('ui-datepicker-next') != -1) $(this).removeClass('ui-datepicker-next-hover');
685-
})
686-
.bind('mouseover', function(){
687-
if (!self._isDisabledDatepicker( inst.inline ? inst.dpDiv.parent()[0] : inst.input[0])) {
688-
$(this).parents('.ui-datepicker-calendar').find('a').removeClass('ui-state-hover');
689-
$(this).addClass('ui-state-hover');
690-
if(this.className.indexOf('ui-datepicker-prev') != -1) $(this).addClass('ui-datepicker-prev-hover');
691-
if(this.className.indexOf('ui-datepicker-next') != -1) $(this).addClass('ui-datepicker-next-hover');
692-
}
693-
})
694-
.end()
695-
.find('.' + this._dayOverClass + ' a')
696-
.trigger('mouseover')
697-
.end();
682+
inst.dpDiv.find('.' + this._dayOverClass + ' a').mouseover();
698683
var numMonths = this._getNumberOfMonths(inst);
699684
var cols = numMonths[1];
700685
var width = 17;
@@ -1715,6 +1700,28 @@ $.extend(Datepicker.prototype, {
17151700
}
17161701
});
17171702

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

0 commit comments

Comments
 (0)