Browse Source

Working fully dynamic home carousel.

Frederic G. MARAND 8 years ago
parent
commit
966e5d4390
7 changed files with 111 additions and 32 deletions
  1. 1 1
      .eslintrc.js
  2. 9 1
      assignment.css
  3. 2 2
      assignment.css.map
  4. 14 7
      assignment.scss
  5. 2 3
      index.html
  6. 49 16
      routing.js
  7. 34 2
      templates/home.hbs

+ 1 - 1
.eslintrc.js

@@ -227,7 +227,7 @@ module.exports = {
     "max-params": [2, 10], // limits the number of parameters that can be used in the function declaration. (off by default)
     "max-statements": 0, // specify the maximum number of statement allowed in a function (off by default)
     "no-bitwise": 0, // disallow use of bitwise operators (off by default)
-    "no-plusplus": 2, // disallow use of unary operators, ++ and -- (off by default)
+    "no-plusplus": 0, // disallow use of unary operators, ++ and -- (off by default)
 
     //
     // eslint-plugin-react

+ 9 - 1
assignment.css

@@ -1,3 +1,11 @@
-
+#home-carousel .item img {
+  height: 600px;
+  width: 100%;
+  object-fit: cover;
+  margin-left: auto;
+  margin-right: auto; }
+#home-carousel h2 {
+  font-size: 400%;
+  text-shadow: 2px 2px 5px #000; }
 
 /*# sourceMappingURL=assignment.css.map */

+ 2 - 2
assignment.css.map

@@ -1,7 +1,7 @@
 {
 "version": 3,
-"mappings": "",
-"sources": [],
+"mappings": "AACE,wBAAU;EACR,MAAM,EAAE,KAAK;EACb,KAAK,EAAE,IAAI;EACX,UAAU,EAAE,KAAK;EACjB,WAAW,EAAE,IAAI;EACjB,YAAY,EAAE,IAAI;AAGpB,iBAAG;EACD,SAAS,EAAE,IAAI;EACf,WAAW,EAAE,gBAAgB",
+"sources": ["assignment.scss"],
 "names": [],
 "file": "assignment.css"
 }

+ 14 - 7
assignment.scss

@@ -1,7 +1,14 @@
-//$bsInfoBg: #5bc0de;
-//$bsInfoBo: #46B8DA;
-//
-//.navbar .active {
-//  background-color: $bsInfoBg;
-//  border-color: $bsInfoBo;
-//}
+#home-carousel {
+  .item img {
+    height: 600px;
+    width: 100%;
+    object-fit: cover;
+    margin-left: auto;
+    margin-right: auto;
+  }
+
+  h2 {
+    font-size: 400%;
+    text-shadow: 2px 2px 5px #000;
+  }
+}

+ 2 - 3
index.html

@@ -16,9 +16,6 @@
   <script id="tpl-class" type="text/x-handlebars-template" src="templates/class.hbs"></script>
   <script id="tpl-species" type="text/x-handlebars-template" src="templates/species.hbs"></script>
 
-  <!-- Custom code -->
-  <script type="text/ecmascript" src="AnimalsData.js"></script>
-
   <link rel="stylesheet" type="text/css" href="assignment.css" />
 </head>
 
@@ -26,6 +23,8 @@
 
   <div id="content"></div>
 
+  <!-- Custom code -->
+  <script type="text/ecmascript" src="AnimalsData.js"></script>
   <script type="text/ecmascript" src="assignment.js"></script>
   <script type="text/ecmascript" src="routing.js"></script>
 </body>

+ 49 - 16
routing.js

@@ -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);

+ 34 - 2
templates/home.hbs

@@ -1,4 +1,36 @@
 <div class="home">
   {{> nav activeGallery="active" }}
-  HOME
-</div>
+  <div class="container">
+    <div id="home-carousel" class="carousel slide" data-interval="2000">
+      <ol class="carousel-indicators">
+        {{#each indicators}}
+          <li data-target="#home-carousel" data-slide-to="{{this}}"></li>
+        {{/each}}
+      </ol>
+
+      <div class="carousel-inner" role="listbox">
+        {{#each category}}
+          {{#if @first}}
+        <div class="item active">
+          {{else}}
+        <div class="item">
+          {{/if}}
+          <a href="#class?class={{this.name}}"><img src="{{this.animals.[0].image1}}" alt="{{this.name}}"></a>
+          <div class="carousel-caption">
+            <h2>{{this.name}}</h2>
+          </div>
+          </div>
+        {{/each}}
+
+      <!-- Controls -->
+      <a class="left carousel-control" href="#home-carousel" role="button" data-slide="prev">
+        <span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
+        <span class="sr-only">Previous</span>
+      </a>
+      <a class="right carousel-control" href="#home-carousel" role="button" data-slide="next">
+        <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
+        <span class="sr-only">Next</span>
+      </a>
+    </div>
+  </div>
+</div>