-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
179 lines (158 loc) · 4.65 KB
/
Copy pathmain.js
File metadata and controls
179 lines (158 loc) · 4.65 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
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
var print = function(output){
console.log(output);
};
var getEl = function(el) {
return document.getElementById(el);
};
var logList = [];
var selectedLog;
var logWindow = getEl("logWindow");
//Polyfill for includes
if (!String.prototype.includes) {
String.prototype.includes = function() {'use strict';
return String.prototype.indexOf.apply(this, arguments) !== -1;
};
}
//Only draw a section of lines at a time, so the browser doesn't have to render all lines at once
var drawLog = (function() {
var cursor = 0;
var nextCursor;
return function() {
nextCursor = cursor + 100;
if (nextCursor > logList.length) {
nextCursor = logList.length - 1;
}
for (var i = cursor; i < nextCursor; i++) {
logWindow.appendChild(logList[i]);
}
cursor = nextCursor;
if (cursor < logList.length -1) {
setTimeout(drawLog, 4);
}
};
})();
//Hide all lines which match lines in the search list, show all others
//If searchSting is passed in, only matching are removed. Nothing done with hidden
//If searchlist_box is empty, all are shown
function applySearch(searchString) {
if (typeof searchString === 'string') {
searchString = escape(searchString);
for (var l = logList.length -1; l >= 0; l--) {
if (searchString !== "") {
var log = escape(logList[l].innerHTML);
if (log.includes(searchString)) {
logList[l].style.display = "none";
}
}
}
}
else {
var searchList = getEl("searchlist_box").children;
if (searchList.length !== 0) {
for (var l = logList.length -1; l >= 0; l--) {
for (var s = 0; s < searchList.length; s++) {
var log = escape(logList[l].innerHTML);
var searchString = escape(searchList[s].innerHTML);
if (searchString !== "") {
if (log.includes(searchString)) {
logList[l].style.display = "none";
break;
}
else {
logList[l].style.display = "";
}
}
}
}
}
else {
for (var l = logList.length -1; l >= 0; l--) {
logList[l].style.display = "";
}
}
}
offsetLogwindow();
}
function toggleVis(el) {
if (getComputedStyle(el, null).display == "none") {
el.style.display = 'block';
}
else {
el.style.display = "none";
}
}
function addLogListener(el) {
el.addEventListener("click", function(){
var value = el.innerHTML;
addSearchString(value);
if (selectedLog) {
selectedLog.style.background = "white";
}
selectedLog = this;
this.style.background = "red";
});
}
function addSearchString(value) {
var searchString = document.createElement("div");
searchString.addEventListener('blur', searchChange, false);
searchString.className += "input-search-string";
searchString.setAttribute("contentEditable", true);
if (window.getSelection().toString()) {
value = window.getSelection().toString();
}
searchString.innerHTML = value;
getEl("searchlist_box").appendChild(searchString);
applySearch(value);
}
//Run when a search string is changed by the user
function searchChange() {
if ((this.innerHTML === "") || (this.innerHTML === "<br>")) {
this.parentNode.removeChild(this);
}
applySearch();
}
function offsetLogwindow() {
logWindow.style.marginTop = (getEl('navbar').offsetHeight + 10) + "px";
}
//Read in selected log file
function readSingleFile(evt) {
var f = evt.target.files[0];
if (f) {
toggleVis(getEl("choose_file_button"));
toggleVis(getEl("searchlist_box"));
var r = new FileReader();
r.onload = function(e) {
var contents = e.target.result;
var separatedLogList = contents.split("\n");
var logListLength = separatedLogList.length;
for (var i = 0; i < logListLength; i++) {
var tempLog = document.createElement("div");
tempLog.innerHTML = separatedLogList[i];
tempLog.classList.add("log_line");
logList.push(tempLog);
addLogListener(tempLog);
}
drawLog();
offsetLogwindow();
};
r.readAsText(f);
r.onerror = function () {
var output = r.error.name;
if (r.error.message) {
output += ": " + r.error.message;
}
print(output);
if (r.error.name == "SecurityError") {
alert("Verify you have permissions to access the selected file");
}
};
} else {
alert("Failed to load file");
}
}
// Check for the various File API support.
if (window.File && window.FileReader && window.FileList && window.Blob) {
document.getElementById('inputfile').addEventListener('change', readSingleFile, false);
} else {
alert('The File APIs are not fully supported by your browser.');
}