Lungo.Boot.Layout.coffee 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. ###
  2. Initialize the Layout of LungoJS (if it's a mobile environment)
  3. @namespace Lungo.Boot
  4. @class Layout
  5. @author Javier Jimenez Villar <javi@tapquo.com> || @soyjavi
  6. ###
  7. Lungo.Boot.Layout = do(lng = Lungo) ->
  8. C = lng.Constants
  9. HASHTAG = "#"
  10. ###
  11. Initializes all <section> & <article> of the project
  12. @method init
  13. ###
  14. init = ->
  15. lng.Fallback.fixPositionInAndroid()
  16. if Lungo.Config.history and window.location.hash?.length >= 2
  17. do _initSectionbyUrl
  18. else
  19. do _initSection
  20. _initElement C.QUERY.LIST_IN_ELEMENT, _createListElement
  21. _initElement C.QUERY.ELEMENT_SCROLLABLE, _scrollFix
  22. ###
  23. Private methods
  24. ###
  25. _initSectionbyUrl = ->
  26. history = window.location.hash.replace(HASHTAG, "").split("/")
  27. section_id = history[history.length - 2]
  28. article_id = history[history.length - 1]
  29. if history.length > 2
  30. history.length -= 2
  31. lng.Router.step section for section in history
  32. lng.Router.section section_id
  33. lng.Router.article section_id, article_id
  34. _initSection = ->
  35. section = lng.dom(C.ELEMENT.SECTION).first()
  36. lng.Router.section section.attr(C.ATTRIBUTE.ID) if section
  37. _initElement = (selector, callback) ->
  38. found_elements = lng.dom(selector)
  39. i = 0
  40. len = found_elements.length
  41. while i < len
  42. element = lng.dom(found_elements[i])
  43. lng.Core.execute callback, element
  44. i++
  45. _createListElement = (element) ->
  46. if element.children().length is 0
  47. element_id = element.attr(C.ATTRIBUTE.ID)
  48. element.append C.ELEMENT.LIST
  49. _scrollFix = (element) ->
  50. element[0].addEventListener "touchstart", ((event) ->
  51. scrollTop = @scrollTop
  52. @scrollTop = 1 if scrollTop <= 1
  53. @scrollTop = @scrollHeight - @offsetHeight - 1 if scrollTop + @offsetHeight >= @scrollHeight
  54. ), false
  55. init: init