document.coffee 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. class app.views.Document extends app.View
  2. MAX_WIDTH_LAYOUT = '_max-width'
  3. SIDEBAR_HIDDEN_LAYOUT = '_sidebar-hidden'
  4. @el: document
  5. @events:
  6. visibilitychange: 'onVisibilityChange'
  7. @shortcuts:
  8. help: 'onHelp'
  9. escape: 'onEscape'
  10. superLeft: 'onBack'
  11. superRight: 'onForward'
  12. init: ->
  13. @addSubview @menu = new app.views.Menu,
  14. @addSubview @sidebar = new app.views.Sidebar
  15. @addSubview @resizer = new app.views.Resizer if app.views.Resizer.isSupported()
  16. @addSubview @content = new app.views.Content
  17. @addSubview @path = new app.views.Path unless app.isSingleDoc() or app.isMobile()
  18. $.on document.body, 'click', @onClick
  19. @activate()
  20. return
  21. toggleLight: ->
  22. css = $('link[rel="stylesheet"][data-alt]')
  23. alt = css.getAttribute('data-alt')
  24. css.setAttribute('data-alt', css.getAttribute('href'))
  25. css.setAttribute('href', alt)
  26. app.settings.setDark(alt.indexOf('dark') > 0)
  27. app.appCache?.updateInBackground()
  28. return
  29. toggleLayout: ->
  30. wantsMaxWidth = !app.el.classList.contains(MAX_WIDTH_LAYOUT)
  31. app.el.classList[if wantsMaxWidth then 'add' else 'remove'](MAX_WIDTH_LAYOUT)
  32. app.settings.setLayout(MAX_WIDTH_LAYOUT, wantsMaxWidth)
  33. app.appCache?.updateInBackground()
  34. return
  35. toggleSidebarLayout: ->
  36. shouldHide = !app.settings.hasLayout(SIDEBAR_HIDDEN_LAYOUT)
  37. app.el.classList[if shouldHide then 'add' else 'remove'](SIDEBAR_HIDDEN_LAYOUT)
  38. app.settings.setLayout(SIDEBAR_HIDDEN_LAYOUT, shouldHide)
  39. app.appCache?.updateInBackground()
  40. return
  41. setTitle: (title) ->
  42. @el.title = if title then "DevDocs — #{title}" else 'DevDocs API Documentation'
  43. onVisibilityChange: =>
  44. return unless @el.visibilityState is 'visible'
  45. @delay ->
  46. location.reload() if app.isMobile() isnt app.views.Mobile.detect()
  47. return
  48. , 300
  49. return
  50. onHelp: ->
  51. app.router.show '/help#shortcuts'
  52. onEscape: ->
  53. path = if !app.isSingleDoc() or location.pathname is app.doc.fullPath()
  54. '/'
  55. else
  56. app.doc.fullPath()
  57. app.router.show(path)
  58. onBack: ->
  59. history.back()
  60. onForward: ->
  61. history.forward()
  62. onClick: (event) ->
  63. return unless event.target.hasAttribute('data-behavior')
  64. $.stopEvent(event)
  65. switch event.target.getAttribute('data-behavior')
  66. when 'back' then history.back()
  67. when 'reload' then window.location.reload()
  68. when 'reboot' then window.location = '/'
  69. when 'hard-reload' then app.reload()
  70. when 'reset' then app.reset() if confirm('Are you sure you want to reset DevDocs?')
  71. return