settings.coffee 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. @shortcuts:
  12. enter: 'onEnter'
  13. init: ->
  14. @addSubview @docPicker = new app.views.DocPicker
  15. return
  16. activate: ->
  17. if super
  18. @render()
  19. document.body.classList.remove(SIDEBAR_HIDDEN_LAYOUT)
  20. app.appCache?.on 'progress', @onAppCacheProgress
  21. return
  22. deactivate: ->
  23. if super
  24. @resetClass()
  25. @docPicker.detach()
  26. document.body.classList.add(SIDEBAR_HIDDEN_LAYOUT) if app.settings.hasLayout(SIDEBAR_HIDDEN_LAYOUT)
  27. app.appCache?.off 'progress', @onAppCacheProgress
  28. return
  29. render: ->
  30. @docPicker.appendTo @sidebar
  31. @refreshElements()
  32. @addClass '_in'
  33. return
  34. save: ->
  35. unless @saving
  36. @saving = true
  37. docs = @docPicker.getSelectedDocs()
  38. app.settings.setDocs(docs)
  39. @saveBtn.textContent = if app.appCache then 'Downloading\u2026' else 'Saving\u2026'
  40. disabledDocs = new app.collections.Docs(doc for doc in app.docs.all() when docs.indexOf(doc.slug) is -1)
  41. disabledDocs.uninstall ->
  42. app.db.migrate()
  43. app.reload()
  44. return
  45. onEnter: =>
  46. @save()
  47. return
  48. onSubmit: (event) =>
  49. event.preventDefault()
  50. @save()
  51. return
  52. onClick: (event) =>
  53. return if event.which isnt 1
  54. if event.target is @backBtn
  55. $.stopEvent(event)
  56. app.router.show '/'
  57. return
  58. onAppCacheProgress: (event) =>
  59. if event.lengthComputable
  60. percentage = Math.round event.loaded * 100 / event.total
  61. @saveBtn.textContent = "Downloading\u2026 (#{percentage}%)"
  62. return