Lungo.Router.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. _toggleSection(current, target);
  37. lng.Router.History.add(section_id);
  38. }
  39. }
  40. };
  41. /**
  42. * Displays the <article> in a particular <section>.
  43. *
  44. * @method article
  45. *
  46. * @param {string} <section> Id
  47. * @param {string} <article> Id
  48. */
  49. var article = function(section_id, article_id, element) {
  50. article_id = lng.Core.parseUrl(article_id);
  51. var current = lng.Element.Cache.article;
  52. if (_notCurrentTarget(article_id, current)) {
  53. section(section_id);
  54. var target = lng.Element.Cache.section.find(ELEMENT.ARTICLE + article_id);
  55. if (target.length > 0) {
  56. if (_sectionId(current) !== _sectionId(target)) {
  57. current = lng.Element.Cache.section.children(ELEMENT.ARTICLE);
  58. }
  59. current.removeClass(CLASS.ACTIVE).trigger(TRIGGER.UNLOAD);
  60. target.addClass(CLASS.ACTIVE).trigger(TRIGGER.LOAD);
  61. lng.Element.Cache.article = target;
  62. lng.View.Article.switchNavItems(article_id);
  63. lng.View.Article.switchReferenceItems(article_id, lng.Element.Cache.section);
  64. if (element) lng.View.Article.title(element.data(ATTRIBUTE.TITLE));
  65. }
  66. }
  67. };
  68. /**
  69. * Return to previous section.
  70. *
  71. * @method back
  72. */
  73. var back = function() {
  74. var current = lng.Element.Cache.section;
  75. if (lng.DEVICE == DEVICE.PHONE && lng.Element.Cache.aside && lng.Element.Cache.aside.hasClass(CLASS.SHOW)) {
  76. lng.View.Aside.hide();
  77. setTimeout(function() {
  78. _back(current);
  79. }, lng.Constants.TRANSITION.DURATION);
  80. } else {
  81. _back(current);
  82. }
  83. };
  84. var _back = function(current) {
  85. current.removeClass(CLASS.SHOW).addClass(CLASS.HIDING);
  86. setTimeout(function() {
  87. current.removeClass(CLASS.HIDING);
  88. }, lng.Constants.TRANSITION.DURATION);
  89. lng.Router.History.removeLast();
  90. target = current.siblings(ELEMENT.SECTION + lng.Router.History.current());
  91. _assignTransition(target, target.data('transition-origin'));
  92. _toggleSection(current, target);
  93. };
  94. var _toggleSection = function(current, target) {
  95. target.removeClass(CLASS.HIDE).addClass(CLASS.SHOW);
  96. lng.Element.Cache.section = target;
  97. lng.Element.Cache.article = target.find(ELEMENT.ARTICLE + "." + CLASS.ACTIVE);
  98. lng.Element.Cache.aside = lng.View.Aside.active(target);
  99. _sectionTriggers(current, target);
  100. };
  101. var _notCurrentTarget = function(target, element) {
  102. return (!element || target !== HASHTAG_CHARACTER + element.attr('id')) ? true : false;
  103. };
  104. var _sectionId = function(element) {
  105. return element.parent('section').attr('id');
  106. };
  107. var _sectionTriggers = function(current, target) {
  108. if (current) current.trigger(TRIGGER.UNLOAD);
  109. target.trigger(TRIGGER.LOAD);
  110. };
  111. var _defineTransition = function(target, current) {
  112. target_transition = target.data('transition');
  113. if (target_transition) {
  114. _assignTransitionOrigin(current);
  115. _assignTransition(current, target_transition);
  116. }
  117. };
  118. var _assignTransition = function(section, transitionName) {
  119. section.data('transition', transitionName);
  120. };
  121. var _assignTransitionOrigin = function(section) {
  122. section.data('transition-origin', section.data('transition'));
  123. };
  124. return {
  125. section: section,
  126. article: article,
  127. back: back
  128. };
  129. })(Lungo);