offline_page.coffee 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. class app.views.OfflinePage extends app.View
  2. @className: '_static'
  3. @events:
  4. click: 'onClick'
  5. deactivate: ->
  6. if super
  7. @empty()
  8. return
  9. render: ->
  10. app.docs.getDownloadStatuses (statuses) =>
  11. if statuses is false
  12. @html @tmpl('offlineError')
  13. else
  14. html = ''
  15. html += @renderDoc(doc, statuses[doc.slug]) for doc in app.docs.all()
  16. @html @tmpl('offlinePage', html)
  17. return
  18. return
  19. renderDoc: (doc, status) ->
  20. app.templates.render('offlineDoc', doc, status)
  21. getTitle: ->
  22. 'Offline'
  23. docByEl: (el) ->
  24. el = el.parentNode until slug = el.getAttribute('data-slug')
  25. app.docs.findBy('slug', slug)
  26. docEl: (doc) ->
  27. @find("[data-slug='#{doc.slug}']")
  28. onRoute: ->
  29. @render()
  30. return
  31. onClick: (event) =>
  32. if event.target.hasAttribute('data-dl')
  33. action = 'download'
  34. else if event.target.hasAttribute('data-del')
  35. action = 'undownload'
  36. if action
  37. $.stopEvent(event)
  38. doc = @docByEl(event.target)
  39. doc[action](@onDownloadSuccess.bind(@, doc), @onDownloadError.bind(@, doc))
  40. @docEl(doc).classList.add("#{action}ing")
  41. return
  42. onDownloadSuccess: (doc) ->
  43. doc.getDownloadStatus (status) =>
  44. @docEl(doc).outerHTML = @renderDoc(doc, status)
  45. return
  46. onDownloadError: (doc) ->
  47. el = @docEl(doc)
  48. el.className = ''
  49. el.classList.add('error')
  50. return