routing.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. var category = animals_data.category;
  100. var animals = null;
  101. var klass;
  102. this.class = q.class ? q.class : null;
  103. if (!this.class) {
  104. window.location.assign("");
  105. }
  106. console.log("Class page(" + this.class + ")");
  107. for (klass in category) {
  108. if (category.hasOwnProperty(klass)) {
  109. if (category[klass].name === this.class) {
  110. animals = category[klass].animals;
  111. break;
  112. }
  113. }
  114. }
  115. this.animals = animals;
  116. }
  117. /**
  118. * Home context.
  119. *
  120. * - category
  121. * - indicators
  122. *
  123. * @constructor
  124. */
  125. function HomeContext() {
  126. var i;
  127. console.log("Home page");
  128. // jQuery doesn't find the carousel without the 0 (or more) timeout.
  129. setTimeout(function () { $("#home-carousel").carousel(); }, 0);
  130. // Expose animals category to build the carousel.
  131. this.category = animals_data.category;
  132. // Carousel indicators count: JS doesn't have a range() function.
  133. this.indicators = [];
  134. for (i = 0; i < Object.keys(this.category).length; i++) {
  135. this.indicators.push(i);
  136. }
  137. }
  138. /**
  139. * Species context
  140. *
  141. * @param {Object} q
  142. * - class
  143. * - species
  144. * @constructor
  145. */
  146. function SpeciesContext(q) {
  147. var category = animals_data.category;
  148. var klass = null;
  149. var speciesArray = null;
  150. var species;
  151. var speciesIndex;
  152. var prop;
  153. if (!q.class || !q.species) {
  154. console.error("Missing class or species parameter.");
  155. window.location.assign("");
  156. return;
  157. }
  158. console.log("Species page(" + q.class + ", " + q.species + ")");
  159. this.class = q.class ? q.class : null;
  160. for (klass in category) {
  161. if (category.hasOwnProperty(klass)) {
  162. if (category[klass].name === this.class) {
  163. speciesArray = category[klass].animals;
  164. break;
  165. }
  166. }
  167. }
  168. // No species means class was not found.
  169. if (!speciesArray) {
  170. console.warn("No species found for class ", this.class);
  171. window.location.assign("");
  172. }
  173. for (speciesIndex in speciesArray) {
  174. if (speciesArray.hasOwnProperty(speciesIndex)) {
  175. species = speciesArray[speciesIndex];
  176. if (species.name === q.species) {
  177. for (prop in species) {
  178. if (species.hasOwnProperty(prop)) {
  179. this[prop] = species[prop];
  180. }
  181. }
  182. break;
  183. }
  184. }
  185. }
  186. }
  187. // ======== Route binding ======================================================
  188. /**
  189. * Route binding:
  190. *
  191. * - route context functions return a context instance.
  192. * - on invalid arguments, they redirect to the home page
  193. */
  194. route("/", HomeContext);
  195. route("class", ClassContext);
  196. route("species", SpeciesContext);
  197. route("about", AboutContext);