offline_page.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. class app.views.OfflinePage extends app.View
  2. @className: '_static'
  3. @events:
  4. click: 'onClick'
  5. change: 'onChange'
  6. deactivate: ->
  7. if super
  8. @empty()
  9. return
  10. render: ->
  11. if app.cookieBlocked
  12. @html @tmpl('offlineError', 'cookie_blocked')
  13. return
  14. app.docs.getInstallStatuses (statuses) =>
  15. return unless @activated
  16. if statuses is false
  17. @html @tmpl('offlineError', app.db.reason, app.db.error)
  18. else
  19. html = ''
  20. html += @renderDoc(doc, statuses[doc.slug]) for doc in app.docs.all()
  21. @html @tmpl('offlinePage', html)
  22. @refreshLinks()
  23. return
  24. return
  25. renderDoc: (doc, status) ->
  26. app.templates.render('offlineDoc', doc, status)
  27. getTitle: ->
  28. 'Offline'
  29. refreshLinks: ->
  30. for action in ['install', 'update', 'uninstall']
  31. @find("[data-action-all='#{action}']").classList[if @find("[data-action='#{action}']") then 'add' else 'remove']('_show')
  32. return
  33. docByEl: (el) ->
  34. el = el.parentNode until slug = el.getAttribute('data-slug')
  35. app.docs.findBy('slug', slug)
  36. docEl: (doc) ->
  37. @find("[data-slug='#{doc.slug}']")
  38. onRoute: (context) ->
  39. @render()
  40. return
  41. onClick: (event) =>
  42. el = $.eventTarget(event)
  43. if action = el.getAttribute('data-action')
  44. doc = @docByEl(el)
  45. action = 'install' if action is 'update'
  46. doc[action](@onInstallSuccess.bind(@, doc), @onInstallError.bind(@, doc), @onInstallProgress.bind(@, doc))
  47. el.parentNode.innerHTML = "#{el.textContent.replace(/e$/, '')}ing…"
  48. else if action = el.getAttribute('data-action-all') || el.parentElement.getAttribute('data-action-all')
  49. return unless action isnt 'uninstall' or window.confirm('Uninstall all docs?')
  50. app.db.migrate()
  51. $.click(el) for el in @findAll("[data-action='#{action}']")
  52. return
  53. onInstallSuccess: (doc) ->
  54. return unless @activated
  55. doc.getInstallStatus (status) =>
  56. return unless @activated
  57. if el = @docEl(doc)
  58. el.outerHTML = @renderDoc(doc, status)
  59. $.highlight el, className: '_highlight'
  60. @refreshLinks()
  61. return
  62. return
  63. onInstallError: (doc) ->
  64. return unless @activated
  65. if el = @docEl(doc)
  66. el.lastElementChild.textContent = 'Error'
  67. return
  68. onInstallProgress: (doc, event) ->
  69. return unless @activated and event.lengthComputable
  70. if el = @docEl(doc)
  71. percentage = Math.round event.loaded * 100 / event.total
  72. el.lastElementChild.textContent = el.lastElementChild.textContent.replace(/(\s.+)?$/, " (#{percentage}%)")
  73. return
  74. onChange: (event) ->
  75. if event.target.name is 'autoUpdate'
  76. app.settings.set 'manualUpdate', !event.target.checked
  77. return