-
Notifications
You must be signed in to change notification settings - Fork 0
/
clickout.coffee
60 lines (42 loc) · 1.73 KB
/
clickout.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# TODO tests
# TODO add example (cofee, full example, gif)
# TODO add js and minifield version
# TODO set minimum jQuery version
# TODO discuss about add feature to close by Esc key?
###
@description Allows to close element when click out of it (like background layer for modals but without it - you dont need to use `background` element that block page content)
@var target - click event target
@var containerClass - CSS class of container inside which opened element is present
@var container - container jQuery object
@var openedElSelector - selector for opened element (or elements), which can be closed.
@var closeFunc - function that close opened element (default: $.hide())
@shortcuts e - event, el - element
###
class Clickout
# set class vars and event handler
constructor: (options) ->
@containerClass = options.containerClass
return unless @present( $(@containerClass) )
@container = $(@containerClass)
@openedElSelector = options.openedElSelector
@closeFunc = jQuery.isFunction(options.close) || @defaultCloseFunc
$('body').on 'click', @clickHandler
clickHandler: (e) =>
@target = $(e.target)
return if @ignorableTarget()
openedEl = @container.find(@openedElSelector)
return unless @present( openedEl )
@closeFunc openedEl
targetIsActiveEl: ->
# Tag names are, by convention, returned capitalized.
@target.prop('tagName') == 'A'
# target or target parent has containerClass
targetIsContainer: ->
(@target.hasClass @containerClass) or (@target.closest(@containerClass).length > 0)
ignorableTarget: ->
@targetIsContaner() or @targetIsActiveEl()
present: (el) ->
el.length > 0
defaultCloseFunc: (el) ->
el.hide()
window.Clickout = Clickout