Javi Jimenez Villar пре 13 година
родитељ
комит
3aeba6808a

+ 1 - 1
src/boot/Lungo.Boot.Events.coffee

@@ -53,7 +53,7 @@ Lungo.Boot.Events = do(lng = Lungo) ->
   _onArticle = (event) ->
     event.preventDefault()
     el = lng.dom @
-    lng.Router.article lng.Router.History.current(), el.data("view-article"), el
+    lng.Router.article lng.Router.history(), el.data("view-article"), el
     lng.Aside.hide()
 
   _onAside = (event) ->

+ 1 - 1
src/boot/Lungo.Boot.Layout.coffee

@@ -33,7 +33,7 @@ Lungo.Boot.Layout = do(lng = Lungo) ->
     article_id = history[history.length - 1]
     if history.length > 2
       history.length -= 2
-      lng.Router.History.add section for section in history
+      lng.Router.step section for section in history
     lng.Router.section section_id
     lng.Router.article section_id, article_id
 

+ 0 - 46
src/router/Lungo.Router.Article.coffee

@@ -1,46 +0,0 @@
-###
-Initialize the <articles> layout of a certain <section>
-
-@namespace Lungo
-@class Article
-
-@author Javier Jimenez Villar <javi@tapquo.com> || @soyjavi
-@author Guillermo Pascual <pasku@tapquo.com> || @pasku1
-###
-
-Lungo.Article = do(lng = Lungo) ->
-  C = lng.Constants
-
-
-  ###
-  @todo   Describe method
-  @method title
-  ###
-  title = (value) -> lng.Element.Cache.section.find(C.QUERY.TITLE).text value if value
-
-
-  ###
-  @todo   Describe method
-  @method switchNavItems
-  ###
-  switchNavItems = (article_id) ->
-    lng.Element.Cache.section.find(C.QUERY.NAVIGATION_ITEM).removeClass C.CLASS.ACTIVE
-    active_nav_items = "a[href=\"" + article_id + "\"][data-router=\"article\"]"
-    lng.Element.Cache.section.find(active_nav_items).addClass C.CLASS.ACTIVE
-    if lng.Element.Cache.aside
-      aside = lng.Element.Cache.aside
-      aside.find(C.QUERY.ACTIVE_LIST_ITEM).removeClass C.CLASS.ACTIVE
-      aside.find(active_nav_items).addClass(C.CLASS.ACTIVE).parent().addClass C.CLASS.ACTIVE
-
-
-  ###
-  @todo   Describe method
-  @method switchReferenceItems
-  ###
-  switchReferenceItems = (article_id, section) ->
-    reference = "[data-article=" + article_id.replace("#", "") + "]"
-    section.find(C.QUERY.REFERENCE_LINK).hide().siblings(reference).show()
-
-  title: title
-  switchReferenceItems: switchReferenceItems
-  switchNavItems: switchNavItems

+ 0 - 51
src/router/Lungo.Router.History.coffee

@@ -1,51 +0,0 @@
-###
-Stores the displayed <sections> as a historical.
-
-@namespace Lungo.Router
-@class History
-
-@author Javier Jimenez Villar <javi@tapquo.com> || @soyjavi
-@author Guillermo Pascual <pasku@tapquo.com> || @pasku1
-###
-
-Lungo.Router.History = do (lng = Lungo) ->
-  _history = []
-
-  ###
-  Create a new element to the browsing history based on the current section id.
-  @method add
-  @param  {string} Id of the section
-  ###
-  add = (section_id) ->
-    _history.push section_id if section_id isnt current()
-
-  ###
-  Returns the current browsing history section id.
-  @method current
-  @return {string} Current section id
-  ###
-  current = ->
-    _history[_history.length - 1]
-
-
-  ###
-  Removes the current item browsing history.
-  @method removeLast
-  ###
-  removeLast = ->
-    _history.length -= 1
-
-  ###
-  @todo
-  ###
-  url = ->
-    _url = ""
-    _url += "#{section}/" for section in _history
-    _url += lng.Element.Cache.article.attr "id"
-    setTimeout (-> window.location.hash = _url), 0
-
-
-  add: add
-  current: current
-  removeLast: removeLast
-  url: url

+ 6 - 1
src/router/Lungo.Router.Section.coffee

@@ -14,7 +14,12 @@ Lungo.Section = do(lng = Lungo) ->
     if lng.DEVICE is C.DEVICE.PHONE then _phone target else _tablet current, target
 
     lng.Element.Cache.section = target
-    lng.Element.Cache.article = target.find "#{C.ELEMENT.ARTICLE}.#{C.CLASS.ACTIVE}"
+
+    active_article = target.find "#{C.ELEMENT.ARTICLE}.#{C.CLASS.ACTIVE}"
+    if active_article.length is 0
+      active_article = target.find(C.ELEMENT.ARTICLE).first().addClass(C.CLASS.ACTIVE)
+
+    lng.Element.Cache.article = active_article
     lng.Element.Cache.aside = lng.Aside.active target
 
     current.trigger C.TRIGGER.UNLOAD if current

+ 55 - 28
src/router/Lungo.Router.coffee

@@ -9,8 +9,9 @@ Handles the <sections> and <articles> to show
 ###
 
 Lungo.Router = do(lng = Lungo) ->
-  C       = lng.Constants
-  HASHTAG = "#"
+  C        = lng.Constants
+  HASHTAG  = "#"
+  _history = []
 
 
   ###
@@ -19,19 +20,19 @@ Lungo.Router = do(lng = Lungo) ->
   @param    {string} Id of the <section>
   ###
   section = (section_id) ->
-    section_id = lng.Core.parseUrl(section_id)
     current = lng.Element.Cache.section
-    if _notCurrentTarget(section_id, current)
-      query = C.ELEMENT.SECTION + section_id
+    if _notCurrentTarget(current, section_id)
+      query = C.ELEMENT.SECTION + HASHTAG + section_id
       target = if current then current.siblings(query) else lng.dom(query)
       if target.length > 0
-        if lng.DEVICE is C.DEVICE.PHONE
-          if current?
-            lng.Section.defineTransition target, current
-            current.removeClass(C.CLASS.SHOW).addClass(C.CLASS.HIDE)
+        if lng.DEVICE is C.DEVICE.PHONE and current?
+          lng.Section.defineTransition target, current
+          current.removeClass(C.CLASS.SHOW).addClass(C.CLASS.HIDE)
 
         lng.Section.show current, target
-        lng.Router.History.add section_id
+        lng.Router.step section_id
+        do _url
+        do _updateNavigationElements
 
 
   ###
@@ -41,19 +42,14 @@ Lungo.Router = do(lng = Lungo) ->
   @param    {string} <article> Id
   ###
   article = (section_id, article_id, element) ->
-    article_id = lng.Core.parseUrl(article_id)
     current = lng.Element.Cache.article
-    if _notCurrentTarget(article_id, current)
-      section section_id
-      target = lng.Element.Cache.section.find(C.ELEMENT.ARTICLE + article_id)
+    if _notCurrentTarget(current, article_id)
+      lng.Router.section section_id
+      target = lng.Element.Cache.section.find "##{article_id}"
       if target.length > 0
-        current = lng.Element.Cache.section.children(C.ELEMENT.ARTICLE)  if _sectionId(current) isnt _sectionId(target)
         current.removeClass(C.CLASS.ACTIVE).trigger C.TRIGGER.UNLOAD
-        target.addClass(C.CLASS.ACTIVE).trigger C.TRIGGER.LOAD
-        lng.Element.Cache.article = target
-        lng.Article.switchNavItems article_id
-        lng.Article.switchReferenceItems article_id, lng.Element.Cache.section
-        lng.Article.title element.data(C.ATTRIBUTE.TITLE)  if element
+        lng.Element.Cache.article = target.addClass(C.CLASS.ACTIVE).trigger(C.TRIGGER.LOAD)
+        do _url
 
 
   ###
@@ -61,29 +57,60 @@ Lungo.Router = do(lng = Lungo) ->
   @method   back
   ###
   back = ->
-    lng.Router.History.removeLast()
-
+    _removeLast()
     current = lng.Element.Cache.section
-    target = current.siblings(C.ELEMENT.SECTION + lng.Router.History.current())
+    query = C.ELEMENT.SECTION + HASHTAG + history()
+    target = current.siblings(query)
     if lng.DEVICE is C.DEVICE.PHONE
       lng.Aside.hide() if lng.Element.Cache.aside and lng.Element.Cache.aside.hasClass(C.CLASS.SHOW)
-
       lng.Section.assignTransition target, target.data C.TRANSITION.ORIGIN
       current.removeClass(C.CLASS.SHOW).addClass(C.CLASS.HIDING)
       setTimeout (-> current.removeClass(C.CLASS.HIDING)), C.TRANSITION.DURATION
 
     lng.Section.show current, target
+    do _url
+    do _updateNavigationElements
+
+
+  ###
+  Create a new element to the browsing history based on the current section id.
+  @method step
+  @param  {string} Id of the section
+  ###
+  step = (section_id) -> _history.push section_id if section_id isnt history()
+
+  ###
+  Returns the current browsing history section id.
+  @method history
+  @return {string} Current section id
+  ###
+  history = -> _history[_history.length - 1]
 
 
   ###
   Private methods
   ###
-  _notCurrentTarget = (target, element) ->
-    (if (not element or target isnt HASHTAG + element.attr(C.ATTRIBUTE.ID)) then true else false)
+  _notCurrentTarget = (current, id) -> current?.attr(C.ATTRIBUTE.ID) isnt id
+
+  _url = ->
+    _hashed_url = ""
+    _hashed_url += "#{section}/" for section in _history
+    _hashed_url += lng.Element.Cache.article.attr "id"
+    setTimeout (-> window.location.hash = _hashed_url), 0
+    do _updateNavigationElements
+
+  _updateNavigationElements = (element) ->
+    article_id = lng.Element.Cache.article.attr C.ATTRIBUTE.ID
+    lng.dom(C.QUERY.ARTICLE_ROUTER).removeClass(C.CLASS.ACTIVE).siblings("[data-view-article=#{article_id}]").addClass(C.CLASS.ACTIVE)
+    lng.dom(C.QUERY.REFERENCE_LINK).hide().siblings("[data-article=#{article_id}]").show()
+    # if element?.data(C.ATTRIBUTE.TITLE)
+    #   lng.Element.Cache.section.find(C.QUERY.TITLE).text element.data(C.ATTRIBUTE.TITLE)
+
+  _removeLast = -> _history.length -= 1
 
-  _sectionId = (element) ->
-    element.parent(C.ELEMENT.SECTION).attr C.ATTRIBUTE.ID
 
   section: section
   article: article
   back: back
+  history: history
+  step: step