Migrated client to module factory pattern

This should _hopefully_ also make it compatible to AMD implementations,
but I have to admit that I haven't tried that yet...
This commit is contained in:
Gina Häußge 2015-09-28 17:07:37 +02:00
parent 1a469e1c97
commit c5f0ccdb94
20 changed files with 878 additions and 706 deletions

View file

@ -1,66 +1,71 @@
(function (global, factory) {
if (typeof define === "function" && define.amd) {
define(["OctoPrint"], factory);
} else {
factory(window.OctoPrint);
}
})(window || this, function(OctoPrint) {
var exports = {};
exports.get = function(refresh, opts) {
return OctoPrint.get(OctoPrint.getSimpleApiUrl("pluginmanager") + ((refresh) ? "?refresh_repository=true" : ""), opts);
};
exports.getWithRefresh = function(opts) {
return exports.get(true, opts);
};
exports.getWithoutRefresh = function(opts) {
return exports.get(false, opts);
};
exports.install = function(pluginUrl, dependencyLinks, opts) {
var data = {
url: pluginUrl,
dependency_links: !!dependencyLinks
};
return OctoPrint.simpleApiCommand("pluginmanager", "install", data, opts);
};
exports.reinstall = function(plugin, pluginUrl, dependencyLinks, opts) {
var data = {
url: pluginUrl,
dependency_links: !!dependencyLinks,
reinstall: plugin,
force: true
};
return OctoPrint.simpleApiCommand("pluginmanager", "install", data, opts);
};
exports.uninstall = function(plugin, opts) {
var data = {
plugin: plugin
};
return OctoPrint.simpleApiCommand("pluginmanager", "uninstall", data, opts);
};
exports.enable = function(plugin, opts) {
var data = {
plugin: plugin
};
return OctoPrint.simpleApiCommand("pluginmanager", "enable", data, opts);
};
exports.disable = function(plugin, opts) {
var data = {
plugin: plugin
};
return OctoPrint.simpleApiCommand("pluginmanager", "disable", data, opts);
};
exports.upload = function(file) {
return OctoPrint.upload(OctoPrint.getBlueprintUrl("pluginmanager") + "upload_archive", file);
};
OctoPrint.plugins.pluginmanager = exports;
});
$(function() {
OctoPrint.plugins.pluginmanager = (function($, _) {
var exports = {};
exports.get = function(refresh, opts) {
return OctoPrint.get(OctoPrint.getSimpleApiUrl("pluginmanager") + ((refresh) ? "?refresh_repository=true" : ""), opts);
};
exports.getWithRefresh = function(opts) {
return exports.get(true, opts);
};
exports.getWithoutRefresh = function(opts) {
return exports.get(false, opts);
};
exports.install = function(pluginUrl, dependencyLinks, opts) {
var data = {
url: pluginUrl,
dependency_links: !!dependencyLinks
};
return OctoPrint.simpleApiCommand("pluginmanager", "install", data, opts);
};
exports.reinstall = function(plugin, pluginUrl, dependencyLinks, opts) {
var data = {
url: pluginUrl,
dependency_links: !!dependencyLinks,
reinstall: plugin,
force: true
};
return OctoPrint.simpleApiCommand("pluginmanager", "install", data, opts);
};
exports.uninstall = function(plugin, opts) {
var data = {
plugin: plugin
};
return OctoPrint.simpleApiCommand("pluginmanager", "uninstall", data, opts);
};
exports.enable = function(plugin, opts) {
var data = {
plugin: plugin
};
return OctoPrint.simpleApiCommand("pluginmanager", "enable", data, opts);
};
exports.disable = function(plugin, opts) {
var data = {
plugin: plugin
};
return OctoPrint.simpleApiCommand("pluginmanager", "disable", data, opts);
};
exports.upload = function(file) {
return OctoPrint.upload(OctoPrint.getBlueprintUrl("pluginmanager") + "upload_archive", file);
};
return exports;
})($, _);
function PluginManagerViewModel(parameters) {
var self = this;

View file

@ -1,38 +1,44 @@
(function (global, factory) {
if (typeof define === "function" && define.amd) {
define(["OctoPrint"], factory);
} else {
factory(window.OctoPrint);
}
})(window || this, function(OctoPrint) {
var exports = {};
var url = OctoPrint.getBlueprintUrl("softwareupdate");
var checkUrl = url + "check";
var updateUrl = url + "update";
exports.check = function(force, opts) {
return OctoPrint.get(checkUrl + ((!!force) ? "?force=true" : ""), opts);
};
exports.update = function(entries, force, opts) {
entries = entries || [];
if (typeof entries == "string") {
entries = [entries];
}
var data = {
entries: entries,
force: !!force
};
return OctoPrint.postJson(updateUrl, data, opts);
};
exports.updateAll = function(force, opts) {
var data = {
force: !!force
};
return OctoPrint.postJson(updateUrl, data, opts);
};
OctoPrint.plugins.softwareupdate = exports;
});
$(function() {
OctoPrint.plugins.softwareupdate = (function($, _) {
var exports = {};
var url = OctoPrint.getBlueprintUrl("softwareupdate");
var checkUrl = url + "check";
var updateUrl = url + "update";
exports.check = function(force, opts) {
return OctoPrint.get(checkUrl + ((!!force) ? "?force=true" : ""), opts);
};
exports.update = function(entries, force, opts) {
entries = entries || [];
if (typeof entries == "string") {
entries = [entries];
}
var data = {
entries: entries,
force: !!force
};
return OctoPrint.postJson(updateUrl, data, opts);
};
exports.updateAll = function(force, opts) {
var data = {
force: !!force
};
return OctoPrint.postJson(updateUrl, data, opts);
};
return exports;
})($, _);
function SoftwareUpdateViewModel(parameters) {
var self = this;

View file

@ -1,5 +1,11 @@
var OctoPrint = (function($, _) {
var exports = {};
(function (global, factory) {
if (typeof define === "function" && define.amd) {
define("OctoPrint", ["jquery", "lodash"], factory);
} else {
global.OctoPrint = factory(window.$, window._);
}
})(window || this, function($, _) {
var OctoPrint = {};
var noCache = function(opts) {
opts = opts || {};
@ -20,31 +26,31 @@ var OctoPrint = (function($, _) {
return params;
};
exports.options = {
OctoPrint.options = {
"baseurl": undefined,
"apikey": undefined
};
exports.plugins = {};
OctoPrint.plugins = {};
exports.getBaseUrl = function() {
var url = exports.options.baseurl;
OctoPrint.getBaseUrl = function() {
var url = OctoPrint.options.baseurl;
if (!_.endsWith(url, "/")) {
url = url + "/";
}
return url;
};
exports.getRequestHeaders = function(additional) {
OctoPrint.getRequestHeaders = function(additional) {
additional = additional || {};
var headers = $.extend({}, additional);
headers["X-Api-Key"] = exports.options.apikey;
headers["X-Api-Key"] = OctoPrint.options.apikey;
return headers;
};
exports.ajax = function(method, url, opts) {
OctoPrint.ajax = function(method, url, opts) {
opts = opts || {};
method = opts.method || method || "GET";
@ -52,10 +58,10 @@ var OctoPrint = (function($, _) {
var urlToCall = url;
if (!_.startsWith(url, "http://") && !_.startsWith(url, "https://")) {
urlToCall = exports.getBaseUrl() + url;
urlToCall = OctoPrint.getBaseUrl() + url;
}
var headers = exports.getRequestHeaders(opts.headers);
var headers = OctoPrint.getRequestHeaders(opts.headers);
var params = $.extend({}, opts);
params.type = method;
@ -65,54 +71,54 @@ var OctoPrint = (function($, _) {
return $.ajax(urlToCall, params);
};
exports.ajaxWithData = function(method, url, data, opts) {
OctoPrint.ajaxWithData = function(method, url, data, opts) {
opts = opts || {};
var params = $.extend({}, opts);
params.data = data;
return exports.ajax(method, url, params);
return OctoPrint.ajax(method, url, params);
};
exports.get = function(url, opts) {
return exports.ajax("GET", url, opts);
OctoPrint.get = function(url, opts) {
return OctoPrint.ajax("GET", url, opts);
};
exports.post = function(url, data, opts) {
return exports.ajaxWithData("POST", url, data, noCache(opts));
OctoPrint.post = function(url, data, opts) {
return OctoPrint.ajaxWithData("POST", url, data, noCache(opts));
};
exports.postJson = function(url, data, opts) {
return exports.post(url, JSON.stringify(data), contentTypeJson(opts));
OctoPrint.postJson = function(url, data, opts) {
return OctoPrint.post(url, JSON.stringify(data), contentTypeJson(opts));
};
exports.put = function(url, data, opts) {
return exports.ajaxWithData("PUT", url, data, noCache(opts));
OctoPrint.put = function(url, data, opts) {
return OctoPrint.ajaxWithData("PUT", url, data, noCache(opts));
};
exports.putJson = function(url, data, opts) {
return exports.put(url, data, contentTypeJson(opts));
OctoPrint.putJson = function(url, data, opts) {
return OctoPrint.put(url, data, contentTypeJson(opts));
};
exports.patch = function(url, data, opts) {
return exports.ajaxWithData("PATCH", url, data, noCache(opts));
OctoPrint.patch = function(url, data, opts) {
return OctoPrint.ajaxWithData("PATCH", url, data, noCache(opts));
};
exports.patchJson = function(url, data, opts) {
return exports.patch(url, JSON.stringify(data), contentTypeJson(opts));
OctoPrint.patchJson = function(url, data, opts) {
return OctoPrint.patch(url, JSON.stringify(data), contentTypeJson(opts));
};
exports.delete = function(url, opts) {
return exports.ajax("DELETE", url, opts);
OctoPrint.delete = function(url, opts) {
return OctoPrint.ajax("DELETE", url, opts);
};
exports.download = function(url, opts) {
OctoPrint.download = function(url, opts) {
var params = $.extend({}, opts || {});
params.dataType = "text";
return exports.get(url, params);
return OctoPrint.get(url, params);
};
exports.upload = function(url, file, filename, additional) {
OctoPrint.upload = function(url, file, filename, additional) {
additional = additional || {};
var fileData;
@ -187,38 +193,38 @@ var OctoPrint = (function($, _) {
return deferred.promise();
};
exports.issueCommand = function(url, command, payload, opts) {
OctoPrint.issueCommand = function(url, command, payload, opts) {
payload = payload || {};
var data = $.extend({}, payload);
data.command = command;
return exports.postJson(url, data, opts);
return OctoPrint.postJson(url, data, opts);
};
exports.getSimpleApiUrl = function(plugin) {
OctoPrint.getSimpleApiUrl = function(plugin) {
return "api/plugin/" + plugin;
};
exports.simpleApiGet = function(plugin, opts) {
return OctoPrint.get(exports.getSimpleApiUrl(plugin), opts);
OctoPrint.simpleApiGet = function(plugin, opts) {
return OctoPrint.get(OctoPrint.getSimpleApiUrl(plugin), opts);
};
exports.simpleApiCommand = function(plugin, command, payload, opts) {
return OctoPrint.issueCommand(exports.getSimpleApiUrl(plugin), command, payload, opts);
OctoPrint.simpleApiCommand = function(plugin, command, payload, opts) {
return OctoPrint.issueCommand(OctoPrint.getSimpleApiUrl(plugin), command, payload, opts);
};
exports.getBlueprintUrl = function(plugin) {
OctoPrint.getBlueprintUrl = function(plugin) {
return "plugin/" + plugin + "/";
};
exports.createRejectedDeferred = function() {
OctoPrint.createRejectedDeferred = function() {
var deferred = $.Deferred();
deferred.reject(arguments);
return deferred;
};
exports.createCustomException = function(name) {
OctoPrint.createCustomException = function(name) {
var constructor;
if (_.isFunction(name)) {
@ -237,7 +243,7 @@ var OctoPrint = (function($, _) {
return constructor;
};
exports.InvalidArgumentError = exports.createCustomException("InvalidArgumentError");
OctoPrint.InvalidArgumentError = OctoPrint.createCustomException("InvalidArgumentError");
return exports;
})($, _);
return OctoPrint;
});

View file

@ -1,22 +1,29 @@
OctoPrint.browser = (function($, _) {
var exports = {};
(function (global, factory) {
if (typeof define === "function" && define.amd) {
define(["OctoPrint"], factory);
} else {
factory(window.OctoPrint);
}
})(window || this, function(OctoPrint) {
var loginUrl = "api/login";
var logoutUrl = "api/logout";
exports.login = function(username, password, remember, opts) {
var data = {
user: username,
pass: password,
remember: !!remember
};
return OctoPrint.postJson("api/login", data, opts);
OctoPrint.browser = {
login: function(username, password, remember, opts) {
var data = {
user: username,
pass: password,
remember: !!remember
};
return OctoPrint.postJson(loginUrl, data, opts);
},
passiveLogin: function(opts) {
return OctoPrint.postJson(loginUrl, {passive: true}, opts);
},
logout: function(opts) {
return OctoPrint.postJson(logoutUrl, {}, opts);
}
};
exports.passiveLogin = function(opts) {
return OctoPrint.postJson("api/login", {passive: true}, opts);
};
exports.logout = function(opts) {
return OctoPrint.postJson("api/logout", {}, opts);
};
return exports;
})($, _);
});

View file

@ -1,25 +1,27 @@
OctoPrint.connection = (function() {
var exports = {};
(function (global, factory) {
if (typeof define === "function" && define.amd) {
define(["OctoPrint"], factory);
} else {
factory(window.OctoPrint);
}
})(window || this, function(OctoPrint) {
var url = "api/connection";
exports.getSettings = function(opts) {
return OctoPrint.get("api/connection", opts);
};
OctoPrint.connection = {
getSettings: function(opts) {
return OctoPrint.get(url, opts);
},
exports.connect = function(data, opts) {
return exports.issueCommand("connect", data || {}, opts);
};
connect: function(data, opts) {
return OctoPrint.issueCommand(url, "connect", data || {}, opts);
},
exports.disconnect = function(opts) {
return exports.issueCommand("disconnect", {}, opts);
};
disconnect: function(opts) {
return OctoPrint.issueCommand(url, "disconnect", {}, opts);
},
exports.fakeAck = function(opts) {
return exports.issueCommand("fake_ack", {}, opts);
};
exports.issueCommand = function(command, data, opts) {
return OctoPrint.issueCommand("api/connection", command, data, opts);
};
return exports;
})($, _);
fakeAck: function(opts) {
return OctoPrint.issueCommand(url, "fake_ack", {}, opts);
}
}
});

View file

@ -1,44 +1,48 @@
OctoPrint.control = (function($, _) {
var exports = {};
(function (global, factory) {
if (typeof define === "function" && define.amd) {
define(["OctoPrint"], factory);
} else {
factory(window.OctoPrint);
}
})(window || this, function(OctoPrint) {
var customUrl = "api/printer/command/custom";
var commandUrl = "api/printer/command";
exports.getCustomControls = function(opts) {
return OctoPrint.get(customUrl, opts);
};
OctoPrint.control = {
getCustomControls: function (opts) {
return OctoPrint.get(customUrl, opts);
},
exports.sendGcode = function(commands, opts) {
commands = commands || [];
sendGcode: function (commands, opts) {
return exports.sendGcodeWithParameters(commands, undefined, opts);
},
if (typeof commands === "string") {
commands = [commands];
sendGcodeWithParameters: function (commands, parameters, opts) {
commands = commands || [];
parameters = parameters || {};
if (typeof commands === "string") {
commands = [commands];
}
return OctoPrint.postJson(commandUrl, {
commands: commands,
parameters: parameters
}, opts);
},
sendGcodeScript: function (script, context, opts) {
script = script || "";
context = context || {};
return OctoPrint.postJson(commandUrl, {
script: script,
context: context
}, opts);
},
executeSystemCommand: function (action, opts) {
return OctoPrint.postJson("api/system", {action: action}, opts);
}
return OctoPrint.postJson(commandUrl, {commands: commands}, opts);
};
exports.sendGcodeWithParameters = function(commands, parameters, opts) {
commands = commands || [];
parameters = parameters || {};
if (typeof commands === "string") {
commands = [commands];
}
return OctoPrint.postJson(commandUrl, {commands: commands, parameters: parameters}, opts);
};
exports.sendGcodeScript = function(script, context, opts) {
script = script || "";
context = context || {};
return OctoPrint.postJson(commandUrl, {script: script, context: context}, opts);
};
exports.executeSystemCommand = function(action, opts) {
return OctoPrint.postJson("api/system", {action: action}, opts);
};
return exports;
})($, _);
}
});

View file

@ -1,8 +1,21 @@
OctoPrint.files = (function($, _) {
var exports = {};
(function (factory) {
'use strict';
if (typeof define === 'function' && define.amd) {
// Register as an anonymous AMD module:
define(["OctoPrint", "jquery"], factory);
} else {
// Browser globals:
factory(window.OctoPrint, window.jQuery);
}
})(function(OctoPrint, $) {
var url = "api/files";
var resourceForLocation = function(location) {
return url + "/" + location;
};
var resourceForFile = function(location, filename) {
return "api/files/" + location + "/" + filename;
return resourceForLocation(location) + "/" + filename;
};
var issueFileCommand = function(location, filename, command, data, opts) {
@ -10,61 +23,61 @@ OctoPrint.files = (function($, _) {
return OctoPrint.issueCommand(url, command, data, opts);
};
exports.list = function(opts) {
return OctoPrint.get("api/files", opts);
};
OctoPrint.files = {
list: function (opts) {
return OctoPrint.get(url, opts);
},
exports.listForLocation = function(location, opts) {
return OctoPrint.get("api/files/" + location, opts);
};
listForLocation: function (location, opts) {
return OctoPrint.get(resourceForLocation(location), opts);
},
exports.get = function(location, filename, opts) {
return OctoPrint.get(resourceForFile(location, filename), opts);
};
get: function (location, filename, opts) {
return OctoPrint.get(resourceForFile(location, filename), opts);
},
exports.select = function(location, filename, print, opts) {
print = print || false;
select: function (location, filename, print, opts) {
print = print || false;
var data = {
print: print
};
var data = {
print: print
};
return issueFileCommand(location, filename, "select", data, opts);
};
return issueFileCommand(location, filename, "select", data, opts);
},
exports.slice = function(location, filename, parameters, opts) {
return issueFileCommand(location, filename, "slice",
parameters || {}, opts);
};
slice: function (location, filename, parameters, opts) {
return issueFileCommand(location, filename, "slice",
parameters || {}, opts);
},
exports.delete = function(location, filename, opts) {
return OctoPrint.delete(resourceForFile(location, filename), opts);
};
delete: function (location, filename, opts) {
return OctoPrint.delete(resourceForFile(location, filename), opts);
},
exports.upload = function(location, file, data) {
data = data || {};
upload: function (location, file, data) {
data = data || {};
var filename = data.filename || undefined;
return OctoPrint.upload("api/files/" + location, file, filename, data);
};
var filename = data.filename || undefined;
return OctoPrint.upload(resourceForLocation(location), file, filename, data);
},
exports.download = function(location, filename, opts) {
var deferred = $.Deferred();
exports.get(location, filename, opts)
.done(function(response) {
OctoPrint.download(response.refs.download, opts)
.done(function() {
deferred.resolve.apply(null, arguments);
})
.fail(function() {
deferred.reject.apply(null, arguments);
});
})
.fail(function() {
deferred.reject.apply(null, arguments);
});
return deferred.promise();
};
return exports;
})($, _);
download: function (location, filename, opts) {
var deferred = $.Deferred();
exports.get(location, filename, opts)
.done(function (response) {
OctoPrint.download(response.refs.download, opts)
.done(function () {
deferred.resolve.apply(null, arguments);
})
.fail(function () {
deferred.reject.apply(null, arguments);
});
})
.fail(function () {
deferred.reject.apply(null, arguments);
});
return deferred.promise();
}
}
});

View file

@ -1,31 +1,31 @@
OctoPrint.job = (function($, _) {
var exports = {};
(function (global, factory) {
if (typeof define === "function" && define.amd) {
define(["OctoPrint"], factory);
} else {
factory(window.OctoPrint);
}
})(window || this, function(OctoPrint) {
var url = "api/job";
var issueCommand = function(command, data, opts) {
return OctoPrint.issueCommand(url, command, data, opts);
var issueCommand = function(command, opts) {
return OctoPrint.issueCommand(url, command, {}, opts);
};
exports.get = function(opts) {
return OctoPrint.get(url, opts);
};
exports.start = function(opts) {
return issueCommand("start", {}, opts);
};
exports.restart = function(opts) {
return issueCommand("restart", {}, opts);
};
exports.pause = function(opts) {
return issueCommand("pause", {}, opts);
};
exports.cancel = function(opts) {
return issueCommand("cancel", {}, opts);
};
return exports;
})($, _);
OctoPrint.job = {
get: function(opts) {
return OctoPrint.get(url, opts);
},
start: function(opts) {
return issueCommand("start", opts);
},
restart: function(opts) {
return issueCommand("restart", opts);
},
pause: function(opts) {
return issueCommand("pause", opts);
},
cancel: function(opts) {
return issueCommand("cancel", opts);
}
}
});

View file

@ -1,20 +1,22 @@
OctoPrint.languages = (function($, _) {
var exports = {};
(function (global, factory) {
if (typeof define === "function" && define.amd) {
define(["OctoPrint"], factory);
} else {
factory(window.OctoPrint);
}
})(window || this, function(OctoPrint) {
var url = "api/languages";
exports.list = function(opt) {
return OctoPrint.get(url, opt);
OctoPrint.languages = {
list: function(opts) {
return OctoPrint.get(url, opts);
},
upload: function(file) {
return OctoPrint.upload(url, file);
},
delete: function(locale, pack, opts) {
var packUrl = url + "/" + locale + "/" + pack;
return OctoPrint.delete(packUrl, opts);
}
};
exports.upload = function(file) {
return OctoPrint.upload(url, file);
};
exports.delete = function(locale, pack, opts) {
var packUrl = url + "/" + locale + "/" + pack;
return OctoPrint.delete(packUrl, opts);
};
return exports;
})($, _);
});

View file

@ -1,21 +1,25 @@
OctoPrint.logs = (function($, _) {
var exports = {};
(function (global, factory) {
if (typeof define === "function" && define.amd) {
define(["OctoPrint"], factory);
} else {
factory(window.OctoPrint);
}
})(window || this, function(OctoPrint) {
var url = "api/logs";
exports.list = function(opts) {
return OctoPrint.get(url, opts);
};
OctoPrint.logs = {
list: function(opts) {
return OctoPrint.get(url, opts);
},
exports.delete = function(file, opts) {
var fileUrl = url + "/" + file;
return OctoPrint.delete(fileUrl, opts);
};
delete: function(file, opts) {
var fileUrl = url + "/" + file;
return OctoPrint.delete(fileUrl, opts);
},
exports.download = function(file, opts) {
var fileUrl = url + "/" + file;
return OctoPrint.download(fileUrl, opts);
};
return exports;
})($, _);
download: function(file, opts) {
var fileUrl = url + "/" + file;
return OctoPrint.download(fileUrl, opts);
}
}
});

View file

@ -1,197 +1,207 @@
OctoPrint.printer = (function($, _) {
var exports = {};
(function (global, factory) {
if (typeof define === "function" && define.amd) {
define(["OctoPrint"], factory);
} else {
factory(window.OctoPrint);
}
})(window || this, function(OctoPrint) {
var url = "api/printer";
var printheadUrl = url + "/printhead";
var toolUrl = url + "/tool";
var bedUrl = url + "/bed";
var sdUrl = url + "/sd";
var issuePrintheadCommand = function(command, payload, opts) {
return OctoPrint.issueCommand("api/printer/printhead", command, payload, opts);
var issuePrintheadCommand = function (command, payload, opts) {
return OctoPrint.issueCommand(printheadUrl, command, payload, opts);
};
var issueToolCommand = function(command, payload, opts) {
return OctoPrint.issueCommand("api/printer/tool", command, payload, opts);
var issueToolCommand = function (command, payload, opts) {
return OctoPrint.issueCommand(toolUrl, command, payload, opts);
};
var issueBedCommand = function(command, payload, opts) {
return OctoPrint.issueCommand("api/printer/bed", command, payload, opts);
var issueBedCommand = function (command, payload, opts) {
return OctoPrint.issueCommand(bedUrl, command, payload, opts);
};
var issueSdCommand = function(command, payload, opts) {
return OctoPrint.issueCommand("api/printer/sd", command, payload, opts);
var issueSdCommand = function (command, payload, opts) {
return OctoPrint.issueCommand(sdUrl, command, payload, opts);
};
exports.getFullState = function(data, opts) {
data = data || {};
OctoPrint.printer = {
getFullState: function (data, opts) {
data = data || {};
var history = data.history || undefined;
var limit = data.limit || undefined;
var exclude = data.exclude || undefined;
var history = data.history || undefined;
var limit = data.limit || undefined;
var exclude = data.exclude || undefined;
var url = "api/printer";
if (history || exclude) {
url += "?";
if (history) {
url += "history=true&";
if (limit) {
url += "limit=" + limit + "&";
var getUrl = url;
if (history || exclude) {
getUrl += "?";
if (history) {
getUrl += "history=true&";
if (limit) {
getUrl += "limit=" + limit + "&";
}
}
if (exclude) {
getUrl += "exclude=" + exclude.join(",") + "&";
}
}
if (exclude) {
url += "exclude=" + exclude.join(",") + "&";
return OctoPrint.get(getUrl, opts);
},
getToolState: function (data, opts) {
data = data || {};
var history = data.history || undefined;
var limit = data.limit || undefined;
var getUrl = toolUrl;
if (history) {
getUrl += "?history=true";
if (limit) {
getUrl += "&limit=" + limit;
}
}
}
return OctoPrint.get(url, opts);
};
return OctoPrint.get(getUrl, opts);
},
exports.getToolState = function(data, opts) {
data = data || {};
getBedState: function (data, opts) {
data = data || {};
var history = data.history || undefined;
var limit = data.limit || undefined;
var history = data.history || undefined;
var limit = data.limit || undefined;
var url = "api/printer/tool";
if (history) {
url += "?history=true";
if (limit) {
url += "&limit=" + limit;
var getUrl = bedUrl;
if (history) {
getUrl += "?history=true";
if (limit) {
getUrl += "&limit=" + limit;
}
}
return OctoPrint.get(getUrl, opts);
},
getSdState: function (opts) {
return OctoPrint.get(sdUrl, opts);
},
jog: function (data, opts) {
data = data || {};
var payload = {};
if (data.x) payload.x = data.x;
if (data.y) payload.y = data.y;
if (data.z) payload.z = data.z;
return issuePrintheadCommand("jog", payload, opts);
},
home: function (axes, opts) {
axes = axes || [];
var payload = {
axes: axes
};
return issuePrintheadCommand("home", payload, opts);
},
setFeedrate: function (factor, opts) {
factor = factor || 100;
var payload = {
factor: factor
};
return issuePrintheadCommand("feedrate", payload, opts);
},
setToolTargetTemperatures: function (targets, opts) {
targets = targets || {};
var payload = {
targets: targets
};
return issueToolCommand("target", payload, opts);
},
setToolTemperatureOffsets: function (offsets, opts) {
offsets = offsets || {};
var payload = {
offsets: offsets
};
return issueToolCommand("offset", payload, opts);
},
selectTool: function (tool, opts) {
tool = tool || undefined;
var payload = {
tool: tool
};
return issueToolCommand("select", payload, opts);
},
extrude: function (amount, opts) {
amount = amount || undefined;
var payload = {
amount: amount
};
return issueToolCommand("extrude", payload, opts);
},
setFlowrate: function (factor, opts) {
factor = factor || 100;
var payload = {
factor: factor
};
return issueToolCommand("flowrate", payload, opts);
},
setBedTargetTemperature: function (temperature, opts) {
temperature = temperature || 0;
var payload = {
target: temperature
};
return issueBedCommand("target", payload, opts);
},
setBedTemperatureOffset: function (offset, opts) {
offset = offset || 0;
var payload = {
offset: offset
};
return issueBedCommand("offset", payload, opts);
},
initSd: function (opts) {
return issueSdCommand("init", {}, opts);
},
refreshSd: function (opts) {
return issueSdCommand("refresh", {}, opts);
},
releaseSd: function (opts) {
return issueSdCommand("release", {}, opts);
}
return OctoPrint.get(url, opts);
};
exports.getBedState = function(data, opts) {
data = data || {};
var history = data.history || undefined;
var limit = data.limit || undefined;
var url = "api/printer/bed";
if (history) {
url += "?history=true";
if (limit) {
url += "&limit=" + limit;
}
}
return OctoPrint.get(url, opts);
};
exports.getSdState = function(opts) {
return OctoPrint.get("api/printer/sd", opts);
};
exports.jog = function(data, opts) {
data = data || {};
var payload = {};
if (data.x) payload.x = data.x;
if (data.y) payload.y = data.y;
if (data.z) payload.z = data.z;
return issuePrintheadCommand("jog", payload, opts);
};
exports.home = function(axes, opts) {
axes = axes || [];
var payload = {
axes: axes
};
return issuePrintheadCommand("home", payload, opts);
};
exports.setFeedrate = function(factor, opts) {
factor = factor || 100;
var payload = {
factor: factor
};
return issuePrintheadCommand("feedrate", payload, opts);
};
exports.setToolTargetTemperatures = function(targets, opts) {
targets = targets || {};
var payload = {
targets: targets
};
return issueToolCommand("target", payload, opts);
};
exports.setToolTemperatureOffsets = function(offsets, opts) {
offsets = offsets || {};
var payload = {
offsets: offsets
};
return issueToolCommand("offset", payload, opts);
};
exports.selectTool = function(tool, opts) {
tool = tool || undefined;
var payload = {
tool: tool
};
return issueToolCommand("select", payload, opts);
};
exports.extrude = function(amount, opts) {
amount = amount || undefined;
var payload = {
amount: amount
};
return issueToolCommand("extrude", payload, opts);
};
exports.setFlowrate = function(factor, opts) {
factor = factor || 100;
var payload = {
factor: factor
};
return issueToolCommand("flowrate", payload, opts);
};
exports.setBedTargetTemperature = function(temperature, opts) {
temperature = temperature || 0;
var payload = {
target: temperature
};
return issueBedCommand("target", payload, opts);
};
exports.setBedTemperatureOffset = function(offset, opts) {
offset = offset || 0;
var payload = {
offset: offset
};
return issueBedCommand("offset", payload, opts);
};
exports.initSd = function(opts) {
return issueSdCommand("init", {}, opts);
};
exports.refreshSd = function(opts) {
return issueSdCommand("refresh", {}, opts);
};
exports.releaseSd = function(opts) {
return issueSdCommand("release", {}, opts);
};
return exports;
})($, _);
}
});

View file

@ -1,37 +1,43 @@
OctoPrint.printerprofiles = (function($, _) {
var exports = {};
(function (global, factory) {
if (typeof define === "function" && define.amd) {
define(["OctoPrint", "jquery"], factory);
} else {
factory(window.OctoPrint, window.$);
}
})(window || this, function(OctoPrint, $) {
var url = "api/printerprofiles";
exports.get = function(opts) {
return OctoPrint.get(url, opts);
var profileUrl = function(profile) {
return url + "/" + profile;
};
exports.add = function(profile, additional, opts) {
profile = profile || {};
additional = additional || {};
OctoPrint.printerprofiles = {
get: function (opts) {
return OctoPrint.get(url, opts);
},
var data = $.extend({}, additional);
data.profile = profile;
add: function (profile, additional, opts) {
profile = profile || {};
additional = additional || {};
return OctoPrint.postJson(url, data, opts);
};
var data = $.extend({}, additional);
data.profile = profile;
exports.update = function(id, profile, additional, opts) {
profile = profile || {};
additional = addtional || {};
return OctoPrint.postJson(url, data, opts);
},
var data = $.extend({}, additional);
data.profile = profile;
update: function (id, profile, additional, opts) {
profile = profile || {};
additional = addtional || {};
var profileUrl = url + "/" + id;
return OctoPrint.patchJson(profileUrl, data, opts);
};
var data = $.extend({}, additional);
data.profile = profile;
exports.delete = function(id, opts) {
var profileUrl = url + "/" + id;
return OctoPrint.delete(profileUrl, opts);
};
return OctoPrint.patchJson(profileUrl(id), data, opts);
},
return exports;
})($, _);
delete: function (id, opts) {
return OctoPrint.delete(profileUrl(id), opts);
}
}
});

View file

@ -1,36 +1,43 @@
OctoPrint.settings = (function($, _) {
var exports = {};
(function (global, factory) {
if (typeof define === "function" && define.amd) {
define(["OctoPrint", "jquery"], factory);
} else {
factory(window.OctoPrint, window.$);
}
})(window || this, function(OctoPrint, $) {
var url = "api/settings";
exports.get = function(opts) {
var get = function(opts) {
return OctoPrint.get(url, opts);
};
exports.save = function(settings, opts) {
var save = function(settings, opts) {
settings = settings || {};
return OctoPrint.postJson(url, settings, opts);
};
exports.getPluginSettings = function(plugin, opts) {
return exports.get(opts)
.then(function(settings, statusText, request) {
if (!settings.plugins || !settings.plugins[plugin]) {
return $.Deferred()
.reject(request, "dataerror", "No settings for plugin " + plugin)
.promise();
} else {
return settings.plugins[plugin];
}
});
};
OctoPrint.settings = {
get: get,
save: save,
exports.savePluginSettings = function(plugin, settings, opts) {
var data = {};
data["plugins"] = {};
data["plugins"][plugin] = settings;
return exports.save(data, opts);
};
getPluginSettings: function (plugin, opts) {
return get(opts)
.then(function (settings, statusText, request) {
if (!settings.plugins || !settings.plugins[plugin]) {
return $.Deferred()
.reject(request, "dataerror", "No settings for plugin " + plugin)
.promise();
} else {
return settings.plugins[plugin];
}
});
},
return exports;
})($, _);
savePluginSettings: function (plugin, settings, opts) {
var data = {};
data["plugins"] = {};
data["plugins"][plugin] = settings;
return save(data, opts);
}
}
});

View file

@ -1,38 +1,45 @@
OctoPrint.slicing = (function($, _) {
var exports = {};
(function (global, factory) {
if (typeof define === "function" && define.amd) {
define(["OctoPrint"], factory);
} else {
factory(window.OctoPrint);
}
})(window || this, function(OctoPrint) {
var url = "api/slicing";
var getProfileUrl = function(slicer, profileId) {
return url + "/" + slicer + "/profiles/" + profileId;
var slicerUrl = function(slicer) {
return url + "/" + slicer;
};
exports.listAllSlicersAndProfiles = function(opts) {
return OctoPrint.get(url, opts);
var profileUrl = function(slicer, profileId) {
return slicerUrl(slicer) + "/profiles/" + profileId;
};
exports.listProfilesForSlicer = function(slicer, opts) {
var slicerUrl = url + "/" + slicer + "/profiles";
return OctoPrint.get(slicerUrl, opts);
};
OctoPrint.slicing = {
listAllSlicersAndProfiles: function(opts) {
return OctoPrint.get(url, opts);
},
exports.getProfileForSlicer = function(slicer, profileId, opts) {
return OctoPrint.get(getProfileUrl(slicer, profileId), opts);
};
listProfilesForSlicer: function(slicer, opts) {
return OctoPrint.get(slicerUrl(slicer) + "/profiles", opts);
},
exports.addProfileForSlicer = function(slicer, profileId, profile, opts) {
profile = profile || {};
return OctoPrint.putJson(getProfileUrl(slicer, profileId), profile, opts);
};
getProfileForSlicer: function(slicer, profileId, opts) {
return OctoPrint.get(profileUrl(slicer, profileId), opts);
},
exports.updateProfileForSlicer = function(slicer, profileId, profile, opts) {
profile = profile || {};
return OctoPrint.patchJson(getProfileUrl(slicer, profileId), profile, opts);
};
addProfileForSlicer: function(slicer, profileId, profile, opts) {
profile = profile || {};
return OctoPrint.putJson(profileUrl(slicer, profileId), profile, opts);
},
exports.deleteProfileForSlicer = function(slicer, profileId, opts) {
return OctoPrint.delete(getProfileUrl(slicer, profileId), opts);
};
updateProfileForSlicer: function(slicer, profileId, profile, opts) {
profile = profile || {};
return OctoPrint.patchJson(profileUrl(slicer, profileId), profile, opts);
},
return exports;
})($, _);
deleteProfileForSlicer: function(slicer, profileId, opts) {
return OctoPrint.delete(profileUrl(slicer, profileId), opts);
}
}
});

View file

@ -1,4 +1,10 @@
OctoPrint.socket = (function($, _, SockJS) {
(function (global, factory) {
if (typeof define === "function" && define.amd) {
define(["OctoPrint", "jquery", "lodash", "sockjs"], factory);
} else {
factory(window.OctoPrint, window.$, window._, window.SockJS);
}
})(window || this, function(OctoPrint, $, _, SockJS) {
var exports = {};
exports.options = {
@ -102,5 +108,5 @@ OctoPrint.socket = (function($, _, SockJS) {
exports.onReconnectAttempt = function(trial) {};
exports.onReconnectFailed = function() {};
return exports;
})($, _, SockJS);
OctoPrint.socket = exports;
});

View file

@ -1,50 +1,60 @@
OctoPrint.timelapse = (function($, _) {
(function (global, factory) {
if (typeof define === "function" && define.amd) {
define(["OctoPrint", "jquery"], factory);
} else {
factory(window.OctoPrint, window.$);
}
})(window || this, function(OctoPrint, $) {
var exports = {};
var url = "api/timelapse";
exports.get = function(opts) {
return OctoPrint.get(url, opts);
var timelapseUrl = function(filename) {
return url + "/" + filename;
};
exports.list = function(opts) {
var deferred = $.Deferred();
OctoPrint.timelapse = {
get: function (opts) {
return OctoPrint.get(url, opts);
},
exports.get(opts)
.done(function(response, status, request) {
deferred.resolve(response.files, status, request);
})
.fail(function() {
deferred.reject.apply(null, arguments);
});
list: function (opts) {
var deferred = $.Deferred();
return deferred.promise();
};
exports.get(opts)
.done(function (response, status, request) {
deferred.resolve(response.files, status, request);
})
.fail(function () {
deferred.reject.apply(null, arguments);
});
exports.download = function(filename, opts) {
return OctoPrint.download(url + "/" + filename, opts);
};
return deferred.promise();
},
exports.delete = function(filename, opts) {
return OctoPrint.delete(url + "/" + filename, opts);
};
download: function (filename, opts) {
return OctoPrint.download(timelapseUrl(filename), opts);
},
exports.getConfig = function(opts) {
var deferred = $.Deferred();
exports.get(opts)
.done(function(response, status, request) {
deferred.resolve(response.config, status, request);
})
.fail(function() {
deferred.reject.apply(null, arguments);
});
return deferred.promise();
};
delete: function (filename, opts) {
return OctoPrint.delete(timelapseUrl(filename), opts);
},
exports.saveConfig = function(config, opts) {
config = config || {};
return OctoPrint.postJson(url, config, opts);
};
getConfig: function (opts) {
var deferred = $.Deferred();
exports.get(opts)
.done(function (response, status, request) {
deferred.resolve(response.config, status, request);
})
.fail(function () {
deferred.reject.apply(null, arguments);
});
return deferred.promise();
},
return exports;
})($, _);
saveConfig: function (config, opts) {
config = config || {};
return OctoPrint.postJson(url, config, opts);
}
}
});

View file

@ -1,6 +1,10 @@
OctoPrint.users = (function($, _) {
var exports = {};
(function (global, factory) {
if (typeof define === "function" && define.amd) {
define(["OctoPrint"], factory);
} else {
factory(window.OctoPrint);
}
})(window || this, function(OctoPrint) {
var baseUrl = "api/users";
var url = function() {
@ -11,64 +15,96 @@ OctoPrint.users = (function($, _) {
}
};
exports.list = function(opts) {
return OctoPrint.get(url(), opts);
};
OctoPrint.users = {
list: function (opts) {
return OctoPrint.get(url(), opts);
},
exports.add = function(user, opts) {
if (!user.name || !user.password) {
throw new OctoPrint.InvalidArgumentError("Both user's name and password need to be set");
add: function (user, opts) {
if (!user.name || !user.password) {
throw new OctoPrint.InvalidArgumentError("Both user's name and password need to be set");
}
var data = {
name: user.name,
password: user.password,
active: user.hasOwnProperty("active") ? !!user.active : true,
admin: user.hasOwnProperty("admin") ? !!user.admin : false
};
return OctoPrint.postJson(url(), data, opts);
},
get: function (name, opts) {
if (!name) {
throw new OctoPrint.InvalidArgumentError("user name must be set");
}
return OctoPrint.get(url(name), opts);
},
update: function (name, active, admin, opts) {
if (!name) {
throw new OctoPrint.InvalidArgumentError("user name must be set");
}
var data = {
active: !!active,
admin: !!admin
};
return OctoPrint.putJson(url(name), data, opts);
},
delete: function (name, opts) {
if (!name) {
throw new OctoPrint.InvalidArgumentError("user name must be set");
}
return OctoPrint.delete(url(name), opts);
},
changePassword: function (name, password, opts) {
if (!name || !password) {
throw new OctoPrint.InvalidArgumentError("user name and password must be set");
}
var data = {
password: password
};
return OctoPrint.putJson(url(name, "password"), data, opts);
},
generateApiKey: function (name, opts) {
if (!name) {
throw new OctoPrint.InvalidArgumentError("user name must be set");
}
return OctoPrint.postJson(url(name, "apikey"), opts);
},
resetApiKey: function (name, opts) {
if (!name) {
throw new OctoPrint.InvalidArgumentError("user name must be set");
}
return OctoPrint.delete(url(name, "apikey"), opts);
},
getSettings: function (name, opts) {
if (!name) {
throw new OctoPrint.InvalidArgumentError("user name must be set");
}
return OctoPrint.get(url(name, "settings"), opts);
},
saveSettings: function (name, settings, opts) {
if (!name) {
throw new OctoPrint.InvalidArgumentError("user name must be set");
}
settings = settings || {};
return OctoPrint.patchJson(url(name, "settings"), settings, opts);
}
var data = {
name: user.name,
password: user.password,
active: user.hasOwnProperty("active") ? !!user.active : true,
admin: user.hasOwnProperty("admin") ? !!user.admin : false
};
return OctoPrint.postJson(url(), data, opts);
};
exports.get = function(name, opts) {
return OctoPrint.get(url(name), opts);
};
exports.update = function(name, active, admin, opts) {
var data = {
active: !!active,
admin: !!admin
};
return OctoPrint.putJson(url(name), data, opts);
};
exports.delete = function(name, opts) {
return OctoPrint.delete(url(name), opts);
};
exports.changePassword = function(name, password, opts) {
var data = {
password: password
};
return OctoPrint.putJson(url(name, "password"), data, opts);
};
exports.generateApiKey = function(name, opts) {
return OctoPrint.postJson(url(name, "apikey"), opts);
};
exports.resetApiKey = function(name, opts) {
return OctoPrint.delete(url(name, "apikey"), opts);
};
exports.getSettings = function(name, opts) {
return OctoPrint.get(url(name, "settings"), opts);
};
exports.saveSettings = function(name, settings, opts) {
settings = settings || {};
return OctoPrint.patchJson(url(name, "settings"), settings, opts);
};
return exports;
})($, _);
});

View file

@ -1,11 +1,47 @@
OctoPrint.util = (function($, _) {
var exports = {};
(function (global, factory) {
if (typeof define === "function" && define.amd) {
define(["OctoPrint", "jquery"], factory);
} else {
factory(window.OctoPrint, window.$);
}
})(window || this, function(OctoPrint, $) {
var url = "api/util";
var testUrl = url + "/test";
exports.test = function(command, data, opts) {
return OctoPrint.issueCommand(url + "/test", command, data, opts);
var test = function(command, data, opts) {
return OctoPrint.issueCommand(testUrl, command, data, opts);
};
return exports;
})($, _);
OctoPrint.util = {
test: test,
testPath: function(path, additional, opts) {
additional = additional || {};
var data = $.extend({}, additional);
data.path = path;
return test("path", data, opts);
},
testExecutable: function(path, additional, opts) {
additional = additional || {};
var data = $.extend({}, additional);
data.path = path;
data.check_type = "file";
data.check_access = "x";
return test("path", data, opts);
},
testUrl: function(url, additional, opts) {
additional = additional || {};
var data = $.extend({}, additional);
data.url = url;
return test("url", data, opts);
}
};
});

View file

@ -1,15 +1,18 @@
OctoPrint.wizard = (function($, _) {
var exports = {};
(function (global, factory) {
if (typeof define === "function" && define.amd) {
define(["OctoPrint"], factory);
} else {
factory(window.OctoPrint);
}
})(window || this, function(OctoPrint) {
var url = "api/setup/wizard";
exports.get = function(opts) {
return OctoPrint.get(url, opts);
OctoPrint.wizard = {
get: function(opts) {
return OctoPrint.get(url, opts);
},
finish: function(handled, opts) {
return OctoPrint.postJson(url, {handled: handled || []}, opts);
}
};
exports.finish = function(handled, opts) {
return OctoPrint.postJson(url, {handled: handled || []}, opts);
};
return exports;
})($, _);
});

View file

@ -1,4 +1,6 @@
$(function() {
OctoPrint = window.OctoPrint;
//~~ Lodash setup
_.mixin({"sprintf": sprintf, "vsprintf": vsprintf});