History

Fix various issues when switching from single doc to full app

Thibaut Courouble committed 9 years ago

Commit 4f62ccb1 parent 7947f2d4 files · +39 −35Browse files

Showing the changes to assets/javascripts (4 of 4 files). Show all

Unified · Split

4 files
  1. assets/javascripts/app/router.coffee +22 −14
  2. assets/javascripts/lib/page.coffee +13 −11
  3. assets/javascripts/views/content/offline_page.coffee +2 −5
  4. assets/javascripts/views/content/settings_page.coffee +2 −5
assets/javascripts/app/router.coffee+22−14
@@ -34,19 +34,24 @@ class app.Router · 33 unmodified lines
3434 return
3535
3636 before: (context, next) ->
37+ previousContext = @context
3738 @context = context
3839 @trigger 'before', context
39− next()
40− return
40+
41+ if res = next()
42+ @context = previousContext
43+ return res
44+ else
45+ return
4146
4247 doc: (context, next) ->
4348 if doc = app.docs.findBySlug(context.params.doc) or app.disabledDocs.findBySlug(context.params.doc)
4449 context.doc = doc
4550 context.entry = doc.toEntry()
4651 @triggerRoute 'entry'
52+ return
4753 else
48− next()
49− return
54+ return next()
5055
5156 type: (context, next) ->
5257 doc = app.docs.findBySlug(context.params.doc)
@@ -55,9 +60,9 @@ class app.Router · 2 unmodified lines
5560 context.doc = doc
5661 context.type = type
5762 @triggerRoute 'type'
63+ return
5864 else
59− next()
60− return
65+ return next()
6166
6267 entry: (context, next) ->
6368 doc = app.docs.findBySlug(context.params.doc)
@@ -66,36 +71,39 @@ class app.Router · 2 unmodified lines
6671 context.doc = doc
6772 context.entry = entry
6873 @triggerRoute 'entry'
74+ return
6975 else
70− next()
71− return
76+ return next()
7277
7378 root: ->
74− if app.isSingleDoc()
75− setTimeout (-> window.location = '/'), 0
76− else
77− @triggerRoute 'root'
79+ return '/' if app.isSingleDoc()
80+ @triggerRoute 'root'
7881 return
7982
80− settings: ->
83+ settings: (context) ->
84+ return "/#/#{context.path}" if app.isSingleDoc()
8185 @triggerRoute 'settings'
8286 return
8387
84− offline: ->
88+ offline: (context)->
89+ return "/#/#{context.path}" if app.isSingleDoc()
8590 @triggerRoute 'offline'
8691 return
8792
8893 about: (context) ->
94+ return "/#/#{context.path}" if app.isSingleDoc()
8995 context.page = 'about'
9096 @triggerRoute 'page'
9197 return
9298
9399 news: (context) ->
100+ return "/#/#{context.path}" if app.isSingleDoc()
94101 context.page = 'news'
95102 @triggerRoute 'page'
96103 return
97104
98105 help: (context) ->
106+ return "/#/#{context.path}" if app.isSingleDoc()
99107 context.page = 'help'
100108 @triggerRoute 'page'
101109 return
assets/javascripts/lib/page.coffee+13−11
@@ -38,11 +38,15 @@ page.stop = -> · 37 unmodified lines
3838 page.show = (path, state) ->
3939 return if path is currentState?.path
4040 context = new Context(path, state)
41+ previousState = currentState
4142 currentState = context.state
42− page.dispatch(context)
43− context.pushState()
44− updateCanonicalLink()
45− track()
43+ if res = page.dispatch(context)
44+ currentState = previousState
45+ location.assign(res)
46+ else
47+ context.pushState()
48+ updateCanonicalLink()
49+ track()
4650 context
4751
4852 page.replace = (path, state, skipDispatch, init) ->
@@ -58,10 +62,9 @@ page.replace = (path, state, skipDispatch, init) -> · 9 unmodified lines
5862 page.dispatch = (context) ->
5963 i = 0
6064 next = ->
61− fn(context, next) if fn = callbacks[i++]
62− return
63− next()
64− return
65+ res = fn(context, next) if fn = callbacks[i++]
66+ return res
67+ return next()
6568
6669 page.canGoBack = ->
6770 not Context.isIntialState(currentState)
@@ -116,10 +119,9 @@ class Route · 48 unmodified lines
116119 (context, next) =>
117120 if @match context.pathname, params = []
118121 context.params = params
119− fn(context, next)
122+ return fn(context, next)
120123 else
121− next()
122− return
124+ return next()
123125
124126 match: (path, params) ->
125127 return unless matchData = @regexp.exec(path)
assets/javascripts/views/content/offline_page.coffee+2−5
@@ -45,11 +45,8 @@ class app.views.OfflinePage extends app.View · 44 unmodified lines
4545 docEl: (doc) ->
4646 @find("[data-slug='#{doc.slug}']")
4747
48− onRoute: (route) ->
49− if app.isSingleDoc()
50− window.location = "/#/#{route.path}"
51− else
52− @render()
48+ onRoute: (context) ->
49+ @render()
5350 return
5451
5552 onClick: (event) =>
assets/javascripts/views/content/settings_page.coffee+2−5
@@ -58,9 +58,6 @@ class app.views.SettingsPage extends app.View · 57 unmodified lines
5858 @toggle input.name, input.checked
5959 return
6060
61− onRoute: (route) =>
62− if app.isSingleDoc()
63− window.location = "/#/#{route.path}"
64− else
65− @render()
61+ onRoute: (context) ->
62+ @render()
6663 return