Lungo.Router.js 5.4 KB

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