Skip to content
This repository was archived by the owner on Sep 5, 2024. It is now read-only.

Commit cde67d6

Browse files
Michael Chenjelbourn
authored andcommitted
fix(calendar): Make calendar directive IDs unique
Prefix all calendar id attributes with the directive scope $id. This makes it possible to include multiple calendar instances on the same page.
1 parent 8a8824a commit cde67d6

File tree

1 file changed

+28
-17
lines changed

1 file changed

+28
-17
lines changed

src/components/calendar/calendar.js

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
angular.module('material.components.calendar', ['material.core'])
1010
.directive('mdCalendar', calendarDirective);
1111

12-
// TODO(jelbourn): Date cell IDs need to be unique per-calendar.
1312
// TODO(jelbourn): internationalize a11y announcements.
1413

1514
// TODO(jelbourn): Update the selected date on [click, tap, enter]
@@ -30,6 +29,9 @@
3029
// TODO(jelbourn): read-only state.
3130

3231
function calendarDirective() {
32+
// Generate a unique ID for each instance of the directive.
33+
var directiveId = 0;
34+
3335
return {
3436
template:
3537
'<div>' +
@@ -48,6 +50,7 @@
4850
link: function(scope, element, attrs, controllers) {
4951
var ngModelCtrl = controllers[0];
5052
var mdCalendarCtrl = controllers[1];
53+
mdCalendarCtrl.directiveId = directiveId++;
5154
mdCalendarCtrl.configureNgModel(ngModelCtrl);
5255
}
5356
};
@@ -71,16 +74,6 @@
7174
/** Class applied to the cell for today. */
7275
var TODAY_CLASS = 'md-calendar-date-today';
7376

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-
8477
/**
8578
* Controller for the mdCalendar component.
8679
* @ngInject @constructor
@@ -255,7 +248,7 @@
255248
self.changeDisplayDate(date).then(function() {
256249
self.focusDateElement(date);
257250
});
258-
})
251+
});
259252
};
260253

261254
/**
@@ -295,7 +288,7 @@
295288
* @param {Date} date
296289
*/
297290
CalendarCtrl.prototype.focusDateElement = function(date) {
298-
var cellId = getDateId(date);
291+
var cellId = this.getDateId_(date);
299292
var cell = this.calendarElement.querySelector('#' + cellId);
300293
cell.focus();
301294
};
@@ -481,15 +474,15 @@
481474

482475
// Remove the selected class from the previously selected date, if any.
483476
if (self.selectedDate) {
484-
var prevDateCell = self.calendarElement.querySelector('#' + getDateId(self.selectedDate));
477+
var prevDateCell = self.calendarElement.querySelector('#' + self.getDateId_(self.selectedDate));
485478
if (prevDateCell) {
486479
prevDateCell.classList.remove(SELECTED_DATE_CLASS);
487480
}
488481
}
489482

490483
// Apply the select class to the new selected date if it is set.
491484
if (date) {
492-
var dateCell = self.calendarElement.querySelector('#' + getDateId(date));
485+
var dateCell = self.calendarElement.querySelector('#' + self.getDateId_(date));
493486
if (dateCell) {
494487
dateCell.classList.add(SELECTED_DATE_CLASS);
495488
}
@@ -544,7 +537,7 @@
544537
* Highlight the cell corresponding to today if it is on the screen.
545538
*/
546539
CalendarCtrl.prototype.highlightToday = function() {
547-
var todayCell = this.calendarElement.querySelector('#' + getDateId(this.today));
540+
var todayCell = this.calendarElement.querySelector('#' + this.getDateId_(this.today));
548541
if (todayCell) {
549542
todayCell.classList.add(TODAY_CLASS);
550543
}
@@ -618,7 +611,7 @@
618611
//selectionIndicator.setAttribute('aria-label', '');
619612

620613
cell.setAttribute('tabindex', '-1');
621-
cell.id = getDateId(opt_date);
614+
cell.id = this.getDateId_(opt_date);
622615
cell.dataset.timestamp = opt_date.getTime();
623616
cell.addEventListener('click', this.cellClickHandler);
624617
}
@@ -696,4 +689,22 @@
696689

697690
return monthBody;
698691
};
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+
};
699710
})();

0 commit comments

Comments
 (0)