Some fixes in the client lib for better usability
This commit is contained in:
parent
52d272bd24
commit
d9e3553c7c
2 changed files with 58 additions and 66 deletions
|
|
@ -26,6 +26,24 @@
|
|||
return params;
|
||||
};
|
||||
|
||||
var contentTypeFalse = function(opts) {
|
||||
opts = opts || {};
|
||||
|
||||
var params = $.extend({}, opts);
|
||||
params.contentType = false;
|
||||
|
||||
return params;
|
||||
};
|
||||
|
||||
var noProcessData = function(opts) {
|
||||
opts = opts || {};
|
||||
|
||||
var params = $.extend({}, opts);
|
||||
params.processData = false;
|
||||
|
||||
return params;
|
||||
};
|
||||
|
||||
OctoPrint.options = {
|
||||
"baseurl": undefined,
|
||||
"apikey": undefined
|
||||
|
|
@ -92,6 +110,15 @@
|
|||
return OctoPrint.ajaxWithData("POST", url, data, noCache(opts));
|
||||
};
|
||||
|
||||
OctoPrint.postForm = function(url, data, opts) {
|
||||
var form = new FormData();
|
||||
_.each(data, function(value, key) {
|
||||
form.append(key, value);
|
||||
});
|
||||
|
||||
return OctoPrint.post(url, form, contentTypeFalse(noProcessData(opts)));
|
||||
};
|
||||
|
||||
OctoPrint.postJson = function(url, data, opts) {
|
||||
return OctoPrint.post(url, JSON.stringify(data), contentTypeJson(opts));
|
||||
};
|
||||
|
|
|
|||
|
|
@ -14,17 +14,17 @@
|
|||
return url + "/" + location;
|
||||
};
|
||||
|
||||
var resourceForFile = function(location, filename) {
|
||||
var resourceForEntry = function(location, filename) {
|
||||
return resourceForLocation(location) + "/" + filename;
|
||||
};
|
||||
|
||||
var issueFileCommand = function(location, filename, command, data, opts) {
|
||||
var url = resourceForFile(location, filename);
|
||||
var issueEntryCommand = function(location, entryname, command, data, opts) {
|
||||
var url = resourceForEntry(location, entryname);
|
||||
return OctoPrint.issueCommand(url, command, data, opts);
|
||||
};
|
||||
|
||||
var getFile = function(location, filename, opts) {
|
||||
return OctoPrint.get(resourceForFile(location, filename), opts);
|
||||
var getEntry = function(location, entryname, opts) {
|
||||
return OctoPrint.get(resourceForEntry(location, entryname), opts);
|
||||
};
|
||||
|
||||
var preProcessList = function(response) {
|
||||
|
|
@ -44,65 +44,69 @@
|
|||
};
|
||||
|
||||
OctoPrint.files = {
|
||||
get: getFile,
|
||||
get: getEntry,
|
||||
|
||||
list: function (opts) {
|
||||
return OctoPrint.get(url, opts)
|
||||
list: function (recursively, opts) {
|
||||
recursively = recursively || false;
|
||||
return OctoPrint.getWithQuery(url, {recursive: recursively}, opts)
|
||||
.done(preProcessList);
|
||||
},
|
||||
|
||||
listForLocation: function (location, opts) {
|
||||
return OctoPrint.get(resourceForLocation(location), opts)
|
||||
listForLocation: function (location, recursively, opts) {
|
||||
recursively = recursively || false;
|
||||
return OctoPrint.getWithQuery(resourceForLocation(location), {recursive: recursively}, opts)
|
||||
.done(preProcessList);
|
||||
},
|
||||
|
||||
select: function (location, filename, print, opts) {
|
||||
select: function (location, path, print, opts) {
|
||||
print = print || false;
|
||||
|
||||
var data = {
|
||||
print: print
|
||||
};
|
||||
|
||||
return issueFileCommand(location, filename, "select", data, opts);
|
||||
return issueEntryCommand(location, path, "select", data, opts);
|
||||
},
|
||||
|
||||
slice: function (location, filename, parameters, opts) {
|
||||
return issueFileCommand(location, filename, "slice",
|
||||
slice: function (location, path, parameters, opts) {
|
||||
return issueEntryCommand(location, path, "slice",
|
||||
parameters || {}, opts);
|
||||
},
|
||||
|
||||
delete: function (location, filename, opts) {
|
||||
return OctoPrint.delete(resourceForFile(location, filename), opts);
|
||||
delete: function (location, path, opts) {
|
||||
return OctoPrint.delete(resourceForEntry(location, path), opts);
|
||||
},
|
||||
|
||||
copy: function(location, filename, destination, opts) {
|
||||
return issueFileCommand(location, filename, "copy", { destination: destination }, opts);
|
||||
copy: function(location, path, destination, opts) {
|
||||
return issueEntryCommand(location, path, "copy", { destination: destination }, opts);
|
||||
},
|
||||
|
||||
move: function(location, filename, destination, opts) {
|
||||
return issueFileCommand(location, filename, "move", { destination: destination }, opts);
|
||||
move: function(location, path, destination, opts) {
|
||||
return issueEntryCommand(location, path, "move", { destination: destination }, opts);
|
||||
},
|
||||
|
||||
createFolder: function (location, name, path, opts) {
|
||||
var data = "foldername=" + name;
|
||||
if (path != undefined && path != "") {
|
||||
data = "foldername=" + path + "/" + name;
|
||||
var data = {foldername: name};
|
||||
if (path !== undefined && path !== "") {
|
||||
data.path = path;
|
||||
}
|
||||
|
||||
return OctoPrint.post(resourceForLocation(location), data, opts);
|
||||
|
||||
return OctoPrint.postForm(resourceForLocation(location), data, opts);
|
||||
},
|
||||
|
||||
upload: function (location, file, data) {
|
||||
data = data || {};
|
||||
|
||||
var filename = data.filename || undefined;
|
||||
if (data.userdata && typeof data.userdata === "object") {
|
||||
data.userdata = JSON.stringify(userdata);
|
||||
}
|
||||
return OctoPrint.upload(resourceForLocation(location), file, filename, data);
|
||||
},
|
||||
|
||||
download: function (location, filename, opts) {
|
||||
download: function (location, path, opts) {
|
||||
var deferred = $.Deferred();
|
||||
getFile(location, filename, opts)
|
||||
getEntry(location, path, opts)
|
||||
.done(function (response) {
|
||||
OctoPrint.download(response.refs.download, opts)
|
||||
.done(function () {
|
||||
|
|
@ -116,45 +120,6 @@
|
|||
deferred.reject.apply(null, arguments);
|
||||
});
|
||||
return deferred.promise();
|
||||
},
|
||||
|
||||
pathForElement: function(element) {
|
||||
if (!element || !element.hasOwnProperty("parent") || element.parent == undefined) {
|
||||
return "";
|
||||
}
|
||||
|
||||
var recursivePath = function(element, path) {
|
||||
if (element.hasOwnProperty("parent") && element.parent != undefined) {
|
||||
return recursivePath(element.parent, element.name + "/" + path);
|
||||
}
|
||||
|
||||
return path;
|
||||
};
|
||||
|
||||
return recursivePath(element.parent, element.name);
|
||||
},
|
||||
|
||||
elementByPath: function(location, startElement) {
|
||||
var recursiveSearch = function(location, element) {
|
||||
if (location.length == 0) {
|
||||
return element;
|
||||
}
|
||||
|
||||
if (!element.hasOwnProperty("children")) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
var name = location.shift();
|
||||
for(var i = 0; i < element.children.length; i++) {
|
||||
if (name == element.children[i].name) {
|
||||
return recursiveSearch(location, element.children[i]);
|
||||
}
|
||||
}
|
||||
|
||||
return undefined;
|
||||
};
|
||||
|
||||
return recursiveSearch(location.split("/"), startElement);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
|
|
|||
Loading…
Reference in a new issue