Lungo.Aside.coffee 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. ###
  2. Initialize the <articles> layout of a certain <section>
  3. @namespace Lungo
  4. @class Aside
  5. @author Javier Jimenez Villar <javi@tapquo.com> || @soyjavi
  6. ###
  7. Lungo.Aside = do (lng = Lungo) ->
  8. C = lng.Constants
  9. _callback = undefined
  10. _customAsideAnimation = undefined
  11. ###
  12. Display an aside element with a particular <section>
  13. @method show
  14. ###
  15. show = (aside_id, animate_section = true, fromX = 0) ->
  16. aside = lng.dom("##{aside_id}")
  17. if aside.length
  18. lng.Element.Cache.aside = aside
  19. aside_transition = aside.data(C.TRANSITION.ATTR) or "left"
  20. aside.addClass(C.CLASS.SHOW)
  21. if lng.DEVICE is C.DEVICE.PHONE
  22. if fromX then _phoneCustomAnimation fromX, false
  23. else lng.Element.Cache.section.data("aside-#{aside_transition}", "show")
  24. else
  25. aside_section = lng.dom("[data-aside=#{aside_id}]")
  26. if aside_section.data("children")
  27. if aside_section.attr("id") isnt lng.Element.Cache.section.attr("id")
  28. lng.Element.Cache.section.addClass "shadowing"
  29. aside_section.removeClass("aside").addClass "asideShowing"
  30. showFix = (aside_id) ->
  31. aside = lng.dom("##{aside_id}")
  32. if aside.length
  33. lng.Element.Cache.aside = aside
  34. aside.addClass(C.CLASS.SHOW).addClass("box")
  35. ###
  36. Hide an aside element with a particular section
  37. @method hide
  38. ###
  39. hide = (callback, fromX) ->
  40. console.error fromX
  41. if lng.Element.Cache.aside
  42. _callback = callback
  43. aside_transition = lng.Element.Cache.aside.data(C.TRANSITION.ATTR) or "left"
  44. if lng.DEVICE is C.DEVICE.PHONE
  45. lng.Element.Cache.section.removeClass("aside").removeClass("aside-right")
  46. if fromX then _phoneCustomAnimation fromX, true
  47. else lng.Element.Cache.section.data("aside-#{aside_transition}", "hide")
  48. else
  49. lng.dom(".aside").removeClass("aside").addClass("asideHidding")
  50. lng.Element.Cache.aside = null
  51. if callback then callback.call callback
  52. lng.dom(".shadow").removeClass("shadow").addClass("unshadowing")
  53. else if callback then callback.call callback
  54. ###
  55. Toggle an aside element
  56. @method toggle
  57. @param {string} Aside id
  58. ###
  59. toggle = (aside) ->
  60. if lng.Element.Cache.aside then do lng.Aside.hide
  61. else lng.Aside.show aside
  62. ###
  63. Triggered when <aside> animation ends.
  64. @method animationEnd
  65. @param {object} event
  66. ###
  67. animationEnd = (event) ->
  68. section = lng.dom(event.target)
  69. aside_transition = lng.Element.Cache.aside.data(C.TRANSITION.ATTR) or "left"
  70. if section.data("aside-#{aside_transition}") is "hide"
  71. lng.Element.Cache.aside.removeClass(C.CLASS.SHOW)
  72. lng.Element.Cache.aside = null
  73. section.removeAttr("data-aside-#{aside_transition}")
  74. if _callback then _callback.call _callback
  75. _callback = undefined
  76. else
  77. className = if aside_transition.indexOf("right") is -1 then "aside" else "aside-right"
  78. section.removeAttr("style").removeAttr("data-aside-#{aside_transition}").addClass(className)
  79. if _customAsideAnimation
  80. _customAsideAnimation.remove()
  81. _customAsideAnimation = undefined
  82. ###
  83. @todo
  84. @method draggable
  85. ###
  86. draggable = ->
  87. return false unless lng.DEVICE is C.DEVICE.PHONE
  88. MIN_XDIFF = 96
  89. lng.dom(C.QUERY.HREF_ASIDE).each ->
  90. started = false
  91. el = lng.dom(this)
  92. section = el.closest("section")
  93. aside = lng.dom("aside#" + el.data("aside"))
  94. section.swiping (gesture) ->
  95. unless section.hasClass("aside") or section.hasClass("aside-right")
  96. xdiff = gesture.currentTouch.x - gesture.iniTouch.x
  97. ydiff = Math.abs(gesture.currentTouch.y - gesture.iniTouch.y)
  98. started = (if started then true else xdiff > 3 * ydiff and xdiff < 50)
  99. if started
  100. xdiff = (if xdiff > 256 then 256 else (if xdiff < 0 then 0 else xdiff))
  101. aside.addClass C.CLASS.SHOW
  102. section.vendor "transform", "translateX(#{xdiff}px)"
  103. section.vendor "transition-duration", "0s"
  104. else
  105. section.attr "style", ""
  106. section.swipe (gesture) ->
  107. diff = gesture.currentTouch.x - gesture.iniTouch.x
  108. ydiff = Math.abs(gesture.currentTouch.y - gesture.iniTouch.y)
  109. section.attr "style", ""
  110. if diff > MIN_XDIFF and started
  111. show(aside.attr("id"), true, gesture.currentTouch.x)
  112. else hide
  113. started = false
  114. ###
  115. Private methods
  116. ###
  117. _asideStylesheet = ->
  118. if lng.Element.Cache.aside?.hasClass(C.CLASS.RIGHT) then "#{C.CLASS.RIGHT}" else " "
  119. _phoneCustomAnimation = (fromX, hide=false) ->
  120. if hide then kfStyle = document.createTextNode("""
  121. @-webkit-keyframes asideCustomKF {
  122. 0% { -webkit-transform: translateX(#{fromX}px); }
  123. 40% { -webkit-transform: translateX(#{fromX + 8}px); }
  124. 100% { -webkit-transform: translateX(0); }
  125. }""")
  126. else kfStyle = document.createTextNode("""
  127. @-webkit-keyframes asideCustomKF {
  128. 0% { -webkit-transform: translateX(#{fromX}px); }
  129. 60% { -webkit-transform: translateX(#{C.ASIDE.NORMAL + 8}px); }
  130. 100% { -webkit-transform: translateX(#{C.ASIDE.NORMAL}px); }
  131. }""")
  132. _customAsideAnimation = document.createElement('style')
  133. _customAsideAnimation.type = 'text/css'
  134. _customAsideAnimation.appendChild(kfStyle)
  135. document.getElementsByTagName("head")[0].appendChild(_customAsideAnimation)
  136. lng.Element.Cache.section.style("-webkit-animation-name", "asideCustomKF")
  137. toggle: toggle
  138. show: show
  139. showFix: showFix
  140. hide: hide
  141. draggable: draggable
  142. animationEnd: animationEnd