-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
80 lines (75 loc) · 2.41 KB
/
index.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
const $ = require("jquery");
const path = require("path");
const fs = require("fs");
$(document).ready(function () {
let src = process.cwd();
let name = path.basename(src);
let pObj = {
id: src,
parent: "#",
text: name
}
let chArr = createChildNode(src);
chArr.unshift(pObj);
console.log(chArr);
$("#tree").jstree({
// so that create works
"core": {
"check_callback": true,
"data": chArr
},
}).on("open_node.jstree", function (e, data) {
let children = data.node.children;
// console.log(children)
for (let i = 0; i < children.length; i++) {
let gcNodes = createChildNode(children[i]);
// console.log(gcNodes);
for (let j = 0; j < gcNodes.length; j++) {
// data array
// console.log("inside gc")
console.log(children[i])
let isGcPresent = $('#tree').jstree(true).get_node(gcNodes[j].id);
if (isGcPresent) {
return;
}
$("#tree").jstree().create_node(children[i], gcNodes[j],"first");
}
}
})
})
function createChildNode(src) {
let isDir = fs.lstatSync(src).isDirectory();
// console.log(src);
if (isDir == false) {
return [];
}
let children = fs.readdirSync(src);
let chArr = [];
for (let i = 0; i < children.length; i++) {
let cPath = path.join(src, children[i]);
let chObj = {
id: cPath,
parent: src,
text: children[i]
}
chArr.push(chObj);
}
return chArr
}
// Event bubbling
// $("#tree").on("click", function () {
// let children = fs.readdirSync(src);
// // console.log(childrens)
// for (let i = 0; i < children.length; i++) {
// $(this).append(`<li>${children[i]}</li>`);
// }
// $("li").on("click", "li", function (e) {
// e.stopImmediatePropagation();
// let fPath = path.join(src, $(this).html());
// let children = fs.readdirSync(fPath);
// // console.log(childrens)
// for (let i = 0; i < children.length; i++) {
// $(this).append(`<li>${children[i]}</li>`);
// }
// })
// })