-
Notifications
You must be signed in to change notification settings - Fork 1
/
load.js
211 lines (177 loc) · 7.74 KB
/
load.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
201
202
203
204
205
206
207
208
209
210
//TODO: prompt when loading or creating new to save any unsaved changes
//TODO: style the menu for desktop and mobile
//TODO: style the row buttons for mobile
//TODO: put an initial text on the page
var filename;
var strings;
function encodeHtml(text) {
return text.replace(/&/g, '&').replace(/"/g, '"').replace(/'/g, ''').replace(/</g, '<').replace(/>/g, '>');
}
function changeEnabledStatus(sender) {
var li = sender.parentNode.parentNode.parentNode; /*the li is the great-grandparent*/
if (li.classList.contains('enabled-true')) {
li.className=li.className.replace('true', 'false');
} else if (li.classList.contains('enabled-false')) {
li.className=li.className.replace('false', 'true');
}
}
function menuToggle(sender) {
var siblingMenuNodes = sender.parentNode.children;
for(i=0; i<siblingMenuNodes.length; i++) {
if(siblingMenuNodes[i].classList.contains('menu-closed')) {
siblingMenuNodes[i].className=siblingMenuNodes[i].className.replace('closed', 'open');
} else if(siblingMenuNodes[i].classList.contains('menu-open')) {
siblingMenuNodes[i].className=siblingMenuNodes[i].className.replace('open', 'closed');
}
}
}
function menuClose() {
var mainNode = document.getElementsByClassName('menu-level-0')[0];
var childElements = mainNode.children;
for(i=0; i<childElements.length; i++) {
if(childElements[i].classList.contains('menu-node')) {
childElements[i].className=childElements[i].className.replace('open', 'closed');
}
}
}
window.onload = function(){
var userLang = navigator.language || navigator.userLanguage;
var lang = userLang.substring(0,2);
strings = getStrings(lang);
localizeMenuStrings();
}
function localizeMenuStrings () {
document.getElementById('menu-label-main').innerText = strings.menuMain;
document.getElementById('menu-label-file').innerText = strings.menuFile;
document.getElementById('menu-label-load').innerText = strings.menuLoad;
document.getElementById('menu-label-save').innerText = strings.menuSave;
document.getElementById('menu-label-newfile').innerText = strings.menuNewFile;
document.getElementById('menu-label-newrow').innerText = strings.menuNewRow;
}
function toggleMenuButton() {
var menuDiv = document.getElementsByClassName('dropdown')[0];
var className = menuDiv.className;
if (className=='dropdown') {
menuDiv.className = 'dropdown open';
} else {
menuDiv.className = 'dropdown';
}
}
function doDelete(sender) {
var li = sender.parentNode.parentNode.parentNode;
li.parentNode.removeChild(li); /*delete the li element from the list*/
}
function doMoveUp(sender) {
var li = sender.parentNode.parentNode.parentNode;
var ol = li.parentNode;
var prevSib = li.previousElementSibling;
if(prevSib) {
ol.insertBefore(li, prevSib);
}
}
function doMoveDown(sender) {
var li = sender.parentNode.parentNode.parentNode;
var ol = li.parentNode;
var nextSib = li.nextElementSibling;
if(nextSib) {
ol.insertBefore(nextSib, li);
}
}
function createNewFile() {
var listElement = document.getElementById("main-listview");
listElement.innerHTML = '';/*clear first to load new*/
document.getElementById("list-file-name").innerHTML = ''; /*what to do here???*/
addNewRow();
}
function addNewRow() {
var header='<?xml version="1.0" encoding="utf-8"?>\r\n<EditCollection xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">\r\n <Items>\r\n ';
var content = '<EditItem Enabled="true" EditItemType="regular_expression">\r\n ' +
'<FindText></FindText>\r\n <ReplaceText></ReplaceText>\r\n</EditItem>';
var footer = '\r\n </Items>\r\n</EditCollection>';
var result = getResult(header+content+footer);
document.getElementById("main-listview").appendChild(result);
}
function saveEditedFile() {
/*error handling to check for empty values in find and replace text...MB allow it in replace???...or mb allow altogether???*/
/*mb something like open/load/download a blank template???*/
var filename = document.getElementById("list-file-name").innerHTML;
var header = '<?xml version="1.0" encoding="utf-8"?>\r\n<EditCollection>\r\n <Items>\r\n ';
var footer = '\r\n </Items>\r\n</EditCollection>';
var records = document.getElementsByClassName('record');
var content = '';
for (i = 0; i < records.length; i++) {
var listindex = i+1;
var findElement = records[i].querySelectorAll('.find-text-input')[0];
var findText = findElement.value;
findText = encodeHtml(findText);
var replaceElement = records[i].querySelectorAll('.replace-text-input')[0];
var replaceText = replaceElement.value;
replaceText = encodeHtml(replaceText);
var typeElement = records[i].querySelectorAll('.replace-type-combo')[0];
var typeText = typeElement.options[typeElement.selectedIndex].value;
var enabledElement = records[i].querySelectorAll('.replace-enabled-combo')[0];
var enabledText = enabledElement.options[enabledElement.selectedIndex].value;
content += '<EditItem Enabled="'+ enabledText +'" EditItemType="'+ typeText +'">\r\n ';
content += '<FindText>'+ findText +'</FindText>\r\n ';
content += '<ReplaceText>'+ replaceText +'</ReplaceText>\r\n </EditItem>';
if(i < records.length-1) content += '\r\n '; /*only add the ending if not on the last one*/
}
var filetext = header + content + footer;
var data = new Blob([filetext]);
var a = document.createElement("a");
document.body.appendChild(a);
a.href = URL.createObjectURL(data);
a.style = "display: none";
a.download = filename;
a.click();
}
function loadSingleXmlFile() {
var fileInputElement = document.getElementById('file-to-upload');
var file = fileInputElement.files[0];
filename = file.name;
if (!file) {
return;
}
var reader = new FileReader();
reader.onload = function(e) {
var contents = e.target.result;
var result = getResult(contents);
var listElement = document.getElementById("main-listview");
listElement.innerHTML = '';/*clear first to load new*/
listElement.appendChild(result);
document.getElementById("list-file-name").innerHTML = filename;
fileInputElement.value = null; /*have to clear the value of the file input or the onchange never fires again*/
};
reader.readAsText(file);
}
/*works in Chrome, not IE, not sure about FF*/
function getResult(xml)
{
var xsl = getXsl();
parser=new DOMParser();
xslDoc=parser.parseFromString(xsl,"text/xml");
xmlDoc=parser.parseFromString(xml,"text/xml");
xsltProcessor = new XSLTProcessor();
xsltProcessor.importStylesheet(xslDoc);
resultDocument = xsltProcessor.transformToFragment(xmlDoc, document);
/*localize result here..*/
var findLabels = resultDocument.querySelectorAll('.label-find');
var replaceLabels = resultDocument.querySelectorAll('.label-replace');
var typeLabels = resultDocument.querySelectorAll('.label-type');
var statusLabels = resultDocument.querySelectorAll('.label-status');
var typeOptionsRegex = resultDocument.querySelectorAll('.option-regex');
var typeOptionsPlaintext = resultDocument.querySelectorAll('.option-plaintext');
var statusOptionsEnabled = resultDocument.querySelectorAll('.option-enabled');
var statusOptionsDisabled = resultDocument.querySelectorAll('.option-disabled');
for (i=0; i<findLabels.length; i++) {
findLabels[i].innerText = strings.itemLabelFind;
replaceLabels[i].innerText = strings.itemLabelReplace;
typeLabels[i].innerText = strings.itemLabelType;
statusLabels[i].innerText = strings.itemLabelStatus;
typeOptionsRegex[i].innerText = strings.optionTypeRegex;
typeOptionsPlaintext[i].innerText = strings.optionTypePlaintext;
statusOptionsEnabled[i].innerText = strings.optionStatusEnabled;
statusOptionsDisabled[i].innerText = strings.optionStatusDisabled;
}
return resultDocument;
}