offline_page.coffee 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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)
  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("a[data-action-all='#{action}']").classList[if @find("a[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: (route) ->
  39. if app.isSingleDoc()
  40. window.location = "/#/#{route.path}"
  41. else
  42. @render()
  43. return
  44. onClick: (event) =>
  45. return unless link = $.closestLink(event.target)
  46. if action = link.getAttribute('data-action')
  47. $.stopEvent(event)
  48. doc = @docByEl(link)
  49. action = 'install' if action is 'update'
  50. doc[action](@onInstallSuccess.bind(@, doc), @onInstallError.bind(@, doc), @onInstallProgress.bind(@, doc))
  51. link.parentNode.innerHTML = "#{link.textContent.replace(/e$/, '')}ing…"
  52. else if action = link.getAttribute('data-action-all')
  53. $.stopEvent(event)
  54. app.db.migrate()
  55. el.click() for el in @findAll("a[data-action='#{action}']")
  56. return
  57. onInstallSuccess: (doc) ->
  58. return unless @activated
  59. doc.getInstallStatus (status) =>
  60. return unless @activated
  61. if el = @docEl(doc)
  62. el.outerHTML = @renderDoc(doc, status)
  63. $.highlight el, className: '_highlight'
  64. @refreshLinks()
  65. return
  66. return
  67. onInstallError: (doc) ->
  68. return unless @activated
  69. if el = @docEl(doc)
  70. el.lastElementChild.textContent = 'Error'
  71. return
  72. onInstallProgress: (doc, event) ->
  73. return unless @activated and event.lengthComputable
  74. if el = @docEl(doc)
  75. percentage = Math.round event.loaded * 100 / event.total
  76. el.lastElementChild.textContent = el.lastElementChild.textContent.replace(/(\s.+)?$/, " (#{percentage}%)")
  77. return
  78. onChange: (event) ->
  79. if event.target.name is 'autoUpdate'
  80. app.settings.set 'manualUpdate', !event.target.checked
  81. return