Permalink
Browse files

add new jquery.trap.js

  • Loading branch information...
1 parent efcbc30 commit 4943b6dc28317c93301c10e07e8c12730d805385 @julienw committed May 10, 2012
Showing with 136 additions and 123 deletions.
  1. +136 −123 jquery.trap.js
View
@@ -23,137 +23,137 @@ IS" AND ANY EXPRESS OR IMPLIED WARRANTIES ARE DISCLAIMED.
*/
/*jshint boss: true, bitwise: true, curly: true, expr: true, newcap: true, noarg: true, nonew: true, latedef: true, regexdash: true */
-
- var DATA_ISTRAPPING_KEY = "trap.isTrapping";
+
+ var DATA_ISTRAPPING_KEY = "trap.isTrapping";
- function onkeypress(e) {
- if (e.keyCode === 9) {
- var goReverse = !!(e.shiftKey);
- if (processTab(this, e.target, goReverse)) {
- e.preventDefault();
- e.stopPropagation();
- }
- }
- }
-
- // will return true if we could process the tab event
- // otherwise, return false
- function processTab(container, elt, goReverse) {
- var $focussable = getFocusableElementsInContainer(container),
- curElt = elt,
- index, nextIndex, prevIndex, lastIndex;
-
- do {
-
- index = $focussable.index(curElt);
- nextIndex = index + 1;
- prevIndex = index - 1;
- lastIndex = $focussable.length - 1;
+ function onkeypress(e) {
+ if (e.keyCode === 9) {
+ var goReverse = !!(e.shiftKey);
+ if (processTab(this, e.target, goReverse)) {
+ e.preventDefault();
+ e.stopPropagation();
+ }
+ }
+ }
+
+ // will return true if we could process the tab event
+ // otherwise, return false
+ function processTab(container, elt, goReverse) {
+ var $focussable = getFocusableElementsInContainer(container),
+ curElt = elt,
+ index, nextIndex, prevIndex, lastIndex;
+
+ do {
+
+ index = $focussable.index(curElt);
+ nextIndex = index + 1;
+ prevIndex = index - 1;
+ lastIndex = $focussable.length - 1;
- switch(index) {
- case -1:
- return false; // that's strange, let the browser do its job
- case 0:
- prevIndex = lastIndex;
- break;
- case lastIndex:
- nextIndex = 0;
- break;
- }
-
- if (goReverse) {
- nextIndex = prevIndex;
- }
-
- curElt = $focussable.get(nextIndex);
+ switch(index) {
+ case -1:
+ return false; // that's strange, let the browser do its job
+ case 0:
+ prevIndex = lastIndex;
+ break;
+ case lastIndex:
+ nextIndex = 0;
+ break;
+ }
+
+ if (goReverse) {
+ nextIndex = prevIndex;
+ }
+
+ curElt = $focussable.get(nextIndex);
// IE sometimes throws when an element is not visible
try {
curElt.focus();
} catch(e) {
}
-
- } while (elt === elt.ownerDocument.activeElement);
+
+ } while (elt === elt.ownerDocument.activeElement);
- return true;
- }
-
- function filterKeepSpeciallyFocusable() {
- return this.tabIndex > 0;
- }
-
- function filterKeepNormalElements() {
- return !this.tabIndex; // true if no tabIndex or tabIndex == 0
- }
-
- function sortFocusable(a, b) {
- return (a.t - b.t) || (a.i - b.i);
- }
-
- function getFocusableElementsInContainer(container) {
- var $container = $(container);
- var result = [],
- cnt = 0;
+ return true;
+ }
+
+ function filterKeepSpeciallyFocusable() {
+ return this.tabIndex > 0;
+ }
+
+ function filterKeepNormalElements() {
+ return !this.tabIndex; // true if no tabIndex or tabIndex == 0
+ }
+
+ function sortFocusable(a, b) {
+ return (a.t - b.t) || (a.i - b.i);
+ }
+
+ function getFocusableElementsInContainer(container) {
+ var $container = $(container);
+ var result = [],
+ cnt = 0;
fixIndexSelector.enable && fixIndexSelector.enable();
-
- // leaving away command and details for now
- $container.find("a[href], link[href], [draggable=true], [contenteditable=true], :input:enabled, [tabindex=0]")
- .filter(":visible")
- .filter(filterKeepNormalElements)
- .each(function(i, val) {
- result.push({
- v: val, // value
- t: 0, // tabIndex
- i: cnt++ // index for stable sort
- });
- });
-
- $container
- .find("[tabindex]")
- .filter(":visible")
- .filter(filterKeepSpeciallyFocusable)
- .each(function(i, val) {
- result.push({
- v: val, // value
- t: val.tabIndex, // tabIndex
- i: cnt++ // index
- });
- });
+
+ // leaving away command and details for now
+ $container.find("a[href], link[href], [draggable=true], [contenteditable=true], :input:enabled, [tabindex=0]")
+ .filter(":visible")
+ .filter(filterKeepNormalElements)
+ .each(function(i, val) {
+ result.push({
+ v: val, // value
+ t: 0, // tabIndex
+ i: cnt++ // index for stable sort
+ });
+ });
+
+ $container
+ .find("[tabindex]")
+ .filter(":visible")
+ .filter(filterKeepSpeciallyFocusable)
+ .each(function(i, val) {
+ result.push({
+ v: val, // value
+ t: val.tabIndex, // tabIndex
+ i: cnt++ // index
+ });
+ });
fixIndexSelector.disable && fixIndexSelector.disable();
-
- result = $.map(result.sort(sortFocusable), // needs stable sort
- function(val) {
- return val.v;
- }
- );
-
-
- return $(result);
-
- }
-
- function trap() {
- this.keydown(onkeypress);
- this.data(DATA_ISTRAPPING_KEY, true);
- return this;
- }
-
- function untrap() {
- this.unbind('keydown', onkeypress);
- this.removeData(DATA_ISTRAPPING_KEY);
- return this;
- }
-
- function isTrapping() {
- return !!this.data(DATA_ISTRAPPING_KEY);
- }
-
- $.fn.extend({
- trap: trap,
- untrap: untrap,
- isTrapping: isTrapping
- });
+
+ result = $.map(result.sort(sortFocusable), // needs stable sort
+ function(val) {
+ return val.v;
+ }
+ );
+
+
+ return $(result);
+
+ }
+
+ function trap() {
+ this.keydown(onkeypress);
+ this.data(DATA_ISTRAPPING_KEY, true);
+ return this;
+ }
+
+ function untrap() {
+ this.unbind('keydown', onkeypress);
+ this.removeData(DATA_ISTRAPPING_KEY);
+ return this;
+ }
+
+ function isTrapping() {
+ return !!this.data(DATA_ISTRAPPING_KEY);
+ }
+
+ $.fn.extend({
+ trap: trap,
+ untrap: untrap,
+ isTrapping: isTrapping
+ });
// jQuery 1.6.x tabindex attr hooks management
// this triggers problems for tabindex attribute
@@ -163,13 +163,26 @@ IS" AND ANY EXPRESS OR IMPLIED WARRANTIES ARE DISCLAIMED.
var fixIndexSelector = {};
(function() {
var tabindexKey = "tabindex";
- var attrHooks = $.attrHooks,
- tabindexAttrHook = attrHooks[tabindexKey],
- sizzleAttrHandle = $.expr.attrHandle;
+ var sizzleAttrHandle = $.expr.attrHandle;
+ // this function comes directly from jQuery
+ // we have to put it here if we want to support jQuery < 1.6 which
+ // doesn't have an attrHooks object to reference.
+ function getTabindexAttr(elem) {
+ // elem.tabIndex doesn't always return the correct value when it hasn't been explicitly set
+ // http://fluidproject.org/blog/2008/01/09/getting-setting-and-removing-tabindex-values-with-javascript/
+ var attributeNode = elem.getAttributeNode("tabIndex");
+
+ return attributeNode && attributeNode.specified ?
+ parseInt( attributeNode.value, 10 ) :
+ rfocusable.test( elem.nodeName ) || rclickable.test( elem.nodeName ) && elem.href ?
+ 0 :
+ undefined;
+ }
+
function fixSizzleAttrHook() {
// in jQ <= 1.6.x, we add to Sizzle the attrHook from jQuery's attr method
- sizzleAttrHandle[tabindexKey] = sizzleAttrHandle.tabIndex = attrHooks.tabIndex.get;
+ sizzleAttrHandle[tabindexKey] = sizzleAttrHandle.tabIndex = getTabindexAttr;
}
function unfixSizzleAttrHook() {

0 comments on commit 4943b6d

Please sign in to comment.