settings.coffee 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. class app.views.Settings extends app.View
  2. SIDEBAR_HIDDEN_LAYOUT = '_sidebar-hidden'
  3. @el: '._settings'
  4. @elements:
  5. sidebar: '._sidebar'
  6. saveBtn: 'button[type="submit"]'
  7. backBtn: 'button[data-back]'
  8. @events:
  9. submit: 'onSubmit'
  10. click: 'onClick'
  11. focus: 'onFocus'
  12. @shortcuts:
  13. enter: 'onEnter'
  14. init: ->
  15. @addSubview @docPicker = new app.views.DocPicker
  16. return
  17. activate: ->
  18. if super
  19. @render()
  20. app.el.classList.remove(SIDEBAR_HIDDEN_LAYOUT)
  21. app.appCache?.on 'progress', @onAppCacheProgress
  22. return
  23. deactivate: ->
  24. if super
  25. @resetClass()
  26. @docPicker.detach()
  27. app.el.classList.add(SIDEBAR_HIDDEN_LAYOUT) if app.settings.hasLayout(SIDEBAR_HIDDEN_LAYOUT)
  28. app.appCache?.off 'progress', @onAppCacheProgress
  29. return
  30. render: ->
  31. @docPicker.appendTo @sidebar
  32. @refreshElements()
  33. @addClass '_in'
  34. return
  35. save: ->
  36. unless @saving
  37. @saving = true
  38. docs = @docPicker.getSelectedDocs()
  39. app.settings.setDocs(docs)
  40. @saveBtn.textContent = if app.appCache then 'Downloading\u2026' else 'Saving\u2026'
  41. disabledDocs = new app.collections.Docs(doc for doc in app.docs.all() when docs.indexOf(doc.slug) is -1)
  42. disabledDocs.uninstall ->
  43. app.db.migrate()
  44. app.reload()
  45. return
  46. onEnter: =>
  47. @save()
  48. return
  49. onSubmit: (event) =>
  50. event.preventDefault()
  51. @save()
  52. return
  53. onClick: (event) =>
  54. return if event.which isnt 1
  55. if event.target is @backBtn
  56. $.stopEvent(event)
  57. app.router.show '/'
  58. return
  59. onFocus: (event) =>
  60. $.scrollTo event.target, @el, 'continuous', bottomGap: 2
  61. return
  62. onAppCacheProgress: (event) =>
  63. if event.lengthComputable
  64. percentage = Math.round event.loaded * 100 / event.total
  65. @saveBtn.textContent = "Downloading\u2026 (#{percentage}%)"
  66. return