|
1 | 1 | (function() {
|
2 | 2 | 'use strict';
|
3 | 3 |
|
| 4 | + // TODO(jelbourn): md-calendar shown in floating panel. |
| 5 | + // TODO(jelbourn): little calendar icon next to input |
| 6 | + // TODO(jelbourn): only one open md-calendar panel at a time per application |
| 7 | + |
| 8 | + |
4 | 9 | angular.module('material.components.calendar')
|
5 | 10 | .directive('mdDatePicker', datePickerDirective);
|
6 | 11 |
|
7 | 12 | function datePickerDirective() {
|
8 | 13 | return {
|
9 | 14 | template:
|
10 |
| - '<div>' + |
11 |
| - '<input ng-model="textValue"> <br>' + |
12 |
| - '<md-calendar ng-model="dateValue"></md-calendar>' + |
| 15 | + '<div class="md-date-picker">' + |
| 16 | + '<input> <br>' + |
| 17 | + '<md-calendar ng-model="ctrl.date"></md-calendar>' + |
13 | 18 | '</div>',
|
14 | 19 | require: ['ngModel', 'mdDatePicker'],
|
15 | 20 | scope: {},
|
16 | 21 | controller: DatePickerCtrl,
|
17 |
| - controllerAs: 'ctrl' |
| 22 | + controllerAs: 'ctrl', |
| 23 | + link: function(scope, element, attr, controllers) { |
| 24 | + var ngModelCtrl = controllers[0]; |
| 25 | + var mdDatePickerCtrl = controllers[1]; |
| 26 | + mdDatePickerCtrl.configureNgModel(ngModelCtrl); |
| 27 | + } |
18 | 28 | };
|
19 | 29 | }
|
20 | 30 |
|
21 |
| - function DatePickerCtrl($$mdDateLocale) { |
| 31 | + function DatePickerCtrl($scope, $element, $$mdDateLocale, $$mdDateUtil) { |
22 | 32 | /** @final */
|
23 | 33 | this.dateLocale = $$mdDateLocale;
|
24 | 34 |
|
| 35 | + /** @final */ |
| 36 | + this.dateUtil = $$mdDateUtil; |
| 37 | + |
25 | 38 | /** @type {!angular.NgModelController} */
|
26 | 39 | this.ngModelCtrl = null;
|
27 | 40 |
|
28 |
| - /** @type {string} */ |
29 |
| - this.textValue = ''; |
| 41 | + /** @type {HTMLInputElement} */ |
| 42 | + this.inputElement = $element[0].querySelector('input'); |
30 | 43 |
|
31 | 44 | /** @type {Date} */
|
32 |
| - this.dateValue = null; |
| 45 | + this.date = null; |
| 46 | + |
| 47 | + /** @final {!angular.JQLite} */ |
| 48 | + this.$element = $element; |
| 49 | + |
| 50 | + /** @final {!angular.Scope} */ |
| 51 | + this.$scope = $scope; |
| 52 | + |
| 53 | + this.attachChangeListeners(); |
33 | 54 | }
|
34 | 55 |
|
35 | 56 | /**
|
|
41 | 62 |
|
42 | 63 | var self = this;
|
43 | 64 | ngModelCtrl.$render = function() {
|
44 |
| - self.dateValue = self.ngModelCtrl.$viewValue |
45 |
| - self.textValue = self.dateLocale.format(self.ngModelCtrl.$viewValue); |
| 65 | + self.date = self.ngModelCtrl.$viewValue; |
| 66 | + self.inputElement.value = self.dateLocale.formatDate(self.date); |
46 | 67 | };
|
47 |
| - } |
| 68 | + }; |
| 69 | + |
| 70 | + /** |
| 71 | + * Attach event listeners for both the text input and the md-calendar. |
| 72 | + * Events are used instead of ng-model so that updates don't infinitely update the other |
| 73 | + * on a change. This should also be more performant than using a $watch. |
| 74 | + */ |
| 75 | + DatePickerCtrl.prototype.attachChangeListeners = function() { |
| 76 | + var self = this; |
| 77 | + |
| 78 | + self.$scope.$on('md-calendar-change', function(event, date) { |
| 79 | + self.ngModelCtrl.$setViewValue(date); |
| 80 | + self.inputElement.value = self.dateLocale.formatDate(date); |
| 81 | + }); |
| 82 | + |
| 83 | + // TODO(jelbourn): debounce |
| 84 | + self.inputElement.addEventListener('input', function() { |
| 85 | + var parsedDate = self.dateLocale.parseDate(self.inputElement.value); |
| 86 | + if (self.dateUtil.isValidDate(parsedDate)) { |
| 87 | + self.date = parsedDate; |
| 88 | + self.$scope.$apply(); |
| 89 | + } |
| 90 | + }); |
| 91 | + }; |
48 | 92 | })();
|
0 commit comments