This repository has been archived by the owner on Jan 7, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathselectize-plugin-a11y.js
141 lines (129 loc) · 3.8 KB
/
selectize-plugin-a11y.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
/* global Selectize */
Selectize.define("selectize-plugin-a11y", function (options) {
var self = this;
var KEY_RETURN = 13;
if (typeof self.accessibility === "undefined") {
self.accessibility = {};
}
self.accessibility.helpers = {
randomId: function (len) {
var str = "",
strLength = len || 10,
base = "abcdefghijklmnopqrstuvwxyz0123456789",
baseLength = base.length;
for (var i = 0; i < strLength; i++) {
str += base[Math.floor(baseLength * Math.random())];
}
return str;
}
};
self.accessibility.liveRegion = {
$region: "",
speak: function (msg) {
var $container = $("<div></div>");
$container.text(msg);
this.$region.html($container);
},
domListener: function () {
var observer = new MutationObserver(function (mutations) {
mutations.forEach(function (mutation) {
var $target = $(mutation.target);
if ($target.hasClass("items")) {
if ($target.hasClass("dropdown-active")) {
// open
self.$control_input.attr("aria-expanded", "true");
} else {
// close
self.$control_input.attr("aria-expanded", "false");
self.$control_input.removeAttr("aria-activedescendant");
}
} else {
// option change
if ($target.hasClass("active")) {
if (!!$target.attr("data-value")) { // eslint-disable-line no-extra-boolean-cast
self.$control_input.attr(
"aria-activedescendant",
$target.attr("id")
);
self.accessibility.liveRegion.speak($target.text(), 500);
}
}
}
});
});
observer.observe(self.$dropdown[0], {
attributes: true,
attributeFilter: ["class"],
subtree: true,
attributeOldValue: true
});
observer.observe(self.$control[0], {
attributes: true,
attributeFilter: ["class"]
});
observer.observe(self.$control_input[0], {
attributes: true,
attributeFilter: ["value"]
});
},
setAttributes: function () {
this.$region.attr({
"aria-live": "assertive",
role: "log",
"aria-relevant": "additions",
"aria-atomic": "true"
});
},
setStyles: function () {
this.$region.css({
position: "absolute",
width: "1px",
height: "1px",
"margin-top": "-1px",
clip: "rect(1px, 1px, 1px, 1px)",
overflow: "hidden"
});
},
init: function () {
this.$region = $("<div>");
this.setAttributes();
this.setStyles();
$("body").append(this.$region);
this.domListener();
}
};
this.setup = (function () {
var original = self.setup;
return function () {
original.apply(this, arguments);
var inputId = self.accessibility.helpers.randomId(),
listboxId = self.accessibility.helpers.randomId();
self.$control.on("keydown", function (e) {
if (e.keyCode === KEY_RETURN) {
$(this).click();
}
});
self.$control_input.attr({
role: "combobox",
"aria-expanded": "false",
haspopup: "listbox",
"aria-owns": listboxId,
"aria-label": self.$wrapper
.closest("[data-accessibility-selectize-label]")
.attr("data-accessibility-selectize-label")
});
self.$dropdown_content.attr({
role: "listbox",
id: listboxId
});
self.accessibility.liveRegion.init();
};
})();
this.destroy = (function () {
var original = self.destroy;
return function () {
self.accessibility.liveRegion.$region.remove();
return original.apply(this, arguments);
};
})();
});