Lungo.View.Aside.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /**
  2. * Initialize the <articles> layout of a certain <section>
  3. *
  4. * @namespace Lungo.View
  5. * @class Aside
  6. *
  7. * @author Javier Jimenez Villar <javi@tapquo.com> || @soyjavi
  8. */
  9. Lungo.View.Aside = (function(lng, undefined) {
  10. var ELEMENT = lng.Constants.ELEMENT;
  11. var CLASS = lng.Constants.CLASS;
  12. var ATTRIBUTE = lng.Constants.ATTRIBUTE;
  13. var DEVICE = lng.Constants.DEVICE;
  14. /**
  15. * Toggle an aside element
  16. *
  17. * @method toggle
  18. *
  19. * @param {string} Aside id
  20. */
  21. var toggle = function(aside_id) {
  22. aside = _findAside(aside_id);
  23. if (aside) {
  24. var is_visible = aside.hasClass(CLASS.SHOW);
  25. if (is_visible) {
  26. lng.View.Aside.hide();
  27. } else {
  28. lng.View.Aside.show(aside);
  29. }
  30. }
  31. aside = null;
  32. };
  33. /**
  34. * Display an aside element with a particular <section>
  35. *
  36. * @method show
  37. *
  38. * @param {string} Aside id
  39. */
  40. var show = function(aside) {
  41. if (lng.Core.toType(aside) == 'string') aside = _findAside(lng.Core.parseUrl(aside));
  42. if (aside) {
  43. lng.Element.Cache.aside = aside;
  44. aside.addClass(CLASS.SHOW);
  45. if (lng.DEVICE == DEVICE.phone) {
  46. var aside_stylesheet = _asideStylesheet(aside);
  47. lng.Element.Cache.section.addClass(aside_stylesheet).addClass(CLASS.ASIDE);
  48. }
  49. }
  50. aside = null;
  51. };
  52. /**
  53. * Hide an aside element with a particular section
  54. *
  55. * @method hide
  56. */
  57. var hide = function(target) {
  58. if (lng.DEVICE == DEVICE.phone) {
  59. var aside = target || lng.Element.Cache.aside;
  60. if (aside) {
  61. lng.Element.Cache.section.removeClass(CLASS.ASIDE).removeClass(CLASS.RIGHT).removeClass(CLASS.SMALL);
  62. var aside_stylesheet = _asideStylesheet(aside);
  63. if (aside_stylesheet) {
  64. lng.Element.Cache.section.removeClass(aside_stylesheet);
  65. }
  66. setTimeout(function() {
  67. lng.Element.Cache.aside = null;
  68. aside.removeClass(CLASS.SHOW);
  69. }, lng.Constants.TRANSITION.DURATION);
  70. }
  71. }
  72. };
  73. var suscribeEvents = function(hrefs) {
  74. var MIN_XDIFF = parseInt(document.body.getBoundingClientRect().width / 3, 10);
  75. hrefs.each(function() {
  76. var STARTED = false;
  77. var a = lng.dom(this);
  78. var section = a.closest("section");
  79. var aside = lng.dom(a.attr("href"));
  80. section.swiping(function(gesture) {
  81. if(!section.hasClass("aside")) {
  82. var xdiff = gesture.currentTouch.x - gesture.iniTouch.x;
  83. var ydiff = Math.abs(gesture.currentTouch.y - gesture.iniTouch.y);
  84. STARTED = STARTED ? true : xdiff > 3*ydiff && xdiff < 50;
  85. if(STARTED) {
  86. xdiff = xdiff > 256 ? 256 : xdiff < 0 ? 0 : xdiff;
  87. aside.addClass(CLASS.SHOW);
  88. section.vendor('transform', 'translateX(' + xdiff + 'px)');
  89. section.vendor('transition-duration', '0s');
  90. } else {
  91. section.attr('style', '');
  92. }
  93. }
  94. });
  95. section.swipe(function(gesture) {
  96. var diff = gesture.currentTouch.x - gesture.iniTouch.x;
  97. var ydiff = Math.abs(gesture.currentTouch.y - gesture.iniTouch.y);
  98. section.attr('style', '');
  99. if(diff > MIN_XDIFF && STARTED) show(aside);
  100. else hide(aside);
  101. STARTED = false;
  102. });
  103. });
  104. };
  105. var _findAside = function(aside_id) {
  106. var aside = null;
  107. var asides = lng.dom(ELEMENT.ASIDE);
  108. if (asides.length == 1) {
  109. var current_id = '#' + asides[0].id ;
  110. if (current_id == aside_id) {
  111. aside = lng.dom(asides[0]);
  112. }
  113. }
  114. else if (asides.length > 1) {
  115. aside = asides.siblings(ELEMENT.ASIDE + aside_id);
  116. }
  117. return aside;
  118. };
  119. var _asideStylesheet = function(aside) {
  120. var aside_stylesheet = aside.attr(ATTRIBUTE.CLASS);
  121. var classes = '';
  122. //@todo: Refactor
  123. if (aside_stylesheet) {
  124. classes += (aside_stylesheet.indexOf(CLASS.RIGHT) > -1) ? CLASS.RIGHT + ' ': '';
  125. classes += (aside_stylesheet.indexOf(CLASS.SMALL) > -1) ? CLASS.SMALL + ' ': '';
  126. }
  127. return classes;
  128. };
  129. return {
  130. suscribeEvents: suscribeEvents,
  131. toggle: toggle,
  132. show: show,
  133. hide: hide
  134. };
  135. })(Lungo);