|
9 | 9 | angular.module('material.components.calendar', ['material.core'])
|
10 | 10 | .directive('mdCalendar', calendarDirective);
|
11 | 11 |
|
12 |
| - // TODO(jelbourn): Date cell IDs need to be unique per-calendar. |
13 | 12 | // TODO(jelbourn): internationalize a11y announcements.
|
14 | 13 |
|
15 | 14 | // TODO(jelbourn): Update the selected date on [click, tap, enter]
|
|
30 | 29 | // TODO(jelbourn): read-only state.
|
31 | 30 |
|
32 | 31 | function calendarDirective() {
|
| 32 | + // Generate a unique ID for each instance of the directive. |
| 33 | + var directiveId = 0; |
| 34 | + |
33 | 35 | return {
|
34 | 36 | template:
|
35 | 37 | '<div>' +
|
|
48 | 50 | link: function(scope, element, attrs, controllers) {
|
49 | 51 | var ngModelCtrl = controllers[0];
|
50 | 52 | var mdCalendarCtrl = controllers[1];
|
| 53 | + mdCalendarCtrl.directiveId = directiveId++; |
51 | 54 | mdCalendarCtrl.configureNgModel(ngModelCtrl);
|
52 | 55 | }
|
53 | 56 | };
|
|
71 | 74 | /** Class applied to the cell for today. */
|
72 | 75 | var TODAY_CLASS = 'md-calendar-date-today';
|
73 | 76 |
|
74 |
| - |
75 |
| - /** |
76 |
| - * Gets a unique identifier for a date for internal purposes. Not to be displayed. |
77 |
| - * @param {Date} date |
78 |
| - * @returns {string} |
79 |
| - */ |
80 |
| - function getDateId(date) { |
81 |
| - return 'md-' + date.getFullYear() + '-' + date.getMonth() + '-' + date.getDate(); |
82 |
| - } |
83 |
| - |
84 | 77 | /**
|
85 | 78 | * Controller for the mdCalendar component.
|
86 | 79 | * @ngInject @constructor
|
|
255 | 248 | self.changeDisplayDate(date).then(function() {
|
256 | 249 | self.focusDateElement(date);
|
257 | 250 | });
|
258 |
| - }) |
| 251 | + }); |
259 | 252 | };
|
260 | 253 |
|
261 | 254 | /**
|
|
295 | 288 | * @param {Date} date
|
296 | 289 | */
|
297 | 290 | CalendarCtrl.prototype.focusDateElement = function(date) {
|
298 |
| - var cellId = getDateId(date); |
| 291 | + var cellId = this.getDateId_(date); |
299 | 292 | var cell = this.calendarElement.querySelector('#' + cellId);
|
300 | 293 | cell.focus();
|
301 | 294 | };
|
|
481 | 474 |
|
482 | 475 | // Remove the selected class from the previously selected date, if any.
|
483 | 476 | if (self.selectedDate) {
|
484 |
| - var prevDateCell = self.calendarElement.querySelector('#' + getDateId(self.selectedDate)); |
| 477 | + var prevDateCell = self.calendarElement.querySelector('#' + self.getDateId_(self.selectedDate)); |
485 | 478 | if (prevDateCell) {
|
486 | 479 | prevDateCell.classList.remove(SELECTED_DATE_CLASS);
|
487 | 480 | }
|
488 | 481 | }
|
489 | 482 |
|
490 | 483 | // Apply the select class to the new selected date if it is set.
|
491 | 484 | if (date) {
|
492 |
| - var dateCell = self.calendarElement.querySelector('#' + getDateId(date)); |
| 485 | + var dateCell = self.calendarElement.querySelector('#' + self.getDateId_(date)); |
493 | 486 | if (dateCell) {
|
494 | 487 | dateCell.classList.add(SELECTED_DATE_CLASS);
|
495 | 488 | }
|
|
544 | 537 | * Highlight the cell corresponding to today if it is on the screen.
|
545 | 538 | */
|
546 | 539 | CalendarCtrl.prototype.highlightToday = function() {
|
547 |
| - var todayCell = this.calendarElement.querySelector('#' + getDateId(this.today)); |
| 540 | + var todayCell = this.calendarElement.querySelector('#' + this.getDateId_(this.today)); |
548 | 541 | if (todayCell) {
|
549 | 542 | todayCell.classList.add(TODAY_CLASS);
|
550 | 543 | }
|
|
618 | 611 | //selectionIndicator.setAttribute('aria-label', '');
|
619 | 612 |
|
620 | 613 | cell.setAttribute('tabindex', '-1');
|
621 |
| - cell.id = getDateId(opt_date); |
| 614 | + cell.id = this.getDateId_(opt_date); |
622 | 615 | cell.dataset.timestamp = opt_date.getTime();
|
623 | 616 | cell.addEventListener('click', this.cellClickHandler);
|
624 | 617 | }
|
|
696 | 689 |
|
697 | 690 | return monthBody;
|
698 | 691 | };
|
| 692 | + |
| 693 | + |
| 694 | + /** |
| 695 | + * Gets an identifier for a date unique to the calendar instance for internal |
| 696 | + * purposes. Not to be displayed. |
| 697 | + * @param {Date} date |
| 698 | + * @returns {string} |
| 699 | + * @private |
| 700 | + */ |
| 701 | + CalendarCtrl.prototype.getDateId_ = function (date) { |
| 702 | + return [ |
| 703 | + 'md', |
| 704 | + this.directiveId, |
| 705 | + date.getFullYear(), |
| 706 | + date.getMonth(), |
| 707 | + date.getDate() |
| 708 | + ].join('-'); |
| 709 | + }; |
699 | 710 | })();
|
0 commit comments