Lungo.Router.Aside.coffee 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. ###
  2. Initialize the <articles> layout of a certain <section>
  3. @namespace Lungo.View
  4. @class Aside
  5. @author Javier Jimenez Villar <javi@tapquo.com> || @soyjavi
  6. ###
  7. Lungo.Aside = do(lng = Lungo) ->
  8. C = lng.Constants
  9. ###
  10. Active aside for a determinate section
  11. @method active
  12. @param {object} Section element
  13. ###
  14. active = (section) ->
  15. aside_id = section.data("aside")
  16. current_aside = lng.Element.Cache.aside
  17. # Deactive
  18. if current_aside and aside_id isnt current_aside?.attr(C.ATTRIBUTE.ID)
  19. current_aside.removeClass(C.CLASS.SHOW).removeClass C.CLASS.ACTIVE
  20. lng.Element.Cache.aside = null
  21. # Active
  22. if aside_id
  23. lng.Element.Cache.aside = lng.dom(C.ELEMENT.ASIDE + "#" + aside_id)
  24. lng.Element.Cache.aside.addClass C.CLASS.ACTIVE
  25. lng.Aside.show aside_id unless lng.DEVICE is C.DEVICE.PHONE
  26. lng.Element.Cache.aside
  27. ###
  28. Toggle an aside element
  29. @method toggle
  30. @param {string} Aside id
  31. ###
  32. toggle = ->
  33. if lng.Element.Cache.aside
  34. is_visible = lng.Element.Cache.aside.hasClass(C.CLASS.SHOW)
  35. if is_visible then lng.Aside.hide() else lng.Aside.show()
  36. ###
  37. Display an aside element with a particular <section>
  38. @method show
  39. ###
  40. show = ->
  41. if lng.Element.Cache.aside?
  42. setTimeout (-> lng.Element.Cache.aside.addClass C.CLASS.SHOW), C.TRANSITION.DURATION
  43. if lng.DEVICE is C.DEVICE.PHONE
  44. lng.Element.Cache.aside.addClass C.CLASS.SHOW
  45. lng.Element.Cache.section.addClass(_asideStylesheet()).addClass(C.CLASS.ASIDE)
  46. ###
  47. Hide an aside element with a particular section
  48. @method hide
  49. ###
  50. hide = ->
  51. if lng.Element.Cache.aside? and lng.DEVICE is C.DEVICE.PHONE
  52. lng.Element.Cache.section.removeClass(C.CLASS.ASIDE)
  53. setTimeout (-> lng.Element.Cache.aside.removeClass C.CLASS.SHOW), C.TRANSITION.DURATION
  54. ###
  55. @todo
  56. @method draggable
  57. ###
  58. draggable = ->
  59. MIN_XDIFF = parseInt(document.body.getBoundingClientRect().width / 3, 10)
  60. MIN_XDIFF = 128
  61. lng.dom(C.QUERY.HREF_ASIDE).each ->
  62. started = false
  63. el = lng.dom(this)
  64. section = el.closest("section")
  65. aside = lng.dom("aside#" + el.data("aside"))
  66. section.swiping (gesture) ->
  67. unless section.hasClass("aside")
  68. xdiff = gesture.currentTouch.x - gesture.iniTouch.x
  69. ydiff = Math.abs(gesture.currentTouch.y - gesture.iniTouch.y)
  70. started = (if started then true else xdiff > 3 * ydiff and xdiff < 50)
  71. if started
  72. xdiff = (if xdiff > 256 then 256 else (if xdiff < 0 then 0 else xdiff))
  73. aside.addClass C.CLASS.SHOW
  74. section.vendor "transform", "translateX(#{xdiff}px)"
  75. section.vendor "transition-duration", "0s"
  76. else
  77. section.attr "style", ""
  78. section.swipe (gesture) ->
  79. diff = gesture.currentTouch.x - gesture.iniTouch.x
  80. ydiff = Math.abs(gesture.currentTouch.y - gesture.iniTouch.y)
  81. section.attr "style", ""
  82. if diff > MIN_XDIFF and started
  83. show aside
  84. else
  85. hide aside
  86. started = false
  87. ###
  88. Private methods
  89. ###
  90. _asideStylesheet = ->
  91. if lng.Element.Cache.aside?.hasClass(C.CLASS.RIGHT) then "#{C.CLASS.RIGHT}" else " "
  92. active: active
  93. toggle: toggle
  94. show: show
  95. hide: hide
  96. draggable: draggable