-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathoverlay.coffee
103 lines (95 loc) · 2.64 KB
/
overlay.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
{noop,isString,clone} = require("./_helpers")
overlay = document.createElement "div"
s = overlay.style
s.position = "fixed"
s.top = "-10px"
s.left = 0
s.right = 0
s.height = "120vh"
s.willChange = "opacity"
stack = []
close = (e) ->
if not e.defaultPrevented and (e.type == "click" or e.which == 27)
if (li = getLastItem())? and not li.keepOpen
li.close()
e.preventDefault()
overlay.addEventListener "click", close
getLastItem = (steps = 1) -> stack[stack.length-steps]
attach = ->
document.body.appendChild overlay
document.addEventListener "keyup", close
detach = ->
document.body.removeChild overlay
document.removeEventListener "keyup", close
dEl = document.documentElement
getAnimateObj = (o, li, getViewportSize) ->
if li?
li.animation?.stop?()
else
li = opacity: 0, done: detach, allowScroll: true
attach() if o.open
if o.open
duration = o.durationIn || 300
source = li
target = o
else
duration = o.durationOut || 200
source = o
target = li
s = dEl.style
if target.allowScroll
s.overflow = null
s.marginRight = null
else
s.marginRight = getViewportSize().width - dEl.clientWidth + "px"
s.overflow = "hidden"
target.animation =
animate: o.animate
el: overlay
style:
opacity: [source.opacity, target.opacity]
init:
backgroundColor: target.color || "black"
zIndex: target.zIndex
duration: duration
done: target.done
return target.animation
module.exports =
mixins:[
require("./animate")
require("./getViewportSize")
require("./path")
require("./parseActive")
]
methods:
$overlay: (o) ->
o ?= clone(@overlay) || {}
o.activate = ->
for k,v of {zIndex: 995, opacity: 0.5, keepOpen: false, animate: true}
if (val = o[k])?
o[k] = @$path.resolveValue(val)
else
o[k] = v
o.open = true
li = getLastItem()
stack.push o
o.close = =>
o.close = noop
return unless o.open
o.open = false
@$path.resolveValue(o.onClose)?.call(@)
i = stack.indexOf o
if i == stack.length - 1
@$animate getAnimateObj(stack.pop(), getLastItem(), @getViewportSize)
else
stack.splice i,1
o.cancel = ->
o.animation?.toEnd?()
o.zIndex = Math.max o.zIndex, li.zIndex+5 if li?
@$path.resolveValue(o.onOpen)?.call(@,o.zIndex)
@$animate getAnimateObj(o, li, @getViewportSize)
return o.close
return @$parseActive(o)
connectedCallback: ->
if @_isFirstConnect and @overlay
@$overlay(clone(@overlay))