Lungo.Router.js 4.4 KB

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