// Generated by LiveScript 1.3.0
(function(){
  angular.module('500lines', []).controller('Spreadsheet', function($scope, $timeout){
    var ref$;
    $scope.Cols = ["A", "B", "C", "D", "E", "F", "G", "H"];
    $scope.Rows = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20];
    $scope.keydown = function(arg$, col, row){
      var which;
      which = arg$.which;
      switch (false) {
      case which !== 38 && which !== 40 && which !== 13:
        return $timeout(function(){
          var direction, cell;
          direction = which === 38
            ? -1
            : +1;
          cell = document.querySelector("#" + col + (row + direction));
          return cell != null ? cell.focus() : void 8;
        });
      }
    };
    $scope.reset = function(){
      return $scope.sheet = {
        A1: 1874,
        B1: '+',
        C1: 2046,
        D1: '⇒',
        E1: '=A1+C1'
      };
    };
    ($scope.init = function(){
      $scope.sheet = angular.fromJson(localStorage.getItem(''));
      if (!$scope.sheet) {
        $scope.reset();
      }
      return $scope.worker = new Worker('worker.js');
    })();
    ref$ = [{}, {}], $scope.errs = ref$[0], $scope.vals = ref$[1];
    $scope.calc = function(){
      var json, promise;
      json = angular.toJson($scope.sheet);
      promise = $timeout(function(){
        $scope.worker.terminate();
        $scope.init();
        return $scope.calc();
      }, 99);
      $scope.worker.onmessage = function(arg$){
        var data;
        data = arg$.data;
        return $timeout(function(){
          $scope.errs = data[0], $scope.vals = data[1];
          localStorage.setItem('', json);
          return $timeout.cancel(promise);
        });
      };
      return $scope.worker.postMessage($scope.sheet);
    };
    $scope.worker.onmessage = $scope.calc;
    return $scope.worker.postMessage(null);
  });
}).call(this);
