Lungo.Router.js 5.9 KB

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