Repository navigation
Expand file tree
/
Copy pathRouter.coffee
More file actions
106 lines (75 loc) · 2.82 KB
/
Copy pathRouter.coffee
File metadata and controls
106 lines (75 loc) · 2.82 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
@import "cafe/Location"
package "cafe"
Router: class Router
@contentLoaded: no
@beforeRunCallbacks: []
@afterRunCallbacks: []
@beforeAllCallbacks: []
@afterAllCallbacks: []
@aliasesJSON: null
@aliases: (json) ->
@aliasesJSON = json
@beforeRun: (callback) ->
@beforeRunCallbacks.push(callback)
@afterRun: (callback) ->
@afterRunCallbacks.push(callback)
@beforeAll: (callback) ->
@beforeAllCallbacks.push(callback)
@afterAll: (callback) ->
@afterAllCallbacks.push(callback)
@runModule: (className, Class, callbacks) ->
wait = ( cafe.External.wait?() if cafe.External? ) or (f) -> f.call()
wait =>
beforeRun?() for beforeRun in callbacks.beforeRun
module = new Class()
module.name = className
module.run()
afterRun?() for afterRun in callbacks.afterRun
afterAll?() for afterAll in callbacks.afterAll
return
@dispatch: (defaultModule, modules) ->
callbacks =
beforeAll: @beforeAllCallbacks.splice(0, @beforeAllCallbacks.length)
beforeRun: @beforeRunCallbacks.splice(0, @beforeRunCallbacks.length)
afterRun : @afterRunCallbacks.splice(0, @afterRunCallbacks.length)
afterAll : @afterAllCallbacks.splice(0, @afterAllCallbacks.length)
section = Location.getSection() or defaultModule
wait = ( cafe.External.wait?() if cafe.External? ) or (f) -> f.call()
wait ->
beforeAll?() for beforeAll in callbacks.beforeAll
return
run = no
matchModule = (section, className) -> (section + "Module").toLowerCase() is className.toLowerCase()
for className, Class of modules
unless run = matchModule(section, className)
for module, aliases of @aliasesJSON
break if (section in aliases) and run = matchModule(module, className)
if run
Router.onDOMContentLoaded -> Router.runModule(className, Class, callbacks)
break
unless run
Router.onDOMContentLoaded ->
wait = ( cafe.External.wait?() if cafe.External? ) or (f) -> f.call()
wait ->
afterAll?() for afterAll in callbacks.afterAll
return
@onDOMContentLoaded: (callback) ->
return unless typeof callback is "function"
if Router.contentLoaded or yes
callback()
return
if addEventListener?
addEventListener(
"DOMContentLoaded"
->
callback.apply(null, arguments)
removeEventListener("DOMContentLoaded", arguments.callee, no)
no)
else if attachEvent?
attachEvent(
"onload"
->
callback.apply(null, arguments)
detachEvent("onload", arguments.callee)
)
Router.onDOMContentLoaded -> Router.contentLoaded = yes