Lungo.Router.Tablet.coffee 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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.RouterTablet = 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. _show future, current
  27. step section_id
  28. do _url unless Lungo.Config.history is false
  29. do _updateNavigationElements
  30. # else do lng.Aside.hide
  31. ###
  32. Return to previous section.
  33. @method back
  34. ###
  35. back = (animating = true) ->
  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. _show future, current, true, animating
  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 not _sameSection() then back false
  53. target = lng.dom "article##{article_id}"
  54. if target.length > 0
  55. section = target.closest C.ELEMENT.SECTION
  56. lng.Router.section(section.attr("id"))
  57. section.children("#{C.ELEMENT.ARTICLE}.#{C.CLASS.ACTIVE}").removeClass(C.CLASS.ACTIVE).trigger C.TRIGGER.UNLOAD
  58. target.addClass(C.CLASS.ACTIVE).trigger(C.TRIGGER.LOAD)
  59. # lng.Element.Cache.article.removeClass(C.CLASS.ACTIVE).trigger C.TRIGGER.UNLOAD
  60. # lng.Element.Cache.article = target.addClass(C.CLASS.ACTIVE).trigger(C.TRIGGER.LOAD)
  61. if element?.data(C.ATTRIBUTE.TITLE)?
  62. # lng.Element.Cache.section.find(C.QUERY.TITLE).text element.data(C.ATTRIBUTE.TITLE)
  63. section.find(C.QUERY.TITLE).text element.data(C.ATTRIBUTE.TITLE)
  64. do _url unless Lungo.Config.history is false
  65. _updateNavigationElements article_id
  66. ###
  67. Triggered when <section> animation ends. Reset animation classes of section and aside
  68. @method animationEnd
  69. @param {eventObject}
  70. ###
  71. animationEnd = (event) ->
  72. section = lng.dom(event.target)
  73. direction = section.data(C.ATTRIBUTE.DIRECTION)
  74. if direction
  75. section.removeClass C.CLASS.SHOW if direction is "out" or direction is "back-out"
  76. section.removeAttr "data-#{C.ATTRIBUTE.DIRECTION}"
  77. if section.hasClass("asideHidding")
  78. section.removeClass("asideHidding").removeClass("aside")
  79. if section.hasClass("asideShowing")
  80. section.removeClass("asideShowing").addClass("aside")
  81. if section.hasClass("shadowing")
  82. section.removeClass("shadowing").addClass("shadow")
  83. if section.hasClass("unshadowing")
  84. section.removeClass("unshadowing").removeClass("shadow")
  85. _animating = false
  86. ###
  87. Create a new element to the browsing history based on the current section id.
  88. @method step
  89. @param {string} Id of the section
  90. ###
  91. step = (section_id) -> _history.push section_id if section_id isnt history()
  92. ###
  93. Returns the current browsing history section id.
  94. @method history
  95. @return {string} Current section id
  96. ###
  97. history = -> _history[_history.length - 1]
  98. ###
  99. Private methods
  100. ###
  101. _show = (future, current, backward, animating = true) ->
  102. if not backward and not _sameSection() then back false
  103. if current?
  104. if backward
  105. if animating then current.data(C.ATTRIBUTE.DIRECTION, "back-out")
  106. else current.removeClass("show")
  107. else
  108. future.addClass(C.CLASS.SHOW)
  109. future.data(C.ATTRIBUTE.DIRECTION, "in") if future.data(C.TRANSITION.ATTR)
  110. lng.Section.show current, future
  111. do _checkSectionAside if animating
  112. _checkSectionAside = (section) ->
  113. aside_id = lng.Element.Cache.section?.data("aside")
  114. is_other_aside = aside_id isnt lng.Element.Cache.aside?.attr("id")
  115. lng.Aside.hide (-> lng.Aside.show aside_id)
  116. _sameSection = ->
  117. if not event or not lng.Element.Cache.section then return true
  118. dispacher_section = lng.dom(event.target).closest("section,aside")
  119. same = dispacher_section.attr("id") is lng.Element.Cache.section.attr("id")
  120. return same
  121. _notCurrentTarget = (current, id) -> current?.attr(C.ATTRIBUTE.ID) isnt id
  122. _url = ->
  123. _hashed_url = ""
  124. _hashed_url += "#{section}/" for section in _history
  125. _hashed_url += lng.Element.Cache.article.attr "id"
  126. setTimeout (-> window.location.hash = _hashed_url), 0
  127. _updateNavigationElements = (article_id) ->
  128. article_id = lng.Element.Cache.article.attr(C.ATTRIBUTE.ID) unless article_id
  129. # Active visual signal for elements
  130. links = lng.dom(C.QUERY.ARTICLE_ROUTER).removeClass(C.CLASS.ACTIVE)
  131. links.filter("[data-view-article=#{article_id}]").addClass(C.CLASS.ACTIVE)
  132. # Hide/Show elements in current article
  133. nav = lng.Element.Cache.section.find(C.QUERY.ARTICLE_REFERENCE).addClass C.CLASS.HIDE
  134. nav.filter("[data-article~='#{article_id}']").removeClass C.CLASS.HIDE
  135. _removeLast = ->
  136. if _history.length > 1
  137. _history.length -= 1
  138. section : section
  139. back : back
  140. article : article
  141. history : history
  142. step : step
  143. animationEnd : animationEnd