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 | export var AnnouncerPriority;
|
27 | (function (AnnouncerPriority) {
|
28 | AnnouncerPriority["POLITE"] = "polite";
|
29 | AnnouncerPriority["ASSERTIVE"] = "assertive";
|
30 | })(AnnouncerPriority || (AnnouncerPriority = {}));
|
31 |
|
32 |
|
33 |
|
34 | export var DATA_MDC_DOM_ANNOUNCE = 'data-mdc-dom-announce';
|
35 |
|
36 |
|
37 |
|
38 | export function announce(message, options) {
|
39 | Announcer.getInstance().say(message, options);
|
40 | }
|
41 | var Announcer = (function () {
|
42 |
|
43 | function Announcer() {
|
44 | this.liveRegions = new Map();
|
45 | }
|
46 | Announcer.getInstance = function () {
|
47 | if (!Announcer.instance) {
|
48 | Announcer.instance = new Announcer();
|
49 | }
|
50 | return Announcer.instance;
|
51 | };
|
52 | Announcer.prototype.say = function (message, options) {
|
53 | var _a, _b;
|
54 | var priority = (_a = options === null || options === void 0 ? void 0 : options.priority) !== null && _a !== void 0 ? _a : AnnouncerPriority.POLITE;
|
55 | var ownerDocument = (_b = options === null || options === void 0 ? void 0 : options.ownerDocument) !== null && _b !== void 0 ? _b : document;
|
56 | var liveRegion = this.getLiveRegion(priority, ownerDocument);
|
57 |
|
58 |
|
59 | liveRegion.textContent = '';
|
60 |
|
61 | setTimeout(function () {
|
62 | liveRegion.textContent = message;
|
63 | ownerDocument.addEventListener('click', clearLiveRegion);
|
64 | }, 1);
|
65 | function clearLiveRegion() {
|
66 | liveRegion.textContent = '';
|
67 | ownerDocument.removeEventListener('click', clearLiveRegion);
|
68 | }
|
69 | };
|
70 | Announcer.prototype.getLiveRegion = function (priority, ownerDocument) {
|
71 | var documentLiveRegions = this.liveRegions.get(ownerDocument);
|
72 | if (!documentLiveRegions) {
|
73 | documentLiveRegions = new Map();
|
74 | this.liveRegions.set(ownerDocument, documentLiveRegions);
|
75 | }
|
76 | var existingLiveRegion = documentLiveRegions.get(priority);
|
77 | if (existingLiveRegion &&
|
78 | ownerDocument.body.contains(existingLiveRegion)) {
|
79 | return existingLiveRegion;
|
80 | }
|
81 | var liveRegion = this.createLiveRegion(priority, ownerDocument);
|
82 | documentLiveRegions.set(priority, liveRegion);
|
83 | return liveRegion;
|
84 | };
|
85 | Announcer.prototype.createLiveRegion = function (priority, ownerDocument) {
|
86 | var el = ownerDocument.createElement('div');
|
87 | el.style.position = 'absolute';
|
88 | el.style.top = '-9999px';
|
89 | el.style.left = '-9999px';
|
90 | el.style.height = '1px';
|
91 | el.style.overflow = 'hidden';
|
92 | el.setAttribute('aria-atomic', 'true');
|
93 | el.setAttribute('aria-live', priority);
|
94 | el.setAttribute(DATA_MDC_DOM_ANNOUNCE, 'true');
|
95 | ownerDocument.body.appendChild(el);
|
96 | return el;
|
97 | };
|
98 | return Announcer;
|
99 | }());
|
100 |
|
\ | No newline at end of file |