|
208 | 208 | nuget.configureFileInputButton = function (id) { |
209 | 209 | // File input buttons should respond to keyboard events. |
210 | 210 | $("#" + id).on("keypress", function (e) { |
211 | | - var code = (e.keyCode || e.which); |
212 | | - var isInteract = (code == 13 /*enter*/ || code == 32 /*space*/) && !e.altKey && !e.ctrlKey && !e.metaKey && !e.shiftKey; |
| 211 | + var code = e.keyCode || e.which; |
| 212 | + var isInteract = (code === 13 /*enter*/ || code === 32 /*space*/) && !e.altKey && !e.ctrlKey && !e.metaKey && !e.shiftKey; |
213 | 213 | if (isInteract) { |
214 | 214 | $(this).click(); |
215 | 215 | } |
216 | 216 | }); |
217 | | - } |
218 | | - |
219 | | - nuget.canElementBeTabbedTo = function (element) { |
220 | | - var isElement = function (type) { |
221 | | - return element.is(type); |
222 | | - } |
223 | | - |
224 | | - var hasAttribute = function (attributeName) { |
225 | | - var attribute = element.attr(attributeName); |
226 | | - return typeof attribute !== typeof undefined && attribute !== false; |
227 | | - } |
| 217 | + }; |
228 | 218 |
|
229 | | - if (hasAttribute("tabindex")) { |
230 | | - // Elements that have had their tabindex set to -1 cannot be tabbed to. |
231 | | - return element.attr("tabindex") !== "-1"; |
| 219 | + nuget.canElementBeFocused = function (element) { |
| 220 | + element = $(element); |
| 221 | + if (!element.is(':visible')) { |
| 222 | + return false; |
232 | 223 | } |
233 | 224 |
|
234 | 225 | // See https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Content_categories#Interactive_content |
235 | 226 | var alwaysInteractiveElements = ['a', 'button', 'details', 'embed', 'iframe', 'keygen', 'label', 'select', 'textarea']; |
236 | 227 | var i; |
237 | 228 | for (i = 0; i < alwaysInteractiveElements.length; i++) { |
238 | | - if (isElement(alwaysInteractiveElements[i])) { |
| 229 | + if (element.is(alwaysInteractiveElements[i])) { |
239 | 230 | return true; |
240 | 231 | } |
241 | 232 | } |
242 | 233 |
|
243 | | - return ((isElement("audio") && hasAttribute("controls")) || |
244 | | - (isElement("img") && hasAttribute("usemap")) || |
245 | | - (isElement("input") && element.attr("type") !== "hidden") || |
246 | | - (isElement("menu") && element.attr("type") !== "toolbar") || |
247 | | - (isElement("object") && hasAttribute("usemap")) || |
248 | | - (isElement("video") && hasAttribute("controls"))); |
249 | | - } |
250 | | - |
251 | | - nuget.getFirstChildThatCanBeTabbedTo = function (element) { |
252 | | - if (window.nuget.canElementBeTabbedTo(element)) { |
253 | | - return element; |
254 | | - } |
255 | | - |
256 | | - // If an element has its tabindex set to -1, none of its children can be tabbed to. |
257 | | - if (element.attr("tabindex") === "-1") { |
258 | | - return null; |
259 | | - } |
260 | | - |
261 | | - var i; |
262 | | - var children = element.children(); |
263 | | - for (i = 0; i < children.length; i++) { |
264 | | - var child = children.eq(i); |
265 | | - if (window.nuget.canElementBeTabbedTo(child)) { |
266 | | - return child; |
267 | | - } |
268 | | - |
269 | | - var childChild = window.nuget.getFirstChildThatCanBeTabbedTo(child); |
270 | | - if (childChild !== null) { |
271 | | - return childChild; |
272 | | - } |
273 | | - } |
| 234 | + return element.is("audio") && !!element.attr("controls") || |
| 235 | + element.is("img") && !!element.attr("usemap") || |
| 236 | + element.is("input") && element.attr("type") !== "hidden" || |
| 237 | + element.is("menu") && element.attr("type") !== "toolbar" || |
| 238 | + element.is("object") && !!element.attr("usemap") || |
| 239 | + element.is("video") && !!element.attr("controls"); |
| 240 | + }; |
274 | 241 |
|
275 | | - return null; |
276 | | - } |
| 242 | + nuget.canElementBeTabbedTo = function (element) { |
| 243 | + return window.nuget.canElementBeFocused(element) && $(element).attr('tabindex') !== "-1"; |
| 244 | + }; |
277 | 245 |
|
278 | 246 | // Source: https://stackoverflow.com/a/27568129/52749 |
279 | 247 | // Detects whether SVG is supported in the browser. |
|
365 | 333 | var $field = $("#AntiForgeryForm input[name=__RequestVerificationToken]"); |
366 | 334 | if (data instanceof FormData) |
367 | 335 | { |
368 | | - data.append($tokenKey, $field.val()) |
| 336 | + data.append($tokenKey, $field.val()); |
369 | 337 | } |
370 | 338 | else |
371 | 339 | { |
|
383 | 351 | } |
384 | 352 |
|
385 | 353 | return stringToFormat; |
386 | | - } |
| 354 | + }; |
387 | 355 |
|
388 | 356 | window.nuget = nuget; |
389 | 357 |
|
| 358 | + jQuery.extend(jQuery.expr[':'], { |
| 359 | + focusable: window.nuget.canElementBeFocused, |
| 360 | + tabbable: window.nuget.canElementBeTabbedTo |
| 361 | + }); |
| 362 | + |
390 | 363 | initializeJQueryValidator(); |
391 | 364 |
|
392 | 365 | $(function () { |
|
454 | 427 | }); |
455 | 428 |
|
456 | 429 | $(document).on('keydown', function (e) { |
457 | | - var code = (e.keyCode || e.which); |
| 430 | + var code = e.keyCode || e.which; |
458 | 431 | var isValidInputCharacter = |
459 | | - ((code >= 48 && code <= 57) // numbers 0-9 |
460 | | - || (code >= 64 && code <= 90) // letters a-z |
461 | | - || (code >= 96 && code <= 111) // numpad |
462 | | - || (code >= 186 && code <= 192) // ; = , - . / ` |
463 | | - || (code >= 219 && code <= 222)) // [\ ] ' |
| 432 | + (code >= 48 && code <= 57 // numbers 0-9 |
| 433 | + || code >= 64 && code <= 90 // letters a-z |
| 434 | + || code >= 96 && code <= 111 // numpad |
| 435 | + || code >= 186 && code <= 192 // ; = , - . / ` |
| 436 | + || code >= 219 && code <= 222) // [\ ] ' |
464 | 437 | && !e.altKey && !e.ctrlKey && !e.metaKey; |
465 | 438 |
|
466 | | - if (isValidInputCharacter && document.activeElement == document.body) { |
| 439 | + if (isValidInputCharacter && document.activeElement === document.body) { |
467 | 440 | var searchbox = $("#search"); |
468 | 441 | searchbox.focus(); |
469 | 442 | var currInput = searchbox.val(); |
|
475 | 448 | $("#skipToContent").on('click', function () { |
476 | 449 | // Focus on the first element that can be tabbed to inside the "skippedToContent" element. |
477 | 450 | var skippedToContent = $("#skippedToContent"); |
478 | | - var firstChildThatCanBeTabbedTo = window.nuget.getFirstChildThatCanBeTabbedTo(skippedToContent.first()); |
| 451 | + var firstChildThatCanBeTabbedTo = skippedToContent.find(':tabbable').first(); |
479 | 452 | if (firstChildThatCanBeTabbedTo !== null) { |
480 | 453 | firstChildThatCanBeTabbedTo.focus(); |
481 | 454 | } else { |
|
486 | 459 | }); |
487 | 460 | }); |
488 | 461 | }()); |
489 | | - |
0 commit comments