-
Notifications
You must be signed in to change notification settings - Fork 0
/
tree.js
86 lines (81 loc) · 2.83 KB
/
tree.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
83
84
85
86
define(function (require) {
var $ = require('jquery');
require('jstree');
var _ = require('underscore');
var primaryNode;
function Tree(container, state) {
this.container = container;
this.hub = container.layoutManager.eventHub;
this.root = container.getElement().html($('#tree').html());
var node = this.root.find('.tree').jstree();
this.tree = node.jstree(true);
this.hub.on('projectChange', this.onProjectChange, this);
node.on('activate_node.jstree', function (e, data) {
if (data == undefined || data.node == undefined)
return;
if (data.node.children.length == 0) {
container.layoutManager.eventHub.emit('fileSelected', data.node);
}
});
}
function compareNodes( a, b ) {
if (a.isPrimary) return -1;
if (b.isPrimary) return 1;
const aIsDir = a.children.length > 0;
const bIsDir = b.children.length > 0;
if (!aIsDir && bIsDir) {
return -1;
}
if (aIsDir && !bIsDir) {
return 1;
}
const aIsAsm = a.text.endsWith(".asm");
const bIsAsm = b.text.endsWith(".asm");
if (aIsAsm && !bIsAsm) {
return -1;
}
if (!aIsAsm && bIsAsm) {
return 1;
}
return a.text < b.text;
}
function addToTree(parent, projectObj) {
for (var fileName in projectObj) {
var val = projectObj[fileName];
var node = {
id: parent.id + "/" + fileName,
text: fileName,
children: []
};
parent.children.push(node);
if (Object.prototype.toString.call(val) === '[object String]') {
node.isPrimary = fileName == "starquake.asm"; // todo: make this a project property
if (fileName.endsWith(".bmp")) node.icon = "images/ic_bmp.png";
else if (fileName.endsWith(".bin")) node.icon = "images/ic_bin.png";
else node.icon = node.isPrimary ? "images/ic_asm_main.png" : "images/ic_asm.png";
if (node.isPrimary) {
primaryNode = node;
}
} else {
node.state = {
opened : true
};
addToTree(node, val);
}
}
parent.children.sort(compareNodes);
}
Tree.prototype.onProjectChange = function (project) {
var root = {
id: "",
children: []
};
addToTree(root, project.files);
this.tree.settings.core.data = root.children;
this.tree.refresh();
if (primaryNode !== undefined) {
this.container.layoutManager.eventHub.emit('fileSelected', primaryNode);
}
};
return Tree;
});