Lungo.Router.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /**
  2. * Handles the <sections> and <articles> to show
  3. *
  4. * @namespace Lungo
  5. * @class Router
  6. *
  7. * @author Javier Jimenez Villar <javi@tapquo.com> || @soyjavi
  8. * @author Guillermo Pascual <pasku@tapquo.com> || @pasku1
  9. */
  10. Lungo.Router = (function(lng, undefined) {
  11. var CLASS = lng.Constants.CLASS;
  12. var ELEMENT = lng.Constants.ELEMENT;
  13. var ERROR = lng.Constants.ERROR;
  14. var TRIGGER = lng.Constants.TRIGGER;
  15. var ATTRIBUTE = lng.Constants.ATTRIBUTE;
  16. var HASHTAG_CHARACTER = '#';
  17. /**
  18. * Navigate to a <section>.
  19. *
  20. * @method section
  21. *
  22. * @param {string} Id of the <section>
  23. */
  24. var section = function(section_id) {
  25. section_id = lng.Core.parseUrl(section_id);
  26. var current = lng.Element.Cache.section;
  27. if (_notCurrentTarget(section_id, current)) {
  28. var target = current.siblings(ELEMENT.SECTION + section_id);
  29. if (target.length > 0) {
  30. target_transition = target.data('transition');
  31. if (target_transition) {
  32. _assignTransitionOrigin(current);
  33. _assignTransition(current, target_transition);
  34. }
  35. current.removeClass(CLASS.SHOW).addClass(CLASS.HIDE);
  36. target.removeClass(CLASS.HIDE).addClass(CLASS.SHOW);
  37. lng.Element.Cache.section = target;
  38. lng.Element.Cache.article = target.find(ELEMENT.ARTICLE + '.' + CLASS.ACTIVE);
  39. lng.Router.History.add(section_id);
  40. _sectionTriggers(current, target);
  41. }
  42. }
  43. };
  44. /**
  45. * Displays the <article> in a particular <section>.
  46. *
  47. * @method article
  48. *
  49. * @param {string} <section> Id
  50. * @param {string} <article> Id
  51. */
  52. var article = function(section_id, article_id, element) {
  53. article_id = lng.Core.parseUrl(article_id);
  54. var current = lng.Element.Cache.article;
  55. if (_notCurrentTarget(article_id, current)) {
  56. section(section_id);
  57. var target = lng.Element.Cache.section.find(ELEMENT.ARTICLE + article_id);
  58. if (target.length > 0) {
  59. if (_sectionId(current) !== _sectionId(target)) {
  60. current = lng.Element.Cache.section.children(ELEMENT.ARTICLE);
  61. }
  62. current.removeClass(CLASS.ACTIVE).trigger(TRIGGER.UNLOAD);
  63. target.addClass(CLASS.ACTIVE).trigger(TRIGGER.LOAD);
  64. lng.Element.Cache.article = target;
  65. lng.View.Article.switchNavItems(article_id);
  66. lng.View.Article.switchReferenceItems(article_id, lng.Element.Cache.section);
  67. if (element) lng.View.Article.title(element.data(ATTRIBUTE.TITLE));
  68. }
  69. }
  70. };
  71. /**
  72. * Displays the <aside> in a particular <section>.
  73. *
  74. * @method aside
  75. *
  76. * @param {string} <section> Id
  77. * @param {string} <aside> Id
  78. */
  79. var aside = function(section_id, aside_id) {
  80. aside_id = lng.Core.parseUrl(aside_id);
  81. lng.View.Aside.toggle(aside_id);
  82. };
  83. /**
  84. * Return to previous section.
  85. *
  86. * @method back
  87. */
  88. var back = function() {
  89. var current = lng.Element.Cache.section;
  90. if (lng.Element.Cache.aside) {
  91. lng.View.Aside.hide();
  92. setTimeout(function() {
  93. _back(current);
  94. }, lng.Constants.TRANSITION.DURATION);
  95. } else {
  96. _back(current);
  97. }
  98. };
  99. var _back = function(current) {
  100. current.removeClass(CLASS.SHOW).addClass(CLASS.HIDING);
  101. setTimeout(function() {
  102. current.removeClass(CLASS.HIDING);
  103. }, lng.Constants.TRANSITION.DURATION);
  104. lng.Router.History.removeLast();
  105. target = current.siblings(ELEMENT.SECTION + lng.Router.History.current());
  106. _assignTransition(target, target.data('transition-origin'));
  107. target.removeClass(CLASS.HIDE).addClass(CLASS.SHOW);
  108. lng.Element.Cache.section = target;
  109. lng.Element.Cache.article = target.find(ELEMENT.ARTICLE + "." + CLASS.ACTIVE);
  110. _sectionTriggers(current, target);
  111. };
  112. var _notCurrentTarget = function(target, element) {
  113. return (target !== HASHTAG_CHARACTER + element.attr('id')) ? true : false;
  114. };
  115. var _sectionId = function(element) {
  116. return element.parent('section').attr('id');
  117. };
  118. var _sectionTriggers = function(current, target) {
  119. current.trigger(TRIGGER.UNLOAD);
  120. target.trigger(TRIGGER.LOAD);
  121. };
  122. var _assignTransition = function(section, transitionName) {
  123. section.data('transition', transitionName);
  124. };
  125. var _assignTransitionOrigin = function(section) {
  126. section.data('transition-origin', section.data('transition'));
  127. };
  128. return {
  129. section: section,
  130. article: article,
  131. aside: aside,
  132. back: back
  133. };
  134. })(Lungo);