Re-added two utility methods removed in d9e3553c7c

They are still useful for other clients than the core application. Renamed them to fit the
general naming on the API however:

  * pathForElement is now called pathForEntry
  * elementByPath is now called entryForPath
This commit is contained in:
Gina Häußge 2016-09-28 10:21:27 +02:00
parent aca3dfae49
commit 82ae3f6f6e
2 changed files with 108 additions and 0 deletions

View file

@ -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 <http://api.jquery.com/Types/#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

View file

@ -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);
}
}
});