-
Notifications
You must be signed in to change notification settings - Fork 0
/
LoadingPlaceholder.coffee
167 lines (131 loc) · 3.83 KB
/
LoadingPlaceholder.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# Functions
_getDepthElements = (element, depth) ->
placeLoaders = [element]
i = 0
while i < depth
whatineed = []
for layer in placeLoaders
if layer.children.length > 0
placeLoaders = layer.children
i++
return placeLoaders
_destroyPlaceHolder = (placeholder) ->
placeholder.destroy()
_destroyMask = (currentLayer) ->
mask = currentLayer.parent
currentLayer.parent = mask.parent
currentLayer.x = mask.x
currentLayer.y = mask.y
delete currentLayer.states.placeholderOut
delete currentLayer.states.placeholderIn
delete currentLayer.placeholderParent
mask.destroy()
_startAnimateLoaded = (layer, delay) ->
currentLayer = layer.children[1]
placeholder = layer.children[0]
currentLayer.animate "placeholderOut",
delay: delay
placeholder.animate "placeholderOut",
delay: delay
currentLayer.on Events.AnimationEnd, ->
_destroyMask(currentLayer)
placeholder.on Events.AnimationEnd, ->
_destroyPlaceHolder(placeholder)
# Drawing
_drawTemplateLayer = (layer) ->
placeholder = new Layer
width: layer.width
height: layer.height
borderRadius: layer.borderRadius
backgroundColor: "#eeeeee"
x: 0
y: 0
opacity: 1
gradient = new Layer
name: "Gradient"
parent: placeholder
height: placeholder.height
width: placeholder.width *2
x: -placeholder.width
opacity: 0.7
style:
background: "linear-gradient(to right, #eeeeee 10%, #dddddd 25%, #dddddd 30%, #eeeeee 45%)"
animationLoading = new Animation gradient,
x: placeholder.width
options:
time: 0.7
animationLoading.start()
animationLoading.on Events.AnimationEnd, ->
Utils.delay 0.4, -> animationLoading.restart()
return placeholder
_showAnimation = (layer, placeholder, delay) ->
layer.states.placeholderIn =
y: layer.height
layer.states.placeholderOut =
y: 0
options:
time: 0.2
curve: Bezier(0.25, 0.1, 0.25, 1)
delay: delay
placeholder.states.placeholderOut =
opacity: 0
options:
time: 0.2
_createPlaceHolders = (placeLoaders, customElement, customAnimation) ->
delay = 0
for layer in placeLoaders
layer.off(Events.AnimationEnd)
mask = new Layer
name: "placeholder"
parent: layer.parent
width: layer.width
height: layer.height
backgroundColor: "transparent"
x: layer.x
y: layer.y
clip: true
opacity: 1
placeholder = customElement(layer)
placeholder.parent = mask
layer.x = 0
layer.y = 0
layer.parent = mask
layer.placeholderParent = true
customAnimation(layer, placeholder, delay)
layer.stateSwitch("placeholderIn")
delay = delay + 0.1
# Methods
Layer::placeholder = (options={}) ->
@depth = options.depth ? 0
placeLoaders = _getDepthElements(@, @depth)
if options.customElement
customElement = options.customElement
else
customElement = exports.defaultElement
if options.customAnimation
customAnimation = options.customAnimation
else
customAnimation = exports.defaultAnimation
_createPlaceHolders placeLoaders, customElement, customAnimation
Layer::loaded = () ->
if @placeholderParent
placeLoaders = [@.parent]
else
layers = @descendants
placeLoaders = _.filter layers, (layer) ->
if layer.name is "placeholder" then true
for layer in placeLoaders
_startAnimateLoaded(layer)
exports.loadedAll = (options={}) ->
delay = options.delay ? 0.5
layers = Framer.CurrentContext._layers
placeLoaders = _.filter layers, (layer) ->
if layer.name is "placeholder" then true
newDelay = delay
for layer in placeLoaders
_startAnimateLoaded(layer, newDelay)
newDelay = newDelay + delay
exports.defaultElement = (layer) ->
return _drawTemplateLayer(layer)
exports.defaultAnimation = (layer, placeholder, delay) ->
return _showAnimation(layer, placeholder, delay)