Commits

Anonymous committed 41ab334

added play_back feature. Controller is now feature complete

Comments (0)

Files changed (2)

coffee/app.coffee

             @first()
         @view.$container.querySelector('.previous').addEventListener 'click', =>
             @previous()
+        @view.$container.querySelector('.previous-play').addEventListener 'click', =>
+            @previous_play()
 
     end_callback: =>
         if @mode is 'play'
+            @mode = 'stop'
             if @current < @turnlist.length - 1
-                @mode = 'stop'
                 @next_play()
-            else
-                @mode = 'stop'
+        else if @mode is 'play_back'
+            @mode = 'stop'
+            if @current >= 0
+                @previous_play()
         else if @mode is 'play_stop'
             @mode = 'stop'
 
                 inverted = @model.invert([@turnlist[@current+1]])
                 @view.turn inverted[0], @end_callback
 
+    previous_play: =>
+        window.setTimeout =>
+            if @mode is 'stop'
+                @mode = 'play_back'
+                @current--
+                inverted = @model.invert([@turnlist[@current+1]])
+                @view.turn inverted[0], @end_callback
+
 document.addEventListener 'DOMContentLoaded', ->
 
     for $container in document.querySelectorAll '.cubeofcoffee'
     CubeController.prototype.turnlist = ["R2", "U", "R", "U", "R'", "U'", "R'", "U'", "R'", "U", "R'"];
 
     function CubeController(cubeview) {
+      this.previous_play = __bind(this.previous_play, this);
+
       this.previous = __bind(this.previous, this);
 
       this.first = __bind(this.first, this);
       this.view.$container.querySelector('.previous').addEventListener('click', function() {
         return _this.previous();
       });
+      this.view.$container.querySelector('.previous-play').addEventListener('click', function() {
+        return _this.previous_play();
+      });
     }
 
     CubeController.prototype.end_callback = function() {
       if (this.mode === 'play') {
+        this.mode = 'stop';
         if (this.current < this.turnlist.length - 1) {
-          this.mode = 'stop';
           return this.next_play();
-        } else {
-          return this.mode = 'stop';
+        }
+      } else if (this.mode === 'play_back') {
+        this.mode = 'stop';
+        if (this.current >= 0) {
+          return this.previous_play();
         }
       } else if (this.mode === 'play_stop') {
         return this.mode = 'stop';
       });
     };
 
+    CubeController.prototype.previous_play = function() {
+      var _this = this;
+      return window.setTimeout(function() {
+        var inverted;
+        if (_this.mode === 'stop') {
+          _this.mode = 'play_back';
+          _this.current--;
+          inverted = _this.model.invert([_this.turnlist[_this.current + 1]]);
+          return _this.view.turn(inverted[0], _this.end_callback);
+        }
+      });
+    };
+
     return CubeController;
 
   })();