-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
190 lines (155 loc) · 5.13 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
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
// DOM ELEMENTS
const body = document.getElementsByTagName('body')[0];
const dropArea = document.querySelector(".drag-area"),
dragText = document.querySelector(".drag-text h2"),
btnContainerTouchScreen = document.querySelector(".container-touch-screen");
let inputFile, spin;
let file;
// check if device is touchscreen
if (!("ontouchstart" in document.documentElement)) {
body.classList.add("can-hover");
dropArea.classList.remove("dsp-none");
btnContainerTouchScreen.classList.add("dsp-none");
inputFile = document.querySelectorAll('#file')[0];
spin = document.querySelectorAll('#spinner')[0];
} else {
inputFile = document.querySelectorAll('#file')[1];
spin = document.querySelectorAll('#spinner')[1];
}
function startSpinBtn() {
spin.classList.add('spinner-border');
spin.classList.add('spinner-border-sm');
}
function stopSpinBtn() {
spin.classList.remove('spinner-border');
spin.classList.remove('spinner-border-sm');
}
function scrollToGraphs() {
location.hash = '#cloud';
}
// disable loading button
let fileBtn = document.querySelector(".btn-file");
fileBtn.addEventListener("click", function() {
this.classList.add('disabled');
this.classList.add('nohover');
// this.disabled = true;
});
// DRAG AND DROP FUNCTION AREA (does not work if you drop file in the area)
// works only if you drop file on file input
dropArea.addEventListener("change", () => {
//file = this.files[0];
});
dropArea.addEventListener("dragover", function(e) {
//e.stopPropagation();
e.preventDefault();
dropArea.classList.add("dragover");
dragText.textContent = 'Drop your PDF here';
}, false);
dropArea.addEventListener("dragleave", () => {
dropArea.classList.remove("dragover");
dragText.textContent = 'Drag & drop your PDF here';
});
dropArea.addEventListener("drop", function(e) {
//e.stopPropagation();
e.target.preventDeafault();
dropArea.classList.add("drop");
dragText.textContent = 'Uploading...';
file = e.dataTransfer.files[0];
}, false);
// does not work
dropArea.addEventListener("dragenter dragstart dragend dragleave dragover drag drop", function (e) {
e.preventDefault();
e.stopPropagation();
});
function sanitize(a){
// merge array of arrays
let merged = [].concat.apply([], a);
// to keep only words
let filtered_ = merged.filter(function(el) {
return (/^[a-z]+$/i.test(el));
});
// to remove falsy value (null, undefiened, "", NaN, false)
let filtered_bool = filtered_.filter(Boolean);
// to convert all strings to lowercase
let converted_lowercase = filtered_bool.map(el => {
return el.toLowerCase();
});
// to remove stopwords
sanitized = removeStopWords(converted_lowercase);
return sanitized;
}
// to count occurrences
function occ(final) {
let a = [], b = [], arr = final , prev;
arr.sort();
for (let element of arr) {
if (element !== prev) {
a.push(element);
b.push(1);
}
else ++b[b.length - 1];
prev = element;
}
return [a, b];
}
// sort based on occurrences
function sortOcc(words, occurrences) {
//1) combine the arrays:
var list = [];
for (var j = 0; j < words.length; j++)
list.push({'word': words[j], 'occ': occurrences[j]});
//2) sort:
list.sort(function(a, b) {
return ((a.occ > b.occ) ? -1 : ((a.occ == b.occ) ? 0 : 1)); //> was <
});
//3) separate them back out:
for (var k = 0; k < list.length; k++) {
words[k] = list[k].word;
occurrences[k] = list[k].occ;
}
// return words, occurrences;
}
// array of words to list w/ size and links
function createObjArr(words, occurrences)
{
let objArr = new Array();
// create new obj w/ cusstom fields
for (let index = 0; index < words.length; index++) {
var obj = Object.create({
node: words[index],
size: occurrences[index]
});
objArr.push(obj);
}
return objArr;
}
function addHeading(id, headingText) {
var cloudContainer = document.querySelector("#" + id);
var headingContainer = document.createElement("div");
var heading = document.createElement("h2");
var downloadBtn = document.createElement("button");
var downloadIcon = document.createElement("i");
downloadIcon.classList.add("far", "fa-arrow-alt-circle-down");
downloadIcon.style.marginRight = "5px";
downloadBtn.classList.add("btn", "btn-secondary", "btn-download", "download-"+id);
downloadBtn.innerText = "PNG"
heading.innerText = headingText;
headingContainer.classList.add("hd-container-" + id);
headingContainer.classList.add("hd-container");
downloadBtn.insertBefore(downloadIcon, downloadBtn.firstChild);
headingContainer.appendChild(heading);
headingContainer.appendChild(downloadBtn);
cloudContainer.insertBefore(headingContainer, cloudContainer.firstChild);
}
function removeDOMs() {
const frames = document.querySelectorAll(".frame");
frames.forEach((el) => {
while(el.firstChild) {
el.removeChild(el.firstChild);
}
});
}
function calculateSumOcc(occurrences) {
const reducer = (accumulator, curr) => accumulator + curr;
return occurrences.reduce(reducer);
}