routing.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /**
  2. * @file
  3. * Contains the router code and route definitions
  4. *
  5. * Inspired by Joakim Beng"s example.
  6. *
  7. * @see http://joakim.beng.se/blog/posts/a-javascript-router-in-20-lines.html
  8. */
  9. // The routes object.
  10. var routes = {};
  11. // The route registering function.
  12. function route(path, controller) {
  13. routes[path] = controller;
  14. }
  15. /**
  16. * Set the target content to the template result.
  17. *
  18. * Use only if global "ready" is true, otherwise global "templates" may not be
  19. * entirely initialized.
  20. *
  21. * @param {DOMElement} target
  22. * The element in which to inject the template output.
  23. * @param {string} name
  24. * The name of the template to render.
  25. * @param {Object} context
  26. * The template context.
  27. *
  28. * @returns {void}
  29. *
  30. * @globals templates
  31. */
  32. function render(target, name, context) {
  33. var output = null;
  34. var template = templates[name];
  35. if (!template) {
  36. console.error("Template not found", name);
  37. return;
  38. }
  39. output = template(context);
  40. target.innerHTML = output;
  41. }
  42. /**
  43. * The router.
  44. *
  45. * @returns {void}
  46. *
  47. * @globals contentElement, ready, EVENT_TEMPLATES_LOADED
  48. */
  49. function router() {
  50. var context = null;
  51. var CurrentRoute = null;
  52. var listener = null;
  53. var path = null;
  54. var query;
  55. var templateName = null;
  56. // Current route url (getting rid of "#" in hash as well):
  57. var matches = location.hash.match(/^#?([\w]*)(?:\?)?(.*)?/);
  58. path = matches[1] ? matches[1] : "/";
  59. query = matches[2] ? matches[2].split("&").reduce(function (accu, v) {
  60. var vArray = v.split("=");
  61. accu[vArray[0]] = vArray[1];
  62. return accu;
  63. }, {}) : {};
  64. // console.log("url", url, "path", path, "queryObject", queryObject);
  65. // Get route by path:
  66. CurrentRoute = routes[path];
  67. // Do we have a route ?
  68. if (CurrentRoute) {
  69. // Obtain the template context by passing the query to the controller.
  70. context = new CurrentRoute(query);
  71. // Render template with Handlebars
  72. templateName = (path === "/") ? "home" : path;
  73. if (ready) {
  74. console.log("Immediate render");
  75. render(contentElement, templateName, context);
  76. }
  77. else {
  78. console.log("Deferred render");
  79. listener = contentElement.addEventListener(EVENT_TEMPLATES_LOADED, function () {
  80. render(contentElement, templateName, context);
  81. contentElement.removeEventListener(EVENT_TEMPLATES_LOADED, listener);
  82. });
  83. }
  84. }
  85. else {
  86. console.warn("Content found but no route matching", path);
  87. window.location.assign("");
  88. }
  89. }
  90. // Bind router to browser events.
  91. window.addEventListener("hashchange", router);
  92. window.addEventListener("load", router);
  93. /**
  94. * Route declarations:
  95. *
  96. * - route context functions return a context instance.
  97. * - on invalid arguments, they redirect to the home page
  98. */
  99. route("/", function HomeContext() {
  100. console.log("Home page");
  101. });
  102. route("class", function ClassContext(q) {
  103. this.class = q.class ? q.class : null;
  104. if (!this.class) {
  105. window.location.assign("");
  106. }
  107. console.log("Class page", this.class);
  108. });
  109. route("species", function SpeciesContext(q) {
  110. this.species = q.species ? q.species : null;
  111. if (!this.species) {
  112. window.location.assign("");
  113. }
  114. console.log("Species page", this.species);
  115. });
  116. route("about", function AboutContext() {
  117. console.log("About page");
  118. });