Lungo.View.Aside.js 4.4 KB

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