Lungo.Router.Phone.coffee 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. ###
  2. Handles the <sections> and <articles> to show
  3. @namespace Lungo
  4. @class Router
  5. @author Javier Jimenez Villar <javi@tapquo.com> || @soyjavi
  6. @author Guillermo Pascual <pasku@tapquo.com> || @pasku1
  7. @author Ignacio Olalde <ina@tapquo.com> || @piniphone
  8. ###
  9. Lungo.RouterPhone = do (lng = Lungo) ->
  10. C = lng.Constants
  11. HASHTAG = "#"
  12. _history = []
  13. _animating = false
  14. ###
  15. Navigate to a <section>.
  16. @method section
  17. @param {string} Id of the <section>
  18. ###
  19. section = (section_id) ->
  20. return false if _animating
  21. current = lng.Element.Cache.section
  22. if _notCurrentTarget current, section_id
  23. query = C.ELEMENT.SECTION + HASHTAG + section_id
  24. future = if current then current.siblings(query) else lng.dom(query)
  25. if future.length
  26. _section future, current
  27. lng.Router.step section_id
  28. do _url unless Lungo.Config.history is false
  29. do _updateNavigationElements
  30. else if lng.Element.Cache.aside then do lng.Aside.hide
  31. ###
  32. Return to previous section.
  33. @method back
  34. ###
  35. back = ->
  36. return false if _animating
  37. do _removeLast
  38. current = lng.Element.Cache.section
  39. query = C.ELEMENT.SECTION + HASHTAG + history()
  40. future = current.siblings(query)
  41. if future.length
  42. _section future, current, true
  43. do _url unless Lungo.Config.history is false
  44. do _updateNavigationElements
  45. ###
  46. Displays the <article> in a particular <section>.
  47. @method article
  48. @param {string} <section> Id
  49. @param {string} <article> Id
  50. ###
  51. article = (section_id, article_id, element) ->
  52. if _notCurrentTarget(lng.Element.Cache.article, article_id)
  53. lng.Router.section section_id
  54. target = lng.Element.Cache.section.find "##{article_id}"
  55. if target.length > 0
  56. lng.Element.Cache.article.removeClass(C.CLASS.ACTIVE).trigger C.TRIGGER.UNLOAD
  57. lng.Element.Cache.article = target.addClass(C.CLASS.ACTIVE).trigger(C.TRIGGER.LOAD)
  58. if element?.data(C.ATTRIBUTE.TITLE)?
  59. lng.Element.Cache.section.find(C.QUERY.TITLE).text element.data(C.ATTRIBUTE.TITLE)
  60. do _url unless Lungo.Config.history is false
  61. do _updateNavigationElements
  62. ###
  63. Triggered when <section> animation ends. Reset animation classes of section and aside
  64. @method animationEnd
  65. @param {eventObject}
  66. ###
  67. animationEnd = (event) ->
  68. section = lng.dom(event.target)
  69. direction = section.data(C.ATTRIBUTE.DIRECTION)
  70. if section.data("original-transition")
  71. section.data(C.TRANSITION.ATTR, section.data("original-transition"))
  72. section.removeAttr("data-original-transition")
  73. section.removeClass C.CLASS.SHOW if direction is "out" or direction is "back-out"
  74. section.removeAttr "data-#{C.ATTRIBUTE.DIRECTION}"
  75. _animating = false
  76. ###
  77. Create a new element to the browsing history based on the current section id.
  78. @method step
  79. @param {string} Id of the section
  80. ###
  81. step = (section_id) -> _history.push section_id if section_id isnt history()
  82. ###
  83. Returns the current browsing history section id.
  84. @method history
  85. @return {string} Current section id
  86. ###
  87. history = -> _history[_history.length - 1]
  88. ###
  89. Private methods
  90. ###
  91. _section = (future, current, backward = false) ->
  92. callback = -> _show future, current, backward
  93. if lng.Element.Cache.aside then lng.Aside.hide callback
  94. else do callback
  95. _show = (future, current, backward) ->
  96. if current? then _setSectionDirections future, current, backward
  97. lng.Section.show current, future
  98. _setSectionDirections = (future, current, backward=false) ->
  99. if not current? or not future.length then return false
  100. _animating = true
  101. dirPrefix = if backward then "back-" else ""
  102. if not backward
  103. current.data("original-transition", current.data(C.TRANSITION.ATTR))
  104. current.data(C.TRANSITION.ATTR, future.data(C.TRANSITION.ATTR))
  105. else
  106. future.data("original-transition", future.data(C.TRANSITION.ATTR))
  107. future.data(C.TRANSITION.ATTR, current.data(C.TRANSITION.ATTR))
  108. future.addClass(C.CLASS.SHOW)
  109. future.data(C.ATTRIBUTE.DIRECTION, "#{dirPrefix}in") if future.data(C.TRANSITION.ATTR)
  110. if current.data(C.TRANSITION.ATTR) then current.data(C.ATTRIBUTE.DIRECTION, "#{dirPrefix}out")
  111. else current.removeClass(C.CLASS.SHOW)
  112. _notCurrentTarget = (current, id) -> current?.attr(C.ATTRIBUTE.ID) isnt id
  113. _url = ->
  114. _hashed_url = ""
  115. _hashed_url += "#{section}/" for section in _history
  116. _hashed_url += lng.Element.Cache.article.attr "id"
  117. setTimeout (-> window.location.hash = _hashed_url), 0
  118. _updateNavigationElements = ->
  119. article_id = lng.Element.Cache.article.attr C.ATTRIBUTE.ID
  120. # Active visual signal for elements
  121. links = lng.dom(C.QUERY.ARTICLE_ROUTER).removeClass(C.CLASS.ACTIVE)
  122. links.filter("[data-view-article=#{article_id}]").addClass(C.CLASS.ACTIVE)
  123. # Hide/Show elements in current article
  124. nav = lng.Element.Cache.section.find(C.QUERY.ARTICLE_REFERENCE).addClass C.CLASS.HIDE
  125. nav.filter("[data-article*='#{article_id}']").removeClass C.CLASS.HIDE
  126. _removeLast = ->
  127. if _history.length > 1
  128. _history.length -= 1
  129. section : section
  130. back : back
  131. article : article
  132. history : history
  133. step : step
  134. animationEnd : animationEnd