<commit_before>angular.module('blissful', ['ngResource'])

.config(function($httpProvider) {
  $httpProvider.responseInterceptors.push('blissHttpInterceptor');
})

.factory('blissHttpInterceptor', function($q) {
  return function(promise) {
    return promise.then(function(response) {
      return response;
    }, function(err) {
      if (err instanceof Error) {
        alert(err);
      } else if (err.headers('X-Bliss-Error')) {
        alert('Error:\n' + err.data);
      } else {
        alert(err.status + ' <- ' + err.method + ' ' + err.url + '\n' +
              err.data);
      }
      return $q.reject(err);
    });
  };
});

function ProjectController($scope, $http, $resource, $filter) {

  var Files = $resource('listfiles');

  var _dirty = false;
  var _save_timeout;

  $scope.run = function(url, project_id) {
    $scope.save(function() {
      var container = document.getElementById('output-container');
      if (_output_window && _output_window.closed) {
        _popout = false;
      }
      if (_popout) {
        container.style.display = 'none';
        _output_window = window.open(url, project_id);
      } else {
        container.style.display = 'block';
        var container = document.getElementById('output-container');
        var where = document.getElementById('output-url');
        var iframe = document.getElementById('output-iframe');
        iframe.src = url;
        where.innerHTML = iframe.src;
      }
    });
  }

  // called from setTimeout after editor is marked dirty
  $scope.save = function(callback) {
    if (!_dirty) {
      if (callback) {
        callback.call();
      }
      return;
    }

    // TODO: fix me
    _dirty = false;

    set_status('Saving...');
    $scope.putfile($scope.currentFilename(), _editor.getValue(), function () {
      set_status(''); // saved
      if (callback) {
        callback.call();
      }
    });
  }

  function markDirty() {
    _dirty = true;

    if (_save_timeout) {
      return;
    }
    _save_timeout = setTimeout(function() {
      _save_timeout = null;
      $scope.save();
    }, 1000);
  }

  // editor onChange
  function editorOnChange(from, to, text, next) {
     markDirty();
  }

  $scope.prompt_file_delete = function() {
    var answer = prompt("Are you sure you want to delete " + $scope.currentFilename() + "?\nType 'yes' to confirm.", "no");
    if (!answer || answer.toLowerCase()[0] != 'y') {
      return;
    }
    $scope.deletepath($scope.currentFilename());
  }

  $scope.prompt_file_rename = function() {
    var new_filename = prompt(
        'New filename?\n(You may specify a full path such as: foo/bar.txt)',
        $scope.currentFilename());
    if (!new_filename) {
      return;
    }
    if (new_filename[0] == '/') {
      new_filename = new_filename.substr(1);
    }
    if (!new_filename || new_filename == $scope.currentFilename()) {
      return;
    }
    $scope.movefile($scope.currentFilename(), new_filename);
  }

  // setup file context menu clear handler
  window.addEventListener('click', function(evt) {
    document.getElementById('file-context-menu').style.display = 'None';
  }, false);

  $scope.file_context_menu = function(evt, i) {
    evt.stopPropagation();
    $scope.select(i);
    var menuDiv = document.getElementById('file-context-menu');
    menuDiv.style.display = 'block';
    menuDiv.style.left = evt.pageX + 'px';
    menuDiv.style.top = evt.pageY + 'px';
    var elem = document.getElementById('file-' + i);
    insertAfter(menuDiv, elem);
  };

  $scope.orderFilesAndSelectByPath = function(path) {
    $scope.files = $filter('orderBy')($scope.files, 'name');
    $scope.selectByPath(path);
  };

  $scope.insertPath = function(path) {
    if ($scope.selectByPath(path)) {
      return;
    }
    $scope.putfile(path, '');
    files = $scope.files;
    files.push({name: path});
    $scope.orderFilesAndSelectByPath(path);
  };

  $scope.prompt_for_new_file = function() {
    var filename = prompt('New filename?', '');
    if (!filename) {
      return;
    }
    if (filename[0] == '/') {
      filename = filename.substr(1)
    }

    $scope.insertPath(filename);
  };

  $scope.currentFilename = function() {
    if (!$scope.files) return '';
    return $scope.files[$scope.currentIndex].name;
  };

  $scope.deletepath = function(filename) {
    $http({method: 'POST',
           url: 'deletepath/' + encodeURI(filename)})
    .success(function(data, status, headers, config) {
      $scope.files.splice($scope.currentIndex, 1);
      $scope.select(0);
    });
  };

  $scope.movefile = function(path, newpath) {
    $http({method: 'POST',
         url: 'movefile/' + encodeURI(path),
         data: {newpath: newpath}})
    .success(function(data, status, headers, config) {
      $scope.files[$scope.currentIndex].name = newpath;
      $scope.orderFilesAndSelectByPath(newpath);
    });
  };

  $scope.putfile = function(filename, data, callback) {
    $http({method: 'PUT',
           url: 'putfile/' + encodeURI(filename),
           data: data})
    .success(function(data, status, headers, config) {
      if (callback) {
        callback.call();
      }
    });
  };

  $scope.selectByPath = function(path) {
    for (i in $scope.files) {
      if ($scope.files[i].name == path) {
        $scope.select(i);
        return true;
      }
    }
    return false;
  };
<commit_msg>putfile failure results in getfile<commit_after>angular.module('blissful', ['ngResource'])

.config(function($httpProvider) {
  $httpProvider.responseInterceptors.push('blissHttpInterceptor');
})

.factory('blissHttpInterceptor', function($q) {
  return function(promise) {
    return promise.then(function(response) {
      return response;
    }, function(err) {
      if (err instanceof Error) {
        alert(err);
      } else if (err.headers('X-Bliss-Error')) {
        alert('Error:\n' + err.data);
      } else {
        alert(err.status + ' <- ' + err.method + ' ' + err.url + '\n' +
              err.data);
      }
      return $q.reject(err);
    });
  };
});

function ProjectController($scope, $http, $resource, $filter) {

  var Files = $resource('listfiles');

  var _dirty = false;
  var _save_timeout;

  $scope.run = function(url, project_id) {
    $scope.save(function() {
      var container = document.getElementById('output-container');
      if (_output_window && _output_window.closed) {
        _popout = false;
      }
      if (_popout) {
        container.style.display = 'none';
        _output_window = window.open(url, project_id);
      } else {
        container.style.display = 'block';
        var container = document.getElementById('output-container');
        var where = document.getElementById('output-url');
        var iframe = document.getElementById('output-iframe');
        iframe.src = url;
        where.innerHTML = iframe.src;
      }
    });
  }

  // called from setTimeout after editor is marked dirty
  $scope.save = function(callback) {
    if (!_dirty) {
      if (callback) {
        callback.call();
      }
      return;
    }

    // TODO: fix me
    _dirty = false;

    set_status('Saving...');
    $scope.putfile($scope.currentFilename(), _editor.getValue(), function () {
      set_status(''); // saved
      if (callback) {
        callback.call();
      }
    });
  }

  function markDirty() {
    _dirty = true;

    if (_save_timeout) {
      return;
    }
    _save_timeout = setTimeout(function() {
      _save_timeout = null;
      $scope.save();
    }, 1000);
  }

  // editor onChange
  function editorOnChange(from, to, text, next) {
     markDirty();
  }

  $scope.prompt_file_delete = function() {
    var answer = prompt("Are you sure you want to delete " + $scope.currentFilename() + "?\nType 'yes' to confirm.", "no");
    if (!answer || answer.toLowerCase()[0] != 'y') {
      return;
    }
    $scope.deletepath($scope.currentFilename());
  }

  $scope.prompt_file_rename = function() {
    var new_filename = prompt(
        'New filename?\n(You may specify a full path such as: foo/bar.txt)',
        $scope.currentFilename());
    if (!new_filename) {
      return;
    }
    if (new_filename[0] == '/') {
      new_filename = new_filename.substr(1);
    }
    if (!new_filename || new_filename == $scope.currentFilename()) {
      return;
    }
    $scope.movefile($scope.currentFilename(), new_filename);
  }

  // setup file context menu clear handler
  window.addEventListener('click', function(evt) {
    document.getElementById('file-context-menu').style.display = 'None';
  }, false);

  $scope.file_context_menu = function(evt, i) {
    evt.stopPropagation();
    $scope.select(i);
    var menuDiv = document.getElementById('file-context-menu');
    menuDiv.style.display = 'block';
    menuDiv.style.left = evt.pageX + 'px';
    menuDiv.style.top = evt.pageY + 'px';
    var elem = document.getElementById('file-' + i);
    insertAfter(menuDiv, elem);
  };

  $scope.orderFilesAndSelectByPath = function(path) {
    $scope.files = $filter('orderBy')($scope.files, 'name');
    $scope.selectByPath(path);
  };

  $scope.insertPath = function(path) {
    if ($scope.selectByPath(path)) {
      return;
    }
    $scope.putfile(path, '');
    files = $scope.files;
    files.push({name: path});
    $scope.orderFilesAndSelectByPath(path);
  };

  $scope.prompt_for_new_file = function() {
    var filename = prompt('New filename?', '');
    if (!filename) {
      return;
    }
    if (filename[0] == '/') {
      filename = filename.substr(1)
    }

    $scope.insertPath(filename);
  };

  $scope.currentFilename = function() {
    if (!$scope.files) return '';
    return $scope.files[$scope.currentIndex].name;
  };

  $scope.deletepath = function(filename) {
    $http({method: 'POST',
           url: 'deletepath/' + encodeURI(filename)})
    .success(function(data, status, headers, config) {
      $scope.files.splice($scope.currentIndex, 1);
      $scope.select(0);
    });
  };

  $scope.movefile = function(path, newpath) {
    $http({method: 'POST',
         url: 'movefile/' + encodeURI(path),
         data: {newpath: newpath}})
    .success(function(data, status, headers, config) {
      $scope.files[$scope.currentIndex].name = newpath;
      $scope.orderFilesAndSelectByPath(newpath);
    });
  };

  $scope.putfile = function(filename, data, callback) {
    $http({method: 'PUT',
           url: 'putfile/' + encodeURI(filename),
           data: data})
    .success(function(data, status, headers, config) {
      if (callback) {
        callback.call();
      }
    }).error(function(resp) {
      $scope.select($scope.currentIndex);
    });
  };

  $scope.selectByPath = function(path) {
    for (i in $scope.files) {
      if ($scope.files[i].name == path) {
        $scope.select(i);
        return true;
      }
    }
    return false;
  };

{'fraction_non_alphanumeric': 0.16193733247065348, 'fraction_numerical': 0.0020334596543118586, 'mean_word_length': 2.7220502235982114, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5027042', 'n_tokens_mistral': 3449, 'n_tokens_neox': 3398, 'n_words': 642}