-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
141 lines (113 loc) · 3.53 KB
/
app.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
var UI = {
devmode: false,
devload: false,
XMLHeader: '',
XMLDocument: '',
documentName: '',
startDepth: 2,
preserveWhitespace: false,
searchResults: 0,
oneLinerLength: 100,
indentChars: ' ',
separator: ' = '
};
function main(){
if(UI.devload) {
load(testXML, 'test-xml-document.xml');
enableDocButtons();
} else {
showLoadFileDialog();
}
}
function load(xml, fname){
let loadXML = xml;
if(!UI.preserveWhitespace){
loadXML = loadXML.replace(/ +/g, ' ');
loadXML = loadXML.replace(/\t+/g, '');
}
loadXML = loadXML.trim();
UI.XMLDocument = loadXMLDocument(loadXML);
consolelog(UI.XMLDocument);
UI.documentName = fname;
let firstTagName = `<${UI.XMLDocument.firstElementChild.tagName}`;
let firstTagIndex = xml.indexOf(firstTagName);
UI.XMLHeader = xml.substring(0, firstTagIndex);
loadContent();
}
function loadContent(){
closeAllDialogs();
let header = document.getElementById('header');
header.innerHTML = '';
let title = createElem('h1', {id: 'documentName', class: 'saved'});
title.innerText = UI.documentName;
document.title = `XMLtagger: ${UI.documentName}`;
let searchIcon = createElem('div', {class: 'searchIcon'});
// searchIcon.append('🔎');
searchIcon.append('⌕');
let searchInput = createElem('input', {type: 'text', placeholder: 'Search', id: 'search'});
searchInput.onchange = handleSearchInput;
searchInput.onfocus = function(){ this.setAttribute('placeholder', ''); };
searchInput.onblur = function(){ this.setAttribute('placeholder', 'Search'); };
let searchClear = createElem('div', {class: 'searchClear'});
searchClear.append('⨯');
searchClear.onclick = clearSearchInput;
let searchWrapper = createElem('div', {class: 'searchWrapper'});
searchWrapper.append(searchIcon);
searchWrapper.append(searchInput);
searchWrapper.append(searchClear);
let searchResultCount = createElem('span', {class: 'searchNote', id: 'searchResultCount'});
header.append(title);
header.append(searchWrapper);
header.append(searchResultCount);
let tree = document.getElementById('tree');
tree.innerHTML = '';
tree.append(makeTreeNode(UI.XMLDocument.documentElement));
}
function loadXMLDocument(inputXML = ''){
let XMLdoc;
let XMLerror;
if (typeof window.DOMParser !== 'undefined') {
XMLdoc = (new window.DOMParser()).parseFromString(inputXML, 'text/xml');
} else if (typeof window.ActiveXObject !== 'undefined' && new window.ActiveXObject('Microsoft.XMLDOM')) {
XMLdoc = new window.ActiveXObject('Microsoft.XMLDOM');
XMLdoc.async = 'false';
XMLdoc.loadXML(inputXML);
} else {
console.warn('No XML document parser found.');
XMLerror = new SyntaxError('No XML document parser found.');
throw XMLerror;
}
let parserError = XMLdoc.getElementsByTagName('parsererror');
if (parserError.length) {
XMLerror = new SyntaxError(parserError[0].textContent);
throw XMLerror;
}
return XMLdoc;
}
function escape(value = ''){
value = value.replace(/&/g, '&');
value = value.replace(/"/g, '"');
value = value.replace(/</g, '<');
return value;
}
function unescape(value = ''){
value = value.replace(/"/g, '"');
value = value.replace(/</g, '<');
value = value.replace(/&/g, '&');
return value;
}
function createElem(nodeName = 'div', attributes = {}){
let elem = document.createElement(nodeName);
for(let a in attributes) {
if(attributes.hasOwnProperty(a)){
elem.setAttribute(a, attributes[a]);
}
}
return elem;
}
function removeElem(elem) {
if(elem) elem.parentElement.removeChild(elem);
}
function consolelog(message){
if(UI.devmode) console.log(message);
}