Lungo.Router.Aside.coffee 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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("id")) or aside_id is null
  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 = (aside) ->
  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. aside_stylesheet = _asideStylesheet()
  46. lng.Element.Cache.section.addClass(aside_stylesheet).addClass C.CLASS.ASIDE
  47. ###
  48. Hide an aside element with a particular section
  49. @method hide
  50. ###
  51. hide = ->
  52. if lng.Element.Cache.aside? and lng.DEVICE is C.DEVICE.PHONE
  53. lng.Element.Cache.section.removeClass C.CLASS.ASIDE
  54. setTimeout (-> lng.Element.Cache.aside.removeClass C.CLASS.SHOW), C.TRANSITION.DURATION
  55. ###
  56. @todo
  57. @method draggable
  58. ###
  59. draggable = ->
  60. MIN_XDIFF = parseInt(document.body.getBoundingClientRect().width / 3, 10)
  61. MIN_XDIFF = 128
  62. lng.dom(C.QUERY.HREF_ASIDE).each ->
  63. started = false
  64. el = lng.dom(this)
  65. section = el.closest("section")
  66. aside = lng.dom("aside#" + el.data("aside"))
  67. section.swiping (gesture) ->
  68. unless section.hasClass("aside")
  69. xdiff = gesture.currentTouch.x - gesture.iniTouch.x
  70. ydiff = Math.abs(gesture.currentTouch.y - gesture.iniTouch.y)
  71. started = (if started then true else xdiff > 3 * ydiff and xdiff < 50)
  72. if started
  73. xdiff = (if xdiff > 256 then 256 else (if xdiff < 0 then 0 else xdiff))
  74. aside.addClass C.CLASS.SHOW
  75. section.vendor "transform", "translateX(#{xdiff}px)"
  76. section.vendor "transition-duration", "0s"
  77. else
  78. section.attr "style", ""
  79. section.swipe (gesture) ->
  80. diff = gesture.currentTouch.x - gesture.iniTouch.x
  81. ydiff = Math.abs(gesture.currentTouch.y - gesture.iniTouch.y)
  82. section.attr "style", ""
  83. if diff > MIN_XDIFF and started
  84. show aside
  85. else
  86. hide aside
  87. started = false
  88. ###
  89. Private methods
  90. ###
  91. _asideStylesheet = ->
  92. aside_stylesheet = lng.Element.Cache.aside.attr(C.ATTRIBUTE.CLASS)
  93. stylesheet = ""
  94. #@todo: Refactor
  95. if aside_stylesheet
  96. stylesheet += (if (aside_stylesheet.indexOf(C.CLASS.RIGHT) > -1) then C.CLASS.RIGHT + " " else "")
  97. stylesheet += (if (aside_stylesheet.indexOf(C.CLASS.SMALL) > -1) then C.CLASS.SMALL + " " else "")
  98. stylesheet
  99. active: active
  100. toggle: toggle
  101. show: show
  102. hide: hide
  103. draggable: draggable