Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 20 additions & 8 deletions mousetrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -420,16 +420,16 @@
};
}

function _belongsTo(element, ancestor) {
function _belongsTo(element, ancestors) {
if (element === null || element === document) {
return false;
}

if (element === ancestor) {
if (ancestors.indexOf(element) !== -1) {
return true;
}

return _belongsTo(element.parentNode, ancestor);
return _belongsTo(element.parentNode, ancestors);
}

function Mousetrap(targetElement) {
Expand All @@ -446,7 +446,7 @@
*
* @type {Element}
*/
self.target = targetElement;
self.targets = [];

/**
* a list of all the callbacks setup via Mousetrap.bind()
Expand Down Expand Up @@ -885,10 +885,22 @@
}
};

/**
* add a target to listen for events
*
* @param {Object} targetElement - DOM node
* @returns void
*/
self.addTarget = function(targetElement) {
self.targets.push(targetElement);
_addEvent(targetElement, 'keypress', _handleKeyEvent);
_addEvent(targetElement, 'keydown', _handleKeyEvent);
_addEvent(targetElement, 'keyup', _handleKeyEvent);
return self;
};

// start!
_addEvent(targetElement, 'keypress', _handleKeyEvent);
_addEvent(targetElement, 'keydown', _handleKeyEvent);
_addEvent(targetElement, 'keyup', _handleKeyEvent);
self.addTarget(targetElement);
}

/**
Expand Down Expand Up @@ -978,7 +990,7 @@
return false;
}

if (_belongsTo(element, self.target)) {
if (_belongsTo(element, self.targets)) {
return false;
}

Expand Down
12 changes: 1 addition & 11 deletions mousetrap.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.