|
48 | 48 | if(this.component && this.component.length === 0)
|
49 | 49 | this.component = false;
|
50 | 50 |
|
| 51 | + this._attachEvents(); |
| 52 | + |
51 | 53 | this.forceParse = true;
|
52 | 54 | if ('forceParse' in options) {
|
53 | 55 | this.forceParse = options.forceParse;
|
54 | 56 | } else if ('dateForceParse' in this.element.data()) {
|
55 | 57 | this.forceParse = this.element.data('date-force-parse');
|
56 | 58 | }
|
57 | 59 |
|
58 |
| - if (this.isInput) { |
59 |
| - this.element.on({ |
60 |
| - focus: $.proxy(this.show, this), |
61 |
| - keyup: $.proxy(this.update, this), |
62 |
| - keydown: $.proxy(this.keydown, this) |
63 |
| - }); |
64 |
| - } else { |
65 |
| - if (this.component && this.hasInput){ |
66 |
| - // For components that are not readonly, allow keyboard nav |
67 |
| - this.element.find('input').on({ |
68 |
| - focus: $.proxy(this.show, this), |
69 |
| - keyup: $.proxy(this.update, this), |
70 |
| - keydown: $.proxy(this.keydown, this) |
71 |
| - }); |
72 |
| - |
73 |
| - this.component.on('click', $.proxy(this.show, this)); |
74 |
| - } else { |
75 |
| - this.element.on('click', $.proxy(this.show, this)); |
76 |
| - } |
77 |
| - } |
78 |
| - |
79 | 60 | $(document).on('mousedown', function (e) {
|
80 | 61 | // Clicked outside the datepicker, hide it
|
81 | 62 | if ($(e.target).closest('.datepicker').length == 0) {
|
|
131 | 112 | Datepicker.prototype = {
|
132 | 113 | constructor: Datepicker,
|
133 | 114 |
|
| 115 | + _events: [], |
| 116 | + _attachEvents: function(){ |
| 117 | + this._detachEvents(); |
| 118 | + if (this.isInput) { |
| 119 | + this._events = [ |
| 120 | + [this.element, { |
| 121 | + focus: $.proxy(this.show, this), |
| 122 | + keyup: $.proxy(this.update, this), |
| 123 | + keydown: $.proxy(this.keydown, this) |
| 124 | + }] |
| 125 | + ]; |
| 126 | + } |
| 127 | + else if (this.component && this.hasInput){ |
| 128 | + this._events = [ |
| 129 | + // For components that are not readonly, allow keyboard nav |
| 130 | + [this.element.find('input'), { |
| 131 | + focus: $.proxy(this.show, this), |
| 132 | + keyup: $.proxy(this.update, this), |
| 133 | + keydown: $.proxy(this.keydown, this), |
| 134 | + }], |
| 135 | + [this.component, { |
| 136 | + click: $.proxy(this.show, this) |
| 137 | + }] |
| 138 | + ]; |
| 139 | + } |
| 140 | + else { |
| 141 | + this._events = [ |
| 142 | + [this.element, { |
| 143 | + click: $.proxy(this.show, this) |
| 144 | + }] |
| 145 | + ]; |
| 146 | + } |
| 147 | + for (var i=0, el, ev; i<this._events.length; i++){ |
| 148 | + el = this._events[i][0]; |
| 149 | + ev = this._events[i][1]; |
| 150 | + el.on(ev); |
| 151 | + } |
| 152 | + }, |
| 153 | + _detachEvents: function(){ |
| 154 | + for (var i=0, el, ev; i<this._events.length; i++){ |
| 155 | + el = this._events[i][0]; |
| 156 | + ev = this._events[i][1]; |
| 157 | + el.off(ev); |
| 158 | + } |
| 159 | + this._events = []; |
| 160 | + }, |
| 161 | + |
134 | 162 | show: function(e) {
|
135 | 163 | this.picker.show();
|
136 | 164 | this.height = this.component ? this.component.outerHeight() : this.element.outerHeight();
|
|
169 | 197 | date: this.date
|
170 | 198 | });
|
171 | 199 | },
|
| 200 | + |
| 201 | + remove: function() { |
| 202 | + this._detachEvents(); |
| 203 | + this.picker.remove(); |
| 204 | + delete this.element.data().datepicker; |
| 205 | + }, |
172 | 206 |
|
173 | 207 | getDate: function() {
|
174 | 208 | var d = this.getUTCDate();
|
|
0 commit comments