Jelajahi Sumber

New async navigation via URL

Javi Jimenez Villar 13 tahun lalu
induk
melakukan
20d99af15f

+ 21 - 13
src/boot/Lungo.Boot.Layout.coffee

@@ -8,10 +8,8 @@ Initialize the Layout of LungoJS (if it's a mobile environment)
 ###
 
 Lungo.Boot.Layout = do(lng = Lungo) ->
-  ELEMENT = lng.Constants.ELEMENT
-  CLASS = lng.Constants.CLASS
-  ATTRIBUTE = lng.Constants.ATTRIBUTE
-  QUERY = lng.Constants.QUERY
+  C       = lng.Constants
+  HASHTAG = "#"
 
   ###
   Initializes all <section> & <article> of the project
@@ -19,19 +17,29 @@ Lungo.Boot.Layout = do(lng = Lungo) ->
   ###
   init = ->
     lng.Fallback.fixPositionInAndroid()
-    _initFirstSection()
-    _initElement QUERY.LIST_IN_ELEMENT, _createListElement
-    _initElement QUERY.ELEMENT_SCROLLABLE, _scrollFix
 
-  _initFirstSection = ->
-    section = lng.dom(ELEMENT.SECTION).first()
-    lng.Router.section section.attr("id")  if section
+    if window.location.hash?.length >= 2 then _initSectionbyUrl() else _initSection()
+    _initElement C.QUERY.LIST_IN_ELEMENT, _createListElement
+    _initElement C.QUERY.ELEMENT_SCROLLABLE, _scrollFix
+
+  _initSectionbyUrl = ->
+    history = window.location.hash.replace(HASHTAG, "").split("/")
+    section_id = history[history.length - 2]
+    article_id = history[history.length - 1]
+    if history.length > 2
+      history.length -= 2
+      lng.Router.History.add section for section in history
+    lng.Router.section section_id
+    lng.Router.article section_id, article_id
+
+  _initSection = ->
+    section = lng.dom(C.ELEMENT.SECTION).first()
+    lng.Router.section section.attr(C.ATTR.ID)  if section
 
   _initElement = (selector, callback) ->
     found_elements = lng.dom(selector)
     i = 0
     len = found_elements.length
-
     while i < len
       element = lng.dom(found_elements[i])
       lng.Core.execute callback, element
@@ -39,8 +47,8 @@ Lungo.Boot.Layout = do(lng = Lungo) ->
 
   _createListElement = (element) ->
     if element.children().length is 0
-      element_id = element.attr(ATTRIBUTE.ID)
-      element.append ELEMENT.LIST
+      element_id = element.attr(C.ATTRIBUTE.ID)
+      element.append C.ELEMENT.LIST
 
   _scrollFix = (element) ->
     element[0].addEventListener "touchstart", ((event) ->

+ 20 - 65
src/modules/Lungo.Core.coffee

@@ -10,30 +10,22 @@ Contains all the common functions used in Lungo.
 
 Lungo.Core = do(lng = Lungo, $$ = Quo) ->
   ARRAY_PROTO = Array::
-  HASHTAG_CHARACTER = "#"
 
   ###
   Console system to display messages when you are in debug mode.
-
   @method log
-
-  @param {number} Severity based in (1)Log, (2)Warn, (>2)Error
-  @param {string} Message to show in console
+  @param  {number} Severity based in (1)Log, (2)Warn, (>2)Error
+  @param  {string} Message to show in console
   ###
   log = (severity, message) ->
     unless lng.Core.isMobile()
       console[(if (severity is 1) then "log" else (if (severity is 2) then "warn" else "error"))] message
-    else
 
 
-  # @todo : send to the server
-
   ###
   Executes callbacks based on the parameters received.
-
   @method execute
-
-  @param {Function} callback to execute
+  @param  {Function} callback to execute
   ###
   execute = ->
     args = toArray(arguments)
@@ -45,11 +37,9 @@ Lungo.Core = do(lng = Lungo, $$ = Quo) ->
   Creates a new function that, when called, itself calls this function in
   the context of the provided this value, with a given sequence of arguments
   preceding any provided when the new function was called.
-
   @method bind
-
-  @param {object} object to which the 'this' can refer in the new function when the new function is called.
-  @param {Function} method A function object.
+  @param  {object} object to which the 'this' can refer in the new function when the new function is called.
+  @param  {Function} method A function object.
   ###
   bind = (object, method) ->
     ->
@@ -60,10 +50,8 @@ Lungo.Core = do(lng = Lungo, $$ = Quo) ->
   Copy from any number of objects and mix them all into a new object.
   The implementation is simple; just loop through arguments and
   copy every property of every object passed to the function.
-
   @method mix
-
-  @param {object} arguments to mix them all into a new object.
+  @param  {object} arguments to mix them all into a new object.
   @return {object} child a new object with all the objects from the arguments mixed.
   ###
   mix = ->
@@ -83,9 +71,8 @@ Lungo.Core = do(lng = Lungo, $$ = Quo) ->
   Every object descended from Object inherits the hasOwnProperty method.
   This method can be used to determine whether an object has the specified property
   as a direct property of that object.
-
-  @param {object} object to test for a property's existence inside itself.
-  @param {string} property the name of the property to test.
+  @param  {object} object to test for a property's existence inside itself.
+  @param  {string} property the name of the property to test.
   @return {boolean} indicating whether the object has the specified property.
   ###
   isOwnProperty = (object, property) ->
@@ -94,54 +81,42 @@ Lungo.Core = do(lng = Lungo, $$ = Quo) ->
 
   ###
   Determine the internal JavaScript [[Class]] of an object.
-
-  @param {object} obj to get the real type of itself.
+  @param  {object} obj to get the real type of itself.
   @return {string} with the internal JavaScript [[Class]] of itself.
   ###
-  toType = (obj) ->
-    $$.toType obj
+  toType = (obj) -> $$.toType obj
 
 
   ###
   Convert an array-like object into a true JavaScript array.
-
-  @param {object} obj Any object to turn into a native Array.
+  @param  {object} obj Any object to turn into a native Array.
   @return {object} The object is now a plain array.
   ###
-  toArray = (obj) ->
-    ARRAY_PROTO.slice.call obj, 0
+  toArray = (obj) -> ARRAY_PROTO.slice.call obj, 0
 
 
   ###
   Determine if the current environment is a mobile environment
-
   @method isMobile
-
   @return {boolean} true if is mobile environment, false if not.
   ###
-  isMobile = ->
-    $$.isMobile()
+  isMobile = -> $$.isMobile()
 
 
   ###
   Returns information of execute environment
-
   @method environment
-
   @return {object} Environment information
   ###
-  environment = ->
-    $$.environment()
+  environment = -> $$.environment()
 
 
   ###
   Returns a ordered list of objects by a property
-
   @method orderByProperty
-
-  @param {list} List of objects
-  @param {string} Name of property
-  @param {string} Type of order: asc (ascendent) or desc (descendent)
+  @param  {list} List of objects
+  @param  {string} Name of property
+  @param  {string} Type of order: asc (ascendent) or desc (descendent)
   @return {list} Ordered list
   ###
   orderByProperty = (data, property, order) ->
@@ -150,31 +125,12 @@ Lungo.Core = do(lng = Lungo, $$ = Quo) ->
       (if (a[property] < b[property]) then -order_operator else (if (a[property] > b[property]) then order_operator else 0))
 
 
-
-  ###
-  Returns a correct URL using hashtag character
-
-  @method parseUrl
-
-  @param {string} Url
-  @return {string} Url parsed
-  ###
-  parseUrl = (href) ->
-    href_hashtag = href.lastIndexOf(HASHTAG_CHARACTER)
-    if href_hashtag > 0
-      href = href.substring(href_hashtag)
-    else href = HASHTAG_CHARACTER + href  if href_hashtag is -1
-    href
-
-
   ###
   Returns a Object in a list by a property value
-
   @method objectInListByProperty
-
-  @param {list} List of objects
-  @param {string} Name of property
-  @param {var} Value for comparision
+  @param  {list} List of objects
+  @param  {string} Name of property
+  @param  {var} Value for comparision
   @return {object} Instance of object founded (if exists)
   ###
   findByProperty = (list, property, value) ->
@@ -200,5 +156,4 @@ Lungo.Core = do(lng = Lungo, $$ = Quo) ->
   isMobile: isMobile
   environment: environment
   orderByProperty: orderByProperty
-  parseUrl: parseUrl
   findByProperty: findByProperty

+ 18 - 5
src/router/Lungo.Router.History.coffee

@@ -8,7 +8,7 @@ Stores the displayed <sections> as a historical.
 @author Guillermo Pascual <pasku@tapquo.com> || @pasku1
 ###
 
-Lungo.Router.History = do ->
+Lungo.Router.History = do (lng = Lungo) ->
   _history = []
 
   ###
@@ -16,23 +16,36 @@ Lungo.Router.History = do ->
   @method add
   @param  {string} Id of the section
   ###
-  add = (section_id) -> _history.push section_id  if section_id isnt current()
-
+  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]
+  current = ->
+    _history[_history.length - 1]
 
 
   ###
   Removes the current item browsing history.
   @method removeLast
   ###
-  removeLast = -> _history.length -= 1
+  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