-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtree.js
200 lines (169 loc) · 5.32 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
function makeTreeNode(thisNode, depth = 1, hasNoSiblings = false, filterTerm = false){
// consolelog(`makeTreeNode ${thisNode.nodeName}`);
let node = createElem('div', {class: 'nodeWrapper'});
node.onclick = closeAllDialogs;
let elem;
let content;
let toggler;
let foundResult = false;
let filterTermIndex;
function highlight(content){
let pre = content.substring(0, filterTermIndex);
let post = content.substring(filterTermIndex+filterTerm.length);
return `${pre}<span class="highlight">${filterTerm}</span>${post}`;
}
if(thisNode.nodeName === '#text'){
// This node is text
content = thisNode.nodeValue.trim();
filterTermIndex = content.indexOf(filterTerm);
if(filterTerm){
if(filterTermIndex < 0) {
// Search does not return this node
return false;
} else {
// consolelog(`Term found in <${thisNode.parentNode.nodeName}>`);
}
}
if(!content && hasNoSiblings) {
// Leaf node with no content
content = ' ';
}
if(content || content === ' ') {
if(filterTermIndex >= 0) {
UI.searchResults += 1;
foundResult = true;
content = highlight(content);
// consolelog(`${content}`);
}
content = content.replace(/\n/g, '<br>');
elem = createElem('div', {class: 'textContent'});
elem.innerHTML = content;
elem.onclick = function(event){ event.stopPropagation(); showEditDialog(thisNode, elem);};
node.append(elem);
node.setAttribute('class', 'textNode');
return node;
}
} else if (thisNode.nodeName === '#comment'){
// This node is a comment
content = thisNode.nodeValue.trim();
filterTermIndex = content.indexOf(filterTerm);
if(filterTerm){
if(filterTermIndex < 0) {
// Search does not return this node
return false;
} else {
// consolelog(`Term found in <${thisNode.parentNode.nodeName}>`);
}
}
if(filterTermIndex >= 0) {
UI.searchResults += 1;
foundResult = true;
content = highlight(content);
// consolelog(`${content}`);
}
if(content) {
content = content.replace(/\n/g, '<br>');
elem = createElem('div', {class: 'commentContent'});
elem.innerHTML = `<!-- ${content} -->`;
elem.onclick = function(event){ event.stopPropagation(); showEditDialog(thisNode, elem);};
node.append(elem);
node.setAttribute('class', 'commentNode');
return node;
}
} else {
// This node is a parent node, possibly with children
elem = createElem('div', {class: 'nodeHeader'});
if(thisNode.childNodes.length){
toggler = createElem('span', {class: 'togglerTitle'});
if(depth < UI.startDepth || filterTerm) {
toggler.append(`⯆ ${thisNode.nodeName}`);
toggler.setAttribute('onclick', 'collapse(this)');
} else {
toggler.append(`⯈ ${thisNode.nodeName}`);
toggler.setAttribute('onclick', 'expand(this)');
}
elem.append(toggler);
} else {
content = createElem('span', {class: 'title'});
content.append(thisNode.nodeName);
elem.append(content);
}
// Attributes
if(thisNode.attributes){
for(let a=0; a<thisNode.attributes.length; a++){
let domNode = createElem('span', {class: 'attributeContent'});
let xmlAttribute = thisNode.attributes[a];
domNode.onclick = function(event){ event.stopPropagation(); showEditDialog(xmlAttribute, domNode); };
domNode.setAttribute('title', `Click to edit '${xmlAttribute.name}'`);
let name = xmlAttribute.name;
if(filterTerm){
filterTermIndex = xmlAttribute.name.indexOf(filterTerm);
if(filterTermIndex >= 0) {
name = highlight(name);
UI.searchResults += 1;
foundResult = true;
}
}
let value = xmlAttribute.value;
if(filterTerm){
filterTermIndex = xmlAttribute.value.indexOf(filterTerm);
if(filterTermIndex >= 0) {
value = highlight(value);
UI.searchResults += 1;
foundResult = true;
}
}
domNode.innerHTML = `${name}${UI.separator}${value}`;
elem.append(domNode);
}
}
node.append(elem);
}
// Child nodes
content = createElem('div', {class: 'nodeContent'});
if(depth > UI.startDepth) content.setAttribute('style', 'display:none;');
if(filterTerm) content.setAttribute('style', 'display:block;');
let kids = thisNode.childNodes;
let isOnlyChild = kids.length === 1;
let kidNode;
let kidNodeCount = 0;
for(let k=0; k<kids.length; k++){
kidNode = makeTreeNode(kids[k], (depth+1), isOnlyChild, filterTerm);
if(kidNode && kidNode.innerHTML){
content.append(kidNode);
kidNodeCount++;
}
}
if(filterTerm && kidNodeCount === 0 && !foundResult) return false;
if(content.innerHTML) node.append(content);
// node.append(createElem('br'));
return node;
}
function expand(node){
closeAllDialogs();
try {
node.innerHTML = "⯆" + node.innerHTML.substring(1);
node.parentNode.parentNode.childNodes[1].style.display = 'block';
node.setAttribute('onclick', 'collapse(this)');
} catch(err) {
console.warn(err);
}
}
function collapse(node){
closeAllDialogs();
try {
node.innerHTML = "⯈" + node.innerHTML.substring(1);
node.parentNode.parentNode.childNodes[1].style.display = 'none';
node.setAttribute('onclick', 'expand(this)');
} catch(err) {
console.warn(err);
}
}
function expandAll(){
closeAllDialogs();
document.querySelectorAll('.togglerTitle').forEach((node) => expand(node));
}
function collapseAll(){
closeAllDialogs();
document.querySelectorAll('.togglerTitle').forEach((node) => collapse(node));
}