Lungo.View.Aside.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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.CURRENT);
  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(aside);
  41. if (aside) {
  42. lng.Element.Current.aside = aside;
  43. var aside_stylesheet = _asideStylesheet(aside);
  44. aside.addClass(CLASS.CURRENT);
  45. lng.Element.Current.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() {
  55. var aside = lng.Element.Current.aside;
  56. if (aside) {
  57. lng.Element.Current.section.removeClass(CLASS.ASIDE);
  58. var aside_stylesheet = _asideStylesheet(aside);
  59. if (aside_stylesheet) {
  60. lng.Element.Current.section.removeClass(aside_stylesheet);
  61. }
  62. setTimeout(function() {
  63. aside.removeClass(CLASS.CURRENT);
  64. lng.Element.Current.aside = null;
  65. }, 300);
  66. }
  67. };
  68. var _findAside = function(aside_id) {
  69. var aside = null;
  70. var asides_length = lng.Element.asides.length;
  71. if (asides_length == 1) {
  72. var current_id = '#' + lng.Element.asides[0].id ;
  73. if (current_id == aside_id) {
  74. aside = lng.dom(lng.Element.asides[0]);
  75. }
  76. }
  77. else if (asides_length > 1) {
  78. aside = lng.Element.asides.siblings(ELEMENT.ASIDE + aside_id);
  79. }
  80. return aside;
  81. };
  82. var _asideStylesheet = function(aside) {
  83. var aside_stylesheet = aside.attr(ATTRIBUTE.CLASS);
  84. var classes = '';
  85. //@todo: Refactor
  86. if (aside_stylesheet) {
  87. classes += (aside_stylesheet.indexOf(CLASS.RIGHT) > -1) ? CLASS.RIGHT : '';
  88. classes += (aside_stylesheet.indexOf(CLASS.SMALL) > -1) ? CLASS.SMALL : '';
  89. }
  90. return classes;
  91. };
  92. return {
  93. toggle: toggle,
  94. show: show,
  95. hide: hide
  96. };
  97. })(LUNGO);