Debian Codegraph @

inst/www/shared/selectize/accessibility/js/selectize-plugin-a11y.js

raw symbols SCIP
1 /* global Selectize */
2 Selectize.define("selectize-plugin-a11y", function (options) {
3 var self = this;
4 var KEY_RETURN = 13;
5
6 if (typeof self.accessibility === "undefined") {
7 self.accessibility = {};
8 }
9
10 self.accessibility.helpers = {
11 randomId: function (len) {
12 var str = "",
13 strLength = len || 10,
14 base = "abcdefghijklmnopqrstuvwxyz0123456789",
15 baseLength = base.length;
16
17 for (var i = 0; i < strLength; i++) {
18 str += base[Math.floor(baseLength * Math.random())];
19 }
20
21 return str;
22 }
23 };
24
25 self.accessibility.liveRegion = {
26 $region: "",
27 speak: function (msg) {
28 var $container = $("<div></div>");
29 $container.text(msg);
30 this.$region.html($container);
31 },
32 domListener: function () {
33 var observer = new MutationObserver(function (mutations) {
34 mutations.forEach(function (mutation) {
35 var $target = $(mutation.target);
36 if ($target.hasClass("items")) {
37 if ($target.hasClass("dropdown-active")) {
38 // open
39 self.$control_input.attr("aria-expanded", "true");
40 // Assign a unique ID for each item. This is necessary for
41 // screen readers. In the selectize-plugin-a11y example, the
42 // random IDs are assigned when .selectize() is called, but we're
43 // doing it here to limit the scope of changes.
44 var kids = self.$dropdown_content[0].children;
45 for (var i = 0; i < kids.length; i++) {
46 var attrs = kids[i].attributes;
47 if (!attrs.role) {
48 kids[i].setAttribute("role", "option");
49 }
50 if (!attrs.id) {
51 kids[i].setAttribute("id", self.accessibility.helpers.randomId());
52 }
53 }
54 } else {
55 // close
56 self.$control_input.attr("aria-expanded", "false");
57 self.$control_input.removeAttr("aria-activedescendant");
58 }
59 } else {
60 // option change
61 if ($target.hasClass("active")) {
62 if (!!$target.attr("data-value")) { // eslint-disable-line no-extra-boolean-cast
63 self.$control_input.attr(
64 "aria-activedescendant",
65 $target.attr("id")
66 );
67 self.accessibility.liveRegion.speak($target.text(), 500);
68 }
69 }
70 }
71 });
72 });
73 observer.observe(self.$dropdown[0], {
74 attributes: true,
75 attributeFilter: ["class"],
76 subtree: true,
77 attributeOldValue: true
78 });
79
80 observer.observe(self.$control[0], {
81 attributes: true,
82 attributeFilter: ["class"]
83 });
84
85 observer.observe(self.$control_input[0], {
86 attributes: true,
87 attributeFilter: ["value"]
88 });
89 },
90 setAttributes: function () {
91 this.$region.attr({
92 "aria-live": "assertive",
93 role: "log",
94 "aria-relevant": "additions",
95 "aria-atomic": "true"
96 });
97 },
98 setStyles: function () {
99 this.$region.css({
100 position: "absolute",
101 width: "1px",
102 height: "1px",
103 "margin-top": "-1px",
104 clip: "rect(1px, 1px, 1px, 1px)",
105 overflow: "hidden"
106 });
107 },
108 init: function () {
109 this.$region = $("<div>");
110 this.setAttributes();
111 this.setStyles();
112 $("body").append(this.$region);
113 this.domListener();
114 }
115 };
116
117 this.setup = (function () {
118 var original = self.setup;
119 return function () {
120 original.apply(this, arguments);
121 var inputId = self.accessibility.helpers.randomId(),
122 listboxId = self.accessibility.helpers.randomId();
123
124 self.$control.on("keydown", function (e) {
125 if (e.keyCode === KEY_RETURN) {
126
127 if (self.settings.openOnFocus) {
128 // This is the common case, where openOnFocus is true. When the user
129 // presses Enter to select an item, the default behavior is to close
130 // the dropdown and have the input lose focus. However, losing focus
131 // is poor behavior for accessibility.
132 //
133 // If `.focus()` is called after a selection is made, the default
134 // (when openOnFocus is true) is to show the dropdown, but this is
135 // annoying for sighted users, because they would expect the
136 // dropdown to not be visible after making a selection, and they
137 // then need to press Esc to make it go away.
138 //
139 // This workaround sets openOnFocus to false, then calls `.focus()`,
140 // then, after a delay, sets openOnFocus back to true. It seems that
141 // the `setTimeout()` must be called after `.focus()` for it to work
142 // reliably, even with a delay of up to 10. If it is called before
143 // `.focus()`, then after pressing Enter, the dropdown still
144 // appears. I think this is probably because `.focus()` in turn
145 // calls `setTimeout(, 0)` to do things, calling the functions in
146 // this order is necessary to ensure that the callbacks are invoked
147 // in the same order.
148 self.settings.openOnFocus = false;
149 self.focus();
150 setTimeout(function() { self.settings.openOnFocus = true; }, 0);
151 } else {
152 self.focus();
153 }
154 }
155 });
156
157 self.$control_input.attr({
158 role: "combobox",
159 "aria-expanded": "false",
160 haspopup: "listbox",
161 "aria-owns": listboxId,
162 "aria-label": self.$wrapper
163 .closest("[data-accessibility-selectize-label]")
164 .attr("data-accessibility-selectize-label")
165 });
166
167 self.$dropdown_content.attr({
168 role: "listbox",
169 id: listboxId
170 });
171 self.accessibility.liveRegion.init();
172 };
173 })();
174
175 this.destroy = (function () {
176 var original = self.destroy;
177 return function () {
178 self.accessibility.liveRegion.$region.remove();
179 return original.apply(this, arguments);
180 };
181 })();
182 });