| 1 |
|
| 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 |
|
| 39 |
self.$control_input.attr("aria-expanded", "true"); |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 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 |
|
| 56 |
self.$control_input.attr("aria-expanded", "false"); |
| 57 |
self.$control_input.removeAttr("aria-activedescendant"); |
| 58 |
} |
| 59 |
} else { |
| 60 |
|
| 61 |
if ($target.hasClass("active")) { |
| 62 |
if (!!$target.attr("data-value")) { |
| 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 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 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 |
}); |