Created
December 12, 2011 01:45
-
-
Save inossidabile/1464161 to your computer and use it in GitHub Desktop.
Small Coffee class allowing to register click on anywhere but selector
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@GlobalClick = | |
initialize: () -> | |
this.items = {} | |
this.elements = {} | |
$('body').click (event) -> GlobalClick.perform(event) | |
bind: (selector, action) -> | |
this.items[selector] = action | |
this.elements[selector] = [] | |
$(selector).each (i) -> | |
GlobalClick.elements[selector].push this | |
perform: (event) -> | |
target = event.target | |
is_parent = this.is_parent | |
for selector, action of this.items | |
do (selector, action) -> | |
for element in GlobalClick.elements[selector] | |
if !(target == element || is_parent(element, target)) | |
action(event.target, element) | |
is_parent: (parent, child) -> | |
node = child.parentNode | |
loop | |
break if node == null | |
return true if node == parent | |
node = node.parentNode | |
return false |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment