OrderManager.coffee 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. # based on https://github.com/johan-gorter/maquette-demo-hero
  2. class OrderManager extends Class
  3. constructor: ->
  4. timer_reorder = null
  5. @entering = {}
  6. @exiting = {}
  7. @
  8. registerEnter: (elem, props, cb) ->
  9. @entering[props.key] = [elem, cb]
  10. registerExit: (elem, props, cb) ->
  11. @exiting[props.key] = [elem, cb]
  12. animateChange: (elem, before, after) ->
  13. height = elem.offsetHeight
  14. elem.style.width = elem.offsetWidth+"px"
  15. elem.style.transition = "none"
  16. elem.style.boxSizing = "border-box"
  17. elem.style.float = "left"
  18. elem.style.marginTop = 0-height+"px"
  19. elem.style.transform = "TranslateY(#{before-after+height}px)"
  20. setTimeout (->
  21. elem.style.transition = null
  22. elem.className += " animate-back"
  23. ), 1
  24. setTimeout (->
  25. elem.style.transform = "TranslateY(0px)"
  26. elem.style.marginTop = "0px"
  27. elem.addEventListener "transitionend", ->
  28. elem.classList.remove("animate-back")
  29. elem.style.boxSizing = elem.style.float = elem.style.marginTop = elem.style.transform = elem.style.width = null
  30. ), 2
  31. execute: (elem, projection_options, selector, properties, childs) =>
  32. s = Date.now()
  33. has_entering = JSON.stringify(@entering) != "{}"
  34. has_exiting = JSON.stringify(@exiting) != "{}"
  35. if not has_exiting and not has_entering
  36. return false
  37. moving = {}
  38. @log Date.now() - s
  39. if childs.length < 5000 # Do not animate with too much childs
  40. if has_entering and has_exiting
  41. for child in childs
  42. key = child.properties.key
  43. if not key
  44. continue
  45. if not @entering[key]
  46. moving[key] = [child, child.domNode.offsetTop]
  47. @log Date.now() - s
  48. for key, [child_elem, exitanim] of @exiting
  49. if not @entering[key]
  50. exitanim()
  51. else
  52. elem.removeChild(child_elem)
  53. @log Date.now() - s
  54. for key, [child_elem, enteranim] of @entering
  55. if not @exiting[key]
  56. enteranim()
  57. @log Date.now() - s
  58. for key, [child, top_before] of moving
  59. top_after = child.domNode.offsetTop
  60. console.log("animateChange", top_before, top_after)
  61. if top_before != top_after
  62. @animateChange(child.domNode, top_before, top_after)
  63. @entering = {}
  64. @exiting = {}
  65. @log Date.now() - s, arguments
  66. window.OrderManager = OrderManager