Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. App.Lib.GlobalEvents =
  2. ###
  3. This will trigger the callback whenever the user clicks on something outside
  4. the given element. If the user clicks on something insied `el`, it will not
  5. trigger the callback.
  6. You can give it many elements using jQuery's `#add`:
  7. $els = $('.my-element').add($('.some-other-element'))
  8. App.Lib.GlobalEvents.onClickedOutside $els, =>
  9. # do something
  10. ###
  11. onClickedOutside: (el, callback) ->
  12. $el = $(el)
  13.  
  14. # This get's executed when leaving the page using Turbolinks. In that case,
  15. # the whole body get's erased, together with our bindings, but in this case,
  16. # `document` stays the same so we have to manually clear the bindings.
  17. $(document).on 'turbolinks:before-cache', ->
  18. $(document).unbind '.app-global-event'
  19.  
  20. $(document).on 'mouseup.app-global-event', (e) =>
  21. return if $el.is(e.target) or $el.has(e.target).length isnt 0
  22. callback()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement