-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Expand file tree
/
Copy pathrouter.coffee
More file actions
161 lines (133 loc) · 4.13 KB
/
router.coffee
File metadata and controls
161 lines (133 loc) · 4.13 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
class app.Router
$.extend @prototype, Events
@routes: [
['*', 'before' ]
['/', 'root' ]
['/settings', 'settings' ]
['/offline', 'offline' ]
['/about', 'about' ]
['/news', 'news' ]
['/help', 'help' ]
['/:doc-:type/', 'type' ]
['/:doc/', 'doc' ]
['/:doc/:path(*)', 'entry' ]
['*', 'notFound' ]
]
constructor: ->
for [path, method] in @constructor.routes
page path, @[method].bind(@)
@setInitialPath()
start: ->
page.start()
return
show: (path) ->
page.show(path)
return
triggerRoute: (name) ->
@trigger name, @context
@trigger 'after', name, @context
return
before: (context, next) ->
previousContext = @context
@context = context
@trigger 'before', context
if res = next()
@context = previousContext
return res
else
return
doc: (context, next) ->
if doc = app.docs.findBySlug(context.params.doc) or app.disabledDocs.findBySlug(context.params.doc)
context.doc = doc
context.entry = doc.toEntry()
@triggerRoute 'entry'
return
else
return next()
type: (context, next) ->
doc = app.docs.findBySlug(context.params.doc)
if type = doc?.types.findBy 'slug', context.params.type
context.doc = doc
context.type = type
@triggerRoute 'type'
return
else
return next()
entry: (context, next) ->
doc = app.docs.findBySlug(context.params.doc)
return next() unless doc
path = context.params.path
hash = context.hash
if entry = doc.findEntryByPathAndHash(path, hash)
context.doc = doc
context.entry = entry
@triggerRoute 'entry'
return
else if path.slice(-6) is '/index'
path = path.substr(0, path.length - 6)
return entry.fullPath() if entry = doc.findEntryByPathAndHash(path, hash)
else
path = "#{path}/index"
return entry.fullPath() if entry = doc.findEntryByPathAndHash(path, hash)
return next()
root: ->
return '/' if app.isSingleDoc()
@triggerRoute 'root'
return
settings: (context) ->
return "/#/#{context.path}" if app.isSingleDoc()
@triggerRoute 'settings'
# Allows check all the checkboxes in /settings.
# This event needs to be added runtime since the button is
# added to the sidebar when the page is loaded, not like
# other elements that are hidden and are shown when the page
# loads
app.document.settings.setCheckboxesEvent()
return
offline: (context)->
return "/#/#{context.path}" if app.isSingleDoc()
@triggerRoute 'offline'
return
about: (context) ->
return "/#/#{context.path}" if app.isSingleDoc()
context.page = 'about'
@triggerRoute 'page'
return
news: (context) ->
return "/#/#{context.path}" if app.isSingleDoc()
context.page = 'news'
@triggerRoute 'page'
return
help: (context) ->
return "/#/#{context.path}" if app.isSingleDoc()
context.page = 'help'
@triggerRoute 'page'
return
notFound: (context) ->
@triggerRoute 'notFound'
return
isIndex: ->
@context?.path is '/' or (app.isSingleDoc() and @context?.entry?.isIndex())
isSettings: ->
@context?.path is '/settings'
setInitialPath: ->
# Remove superfluous forward slashes at the beginning of the path
if (path = location.pathname.replace /^\/{2,}/g, '/') isnt location.pathname
page.replace path + location.search + location.hash, null, true
if location.pathname is '/'
if path = @getInitialPathFromHash()
page.replace path + location.search, null, true
else if path = @getInitialPathFromCookie()
page.replace path + location.search + location.hash, null, true
return
getInitialPathFromHash: ->
try
(new RegExp "#/(.+)").exec(decodeURIComponent location.hash)?[1]
catch
getInitialPathFromCookie: ->
if path = Cookies.get('initial_path')
Cookies.expire('initial_path')
path
replaceHash: (hash) ->
page.replace location.pathname + location.search + (hash or ''), null, true
return