|
| 1 | +import { Injectable } from '@angular/core'; |
| 2 | +import { formatDate, I18nPluralPipe } from '@angular/common'; |
| 3 | +import { A11yParams } from './calendar-a11y.interface'; |
| 4 | + |
| 5 | +/** |
| 6 | + * This class is responsible for adding accessibility to the calendar. |
| 7 | + * You may override any of its methods via angulars DI to suit your requirements. |
| 8 | + * For example: |
| 9 | + * |
| 10 | + * ```typescript |
| 11 | + * import { A11yParams, CalendarA11y } from 'angular-calendar'; |
| 12 | + * import { formatDate, I18nPluralPipe } from '@angular/common'; |
| 13 | + * |
| 14 | + * // adding your own a11y params |
| 15 | + * export interface CustomA11yParams extends A11yParams { |
| 16 | + * isDrSuess?: boolean; |
| 17 | + * } |
| 18 | + * |
| 19 | + * export class CustomCalendarA11y extends CalendarA11y { |
| 20 | + * constructor(protected i18nPlural: I18nPluralPipe) { |
| 21 | + * super(i18nPlural); |
| 22 | + * } |
| 23 | + * |
| 24 | + * // overriding a function |
| 25 | + * public openDayEventsLandmark({ date, locale, isDrSuess }: CustomA11yParams): string { |
| 26 | + * if (isDrSuess) { |
| 27 | + * return ` |
| 28 | + * ${formatDate(date, 'EEEE MMMM d', locale)} |
| 29 | + * Today you are you! That is truer than true! There is no one alive |
| 30 | + * who is you-er than you! |
| 31 | + * `; |
| 32 | + * } |
| 33 | + * } |
| 34 | + * } |
| 35 | + * |
| 36 | + * // in your component that uses the calendar |
| 37 | + * providers: [{ |
| 38 | + * provide: CalendarA11y, |
| 39 | + * useClass: CustomCalendarA11y |
| 40 | + * }] |
| 41 | + * ``` |
| 42 | + */ |
| 43 | +@Injectable() |
| 44 | +export class CalendarA11y { |
| 45 | + constructor(protected i18nPlural: I18nPluralPipe) {} |
| 46 | + |
| 47 | + /** |
| 48 | + * Aria label for the badges/date of a cell |
| 49 | + * @example: `Saturday October 19 1 event click to expand` |
| 50 | + */ |
| 51 | + public monthCell({ day, locale }: A11yParams): string { |
| 52 | + if (day.badgeTotal > 0) { |
| 53 | + return ` |
| 54 | + ${formatDate(day.date, 'EEEE MMMM d', locale)}, |
| 55 | + ${this.i18nPlural.transform(day.badgeTotal, { |
| 56 | + '=0': 'No events', |
| 57 | + '=1': 'One event', |
| 58 | + other: '# events' |
| 59 | + })}, |
| 60 | + click to expand |
| 61 | + `; |
| 62 | + } else { |
| 63 | + return `${formatDate(day.date, 'EEEE MMMM d', locale)}`; |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Aria label for the open day events start landmark |
| 69 | + * @example: `Saturday October 19 expanded view` |
| 70 | + */ |
| 71 | + public openDayEventsLandmark({ date, locale }: A11yParams): string { |
| 72 | + return ` |
| 73 | + Beginning of expanded view for ${formatDate(date, 'EEEE MMMM dd', locale)} |
| 74 | + `; |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Aria label for alert that a day in the month view was expanded |
| 79 | + * @example: `Saturday October 19 expanded` |
| 80 | + */ |
| 81 | + public openDayEventsAlert({ date, locale }: A11yParams): string { |
| 82 | + return `${formatDate(date, 'EEEE MMMM dd', locale)} expanded`; |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Descriptive aria label for an event |
| 87 | + * @example: `Saturday October 19th, Scott's Pizza Party, from 11:00am to 5:00pm` |
| 88 | + */ |
| 89 | + public eventDescription({ event, locale }: A11yParams): string { |
| 90 | + if (event.allDay === true) { |
| 91 | + return this.allDayEventDescription({ event, locale }); |
| 92 | + } |
| 93 | + |
| 94 | + const aria = ` |
| 95 | + ${formatDate(event.start, 'EEEE MMMM dd', locale)}, |
| 96 | + ${event.title}, from ${formatDate(event.start, 'hh:mm a', locale)} |
| 97 | + `; |
| 98 | + if (event.end) { |
| 99 | + return aria + ` to ${formatDate(event.end, 'hh:mm a', locale)}`; |
| 100 | + } |
| 101 | + return aria; |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Descriptive aria label for an all day event |
| 106 | + * @example: |
| 107 | + * `Scott's Party, event spans multiple days: start time October 19 5:00pm, no stop time` |
| 108 | + */ |
| 109 | + public allDayEventDescription({ event, locale }: A11yParams): string { |
| 110 | + const aria = ` |
| 111 | + ${event.title}, event spans multiple days: |
| 112 | + start time ${formatDate(event.start, 'MMMM dd hh:mm a', locale)} |
| 113 | + `; |
| 114 | + if (event.end) { |
| 115 | + return ( |
| 116 | + aria + `, stop time ${formatDate(event.end, 'MMMM d hh:mm a', locale)}` |
| 117 | + ); |
| 118 | + } |
| 119 | + return aria + `, no stop time`; |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * Aria label for the calendar event actions icons |
| 124 | + * @returns 'Edit' for fa-pencil icons, and 'Delete' for fa-times icons |
| 125 | + */ |
| 126 | + public actionButtonLabel({ action }: A11yParams): string { |
| 127 | + return action.a11yLabel; |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * @returns {number} Tab index to be given to month cells |
| 132 | + */ |
| 133 | + public monthCellTabIndex(): number { |
| 134 | + return 0; |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * @returns true if the events inside the month cell should be aria-hidden |
| 139 | + */ |
| 140 | + public hideMonthCellEvents(): boolean { |
| 141 | + return true; |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * @returns true if event titles should be aria-hidden (global) |
| 146 | + */ |
| 147 | + public hideEventTitle(): boolean { |
| 148 | + return true; |
| 149 | + } |
| 150 | + |
| 151 | + /** |
| 152 | + * @returns true if hour segments in the week view should be aria-hidden |
| 153 | + */ |
| 154 | + public hideWeekHourSegment(): boolean { |
| 155 | + return true; |
| 156 | + } |
| 157 | + |
| 158 | + /** |
| 159 | + * @returns true if hour segments in the day view should be aria-hidden |
| 160 | + */ |
| 161 | + public hideDayHourSegment(): boolean { |
| 162 | + return true; |
| 163 | + } |
| 164 | +} |
0 commit comments