-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathe2b.js
More file actions
108 lines (94 loc) · 3.58 KB
/
e2b.js
File metadata and controls
108 lines (94 loc) · 3.58 KB
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
class e2b {
constructor() { }
#getPathway(element) {
let path = [];
while (element) {
path.unshift(element.nodeName);
element = element.parentElement;
}
return path.join(" > ");
}
#parseElements(elements, source) {
return Array.from(elements).map(element => {
const elementName = element.nodeName;
const text = element.textContent;
const pathway = this.#getPathway(element);
return [elementName, text, pathway, source];
});
}
showAll(xmlText, source) {
console.log('showAll called');
// Replace tab characters with two spaces
const formattedXmlText = xmlText.replace(/\t/g, ' ');
const output = document.getElementById('output');
output.textContent = formattedXmlText;
}
ashowTable(xmlText, source, searchTagName) {
const xmlParser = new DOMParser();
console.log('showTable called with xmlText:', xmlText, '\nsource:', source, '\nsearchTagName:', searchTagName);
const xml = xmlParser.parseFromString(xmlText, "application/xml");
const parseError = xml.getElementsByTagName("parsererror");
if (parseError.length > 0) {
console.error("Error parsing XML");
return;
}
console.log("Parsed XML Document:", xml);
console.log('showTable called with searchTagName:', searchTagName);
const elements = xml.documentElement.getElementsByTagName(searchTagName);
console.log('Elements found:', elements);
const data = this.#parseElements(elements, source);
console.log('Data prepared for table:', data);
const tableContainer = document.getElementById('table');
// Destroy the existing Grid.js instance if it exists. Otherwise error.
if (gridInstance) {
gridInstance.destroy();
}
// Create a new Grid.js instance and store it
gridInstance = new gridjs.Grid({
columns: [
{ name: 'Element', sort: true },
{ name: 'Text', sort: true },
{ name: 'Pathway', sort: true },
{ name: 'Source', sort: true }
],
data: data, // Pass the data to Grid.js
resizable: true // Enable column resizing
}).render(tableContainer);
console.log('Table rendered');
}
showTable(json, xml) {
let matches = json.map(row => {
const attributeValue = row[2];
let matchingTexts = "";
if (attributeValue !== "-") {
//Escape the value for inclusion in the query.
const escapedValue = CSS.escape(attributeValue);
// Find elements where any attribute *contains* the value
const matchingElements = Array.from(xml.querySelectorAll(`[root*="${escapedValue}"]`)).map(el => el.parentNode);
//OR if you want to look for elements with an attribute that has an exact value use
//(Replace data-myattr with a known attribute name or make it variable.)
// const matchingElements = Array.from(xml.querySelectorAll(`[data-myattr="${escapedValue}"]`));
matchingTexts = matchingElements.map(el => el.textContent).join("\n");
}
return matchingTexts;
});
matches.unshift('Value');
json = json.map((row, index) => {
return [...row.slice(0, 2), matches[index], ...row.slice(2)];
});
json = json.filter(row => row[2] && row[2].length > 0);
let headers = json[0];
headers = headers.map(header => ({ name: header, sort: true }));
let data = json.slice(1);
const tableContainer = document.getElementById('table');
// Destroy the existing Grid.js instance if it exists. Otherwise error.
if (gridInstance) {
gridInstance.destroy();
}
gridInstance = new gridjs.Grid({
columns: [...headers],
data: data, // Pass the data to Grid.js
resizable: true // Enable column resizing
}).render(tableContainer);
}
}