|
@@ -64,6 +64,7 @@ function router() {
|
|
|
// Current route url (getting rid of "#" in hash as well):
|
|
|
var matches = location.hash.match(/^#?([\w]*)(?:\?)?(.*)?/);
|
|
|
path = matches[1] ? matches[1] : "/";
|
|
|
+ path = path.toLowerCase();
|
|
|
query = matches[2] ? matches[2].split("&").reduce(function (accu, v) {
|
|
|
var vArray = v.split("=");
|
|
|
accu[vArray[0]] = vArray[1];
|
|
@@ -104,32 +105,64 @@ function router() {
|
|
|
window.addEventListener("hashchange", router);
|
|
|
window.addEventListener("load", router);
|
|
|
|
|
|
-/**
|
|
|
- * Route declarations:
|
|
|
- *
|
|
|
- * - route context functions return a context instance.
|
|
|
- * - on invalid arguments, they redirect to the home page
|
|
|
- */
|
|
|
-route("/", function HomeContext() {
|
|
|
- console.log("Home page");
|
|
|
-});
|
|
|
|
|
|
-route("class", function ClassContext(q) {
|
|
|
+// ======== Route context functions ============================================
|
|
|
+
|
|
|
+function AboutContext() {
|
|
|
+ console.log("About page");
|
|
|
+}
|
|
|
+
|
|
|
+function ClassContext(q) {
|
|
|
this.class = q.class ? q.class : null;
|
|
|
if (!this.class) {
|
|
|
window.location.assign("");
|
|
|
}
|
|
|
console.log("Class page", this.class);
|
|
|
-});
|
|
|
+};
|
|
|
+
|
|
|
+/**
|
|
|
+ * Home context.
|
|
|
+ *
|
|
|
+ * - category
|
|
|
+ * - indicators
|
|
|
+ *
|
|
|
+ * @constructor
|
|
|
+ */
|
|
|
+function HomeContext() {
|
|
|
+ var i;
|
|
|
|
|
|
-route("species", function SpeciesContext(q) {
|
|
|
+ console.log("Home page");
|
|
|
+
|
|
|
+ // jQuery doesn't find the carousel without the 0 (or more) timeout.
|
|
|
+ setTimeout(function () { $("#home-carousel").carousel(); }, 0);
|
|
|
+
|
|
|
+ // Expose animals category to build the carousel.
|
|
|
+ this.category = animals_data.category;
|
|
|
+
|
|
|
+ // Carousel indicators count: JS doesn't have a range() function.
|
|
|
+ this.indicators = [];
|
|
|
+ for (i = 0; i < Object.keys(this.category).length; i++) {
|
|
|
+ this.indicators.push(i);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+function SpeciesContext(q) {
|
|
|
this.species = q.species ? q.species : null;
|
|
|
if (!this.species) {
|
|
|
window.location.assign("");
|
|
|
}
|
|
|
console.log("Species page", this.species);
|
|
|
-});
|
|
|
+}
|
|
|
|
|
|
-route("about", function AboutContext() {
|
|
|
- console.log("About page");
|
|
|
-});
|
|
|
+// ======== Route binding ======================================================
|
|
|
+
|
|
|
+/**
|
|
|
+ * Route binding:
|
|
|
+ *
|
|
|
+ * - route context functions return a context instance.
|
|
|
+ * - on invalid arguments, they redirect to the home page
|
|
|
+ */
|
|
|
+route("/", HomeContext);
|
|
|
+route("class", ClassContext);
|
|
|
+route("species", SpeciesContext);
|
|
|
+route("about", AboutContext);
|