-
Notifications
You must be signed in to change notification settings - Fork 0
/
fileLoading.js
220 lines (180 loc) · 7.46 KB
/
fileLoading.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
211
212
213
214
215
216
217
218
219
220
/* Code for dealing with file tabs */
function viewFile(evt, fileName) {
console.log("I've been called with " + fileName);
var i, tabcontent, tablinks;
// Get all elements with class="tabcontent" and hide them
tabcontent = document.getElementsByClassName("tabcontent");
for (i = 0; i < tabcontent.length; i++) {
tabcontent[i].style.display = "none";
}
// Get all elements with class="tablinks" and remove the class "active"
tablinks = document.getElementsByClassName("tablinks");
for (i = 0; i < tablinks.length; i++) {
tablinks[i].className = tablinks[i].className.replace(" active", "");
}
// Show the current tab, and add an "active" class to the button that opened the tab
document.getElementById(fileName).style.display = "block";
evt.currentTarget.className += " active";
// Get Prism to highlight things in case it didn't already.
Prism.highlightAll();
}
/* Old method for loading sources. Leaving it here as we might want it
* when we want to include a full project hierarchy in the tool.
*/
function getFile(fileName,element,preElement){
var output = document.getElementById(element);
var pre = document.getElementById(preElement);
let line = '';
console.log(fileName);
//g_txt = loadStrings(fileName);
//g_txt = loadStrings(fileName);
if(g_txt){
for(var i=0; i < g_txt.length; i++){
line += g_txt[i]+'\n';
}
output.innerHTML = line;
pre.setAttribute("data-line","25-25");
Prism.highlightAll();
}else{
console.log("Could not find file: "+fileName)
}
}
/* Function to display provided <content> onto the <element>
* and <preElement> HTML object. <preElement> must be a prism element.
* Called from: bar objects in entity.js
*/
function writeTo( element, preElement, content, highlightFrom, highlightTo) {
let output = document.getElementById( element);
let pre = document.getElementById( preElement);
if(output){ // If the 'output' != null, then attempt to highlight the line in prism.
output.innerHTML = content;
if (highlightFrom === highlightTo)
pre.setAttribute('data-line', highlightFrom);
else
pre.setAttribute('data-line', highlightFrom + '-' + highlightTo);
Prism.highlightAll();
}
}
let g_filesOpenInViewer = [];
// https://stackoverflow.com/questions/35182928/prismjs-centering-highlighted-lines-vertically
function scrollToLines (pre) {
var lines = document.querySelector('.line-highlight'),
linesHeight = lines.offsetHeight,
preHeight = pre.offsetHeight;
lines.scrollIntoView();
if (preHeight > linesHeight && pre.scrollTop < (pre.scrollHeight - preHeight)) {
pre.scrollTop = pre.scrollTop - (preHeight / 2) + (linesHeight / 2);
}
}
function addFileToView(fileName, fileContents, highlightFrom, highlightTo) {
// Skip if we've already opened the file.
// TODO: Instead change the line highlight, if a new promise is selected.
if (g_filesOpenInViewer.indexOf(fileName) == -1) {
g_filesOpenInViewer.push(fileName);
} else {
// The file is already open, but we should update the highlighted lines.
let theOuterElement = document.getElementById(fileName);
let theInnerPre = theOuterElement.childNodes[0]; // Should only have one child.
theInnerPre.removeAttribute('data-line');
if (highlightFrom === highlightTo)
theInnerPre.setAttribute('data-line', highlightFrom);
else
theInnerPre.setAttribute('data-line', highlightFrom + '-' + highlightTo);
Prism.highlightAll();
// Click the button.
document.getElementById('tabButton-' + fileName).click();
// Set scroll location.
for (let highlightedLine of document.getElementsByClassName(' line-highlight'))
highlightedLine.setAttribute('class', 'line-highlight');
scrollToLines(theInnerPre);
return;
}
/* Add Content... */
let outerDiv = document.createElement('div');
let thePre = document.createElement('pre');
let theCode = document.createElement('code');
outerDiv.className = 'tabcontent';
outerDiv.id = fileName;
thePre.id = 'promisePre';
thePre.className = 'line-numbers';
if (highlightFrom === highlightTo)
thePre.setAttribute('data-line', highlightFrom);
else
thePre.setAttribute('data-line', highlightFrom + '-' + highlightTo);
theCode.className = 'language-javascript';
theCode.innerHTML = fileContents;
thePre.appendChild(theCode);
outerDiv.appendChild(thePre);
var tabController = document.getElementById('listOfOpenFiles');
tabController.appendChild(outerDiv);
/* Add tab Button */
let tabButton = document.createElement('button');
tabButton.className = 'tablinks';
tabButton.setAttribute('onclick', `viewFile(event, '${fileName}')`);
tabButton.innerHTML = fileName;
tabButton.id = 'tabButton-' + fileName;
var tabList = document.getElementById('tabController');
tabList.appendChild(tabButton);
Prism.highlightAll();
tabButton.click();
// Scroll after the thing is in view.
for (let highlightedLine of document.getElementsByClassName(' line-highlight'))
highlightedLine.setAttribute('class', 'line-highlight');
scrollToLines(thePre);
}
/* Create this:
<div class="tab">
<button class="tablinks" onclick="viewFile(event, 'PromiseBrowser')" id="defaultOpen">PromiseBrowser</button>
</div>
*/
// Start reading a file from the a file box specified by
// 'elementID'
function startRead( fileElementID, outputElementID, progressElementID){
// obtain input element through DOM
var file = document.getElementById(fileElementID).files[0];
// If we successfully find the file element in the DOM
// Then move to the next step and attempt to read the
// text from the file.
if(file){
var reader;
try{
reader = new FileReader();
}catch(e){
document.getElementById(outputElementID).innerHTML = "Error: seems File API is not supported on your browser";
return;
}
// Read file into memory as UTF-8
reader.readAsText(readFile, "UTF-8");
// Handle progress, success, and errors
reader.onprogress = updateProgress(progressElementID);
reader.onload = loaded(outputElementID,progressElementID);
reader.onerror = errorHandler(outputElementID);
}
}
// Update progress bar element when loading file.
function updateProgress(evt,elementID){
if (evt.lengthComputable){
// evt.loaded and evt.total are ProgressEvent properties
var loaded = (evt.loaded / evt.total);
if (loaded < 1){
// Increase the prog bar length
// style.width = (loaded * 200) + "px";
document.getElementById(elementID).style.width = (loaded*100) + "%";
}
}
}
function loaded(evt,elementID,progressElementID){
// Obtain the read file data
var fileString = evt.target.result;
document.getElementById(elementID).innerHTML = fileString;
document.getElementById(progressElementID).style.width = 100 + "%";
// Calling this function will force a highlight of
// the file.
Prism.highlightAll();
}
function errorHandler(evt,elementID){
if(evt.target.error.code == evt.target.error.NOT_READABLE_ERR){
// The file could not be read
document.getElementById(elementID).innerHTML = "Error reading file..."
}
}