Lungo.Router.coffee 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. ###
  8. Lungo.Router = do(lng = Lungo) ->
  9. C = lng.Constants
  10. HASHTAG = "#"
  11. _history = []
  12. ###
  13. Navigate to a <section>.
  14. @method section
  15. @param {string} Id of the <section>
  16. ###
  17. section = (section_id) ->
  18. current = lng.Element.Cache.section
  19. if _notCurrentTarget(current, section_id)
  20. query = C.ELEMENT.SECTION + HASHTAG + section_id
  21. target = if current then current.siblings(query) else lng.dom(query)
  22. if target.length > 0
  23. if lng.DEVICE is C.DEVICE.PHONE and current?
  24. lng.Section.defineTransition target, current
  25. current.removeClass(C.CLASS.SHOW).addClass(C.CLASS.HIDE)
  26. lng.Section.show current, target
  27. lng.Router.step section_id
  28. do _url
  29. do _updateNavigationElements
  30. ###
  31. Displays the <article> in a particular <section>.
  32. @method article
  33. @param {string} <section> Id
  34. @param {string} <article> Id
  35. ###
  36. article = (section_id, article_id, element) ->
  37. current = lng.Element.Cache.article
  38. if _notCurrentTarget(current, article_id)
  39. lng.Router.section section_id
  40. target = lng.Element.Cache.section.find "##{article_id}"
  41. if target.length > 0
  42. current.removeClass(C.CLASS.ACTIVE).trigger C.TRIGGER.UNLOAD
  43. lng.Element.Cache.article = target.addClass(C.CLASS.ACTIVE).trigger(C.TRIGGER.LOAD)
  44. if element.data(C.ATTRIBUTE.TITLE)?
  45. lng.Element.Cache.section.find(C.QUERY.TITLE).text element.data(C.ATTRIBUTE.TITLE)
  46. do _url
  47. ###
  48. Return to previous section.
  49. @method back
  50. ###
  51. back = ->
  52. _removeLast()
  53. current = lng.Element.Cache.section
  54. query = C.ELEMENT.SECTION + HASHTAG + history()
  55. target = current.siblings(query)
  56. if lng.DEVICE is C.DEVICE.PHONE
  57. lng.Aside.hide() if lng.Element.Cache.aside and lng.Element.Cache.aside.hasClass(C.CLASS.SHOW)
  58. lng.Section.assignTransition target, target.data C.TRANSITION.ORIGIN
  59. current.removeClass(C.CLASS.SHOW).addClass(C.CLASS.HIDING)
  60. setTimeout (-> current.removeClass(C.CLASS.HIDING)), C.TRANSITION.DURATION
  61. lng.Section.show current, target
  62. lng.Aside.show()
  63. do _url
  64. do _updateNavigationElements
  65. ###
  66. Create a new element to the browsing history based on the current section id.
  67. @method step
  68. @param {string} Id of the section
  69. ###
  70. step = (section_id) -> _history.push section_id if section_id isnt history()
  71. ###
  72. Returns the current browsing history section id.
  73. @method history
  74. @return {string} Current section id
  75. ###
  76. history = -> _history[_history.length - 1]
  77. ###
  78. Private methods
  79. ###
  80. _notCurrentTarget = (current, id) -> current?.attr(C.ATTRIBUTE.ID) isnt id
  81. _url = ->
  82. _hashed_url = ""
  83. _hashed_url += "#{section}/" for section in _history
  84. _hashed_url += lng.Element.Cache.article.attr "id"
  85. setTimeout (-> window.location.hash = _hashed_url), 0
  86. do _updateNavigationElements
  87. _updateNavigationElements = (element) ->
  88. article_id = lng.Element.Cache.article.attr C.ATTRIBUTE.ID
  89. lng.dom(C.QUERY.ARTICLE_ROUTER).removeClass(C.CLASS.ACTIVE).siblings("[data-view-article=#{article_id}]").addClass(C.CLASS.ACTIVE)
  90. lng.dom(C.QUERY.ARTICLE_REFERENCE).hide()
  91. lng.dom("[data-article=#{article_id}]").style("display", "-webkit-box")
  92. _removeLast = -> _history.length -= 1
  93. section: section
  94. article: article
  95. back: back
  96. history: history
  97. step: step