Lungo.Router.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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.Current.section;
  27. if (_notCurrentTarget(section_id, current)) {
  28. var target = lng.Element.sections.siblings(ELEMENT.SECTION + section_id);
  29. if (target.length > 0) {
  30. current.removeClass(CLASS.SHOW).addClass(CLASS.HIDE).trigger(TRIGGER.UNLOAD);
  31. target.addClass(CLASS.SHOW).trigger(TRIGGER.LOAD);
  32. lng.Element.Current.section = target;
  33. lng.Element.Current.article = target.find(ELEMENT.ARTICLE + '.' + CLASS.CURRENT);
  34. lng.Router.History.add(section_id);
  35. }
  36. }
  37. };
  38. /**
  39. * Displays the <article> in a particular <section>.
  40. *
  41. * @method article
  42. *
  43. * @param {string} <section> Id
  44. * @param {string} <article> Id
  45. */
  46. var article = function(section_id, article_id, element) {
  47. article_id = lng.Core.parseUrl(article_id);
  48. var current = lng.Element.Current.article;
  49. if (_notCurrentTarget(article_id, current)) {
  50. var target = lng.Element.Current.section.find(ELEMENT.ARTICLE + article_id);
  51. if (target.length > 0) {
  52. if (_sectionId(current) === _sectionId(target)) {
  53. //current.removeClass(CLASS.CURRENT).trigger(TRIGGER.UNLOAD);
  54. current.removeClass(CLASS.CURRENT);
  55. }
  56. //target.addClass(CLASS.CURRENT).trigger(TRIGGER.LOAD);
  57. target.addClass(CLASS.CURRENT);
  58. lng.Element.Current.article = target;
  59. lng.View.Article.switchNavItems(article_id);
  60. lng.View.Article.switchReferenceItems(article_id, lng.Element.Current.section);
  61. if (element) lng.View.Article.title(element.data(ATTRIBUTE.TITLE));
  62. }
  63. }
  64. };
  65. /**
  66. * Displays the <aside> in a particular <section>.
  67. *
  68. * @method aside
  69. *
  70. * @param {string} <section> Id
  71. * @param {string} <aside> Id
  72. */
  73. var aside = function(section_id, aside_id) {
  74. aside_id = lng.Core.parseUrl(aside_id);
  75. lng.View.Aside.toggle(aside_id);
  76. };
  77. /**
  78. * Return to previous section.
  79. *
  80. * @method back
  81. */
  82. var back = function() {
  83. var current = lng.Element.Current.section;
  84. current.removeClass(CLASS.SHOW).trigger(TRIGGER.UNLOAD);
  85. lng.Router.History.removeLast();
  86. target = lng.Element.sections.siblings(ELEMENT.SECTION + lng.Router.History.current());
  87. target.removeClass(CLASS.HIDE).addClass(CLASS.SHOW).trigger(TRIGGER.LOAD);
  88. lng.Element.Current.section = target;
  89. };
  90. var _notCurrentTarget = function(target, element) {
  91. return (target !== HASHTAG_CHARACTER + element.attr('id')) ? true : false;
  92. };
  93. var _sectionId = function(element) {
  94. return element.parent('section').attr('id');
  95. };
  96. return {
  97. section: section,
  98. article: article,
  99. aside: aside,
  100. back: back
  101. };
  102. })(LUNGO);