-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Announcement.js
178 lines (153 loc) · 3.89 KB
/
Announcement.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import AbstractPlugin from 'shared/AbstractPlugin';
const onInitialize = Symbol('onInitialize');
const onDestroy = Symbol('onDestroy');
const announceEvent = Symbol('announceEvent');
const announceMessage = Symbol('announceMessage');
const ARIA_RELEVANT = 'aria-relevant';
const ARIA_ATOMIC = 'aria-atomic';
const ARIA_LIVE = 'aria-live';
const ROLE = 'role';
/**
* Announcement default options
* @property {Object} defaultOptions
* @property {Number} defaultOptions.expire
* @type {Object}
*/
export const defaultOptions = {
expire: 7000,
};
/**
* Announcement plugin
* @class Announcement
* @module Announcement
* @extends AbstractPlugin
*/
export default class Announcement extends AbstractPlugin {
/**
* Announcement constructor.
* @constructs Announcement
* @param {Draggable} draggable - Draggable instance
*/
constructor(draggable) {
super(draggable);
/**
* Plugin options
* @property options
* @type {Object}
*/
this.options = {
...defaultOptions,
...this.getOptions(),
};
/**
* Original draggable trigger method. Hack until we have onAll or on('all')
* @property originalTriggerMethod
* @type {Function}
*/
this.originalTriggerMethod = this.draggable.trigger;
this[onInitialize] = this[onInitialize].bind(this);
this[onDestroy] = this[onDestroy].bind(this);
}
/**
* Attaches listeners to draggable
*/
attach() {
this.draggable.on('draggable:initialize', this[onInitialize]);
}
/**
* Detaches listeners from draggable
*/
detach() {
this.draggable.off('draggable:destroy', this[onDestroy]);
}
/**
* Returns passed in options
*/
getOptions() {
return this.draggable.options.announcements || {};
}
/**
* Announces event
* @private
* @param {AbstractEvent} event
*/
[announceEvent](event) {
const message = this.options[event.type];
if (message && typeof message === 'string') {
this[announceMessage](message);
}
if (message && typeof message === 'function') {
this[announceMessage](message(event));
}
}
/**
* Announces message to screen reader
* @private
* @param {String} message
*/
[announceMessage](message) {
announce(message, {expire: this.options.expire});
}
/**
* Initialize hander
* @private
*/
[onInitialize]() {
// Hack until there is an api for listening for all events
this.draggable.trigger = (event) => {
try {
this[announceEvent](event);
} finally {
// Ensure that original trigger is called
this.originalTriggerMethod.call(this.draggable, event);
}
};
}
/**
* Destroy hander
* @private
*/
[onDestroy]() {
this.draggable.trigger = this.originalTriggerMethod;
}
}
/**
* @const {HTMLElement} liveRegion
*/
const liveRegion = createRegion();
/**
* Announces message via live region
* @param {String} message
* @param {Object} options
* @param {Number} options.expire
*/
function announce(message, {expire}) {
const element = document.createElement('div');
element.textContent = message;
liveRegion.appendChild(element);
return setTimeout(() => {
liveRegion.removeChild(element);
}, expire);
}
/**
* Creates region element
* @return {HTMLElement}
*/
function createRegion() {
const element = document.createElement('div');
element.setAttribute('id', 'draggable-live-region');
element.setAttribute(ARIA_RELEVANT, 'additions');
element.setAttribute(ARIA_ATOMIC, 'true');
element.setAttribute(ARIA_LIVE, 'assertive');
element.setAttribute(ROLE, 'log');
element.style.position = 'fixed';
element.style.width = '1px';
element.style.height = '1px';
element.style.top = '-1px';
element.style.overflow = 'hidden';
return element;
}
// Append live region element as early as possible
document.addEventListener('DOMContentLoaded', () => {
document.body.appendChild(liveRegion);
});