Lungo.Router.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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.addClass(CLASS.SHOW);
  37. lng.Element.Cache.section = target;
  38. lng.Element.Cache.article = target.find(ELEMENT.ARTICLE + '.' + CLASS.CURRENT);
  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.CURRENT).trigger(TRIGGER.UNLOAD);
  63. target.addClass(CLASS.CURRENT).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. current.removeClass(CLASS.SHOW);
  91. lng.Router.History.removeLast();
  92. target = current.siblings(ELEMENT.SECTION + lng.Router.History.current());
  93. _assignTransition(target, target.data('transition-origin'));
  94. target.removeClass(CLASS.HIDE).addClass(CLASS.SHOW);
  95. lng.Element.Cache.section = target;
  96. _sectionTriggers(current, target);
  97. };
  98. var _notCurrentTarget = function(target, element) {
  99. return (target !== HASHTAG_CHARACTER + element.attr('id')) ? true : false;
  100. };
  101. var _sectionId = function(element) {
  102. return element.parent('section').attr('id');
  103. };
  104. var _sectionTriggers = function(current, target) {
  105. current.trigger(TRIGGER.UNLOAD);
  106. target.trigger(TRIGGER.LOAD);
  107. };
  108. var _assignTransition = function(section, transitionName) {
  109. section.data('transition', transitionName);
  110. };
  111. var _assignTransitionOrigin = function(section) {
  112. section.data('transition-origin', section.data('transition'));
  113. };
  114. return {
  115. section: section,
  116. article: article,
  117. aside: aside,
  118. back: back
  119. };
  120. })(Lungo);