Lungo.View.Aside.js 4.8 KB

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