This repository was archived by the owner on Jan 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathprogress.coffee
More file actions
218 lines (171 loc) · 5.02 KB
/
progress.coffee
File metadata and controls
218 lines (171 loc) · 5.02 KB
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# Default progress options
Router.configure
progress : true
progressDebug : false
progressDelay : false
progressSpinner : true
progressTick : true
# Used to debug the package, if progressDebug is true
debug = -> console.log.apply console, arguments if Router.current().lookupOption 'progressDebug'
Template.__IronRouterProgress__.created = ->
self = @
@ticker = false
@delay = false
@started = false
@loading = new ReactiveVar false
@spinner = new ReactiveVar false
@done = new ReactiveVar false
@percent = new ReactiveVar false
@functions =
reset : (element) ->
debug 'Reset'
self.functions.stop()
# Reset our variables
self.loading.set false
self.done.set false
self.percent.set 0
self.started = false
element.offsetWidth = element.offsetWidth if element
self
start : (element) ->
debug 'Start'
# Reset our progress
self.functions.reset element
# Update the spinner status, if it changed
self.spinner.set Router.current().lookupOption('progressSpinner') or false
self.loading.set true
# If we have a delay, wait with the progress
delay = Router.current().lookupOption 'progressDelay'
if delay > 0
debug 'Delayed'
self.delay = Meteor.setTimeout ->
self.started = true
self.functions.progress()
self.functions.tick()
, delay
else
debug 'Not delayed'
self.started = true
self.functions.progress()
self.functions.tick()
self
progress : (progress = false) ->
debug 'Progress'
# XX We need a better random number generation here
percent = self.percent.get()
percentNew = percent + if progress then progress else (100 - percent) * (Math.random() * 0.45 + 0.05) | 0
if percentNew >= 100
self.functions.done()
else
self.percent.set percentNew
self.functions.tick()
self
tick : ->
debug 'Tick'
if Router.current().lookupOption 'progressTick'
debug 'starting new ticker'
if self.ticker
Meteor.clearTimeout self.ticker
self.ticker = false
self.ticker = Meteor.setTimeout ->
self.ticker = false
self.functions.progress()
, Math.random() * 750 + 750
else
debug 'Not starting ticker'
self
done : ->
debug 'Done'
self.functions.stop()
if not self.started
self.functions.reset()
else
_.defer ->
self.done.set true
self.loading.set true
self.percent.set 100
self
stop : ->
debug 'Stop'
# Clear the timers, if we have any
if self.ticker
Meteor.clearTimeout self.ticker
self.ticker = false
if self.delay
Meteor.clearTimeout self.delay
self.delay = false
self
Router.load ->
debug 'IR:load'
element = self.find '*'
self.functions.start element
@next()
@
Router.unload ->
debug 'IR:unload'
self.functions.reset()
@
Router.onRun ->
debug 'IR:run'
self.loading.set true
@next()
@
Router.onRerun ->
debug 'IR:re-run'
@next()
@
Router.onBeforeAction ->
debug 'IR:before'
if @ready()
self.functions.done()
self.functions.stop()
else
self.functions.progress()
@next()
@
Router.onAfterAction ->
debug 'IR:after'
@
Router.onStop ->
debug 'IR:stop'
self.functions.reset()
@
Template.__IronRouterProgress__.helpers
data : -> Template.instance()
template : ->
# If progress is disabled in general, don't show a template
return null if not Router.current()?.lookupOption 'progress'
if Template.instance().loading.get() then '__IronRouterProgressDefault__' else null
Template.__IronRouterProgressDefault__.rendered = ->
# Used for the CSS reset
@element = @$ '#iron-router-progress'
Template.__IronRouterProgressDefault__.helpers
cssClass : ->
classes = []
classes.push 'loading' if @loading.get()
classes.push 'spinner' if @spinner.get()
classes.push 'done' if @done.get()
classes.join ' '
cssStyle : ->
styles = []
styles.push "width:#{@percent.get()}%" if @percent.get()
styles.join ';'
Template.__IronRouterProgressDefault__.events
'transitionend #iron-router-progress, webkitTransitionEnd #iron-router-progress, oTransitionEnd #iron-router-progress, otransitionend #iron-router-progress, MSTransitionEnd #iron-router-progress' : (e, template) ->
# Only reset, if this is the last transition, and that it's not a psuedo selector, such as `:before` and `:after`
# Due to the open nature, of the CSS, I want people to be able to do whatever they like, and as such
# simply expecting opacity to reach zero, or specific propertyName to execute won't suffice
# A more elegant solution should be added, as not all browsers may support transition-property
# witout their vendor prefixes
if e.originalEvent.pseudoElement is '' and e.originalEvent.propertyName is _.last template.element.css('transition-property').split ', '
debug 'transitionend'
data = Template.currentData()
data.done.set false
data.loading.set false
data.percent.set false
# Prepare our DOM-element
Meteor.startup ->
layout = new Iron.Layout
template : '__IronRouterProgress__'
layout.insert
el : document.body