document.coffee 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. class app.views.Document extends app.View
  2. MAX_WIDTH_CLASS = '_max-width'
  3. HIDE_SIDEBAR_CLASS = '_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 @nav = new app.views.Nav,
  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. @sidebar.search
  19. .on 'searching', @onSearching
  20. .on 'clear', @onSearchClear
  21. $.on document.body, 'click', @onClick
  22. @activate()
  23. return
  24. toggleLight: ->
  25. css = $('link[rel="stylesheet"][data-alt]')
  26. alt = css.getAttribute('data-alt')
  27. css.setAttribute('data-alt', css.getAttribute('href'))
  28. css.setAttribute('href', alt)
  29. app.settings.setDark(alt.indexOf('dark') > 0)
  30. app.appCache?.updateInBackground()
  31. return
  32. toggleLayout: ->
  33. wantsMaxWidth = !app.el.classList.contains(MAX_WIDTH_CLASS)
  34. app.el.classList[if wantsMaxWidth then 'add' else 'remove'](MAX_WIDTH_CLASS)
  35. app.settings.setLayout(MAX_WIDTH_CLASS, wantsMaxWidth)
  36. app.appCache?.updateInBackground()
  37. return
  38. showSidebar: (options = {}) ->
  39. @toggleSidebar(options, true)
  40. return
  41. hideSidebar: (options = {}) ->
  42. @toggleSidebar(options, false)
  43. return
  44. toggleSidebar: (options = {}, shouldShow) ->
  45. shouldShow ?= if options.save then !@hasSidebar() else app.el.classList.contains(HIDE_SIDEBAR_CLASS)
  46. app.el.classList[if shouldShow then 'remove' else 'add'](HIDE_SIDEBAR_CLASS)
  47. if options.save
  48. app.settings.setLayout(HIDE_SIDEBAR_CLASS, !shouldShow)
  49. app.appCache?.updateInBackground()
  50. return
  51. hasSidebar: ->
  52. !app.settings.hasLayout(HIDE_SIDEBAR_CLASS)
  53. onSearching: =>
  54. unless @hasSidebar()
  55. @showSidebar()
  56. return
  57. onSearchClear: =>
  58. unless @hasSidebar()
  59. @hideSidebar()
  60. return
  61. setTitle: (title) ->
  62. @el.title = if title then "DevDocs — #{title}" else 'DevDocs API Documentation'
  63. onVisibilityChange: =>
  64. return unless @el.visibilityState is 'visible'
  65. @delay ->
  66. location.reload() if app.isMobile() isnt app.views.Mobile.detect()
  67. return
  68. , 300
  69. return
  70. onHelp: ->
  71. app.router.show '/help#shortcuts'
  72. onEscape: ->
  73. path = if !app.isSingleDoc() or location.pathname is app.doc.fullPath()
  74. '/'
  75. else
  76. app.doc.fullPath()
  77. app.router.show(path)
  78. onBack: ->
  79. history.back()
  80. onForward: ->
  81. history.forward()
  82. onClick: (event) ->
  83. return unless event.target.hasAttribute('data-behavior')
  84. $.stopEvent(event)
  85. switch event.target.getAttribute('data-behavior')
  86. when 'back' then history.back()
  87. when 'reload' then window.location.reload()
  88. when 'reboot' then window.location = '/'
  89. when 'hard-reload' then app.reload()
  90. when 'reset' then app.reset() if confirm('Are you sure you want to reset DevDocs?')
  91. return