Pārlūkot izejas kodu

Exercice 3: use next body class on right arrow.

Frederic G. MARAND 8 gadi atpakaļ
vecāks
revīzija
70b0ed814a
1 mainītis faili ar 35 papildinājumiem un 1 dzēšanām
  1. 35 1
      Chapter 3/03.js

+ 35 - 1
Chapter 3/03.js

@@ -1,3 +1,20 @@
+/**
+ * Something like PHP array_values().
+ *
+ * @param {Object} a
+ * @returns {Array}
+ */
+function values(a) {
+  "use strict";
+  var result = [];
+  for (var i in a) {
+    if (a.hasOwnProperty(i)) {
+      result.push(a[i]);
+    }
+  }
+  return result;
+}
+
 $(document).ready(function () {
   'use strict';
 
@@ -25,7 +42,7 @@ $(document).ready(function () {
     $('body').removeClass().addClass(className);
 
     $('#switcher button').removeClass('selected');
-    $('#switcher-' +  className).addClass('selected');
+    $('#switcher-' + className).addClass('selected');
 
     $('#switcher').off('click', toggleSwitcher);
 
@@ -71,4 +88,21 @@ $(document).ready(function () {
     $(this).siblings().toggleClass('hidden');
   });
 
+  // Exercise 3.
+  var triggerValues = values(triggers);
+  setBodyClass('default');
+  $('body').keyup(function (event) {
+    if (event.which !== 39) {
+      return;
+    }
+
+    // Assume the only classes are those handled by this code. Not true in the
+    // general case but sufficient for the exercise.
+    var currentClass = $('body').attr('class');
+
+    var currentIndex = triggerValues.indexOf(currentClass);
+    var nextIndex = (currentIndex + 1) % triggerValues.length;
+    var nextClass = triggerValues[nextIndex];
+    setBodyClass(nextClass);
+  });
 });