-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathng-slide-down.coffee
89 lines (80 loc) · 2.44 KB
/
ng-slide-down.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
"use strict"
angular.module("ng-slide-down", []).directive "ngSlideDown", ($timeout )->
getTemplate = (tElement, tAttrs)->
if tAttrs.lazyRender != undefined
"<div ng-if='lazyRender' ng-transclude></div>"
else
"<div ng-transclude></div>"
link = (scope, element, attrs, ctrl, transclude) ->
duration = attrs.duration || 1
timingFunction = attrs.timingFunction || "ease-in-out"
elementScope = element.scope()
emitOnClose = attrs.emitOnClose
onClose = attrs.onClose
lazyRender = attrs.lazyRender != undefined
closePromise = null
openPromise = null
getHeight = (passedScope)->
height = 0
children = element.children()
for c in children
height += c.clientHeight
"#{height}px"
show = ()->
$timeout.cancel(closePromise) if closePromise
scope.lazyRender = true if lazyRender
$timeout ->
$timeout.cancel(openPromise) if openPromise
element.css {
height: getHeight()
}
$timeout ->
element.css {
overflow: "hidden"
transitionProperty: "height"
transitionDuration: "#{duration}s"
transitionTimingFunction: timingFunction
}
openPromise = $timeout ()->
element.css {
overflow: "visible"
transition: "none",
height: "auto"
}
, duration*1000, false
, 0, false
,0, false
hide = ()->
$timeout.cancel(openPromise) if openPromise
element.css {
overflow: "hidden"
transitionProperty: "height"
transitionDuration: "#{duration}s"
transitionTimingFunction: timingFunction
height: '0px'
}
if emitOnClose || onClose || lazyRender
closePromise = $timeout ()->
scope.$emit emitOnClose, {} if emitOnClose
elementScope.$eval(onClose) if onClose
scope.lazyRender = false if lazyRender
, duration*1000
scope.$watch 'expanded', (value, oldValue)->
if value
$timeout show
else
if value?
element.css {
height: getHeight()
}
element[0].clientHeight # Force reflow so the animation triggers reliably
$timeout hide, 0, false
return {
restrict: 'A'
scope: {
expanded: '=ngSlideDown'
}
transclude: true
link: link
template: (tElement, tAttrs)-> getTemplate(tElement, tAttrs)
}