Skip to content

Commit 70402e3

Browse files
committed
Extract event handler manip to separate functions
1 parent 6659401 commit 70402e3

File tree

1 file changed

+50
-26
lines changed

1 file changed

+50
-26
lines changed

js/bootstrap-datepicker.js

Lines changed: 50 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -38,32 +38,7 @@
3838
if(this.component && this.component.length === 0)
3939
this.component = false;
4040

41-
if (this.isInput) {
42-
this.element.on({
43-
focus: $.proxy(this.show, this),
44-
blur: $.proxy(this._hide, this),
45-
keyup: $.proxy(this.update, this),
46-
keydown: $.proxy(this.keydown, this)
47-
});
48-
} else {
49-
if (this.component){
50-
// For components that are not readonly, allow keyboard nav
51-
this.element.find('input').on({
52-
focus: $.proxy(this.show, this),
53-
blur: $.proxy(this._hide, this),
54-
keyup: $.proxy(this.update, this),
55-
keydown: $.proxy(this.keydown, this)
56-
});
57-
58-
this.component.on('click', $.proxy(this.show, this));
59-
var element = this.element.find('input');
60-
element.on({
61-
blur: $.proxy(this._hide, this)
62-
})
63-
} else {
64-
this.element.on('click', $.proxy(this.show, this));
65-
}
66-
}
41+
this._attachEvents();
6742

6843
this.autoclose = false;
6944
if ('autoclose' in options) {
@@ -110,6 +85,55 @@
11085
Datepicker.prototype = {
11186
constructor: Datepicker,
11287

88+
_events: [],
89+
_attachEvents: function(){
90+
this._detachEvents();
91+
if (this.isInput) {
92+
this._events = [
93+
[this.element, {
94+
focus: $.proxy(this.show, this),
95+
blur: $.proxy(this._hide, this),
96+
keyup: $.proxy(this.update, this),
97+
keydown: $.proxy(this.keydown, this)
98+
}]
99+
];
100+
}
101+
else if (this.component){
102+
this._events = [
103+
// For components that are not readonly, allow keyboard nav
104+
[this.element.find('input'), {
105+
focus: $.proxy(this.show, this),
106+
blur: $.proxy(this._hide, this),
107+
keyup: $.proxy(this.update, this),
108+
keydown: $.proxy(this.keydown, this),
109+
}],
110+
[this.component, {
111+
click: $.proxy(this.show, this)
112+
}]
113+
];
114+
}
115+
else {
116+
this._events = [
117+
[this.element, {
118+
click: $.proxy(this.show, this)
119+
}]
120+
];
121+
}
122+
for (var i=0, el, ev; i<this._events.length; i++){
123+
el = this._events[i][0];
124+
ev = this._events[i][1];
125+
el.on(ev);
126+
}
127+
},
128+
_detachEvents: function(){
129+
for (var i=0, el, ev; i<this._events.length; i++){
130+
el = this._events[i][0];
131+
ev = this._events[i][1];
132+
el.off(ev);
133+
}
134+
this._events = [];
135+
},
136+
113137
show: function(e) {
114138
this.picker.show();
115139
this.height = this.component ? this.component.outerHeight() : this.element.outerHeight();

0 commit comments

Comments
 (0)