You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
devdocs/assets/javascripts/app/router.coffee

130 lines
2.9 KiB

11 years ago
class app.Router
$.extend @prototype, Events
@routes: [
['*', 'before' ]
['/', 'root' ]
['/offline', 'offline' ]
11 years ago
['/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) ->
@context = context
@trigger 'before', context
next()
return
doc: (context, next) ->
if doc = app.docs.findBy('slug', context.params.doc) or app.disabledDocs.findBy('slug', context.params.doc)
context.doc = doc
context.entry = doc.toEntry()
11 years ago
@triggerRoute 'entry'
else
next()
return
type: (context, next) ->
doc = app.docs.findBy 'slug', context.params.doc
if type = doc?.types.findBy 'slug', context.params.type
context.doc = doc
context.type = type
@triggerRoute 'type'
else
next()
return
entry: (context, next) ->
doc = app.docs.findBy 'slug', context.params.doc
if entry = doc?.findEntryByPathAndHash(context.params.path, context.hash)
context.doc = doc
context.entry = entry
@triggerRoute 'entry'
else
next()
return
root: ->
if app.isSingleDoc()
setTimeout (-> window.location = '/'), 0
else
@triggerRoute 'root'
11 years ago
return
offline: ->
@triggerRoute 'offline'
return
11 years ago
about: (context) ->
context.page = 'about'
@triggerRoute 'page'
return
news: (context) ->
context.page = 'news'
@triggerRoute 'page'
return
help: (context) ->
context.page = 'help'
@triggerRoute 'page'
return
notFound: (context) ->
@triggerRoute 'notFound'
return
isRoot: ->
location.pathname is '/'
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 @isRoot()
if path = @getInitialPathFromHash()
page.replace path + location.search, null, true
else if path = @getInitialPathFromCookie()
page.replace path + location.search + location.hash, null, true
11 years ago
return
getInitialPathFromHash: ->
11 years ago
try
(new RegExp "#/(.+)").exec(decodeURIComponent location.hash)?[1]
catch
getInitialPathFromCookie: ->
if path = Cookies.get('initial_path')
Cookies.expire('initial_path')
path
11 years ago
replaceHash: (hash) ->
page.replace location.pathname + location.search + (hash or ''), null, true
return