diff --git a/docs/jsclientlib/files.rst b/docs/jsclientlib/files.rst index 399bd868..6220b74a 100644 --- a/docs/jsclientlib/files.rst +++ b/docs/jsclientlib/files.rst @@ -221,3 +221,53 @@ :param string path: The path of the file to download :param object opts: Additional options for the request :returns Promise: A `jQuery Promise `_ for the request's response + +.. js:function:: OctoPrint.files.pathForEntry(entry) + + Utility function to retrieve the path within its location for a given ``entry``. + + Use this if you already have a full list of entries and need the path to one. + + **Example** + + .. code-block:: javascript + + OctoPrint.files.listForLocation("local", True) + .done(function(entries) { + var entry = OctoPrint.files.entryForPath("some/funny/entry", entries.files); + var path = OctoPrint.files.pathForEntry(entry); + console.log(path); // will log some/funny/entry + }); + + :param object entry: The entry object for which to retrieve the path + :returns string: The path of the entry within its location + +.. js:function:: OctoPrint.files.entryForPath(path, root) + + Utility function to retrieve an entry by its ``path`` based on an entry tree provided by its + ``root``. + + Use this if you already have a full list of entries and are looking for a specified entry + within. + + **Example** + + .. code-block:: javascript + + var somePathsToFind = ["some/funny/entry", + "another/entry", + "this/does/not/exist"]; + + OctoPrint.files.listForLocation("local", True) + .done(function(entries) { + // will log two entries and one undefined + _.each(somePathsToFind, function(path) { + console.log(OctoPrint.files.entryForPath(path, entries.files)); + }); + }); + + :param string path: The path of the entry to retrieve + :param object root: The root of the tree in which to resolve the entry by its path, either a list of entries or an entry + element with ``children`` + :returns object or undefined: The retrieved entry, or ``undefined`` if the ``path`` could + not be resolved diff --git a/src/octoprint/static/js/app/client/files.js b/src/octoprint/static/js/app/client/files.js index c3338b1c..b8b03cac 100644 --- a/src/octoprint/static/js/app/client/files.js +++ b/src/octoprint/static/js/app/client/files.js @@ -56,6 +56,49 @@ _.each(response.files, recursiveCheck); }; + var pathForEntry = function(entry) { + if (!entry || !entry.hasOwnProperty("parent") || entry.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(entry.parent, entry.name); + }; + + var entryForPath = function(path, root) { + if (_.isArray(root)) { + root = {children: root}; + } + + var recursiveSearch = function(path, entry) { + if (path.length == 0) { + return entry; + } + + if (!entry.hasOwnProperty("children")) { + return undefined; + } + + var name = path.shift(); + for (var i = 0; i < entry.children.length; i++) { + if (name == entry.children[i].name) { + return recursiveSearch(path, entry.children[i]); + } + } + + return undefined; + }; + + return recursiveSearch(path.split("/"), root); + }; + OctoPrint.files = { get: getEntry, @@ -129,6 +172,21 @@ download: function (location, path, opts) { return OctoPrint.download(downloadForEntry(location, path), opts); + }, + + pathForEntry: pathForEntry, + entryForPath: entryForPath, + + pathForElement: function(element) { + // TODO Remove in 1.4.x + log.warn("OctoPrint.files.pathForElement has been renamed to OctoPrint.files.pathForEntry, please use that instead"); + return pathForEntry(element); + }, + + elementByPath: function(location, startElement) { + // TODO Remove in 1.4.x + log.warn("OctoPrint.files.elementByPath has been renamed to OctoPrint.files.entryForPath, please use that instead"); + return entryForPath(location, startElement); } } });