Commits

Anonymous committed a23c323

added previous command in the controller

  • Participants
  • Parent commits 692515f

Comments (0)

Files changed (4)

coffee/app.coffee

             @last()
         @view.$container.querySelector('.first').addEventListener 'click', =>
             @first()
+        @view.$container.querySelector('.previous').addEventListener 'click', =>
+            @previous()
 
     end_callback: =>
         if @mode is 'play'
                 @next_play()
             else
                 @mode = 'stop'
-                @current = -1
         else if @mode is 'play_stop'
             @mode = 'stop'
 
     next: =>
         window.setTimeout =>
-            if @mode is 'stop'
+            if @mode is 'stop' and @current < @turnlist.length
                 @mode = 'play_stop'
                 @current++
                 @view.turn @turnlist[@current], @end_callback
 
-
     next_play: =>
         window.setTimeout =>
             if @mode is 'stop'
             if @mode is 'stop'
                 remaining_turnlist = @turnlist[@current+1..@turnlist.length-1]
                 @view.straightturn(remaining_turnlist.join(''))
-                @current = @turnlist.length
+                @current = @turnlist.length - 1
     first : =>
         window.setTimeout =>
             if @mode is 'stop'
                 @current = -1
                 @view.initState()
 
+    previous: =>
+        window.setTimeout =>
+            if @mode is 'stop' and @current > -1
+                @mode = 'play_stop'
+                @current--
+                inverted = @model.invert([@turnlist[@current+1]])
+                @view.turn inverted[0], @end_callback
+
 document.addEventListener 'DOMContentLoaded', ->
 
     for $container in document.querySelectorAll '.cubeofcoffee'

coffee/cube.coffee

             couple3 = permutation[3]
             [ @state[couple0[0]][couple0[1]], @state[couple1[0]][couple1[1]], @state[couple2[0]][couple2[1]], @state[couple3[0]][couple3[1]] ] = [ @state[couple3[0]][couple3[1]], @state[couple0[0]][couple0[1]], @state[couple1[0]][couple1[1]], @state[couple2[0]][couple2[1]] ]
 
+    invert : (movlist) ->
+        result = for mov in movlist
+            if mov[-1..] is "'"
+                mov[0]
+            else if mov[-1..] is "2"
+                mov
+            else
+                mov + "'"
+
     convert : (movlist) ->
         # inversion and doubling
         result = for value,key in movlist
     CubeController.prototype.turnlist = ["R2", "U", "R", "U", "R'", "U'", "R'", "U'", "R'", "U", "R'"];
 
     function CubeController(cubeview) {
+      this.previous = __bind(this.previous, this);
+
       this.first = __bind(this.first, this);
 
       this.last = __bind(this.last, this);
       this.view.$container.querySelector('.first').addEventListener('click', function() {
         return _this.first();
       });
+      this.view.$container.querySelector('.previous').addEventListener('click', function() {
+        return _this.previous();
+      });
     }
 
     CubeController.prototype.end_callback = function() {
           this.mode = 'stop';
           return this.next_play();
         } else {
-          this.mode = 'stop';
-          return this.current = -1;
+          return this.mode = 'stop';
         }
       } else if (this.mode === 'play_stop') {
         return this.mode = 'stop';
     CubeController.prototype.next = function() {
       var _this = this;
       return window.setTimeout(function() {
-        if (_this.mode === 'stop') {
+        if (_this.mode === 'stop' && _this.current < _this.turnlist.length) {
           _this.mode = 'play_stop';
           _this.current++;
           return _this.view.turn(_this.turnlist[_this.current], _this.end_callback);
         if (_this.mode === 'stop') {
           remaining_turnlist = _this.turnlist.slice(_this.current + 1, (_this.turnlist.length - 1) + 1 || 9e9);
           _this.view.straightturn(remaining_turnlist.join(''));
-          return _this.current = _this.turnlist.length;
+          return _this.current = _this.turnlist.length - 1;
         }
       });
     };
       });
     };
 
+    CubeController.prototype.previous = function() {
+      var _this = this;
+      return window.setTimeout(function() {
+        var inverted;
+        if (_this.mode === 'stop' && _this.current > -1) {
+          _this.mode = 'play_stop';
+          _this.current--;
+          inverted = _this.model.invert([_this.turnlist[_this.current + 1]]);
+          return _this.view.turn(inverted[0], _this.end_callback);
+        }
+      });
+    };
+
     return CubeController;
 
   })();
       return _results;
     };
 
+    Cube.prototype.invert = function(movlist) {
+      var mov, result;
+      return result = (function() {
+        var _i, _len, _results;
+        _results = [];
+        for (_i = 0, _len = movlist.length; _i < _len; _i++) {
+          mov = movlist[_i];
+          if (mov.slice(-1) === "'") {
+            _results.push(mov[0]);
+          } else if (mov.slice(-1) === "2") {
+            _results.push(mov);
+          } else {
+            _results.push(mov + "'");
+          }
+        }
+        return _results;
+      })();
+    };
+
     Cube.prototype.convert = function(movlist) {
       var key, result, value;
       result = (function() {