1
0
mirror of synced 2024-11-22 21:16:10 +03:00

Merge pull request #1816 from funkjedi/accessibility

Accessibility updates based on jQuery UI Autocomplete
This commit is contained in:
Kevin Brown 2014-01-19 18:40:50 -08:00
commit 9eb12a8906
3 changed files with 52 additions and 6 deletions

View File

@ -297,6 +297,17 @@ Version: @@ver@@ Timestamp: @@timestamp@@
background-position: -18px 1px; background-position: -18px 1px;
} }
.select2-hidden-accessible {
border: 0;
clip: rect(0 0 0 0);
height: 1px;
margin: -1px;
overflow: hidden;
padding: 0;
position: absolute;
width: 1px;
}
/* results */ /* results */
.select2-results { .select2-results {
max-height: 200px; max-height: 200px;

View File

@ -677,6 +677,13 @@ the specific language governing permissions and limitations under the Apache Lic
this.container = this.createContainer(); this.container = this.createContainer();
this.liveRegion = $("<span>", {
role: "status",
"aria-live": "polite"
})
.addClass("select2-hidden-accessible")
.appendTo(document.body);
this.containerId="s2id_"+(opts.element.attr("id") || "autogen"+nextUid()).replace(/([;&,\-\.\+\*\~':"\!\^#$%@\[\]\(\)=>\|])/g, '\\$1'); this.containerId="s2id_"+(opts.element.attr("id") || "autogen"+nextUid()).replace(/([;&,\-\.\+\*\~':"\!\^#$%@\[\]\(\)=>\|])/g, '\\$1');
this.containerSelector="#"+this.containerId; this.containerSelector="#"+this.containerId;
this.container.attr("id", this.containerId); this.container.attr("id", this.containerId);
@ -804,6 +811,7 @@ the specific language governing permissions and limitations under the Apache Lic
if (select2 !== undefined) { if (select2 !== undefined) {
select2.container.remove(); select2.container.remove();
select2.liveRegion.remove();
select2.dropdown.remove(); select2.dropdown.remove();
element element
.removeClass("select2-offscreen") .removeClass("select2-offscreen")
@ -861,7 +869,7 @@ the specific language governing permissions and limitations under the Apache Lic
opts = $.extend({}, { opts = $.extend({}, {
populateResults: function(container, results, query) { populateResults: function(container, results, query) {
var populate, id=this.opts.id; var populate, id=this.opts.id, liveRegion=this.liveRegion;
populate=function(results, container, depth) { populate=function(results, container, depth) {
@ -910,6 +918,8 @@ the specific language governing permissions and limitations under the Apache Lic
node.data("select2-data", result); node.data("select2-data", result);
container.append(node); container.append(node);
} }
liveRegion.text(opts.formatMatches(results.length));
}; };
populate(results, container, 0); populate(results, container, 0);
@ -1484,6 +1494,8 @@ the specific language governing permissions and limitations under the Apache Lic
this.ensureHighlightVisible(); this.ensureHighlightVisible();
this.liveRegion.text(choice.text());
data = choice.data("select2-data"); data = choice.data("select2-data");
if (data) { if (data) {
this.opts.element.trigger({ type: "select2-highlight", val: this.id(data), choice: data }); this.opts.element.trigger({ type: "select2-highlight", val: this.id(data), choice: data });
@ -1591,6 +1603,12 @@ the specific language governing permissions and limitations under the Apache Lic
function postRender() { function postRender() {
search.removeClass("select2-active"); search.removeClass("select2-active");
self.positionDropdown(); self.positionDropdown();
if (results.find('.select2-no-results,.select2-selection-limit,.select2-searching').length) {
self.liveRegion.text(results.text());
}
else {
self.liveRegion.text(self.opts.formatMatches(results.find('.select2-result-selectable').length));
}
} }
function render(html) { function render(html) {
@ -1828,9 +1846,11 @@ the specific language governing permissions and limitations under the Apache Lic
" <span class='select2-chosen'>&nbsp;</span><abbr class='select2-search-choice-close'></abbr>", " <span class='select2-chosen'>&nbsp;</span><abbr class='select2-search-choice-close'></abbr>",
" <span class='select2-arrow' role='presentation'><b role='presentation'></b></span>", " <span class='select2-arrow' role='presentation'><b role='presentation'></b></span>",
"</a>", "</a>",
"<label for='' class='select2-offscreen'></label>",
"<input class='select2-focusser select2-offscreen' type='text' aria-haspopup='true' role='button' />", "<input class='select2-focusser select2-offscreen' type='text' aria-haspopup='true' role='button' />",
"<div class='select2-drop select2-display-none'>", "<div class='select2-drop select2-display-none'>",
" <div class='select2-search'>", " <div class='select2-search'>",
" <label for='' class='select2-offscreen'></label>",
" <input type='text' autocomplete='off' autocorrect='off' autocapitalize='off' spellcheck='false' class='select2-input' role='combobox' aria-expanded='true'", " <input type='text' autocomplete='off' autocorrect='off' autocapitalize='off' spellcheck='false' class='select2-input' role='combobox' aria-expanded='true'",
" aria-autocomplete='list' />", " aria-autocomplete='list' />",
" </div>", " </div>",
@ -1960,7 +1980,10 @@ the specific language governing permissions and limitations under the Apache Lic
// rewrite labels from original element to focusser // rewrite labels from original element to focusser
this.focusser.attr("id", "s2id_autogen"+idSuffix); this.focusser.attr("id", "s2id_autogen"+idSuffix);
elementLabel = $("label[for='" + this.opts.element.attr("id") + "']") elementLabel = $("label[for='" + this.opts.element.attr("id") + "']");
this.focusser.prev()
.text(elementLabel.text())
.attr('for', this.focusser.attr('id')); .attr('for', this.focusser.attr('id'));
// Ensure the original element retains an accessible name // Ensure the original element retains an accessible name
@ -1969,6 +1992,13 @@ the specific language governing permissions and limitations under the Apache Lic
this.focusser.attr("tabindex", this.elementTabIndex); this.focusser.attr("tabindex", this.elementTabIndex);
// write label for search field using the label from the focusser element
this.search.attr("id", this.focusser.attr('id') + '_search');
this.search.prev()
.text($("label[for='" + this.focusser.attr('id') + "']").text())
.attr('for', this.search.attr('id'));
this.search.on("keydown", this.bind(function (e) { this.search.on("keydown", this.bind(function (e) {
if (!this.isInterfaceEnabled()) return; if (!this.isInterfaceEnabled()) return;
@ -2410,6 +2440,7 @@ the specific language governing permissions and limitations under the Apache Lic
}).html([ }).html([
"<ul class='select2-choices'>", "<ul class='select2-choices'>",
" <li class='select2-search-field'>", " <li class='select2-search-field'>",
" <label for='' class='select2-offscreen'></label>",
" <input type='text' autocomplete='off' autocorrect='off' autocapitalize='off' spellcheck='false' class='select2-input'>", " <input type='text' autocomplete='off' autocorrect='off' autocapitalize='off' spellcheck='false' class='select2-input'>",
" </li>", " </li>",
"</ul>", "</ul>",
@ -2521,7 +2552,9 @@ the specific language governing permissions and limitations under the Apache Lic
// rewrite labels from original element to focusser // rewrite labels from original element to focusser
this.search.attr("id", "s2id_autogen"+nextUid()); this.search.attr("id", "s2id_autogen"+nextUid());
$("label[for='" + this.opts.element.attr("id") + "']")
this.search.prev()
.text($("label[for='" + this.opts.element.attr("id") + "']").text())
.attr('for', this.search.attr('id')); .attr('for', this.search.attr('id'));
this.search.on("input paste", this.bind(function() { this.search.on("input paste", this.bind(function() {
@ -3257,6 +3290,7 @@ the specific language governing permissions and limitations under the Apache Lic
}, },
formatResultCssClass: function(data) {return data.css;}, formatResultCssClass: function(data) {return data.css;},
formatSelectionCssClass: function(data, container) {return undefined;}, formatSelectionCssClass: function(data, container) {return undefined;},
formatMatches: function (matches) { return matches + " results are available, use up and down arrow keys to navigate."; },
formatNoMatches: function () { return "No matches found"; }, formatNoMatches: function () { return "No matches found"; },
formatInputTooShort: function (input, min) { var n = min - input.length; return "Please enter " + n + " or more character" + (n == 1? "" : "s"); }, formatInputTooShort: function (input, min) { var n = min - input.length; return "Please enter " + n + " or more character" + (n == 1? "" : "s"); },
formatInputTooLong: function (input, max) { var n = input.length - max; return "Please delete " + n + " character" + (n == 1? "" : "s"); }, formatInputTooLong: function (input, max) { var n = input.length - max; return "Please delete " + n + " character" + (n == 1? "" : "s"); },

View File

@ -7,6 +7,7 @@
"use strict"; "use strict";
$.extend($.fn.select2.defaults, { $.extend($.fn.select2.defaults, {
formatMatches: function (matches) { return matches + " results are available, use up and down arrow keys to navigate."; },
formatNoMatches: function () { return "No matches found"; }, formatNoMatches: function () { return "No matches found"; },
formatInputTooShort: function (input, min) { var n = min - input.length; return "Please enter " + n + " more character" + (n == 1 ? "" : "s"); }, formatInputTooShort: function (input, min) { var n = min - input.length; return "Please enter " + n + " more character" + (n == 1 ? "" : "s"); },
formatInputTooLong: function (input, max) { var n = input.length - max; return "Please delete " + n + " character" + (n == 1 ? "" : "s"); }, formatInputTooLong: function (input, max) { var n = input.length - max; return "Please delete " + n + " character" + (n == 1 ? "" : "s"); },