-
Notifications
You must be signed in to change notification settings - Fork 0
/
feedback-sentiment.js
110 lines (101 loc) · 3.12 KB
/
feedback-sentiment.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
var sentiment = new Sentiment();
let badSentiment = false;
var fallingLetters = [];
let feedbackInput = document.querySelector("#feedback-input");
let feedbackInputCap = document.querySelector("#feedback-input-cap");
let trashWrapper = document.querySelector(".trash-wrapper");
let sentamentBar = document.querySelector(".sentament-bar-indicator__progress");
// Autofocus field
window.onload = function () {
focusInput();
};
function focusInput() {
feedbackInput.focus();
}
// Remap helper function
function mapRange(value, low1, high1, low2, high2) {
return low2 + ((high2 - low2) * (value - low1)) / (high1 - low1);
}
function clickSend() {
if (badSentiment) {
alert(
"We don't appreciate your tone of voice, please try again before sending"
);
} else {
alert("Thank you for your kind feedback");
}
}
feedbackInput.addEventListener("keydown", (e) => {
// If the letters are going to the trash, intercept unless it's backspace, ctrl+a, or ctrl+r
if (
badSentiment &&
!(e.code === "KeyA" && e.ctrlKey) && // Select all
!(e.code === "KeyR" && e.ctrlKey) && // Reload page
![
"ArrowUp",
"ArrowDown",
"ArrowLeft",
"ArrowRight",
"Home",
"End",
"Backspace",
"Delete",
].includes(e.code) // Allowed keys, including deleting offending word
) {
e.preventDefault(); // Stop it from going to the field
// If it's a letter or number make it fall
if (
(e.keyCode > 64 && e.keyCode < 91) || // Letters
(e.keyCode > 47 && e.keyCode < 58) // Numbers
) {
let fallingLetter = document.createElement("div");
fallingLetter.innerText = e.key;
fallingLetter.classList.add("falling-letter");
fallingLetter.style.top =
feedbackInput.getBoundingClientRect().top + "px";
fallingLetter.style.left =
feedbackInput.getBoundingClientRect().right + 1 + "px";
document.body.append(fallingLetter);
fallingLetters.push(fallingLetter);
}
}
});
feedbackInput.addEventListener("keyup", (e) => {
let result = sentiment.analyze(feedbackInput.innerHTML);
sentamentBar.style.width = mapRange(result.score, -5, 5, 0, 100) + "%";
if (result.score < 0) {
badSentiment = true;
feedbackInputCap.className = "rotated";
trashWrapper.style.marginLeft =
window.getComputedStyle(feedbackInput).width;
} else if (badSentiment) {
badSentiment = false;
feedbackInputCap.className = "";
trashWrapper.style.marginLeft = "";
}
});
setInterval(() => {
fallingLetters.forEach((fallingLetter) => {
// If letter is still falling
if (
Number(fallingLetter.style.top.slice(0, -2)) <
trashWrapper.getBoundingClientRect().top + 90
) {
fallingLetter.style.top =
Number(fallingLetter.style.top.slice(0, -2)) + 4 + "px";
fallingLetter.style.transform =
"rotate(" +
(Number(
fallingLetter.style.transform.slice(
7,
fallingLetter.style.transform.indexOf("d")
)
) +
4) +
"deg)";
} else {
// The letter has hit the trash, remove it
fallingLetter.remove();
}
});
}, 30);