routing.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. path = path.toLowerCase();
  60. query = matches[2] ? matches[2].split("&").reduce(function (accu, v) {
  61. var vArray = v.split("=");
  62. accu[vArray[0]] = vArray[1];
  63. return accu;
  64. }, {}) : {};
  65. // console.log("url", url, "path", path, "queryObject", queryObject);
  66. // Get route by path:
  67. CurrentRoute = routes[path];
  68. // Do we have a route ?
  69. if (CurrentRoute) {
  70. // Obtain the template context by passing the query to the controller.
  71. context = new CurrentRoute(query);
  72. // Render template with Handlebars
  73. templateName = (path === "/") ? "home" : path;
  74. if (ready) {
  75. console.log("Immediate render");
  76. render(contentElement, templateName, context);
  77. }
  78. else {
  79. console.log("Deferred render");
  80. listener = contentElement.addEventListener(EVENT_TEMPLATES_LOADED, function () {
  81. render(contentElement, templateName, context);
  82. contentElement.removeEventListener(EVENT_TEMPLATES_LOADED, listener);
  83. });
  84. }
  85. }
  86. else {
  87. console.warn("Content found but no route matching", path);
  88. window.location.assign("");
  89. }
  90. }
  91. // Bind router to browser events.
  92. window.addEventListener("hashchange", router);
  93. window.addEventListener("load", router);
  94. // ======== Route context functions ============================================
  95. function AboutContext() {
  96. console.log("About page");
  97. }
  98. function ClassContext(q) {
  99. this.class = q.class ? q.class : null;
  100. if (!this.class) {
  101. window.location.assign("");
  102. }
  103. console.log("Class page", this.class);
  104. };
  105. /**
  106. * Home context.
  107. *
  108. * - category
  109. * - indicators
  110. *
  111. * @constructor
  112. */
  113. function HomeContext() {
  114. var i;
  115. console.log("Home page");
  116. // jQuery doesn't find the carousel without the 0 (or more) timeout.
  117. setTimeout(function () { $("#home-carousel").carousel(); }, 0);
  118. // Expose animals category to build the carousel.
  119. this.category = animals_data.category;
  120. // Carousel indicators count: JS doesn't have a range() function.
  121. this.indicators = [];
  122. for (i = 0; i < Object.keys(this.category).length; i++) {
  123. this.indicators.push(i);
  124. }
  125. }
  126. function SpeciesContext(q) {
  127. this.species = q.species ? q.species : null;
  128. if (!this.species) {
  129. window.location.assign("");
  130. }
  131. console.log("Species page", this.species);
  132. }
  133. // ======== Route binding ======================================================
  134. /**
  135. * Route binding:
  136. *
  137. * - route context functions return a context instance.
  138. * - on invalid arguments, they redirect to the home page
  139. */
  140. route("/", HomeContext);
  141. route("class", ClassContext);
  142. route("species", SpeciesContext);
  143. route("about", AboutContext);