Lungo.Router.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 DEVICE = lng.Constants.DEVICE;
  17. var HASHTAG_CHARACTER = '#';
  18. /**
  19. * Navigate to a <section>.
  20. *
  21. * @method section
  22. *
  23. * @param {string} Id of the <section>
  24. */
  25. var section = function(section_id) {
  26. section_id = lng.Core.parseUrl(section_id);
  27. var current = lng.Element.Cache.section;
  28. if (_notCurrentTarget(section_id, current)) {
  29. var query = ELEMENT.SECTION + section_id;
  30. var target = (current) ? current.siblings(query) : lng.dom(query);
  31. if (target.length > 0) {
  32. if (current) {
  33. _defineTransition(target, current);
  34. current.removeClass(CLASS.SHOW).addClass(CLASS.HIDE);
  35. }
  36. target.removeClass(CLASS.HIDE).addClass(CLASS.SHOW);
  37. _attachAside(target);
  38. lng.Element.Cache.section = target;
  39. lng.Element.Cache.article = target.find(ELEMENT.ARTICLE + '.' + CLASS.ACTIVE);
  40. lng.Router.History.add(section_id);
  41. _sectionTriggers(current, target);
  42. }
  43. }
  44. };
  45. /**
  46. * Displays the <article> in a particular <section>.
  47. *
  48. * @method article
  49. *
  50. * @param {string} <section> Id
  51. * @param {string} <article> Id
  52. */
  53. var article = function(section_id, article_id, element) {
  54. article_id = lng.Core.parseUrl(article_id);
  55. var current = lng.Element.Cache.article;
  56. if (_notCurrentTarget(article_id, current)) {
  57. section(section_id);
  58. var target = lng.Element.Cache.section.find(ELEMENT.ARTICLE + article_id);
  59. if (target.length > 0) {
  60. if (_sectionId(current) !== _sectionId(target)) {
  61. current = lng.Element.Cache.section.children(ELEMENT.ARTICLE);
  62. }
  63. current.removeClass(CLASS.ACTIVE).trigger(TRIGGER.UNLOAD);
  64. target.addClass(CLASS.ACTIVE).trigger(TRIGGER.LOAD);
  65. lng.Element.Cache.article = target;
  66. lng.View.Article.switchNavItems(article_id);
  67. lng.View.Article.switchReferenceItems(article_id, lng.Element.Cache.section);
  68. if (element) lng.View.Article.title(element.data(ATTRIBUTE.TITLE));
  69. }
  70. }
  71. };
  72. /**
  73. * Displays the <aside> in a particular <section>.
  74. *
  75. * @method aside
  76. *
  77. * @param {string} <section> Id
  78. * @param {string} <aside> Id
  79. */
  80. var aside = function(section_id, aside_id) {
  81. aside_id = lng.Core.parseUrl(aside_id);
  82. lng.View.Aside.toggle(aside_id);
  83. };
  84. /**
  85. * Return to previous section.
  86. *
  87. * @method back
  88. */
  89. var back = function() {
  90. var current = lng.Element.Cache.section;
  91. if (lng.Element.Cache.aside) {
  92. lng.View.Aside.hide();
  93. setTimeout(function() {
  94. _back(current);
  95. }, lng.Constants.TRANSITION.DURATION);
  96. } else {
  97. _back(current);
  98. }
  99. };
  100. var _back = function(current) {
  101. current.removeClass(CLASS.SHOW).addClass(CLASS.HIDING);
  102. setTimeout(function() {
  103. current.removeClass(CLASS.HIDING);
  104. }, lng.Constants.TRANSITION.DURATION);
  105. lng.Router.History.removeLast();
  106. target = current.siblings(ELEMENT.SECTION + lng.Router.History.current());
  107. _assignTransition(target, target.data('transition-origin'));
  108. _attachAside(target);
  109. target.removeClass(CLASS.HIDE).addClass(CLASS.SHOW);
  110. lng.Element.Cache.section = target;
  111. lng.Element.Cache.article = target.find(ELEMENT.ARTICLE + "." + CLASS.ACTIVE);
  112. _sectionTriggers(current, target);
  113. };
  114. var _notCurrentTarget = function(target, element) {
  115. return (!element || target !== HASHTAG_CHARACTER + element.attr('id')) ? true : false;
  116. };
  117. var _sectionId = function(element) {
  118. return element.parent('section').attr('id');
  119. };
  120. var _sectionTriggers = function(current, target) {
  121. if (current) current.trigger(TRIGGER.UNLOAD);
  122. target.trigger(TRIGGER.LOAD);
  123. };
  124. var _defineTransition = function(target, current) {
  125. target_transition = target.data('transition');
  126. if (target_transition) {
  127. _assignTransitionOrigin(current);
  128. _assignTransition(current, target_transition);
  129. }
  130. };
  131. var _assignTransition = function(section, transitionName) {
  132. section.data('transition', transitionName);
  133. };
  134. var _assignTransitionOrigin = function(section) {
  135. section.data('transition-origin', section.data('transition'));
  136. };
  137. var _attachAside = function(target) {
  138. if (lng.Element.Cache.aside) lng.Element.Cache.aside.removeClass(CLASS.SHOW);
  139. if (target.data("aside") && lng.DEVICE != DEVICE.PHONE) {
  140. lng.View.Aside.show(target.data("aside"));
  141. }
  142. };
  143. return {
  144. section: section,
  145. article: article,
  146. aside: aside,
  147. back: back
  148. };
  149. })(Lungo);