UNPKG

2.38 kBJavaScriptView Raw
1
2var d = require("debug")("raptorjs:tree")
3const Base = require("./Base")
4const Tree = require("./model/Tree")
5
6class TreeClient extends Base {
7
8 constructor (container) {
9 super(container)
10 }
11
12 list() {
13 d("Get all tree node")
14 return this.getClient().get(this.route("TREE_LIST"))
15 }
16
17 read(node) {
18 const nodeid = node.id ? node.id : node
19 d("Get %s node tree", nodeid)
20 return this.getClient().get(this.route("TREE_GET", nodeid))
21 .then((json) => new Tree(json))
22 }
23
24
25 update(node) {
26 const nodeid = node.id ? node.id : node
27 d("Get %s node children", nodeid)
28 return this.getClient().put(this.route("TREE_UPDATE", nodeid), node)
29 .then((json) => new Tree(json))
30 }
31
32 tree(node) {
33 console.log("deprecated: instead of Tree().tree() use Tree.read()")
34 return this.read(node)
35 }
36
37 children(node) {
38 const nodeid = node.id ? node.id : node
39 d("Get %s node children", nodeid)
40 return this.getClient().get(this.route("TREE_CHILDREN", nodeid))
41 }
42
43 add(node, childs) {
44 const nodeid = node.id ? node.id : node
45 childs = childs instanceof Array ? childs : [childs]
46 d("Add children to node %s", nodeid)
47 return this.getClient().put(this.route("TREE_ADD", nodeid), childs)
48 .then((json) => new Tree(json))
49 }
50
51 create(node) {
52 d("Create a node")
53 return this.getClient().post(this.route("TREE_CREATE"), node)
54 .then((json) => new Tree(json))
55 }
56
57 search(query) {
58 d("Search for nodes")
59 return this.getClient().post(this.route("TREE_SEARCH"), query)
60 .then((res) => require("./pager").create(res, (r) => new Tree(r)))
61 }
62
63 delete(node) {
64 const nodeid = node.id ? node.id : node
65 d("Remove node %s", nodeid)
66 return this.getClient().delete(this.route("TREE_REMOVE", nodeid))
67 }
68
69 removeTree(node) {
70 const nodeid = node.id ? node.id : node
71 d("Remove full tree of node %s", nodeid)
72 return this.getClient().post(this.route("TREE_REMOVE_TREE", nodeid))
73 }
74
75 subscribe(node, fn) {
76 this.getClient().subscribe("tree/" + node.id, fn)
77 }
78
79 unsubscribe(node, fn) {
80 this.getClient().unsubscribe("tree/" + node.id, fn)
81 }
82
83}
84
85module.exports = TreeClient