-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
91 lines (84 loc) · 2.55 KB
/
app.js
File metadata and controls
91 lines (84 loc) · 2.55 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
// Search
const searchField = document.getElementById('searchField');
const searchedWord = document.getElementById('searchedWord');
// File
const fileField = document.getElementById('fileField');
const fileContent = document.getElementById('fileContent');
fileContent.style.display = 'none';
// Count
const numWords = document.getElementById('numWords');
let wordCount = 0;
// Copywrite Year
const date = new Date().getFullYear();
document.getElementById('currentYear').innerHTML = date;
// Modal
const contactMeModal = document.getElementById('contactMeModal');
const navContactBtn = document.getElementById('navContactBtn');
const closeModalBtn = document.getElementsByClassName('close')[0];
/**
* Sets the searched word.
*/
const setSearchedWord = (event) => {
searchedWord.innerHTML = event.target.value;
}
/**
* Handles the parsing of a file and counting the number of occurences for a searched word.
*/
const parseFileContents = (event) => {
// Re-initialize values
wordCount = 0;
fileContent.value = '';
for (const file of event.target.files) {
console.log(file);
// Get the contents of the file
const fileReader = new FileReader();
// Read the contents of the file
fileReader.readAsText(file);
// Operate on the contents of the file.
fileReader.onload = () => {
fileContent.innerHTML = fileReader.result;
// Separate words in the file
const wordBank = fileContent.innerHTML.split(/[\s.]+/);
wordBank.forEach(word => {
if (word.toLowerCase() === searchedWord.innerHTML.toLowerCase() &&
word.toLowerCase() !== '') {
wordCount++;
}
});
// Assign the total word count.
numWords.innerHTML = wordCount;
// Display file contents if file has words
fileContent.style.display = 'flex';
}
}
}
/**
* ###### Event Listeners #########
*/
searchField.addEventListener('change', setSearchedWord);
fileField.addEventListener('change', parseFileContents);
/**
* ##### Modal #######
*/
/**
* Open the modal
*/
navContactBtn.onclick = () => {
contactMeModal.classList.add('show');
}
/**
* Close modal when clicking the 'x' in the top right.
*/
closeModalBtn.onclick = () => {
contactMeModal.classList.remove('show');
}
/**
* Close modal when clicking outside the modal.
*
* @param {*} event
*/
window.onclick = (event) => {
if (event.target === contactMeModal) {
contactMeModal.classList.remove('show');
}
}