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

Commit

Permalink
a11y(calendar): move aria focus on gridcells
Browse files Browse the repository at this point in the history
  • Loading branch information
jelbourn committed Aug 13, 2015
1 parent 1400d25 commit 91bcb48
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 14 deletions.
33 changes: 23 additions & 10 deletions src/components/calendar/calendar.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,13 @@
function calendarDirective() {
return {
template:
//'<input class="md-visually-hidden md-dummy" aria-label="Select a date">' +
'<table aria-hidden="true" class="md-calendar-day-header"><thead></thead></table>' +
'<div aria-hidden="true" class="md-calendar-scroll-mask">' +
'<div class="md-calendar-scroll-mask">' +
'<md-virtual-repeat-container class="md-calendar-scroll-container">' +
'<table class="md-calendar">' +
'<tbody md-virtual-repeat="i in ctrl.items" md-calendar-month ' +
'md-month-offset="$index" class="md-calendar-month" aria-hidden="true" ' +
'<table role="grid" tabindex="0" class="md-calendar" aria-readonly="true">' +
'<tbody role="rowgroup" md-virtual-repeat="i in ctrl.items" md-calendar-month ' +
'md-month-offset="$index" class="md-calendar-month" ' +
'md-start-index="ctrl.getSelectedMonthIndex()" ' +
'md-item-size="' + TBODY_HEIGHT + '"></tbody>' +
'</table>' +
Expand Down Expand Up @@ -122,8 +123,9 @@
/** @final {HTMLElement} */
this.ariaLiveElement = $element[0].querySelector('[aria-live]');

this.ariaLiveElement.parentNode.removeChild(this.ariaLiveElement);
document.body.appendChild(this.ariaLiveElement);
// TODO(jelbourn): one global live region.
//this.ariaLiveElement.parentNode.removeChild(this.ariaLiveElement);
//document.body.appendChild(this.ariaLiveElement);

/** @final {HTMLElement} */
this.calendarScroller = $element[0].querySelector('.md-virtual-repeat-scroller');
Expand Down Expand Up @@ -251,6 +253,7 @@
/** Attach event listeners for the calendar. */
CalendarCtrl.prototype.attachCalendarEventListeners = function() {
// Keyboard interaction.
//this.calendarElement.addEventListener('keydown', angular.bind(this, this.handleKeyEvent));
this.$element.on('keydown', angular.bind(this, this.handleKeyEvent));
};

Expand Down Expand Up @@ -282,7 +285,9 @@
// Selection isn't occuring, so the key event is either navigation or nothing.
var date = self.getFocusDateFromKeyEvent(event);
if (date) {
console.log('key event');
event.preventDefault();
event.stopPropagation();

// Since this is a keyboard interaction, actually give the newly focused date keyboard
// focus after the been brought into view.
Expand Down Expand Up @@ -359,26 +364,32 @@
CalendarCtrl.prototype.focus = function(opt_date) {
var date = opt_date || this.selectedDate;

//this.$element[0].querySelector('.md-calendar-focus-holder').focus();
this.$element[0].focus();

if (!opt_date) {
this.announceDisplayDateChange(null, date);
}

var previousFocus = this.calendarElement.querySelector('.md-focus');
if (previousFocus) {
previousFocus.classList.remove('md-focus');
//previousFocus.setAttribute('aria-selected', 'false');
}


var cellId = this.getDateId(date);
var cell = this.calendarElement.querySelector('#' + cellId);
if (cell) {
cell.classList.add('md-focus');
cell.focus();
//this.calendarElement.setAttribute('aria-activedescendant', cell.id);
//cell.setAttribute('aria-selected', 'true');
} else {
this.focusDate = date;
}

//this.calendarElement.focus();

//this.$element[0].querySelector('.md-dummy').focus();
//this.calendarElement.focus();
};

/*** Updating the displayed / selected date ***/
Expand Down Expand Up @@ -466,6 +477,8 @@
* @param {Date} currentDate
*/
CalendarCtrl.prototype.announceDisplayDateChange = function(previousDate, currentDate) {
return;

// If the date has not changed at all, do nothing.
if (previousDate && this.dateUtil.isSameDay(previousDate, currentDate)) {
return;
Expand All @@ -480,7 +493,7 @@

// If the date has changed to another date in a different month and/or year, make a long
// announcement.
if (!previousDate || previousDate.getDate() !== currentDate.getDate()) {
if (!previousDate || !this.dateUtil.isSameDay(previousDate, currentDate)) {
this.ariaLiveElement.textContent = this.dateLocale.longAnnounceFormatter(currentDate);
}
};
Expand Down
13 changes: 11 additions & 2 deletions src/components/calendar/calendarMonth.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,18 @@

/** Generate and append the content for this month to the directive element. */
CalendarMonthCtrl.prototype.generateContent = function() {
var offset = (-this.calendarCtrl.items.length / 2) + this.offset;
var date = this.dateUtil.incrementMonths(this.calendarCtrl.today, offset);
var calendarCtrl = this.calendarCtrl;
var offset = (-calendarCtrl.items.length / 2) + this.offset;
var date = this.dateUtil.incrementMonths(calendarCtrl.today, offset);

this.$element.empty();
this.$element.append(this.buildCalendarForMonth(date));

if (this.focusAfterAppend) {
this.focusAfterAppend.classList.add('md-focus');
this.focusAfterAppend.focus();
//this.focusAfterAppend.setAttribute('aria-selected', 'true');
//calendarCtrl.calendarElement.setAttribute('aria-activedescendant', this.focusAfterAppend.id);
this.focusAfterAppend = null;
}
};
Expand All @@ -92,8 +98,10 @@

// TODO(jelbourn): cloneNode is likely a faster way of doing this.
var cell = document.createElement('td');
cell.tabIndex = -1;
cell.classList.add('md-calendar-date');
cell.setAttribute('role', 'gridcell');
//cell.setAttribute('', 'true');

if (opt_date) {
// Add a indicator for select, hover, and focus states.
Expand All @@ -103,6 +111,7 @@
selectionIndicator.textContent = this.dateLocale.dates[opt_date.getDate()];

cell.setAttribute('tabindex', '-1');
cell.setAttribute('aria-label', this.dateLocale.longAnnounceFormatter(opt_date));
cell.id = calendarCtrl.getDateId(opt_date);
cell.addEventListener('click', calendarCtrl.cellClickHandler);

Expand Down
4 changes: 2 additions & 2 deletions src/components/calendar/datePicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
'<md-icon class="md-datepicker-calendar-icon" md-svg-icon="md-calendar"></md-icon>' +
'</md-button>' +
'<div class="md-datepicker-input-container">' +
'<input class="md-datepicker-input" aria-haspopup="true">' +
'<input class="md-datepicker-input" aria-haspopup="true" aria-describedby="xxx">' +
'<span id="xxx" class="md-visually-hidden">Press Alt + Down to open the calendar</span>' +

'<md-button md-no-ink class="md-datepicker-triangle-button md-icon-button" ' +
'ng-click="ctrl.openCalendarPane()" tabindex="-1" ' +
Expand All @@ -45,7 +46,6 @@
'<div class="md-datepicker-input-mask"></div>' +
'<div class="md-datepicker-calendar">' +
'<md-calendar role="dialog" aria-label="{{::ctrl.dateLocale.msgCalendar}}" ' +
'tabindex="0" ' +
'ng-model="ctrl.date" ng-if="ctrl.isCalendarOpen"></md-calendar>' +
'</div>' +
'</div>',
Expand Down

0 comments on commit 91bcb48

Please sign in to comment.