/
objectTree.js
82 lines (71 loc) · 2.42 KB
/
objectTree.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*
* @package jsDAV
* @subpackage DAV
* @copyright Copyright(c) 2011 Ajax.org B.V. <info AT ajax DOT org>
* @author Mike de Boer <info AT mikedeboer DOT nl>
* @license http://github.com/mikedeboer/jsDAV/blob/master/LICENSE MIT License
*/
"use strict";
var jsDAV = require("./../jsdav");
var jsDAV_Tree = require("./tree");
var jsDAV_iCollection = require("./interfaces/iCollection");
var Async = require("asyncjs");
var Util = require("./../shared/util");
var Exc = require("./../shared/exceptions");
/**
* ObjectTree class
*
* This implementation of the Tree class makes use of the INode, IFile and ICollection API's
*/
var jsDAV_ObjectTree = module.exports = jsDAV_Tree.extend({
initialize: function(rootNode) {
this.rootNode = rootNode;
},
/**
* Returns the iNode object for the requested path
*
* @param {String} path
* @return jsDAV_iNode
*/
getNodeForPath: function(path, cbgetnodepath) {
path = Util.trim(path, "/");
// Is it the root node?
if (!path || !path.length)
return cbgetnodepath(null, this.rootNode);
// Attempting to fetch its parent
var parts = Util.splitPath(path);
var parentName = parts[0];
var baseName = parts[1];
var self = this;
// If there was no parent, we must simply ask it from the root node.
if (parentName === "") {
this.rootNode.getChild(baseName, cbgetnodepath);
}
else {
// Otherwise, we recursively grab the parent and ask him/her.
this.getNodeForPath(parentName, function(err, parent) {
if (!parent || !parent.hasFeature(jsDAV_iCollection))
return cbgetnodepath(new Exc.NotFound("Could not find node at path: " + path));
parent.getChild(baseName, cbgetnodepath);
});
}
},
/**
* Returns a list of childnodes for a given path.
*
* @param {String} path
* @return array
*/
getChildren: function(path, cbgetchildren) {
var self = this;
this.getNodeForPath(path, function(err, node) {
if (err)
return cbgetchildren(err);
node.getChildren(function(err, children) {
if (err)
return cbgetchildren(err);
cbgetchildren(null, children);
});
});
}
});