doc_picker.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. class app.views.DocPicker extends app.View
  2. @className: '_list _list-picker'
  3. @events:
  4. mousedown: 'onMouseDown'
  5. mouseup: 'onMouseUp'
  6. init: ->
  7. @addSubview @listFold = new app.views.ListFold(@el)
  8. return
  9. activate: ->
  10. if super
  11. @render()
  12. $.on @el, 'focus', @onDOMFocus, true
  13. return
  14. deactivate: ->
  15. if super
  16. @empty()
  17. $.off @el, 'focus', @onDOMFocus, true
  18. @focusEl = null
  19. return
  20. render: ->
  21. html = @tmpl('docPickerHeader')
  22. docs = app.docs.all().concat(app.disabledDocs.all()...)
  23. while doc = docs.shift()
  24. if doc.version?
  25. [docs, versions] = @extractVersions(docs, doc)
  26. html += @tmpl('sidebarVersionedDoc', doc, @renderVersions(versions), open: app.docs.contains(doc))
  27. else
  28. html += @tmpl('sidebarLabel', doc, checked: app.docs.contains(doc))
  29. @html html + @tmpl('docPickerNote')
  30. $.requestAnimationFrame => @findByTag('input')?.focus()
  31. return
  32. renderVersions: (docs) ->
  33. html = ''
  34. html += @tmpl('sidebarLabel', doc, checked: app.docs.contains(doc)) for doc in docs
  35. html
  36. extractVersions: (originalDocs, version) ->
  37. docs = []
  38. versions = [version]
  39. for doc in originalDocs
  40. (if doc.name is version.name then versions else docs).push(doc)
  41. [docs, versions]
  42. empty: ->
  43. @resetClass()
  44. super
  45. return
  46. getSelectedDocs: ->
  47. for input in @findAllByTag 'input' when input?.checked
  48. input.name
  49. onMouseDown: =>
  50. @mouseDown = Date.now()
  51. return
  52. onMouseUp: =>
  53. @mouseUp = Date.now()
  54. return
  55. onDOMFocus: (event) =>
  56. target = event.target
  57. if target.tagName is 'INPUT'
  58. unless (@mouseDown and Date.now() < @mouseDown + 100) or (@mouseUp and Date.now() < @mouseUp + 100)
  59. $.scrollTo target.parentNode, null, 'continuous'
  60. else if target.classList.contains(app.views.ListFold.targetClass)
  61. target.blur()
  62. unless @mouseDown and Date.now() < @mouseDown + 100
  63. if @focusEl is $('input', target.nextElementSibling)
  64. @listFold.close(target) if target.classList.contains(app.views.ListFold.activeClass)
  65. prev = target.previousElementSibling
  66. prev = prev.previousElementSibling until prev.tagName is 'LABEL' or prev.classList.contains(app.views.ListFold.targetClass)
  67. prev = $.makeArray($$('input', prev.nextElementSibling)).pop() if prev.classList.contains(app.views.ListFold.activeClass)
  68. @delay -> prev.focus()
  69. else
  70. @listFold.open(target) unless target.classList.contains(app.views.ListFold.activeClass)
  71. @delay -> $('input', target.nextElementSibling).focus()
  72. @focusEl = target
  73. return