Lungo.Boot.Layout.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /**
  2. * Initialize the Layout of LungoJS (if it's a mobile environment)
  3. *
  4. * @namespace Lungo.Boot
  5. * @class Layout
  6. *
  7. * @author Javier Jimenez Villar <javi@tapquo.com> || @soyjavi
  8. */
  9. Lungo.Boot.Layout = (function(lng, undefined) {
  10. var ELEMENT = lng.Constants.ELEMENT;
  11. var CLASS = lng.Constants.CLASS;
  12. var ATTRIBUTE = lng.Constants.ATTRIBUTE;
  13. var QUERY = lng.Constants.QUERY;
  14. /**
  15. * Initializes all <section> & <article> of the project
  16. *
  17. * @method init
  18. *
  19. */
  20. var init = function() {
  21. lng.Fallback.fixPositionInAndroid();
  22. _initFirstSection();
  23. _initElement(QUERY.LIST_IN_ELEMENT, _createListElement);
  24. _initElement(QUERY.ELEMENT_SCROLLABLE, _scrollFix);
  25. };
  26. var _initFirstSection = function() {
  27. var section = lng.dom(ELEMENT.SECTION).first().addClass(CLASS.SHOW);
  28. lng.Element.Cache.section = section;
  29. lng.Element.Cache.article = section.children(ELEMENT.ARTICLE + "." + CLASS.ACTIVE);
  30. lng.View.Article.switchReferenceItems(lng.Element.Cache.article.attr("id"), section);
  31. var section_id = '#' + section.attr(ATTRIBUTE.ID);
  32. lng.Router.History.add(section_id);
  33. };
  34. var _initElement = function(selector, callback) {
  35. var found_elements = lng.dom(selector);
  36. for (var i = 0, len = found_elements.length; i < len; i++) {
  37. var element = lng.dom(found_elements[i]);
  38. lng.Core.execute(callback, element);
  39. }
  40. };
  41. var _createListElement = function(element) {
  42. if (element.children().length === 0) {
  43. var element_id = element.attr(ATTRIBUTE.ID);
  44. element.append(ELEMENT.LIST);
  45. }
  46. };
  47. var _scrollFix = function(element) {
  48. element[0].addEventListener('touchstart', function(event) {
  49. scrollTop = this.scrollTop;
  50. if(scrollTop <= 1) {
  51. this.scrollTop = 1;
  52. }
  53. if(scrollTop + this.offsetHeight >= this.scrollHeight) {
  54. this.scrollTop = this.scrollHeight - this.offsetHeight - 1;
  55. }
  56. }, false);
  57. };
  58. return {
  59. init: init
  60. };
  61. })(Lungo);