Lungo.Router.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 HASHTAG_CHARACTER = '#';
  16. var _sections = [];
  17. var init = function() {
  18. if(!_sections) {
  19. _sections = lng.dom(ELEMENT.SECTION);
  20. }
  21. };
  22. /**
  23. * Navigate to a <section>.
  24. *
  25. * @method section
  26. *
  27. * @param {string} Id of the <section>
  28. */
  29. var section = function(section_id) {
  30. section_id = lng.Core.parseUrl(section_id);
  31. var current = lng.Element.Current.section;
  32. if (_notCurrentTarget(section_id, current)) {
  33. var target = lng.dom(ELEMENT.SECTION + section_id);
  34. if (target.length > 0) {
  35. current.removeClass(CLASS.SHOW).addClass(CLASS.HIDE).trigger(TRIGGER.UNLOAD);
  36. target.addClass(CLASS.SHOW).trigger(TRIGGER.LOAD);
  37. lng.Element.Current.section = target;
  38. lng.Router.History.add(section_id);
  39. }
  40. }
  41. };
  42. /**
  43. * Displays the <article> in a particular <section>.
  44. *
  45. * @method article
  46. *
  47. * @param {string} <section> Id
  48. * @param {string} <article> Id
  49. */
  50. var article = function(section_id, article_id, element) {
  51. section_id = lng.Core.parseUrl(section_id);
  52. article_id = lng.Core.parseUrl(article_id);
  53. var current = lng.Element.Current.article;
  54. if (_notCurrentTarget(article_id, current)) {
  55. var target = lng.dom(ELEMENT.SECTION + section_id + ' ' + ELEMENT.ARTICLE + article_id);
  56. if (target.length > 0) {
  57. current.removeClass(CLASS.CURRENT).trigger(TRIGGER.UNLOAD);
  58. target.addClass(CLASS.CURRENT).trigger(TRIGGER.LOAD);
  59. lng.Element.Current.article = target;
  60. lng.View.Article.show(section_id, article_id, element);
  61. }
  62. }
  63. };
  64. /**
  65. * Displays the <aside> in a particular <section>.
  66. *
  67. * @method aside
  68. *
  69. * @param {string} <section> Id
  70. * @param {string} <aside> Id
  71. */
  72. var aside = function(section_id, aside_id) {
  73. section_id = lng.Core.parseUrl(section_id);
  74. aside_id = lng.Core.parseUrl(aside_id);
  75. var target = lng.dom(ELEMENT.ASIDE + aside_id);
  76. if (target.length > 0) {
  77. var is_visible = target.hasClass(CLASS.CURRENT);
  78. if (is_visible) {
  79. lng.View.Aside.hide(section_id, target);
  80. } else {
  81. lng.View.Aside.show(section_id, target);
  82. }
  83. }
  84. target = null;
  85. };
  86. /**
  87. * Return to previous section.
  88. *
  89. * @method back
  90. */
  91. var back = function() {
  92. var current = lng.Element.Current.section;
  93. current.removeClass(CLASS.SHOW).trigger(TRIGGER.UNLOAD);
  94. lng.Router.History.removeLast();
  95. target = lng.dom(lng.Router.History.current());
  96. target.removeClass(CLASS.HIDE).addClass(CLASS.SHOW);
  97. lng.Element.Current.section = target;
  98. };
  99. var _notCurrentTarget = function(target, element) {
  100. return (target !== HASHTAG_CHARACTER + element.attr('id')) ? true : false;
  101. };
  102. return {
  103. section: section,
  104. article: article,
  105. aside: aside,
  106. back: back
  107. };
  108. })(LUNGO);