Lungo.Router.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 CSS_CLASSES = {
  12. SHOW: 'show',
  13. HIDE: 'hide'
  14. };
  15. /**
  16. * Navigate to a <section>.
  17. *
  18. * @method section
  19. *
  20. * @param {string} Id of the <section>
  21. */
  22. var section = function(section_id) {
  23. var section_id = (section_id.indexOf('#')) ? '#' + section_id : section_id;
  24. var target = 'section' + section_id;
  25. if (_existsTarget(target)) {
  26. lng.dom(_getHistoryCurrent()).removeClass(CSS_CLASSES.SHOW).addClass(CSS_CLASSES.HIDE);
  27. lng.dom(section_id).addClass(CSS_CLASSES.SHOW);
  28. lng.Router.History.add(section_id);
  29. }
  30. };
  31. /**
  32. * Displays the <article> in a particular <section>.
  33. *
  34. * @method article
  35. *
  36. * @param {string} <section> Id
  37. * @param {string} <article> Id
  38. */
  39. var article = function(section_id, article_id) {
  40. var target = section_id + ' article' + article_id;
  41. if (_existsTarget(target)) {
  42. lng.View.Article.show(section_id, article_id);
  43. }
  44. };
  45. /**
  46. * Return to previous section.
  47. *
  48. * @method back
  49. */
  50. var back = function() {
  51. lng.dom(_getHistoryCurrent()).removeClass(CSS_CLASSES.SHOW);
  52. lng.Router.History.removeLast();
  53. lng.dom(_getHistoryCurrent()).removeClass(CSS_CLASSES.HIDE).addClass(CSS_CLASSES.SHOW);
  54. };
  55. var _existsTarget = function(target) {
  56. var exists = false;
  57. if (lng.dom(target).length > 0) {
  58. exists = true;
  59. } else {
  60. lng.Core.log(3, 'Lungo.Router ERROR: The target ' + target + ' does not exists.');
  61. }
  62. return exists;
  63. }
  64. var _getHistoryCurrent = function() {
  65. return lng.Router.History.current();
  66. };
  67. return {
  68. section: section,
  69. article: article,
  70. back: back
  71. };
  72. })(LUNGO);