|
88 | 88 | /** Class applied to the container if the date is invalid. */
|
89 | 89 | var INVALID_CLASS = 'md-datepicker-invalid';
|
90 | 90 |
|
| 91 | + /** Default time in ms to debounce input event by. */ |
| 92 | + var DEFAULT_DEBOUNCE_INTERVAL = 500; |
| 93 | + |
91 | 94 | /**
|
92 | 95 | * Controller for md-datepicker.
|
93 | 96 | *
|
|
189 | 192 | ngModelCtrl.$render = function() {
|
190 | 193 | self.date = self.ngModelCtrl.$viewValue;
|
191 | 194 | self.inputElement.value = self.dateLocale.formatDate(self.date);
|
192 |
| - self.inputElement.size = self.inputElement.value.length + EXTRA_INPUT_SIZE; |
| 195 | + self.resizeInputElement(); |
193 | 196 | };
|
194 | 197 | };
|
195 | 198 |
|
|
207 | 210 | self.closeCalendarPane();
|
208 | 211 | });
|
209 | 212 |
|
210 |
| - // TODO(jelbourn): Debounce this input event. |
211 |
| - self.inputElement.addEventListener('input', function() { |
212 |
| - var inputString = self.inputElement.value; |
213 |
| - var parsedDate = self.dateLocale.parseDate(inputString); |
214 |
| - |
215 |
| - self.inputElement.size = inputString.length + EXTRA_INPUT_SIZE; |
216 |
| - |
217 |
| - if (self.dateUtil.isValidDate(parsedDate) && self.dateLocale.isDateComplete(inputString)) { |
218 |
| - self.ngModelCtrl.$setViewValue(parsedDate); |
219 |
| - self.inputContainer.classList.remove(INVALID_CLASS); |
220 |
| - self.$scope.$apply(); |
221 |
| - } else { |
222 |
| - // If there's an input string, it's an invalid date. |
223 |
| - self.inputContainer.classList.toggle(INVALID_CLASS, inputString); |
224 |
| - } |
225 |
| - }); |
| 213 | + self.inputElement.addEventListener('input', angular.bind(self, self.resizeInputElement)); |
| 214 | + // TODO(chenmike): Add ability for users to specify this interval. |
| 215 | + self.inputElement.addEventListener('input', self.$mdUtil.debounce(self.handleInputEvent, |
| 216 | + DEFAULT_DEBOUNCE_INTERVAL, self)); |
226 | 217 | };
|
227 | 218 |
|
228 | 219 | /** Attach event listeners for user interaction. */
|
|
259 | 250 | });
|
260 | 251 |
|
261 | 252 | Object.defineProperty(this, 'placeholder', {
|
262 |
| - get: function() { return self.inputElement.placeholder }, |
| 253 | + get: function() { return self.inputElement.placeholder; }, |
263 | 254 | set: function(value) { self.inputElement.placeholder = value; }
|
264 | 255 | });
|
265 | 256 | };
|
|
274 | 265 | this.calendarButton.disabled = isDisabled;
|
275 | 266 | };
|
276 | 267 |
|
| 268 | + /** |
| 269 | + * Resizes the input element based on the size of its content. |
| 270 | + */ |
| 271 | + DatePickerCtrl.prototype.resizeInputElement = function() { |
| 272 | + this.inputElement.size = this.inputElement.value.length + EXTRA_INPUT_SIZE; |
| 273 | + }; |
| 274 | + |
| 275 | + /** |
| 276 | + * Sets the model value if the user input is a valid date. |
| 277 | + * Adds an invalid class to the input element if not. |
| 278 | + */ |
| 279 | + DatePickerCtrl.prototype.handleInputEvent = function() { |
| 280 | + var inputString = this.inputElement.value; |
| 281 | + var parsedDate = this.dateLocale.parseDate(inputString); |
| 282 | + |
| 283 | + if (this.dateUtil.isValidDate(parsedDate) && this.dateLocale.isDateComplete(inputString)) { |
| 284 | + this.ngModelCtrl.$setViewValue(parsedDate); |
| 285 | + this.inputContainer.classList.remove(INVALID_CLASS); |
| 286 | + this.$scope.$apply(); |
| 287 | + } else { |
| 288 | + // If there's an input string, it's an invalid date. |
| 289 | + this.inputContainer.classList.toggle(INVALID_CLASS, inputString); |
| 290 | + } |
| 291 | + }; |
| 292 | + |
277 | 293 | /** Position and attach the floating calendar to the document. */
|
278 | 294 | DatePickerCtrl.prototype.attachCalendarPane = function() {
|
279 | 295 | var calendarPane = this.calendarPane;
|
|
0 commit comments