define(function(require) {
"use strict";
var $ = require('jquery');
var utils = require('base/js/utils');
var Contents = function(options) {
this.base_url = options.base_url;
};
Contents.DIRECTORY_NOT_EMPTY_ERROR = 'DirectoryNotEmptyError';
Contents.DirectoryNotEmptyError = function() {
this.message = 'A directory must be empty before being deleted.';
};
Contents.DirectoryNotEmptyError.prototype = Object.create(Error.prototype);
Contents.DirectoryNotEmptyError.prototype.name =
Contents.DIRECTORY_NOT_EMPTY_ERROR;
Contents.prototype.api_url = function() {
var url_parts = [this.base_url, 'api/contents'].concat(
Array.prototype.slice.apply(arguments));
return utils.url_join_encode.apply(null, url_parts);
};
Contents.prototype.create_basic_error_handler = function(callback) {
if (!callback) {
return utils.log_ajax_error;
}
return function(xhr, status, error) {
callback(utils.wrap_ajax_error(xhr, status, error));
};
};
Contents.prototype.get = function (path, options) {
var settings = {
processData : false,
cache : false,
type : "GET",
dataType : "json",
};
var url = this.api_url(path);
var params = {};
if (options.type) { params.type = options.type; }
if (options.format) { params.format = options.format; }
if (options.content === false) { params.content = '0'; }
return utils.promising_ajax(url + '?' + $.param(params), settings);
};
Contents.prototype.new_untitled = function(path, options) {
var data = JSON.stringify({
ext: options.ext,
type: options.type
});
var settings = {
processData : false,
type : "POST",
data: data,
dataType : "json",
};
return utils.promising_ajax(this.api_url(path), settings);
};
Contents.prototype.delete = function(path) {
var settings = {
processData : false,
type : "DELETE",
dataType : "json",
};
var url = this.api_url(path);
return utils.promising_ajax(url, settings).catch(
function(error) {
if (error.xhr.status === 400) {
throw new Contents.DirectoryNotEmptyError();
}
throw error;
}
);
};
Contents.prototype.rename = function(path, new_path) {
var data = {path: new_path};
var settings = {
processData : false,
type : "PATCH",
data : JSON.stringify(data),
dataType: "json",
contentType: 'application/json',
};
var url = this.api_url(path);
return utils.promising_ajax(url, settings);
};
Contents.prototype.save = function(path, model) {
var settings = {
processData : false,
type : "PUT",
dataType: "json",
data : JSON.stringify(model),
contentType: 'application/json',
};
var url = this.api_url(path);
return utils.promising_ajax(url, settings);
};
Contents.prototype.copy = function(from_file, to_dir) {
var url = this.api_url(to_dir);
var settings = {
processData : false,
type: "POST",
data: JSON.stringify({copy_from: from_file}),
dataType : "json",
};
return utils.promising_ajax(url, settings);
};
Contents.prototype.create_checkpoint = function(path) {
var url = this.api_url(path, 'checkpoints');
var settings = {
type : "POST",
dataType : "json",
};
return utils.promising_ajax(url, settings);
};
Contents.prototype.list_checkpoints = function(path) {
var url = this.api_url(path, 'checkpoints');
var settings = {
type : "GET",
cache: false,
dataType: "json",
};
return utils.promising_ajax(url, settings);
};
Contents.prototype.restore_checkpoint = function(path, checkpoint_id) {
var url = this.api_url(path, 'checkpoints', checkpoint_id);
var settings = {
type : "POST",
};
return utils.promising_ajax(url, settings);
};
Contents.prototype.delete_checkpoint = function(path, checkpoint_id) {
var url = this.api_url(path, 'checkpoints', checkpoint_id);
var settings = {
type : "DELETE",
};
return utils.promising_ajax(url, settings);
};
Contents.prototype.list_contents = function(path) {
return this.get(path, {type: 'directory'});
};
return {'Contents': Contents};
});