-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
170 lines (144 loc) · 4.55 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
const inputSelected = document.querySelector(".selected");
const inputOptionsContainer = document.querySelector(".options-container");
const inputOptionsList = document.querySelectorAll('.option');
const outputSelected = document.querySelector(".selected1");
const outputOptionsContainer = document.querySelector(".options-container1");
const outputOptionsList = document.querySelectorAll('.option1');
const input = document.querySelector(".input");
const output = document.querySelector(".output-value");
const translaterBtn = document.querySelector(".converter");
const resetBtn = document.querySelector(".btn-reset");
const navBar = document.querySelector("header");
const accordion = document.querySelectorAll(".contentBox");
inputSelected.addEventListener('click', () => {
inputOptionsContainer.classList.toggle('active');
});
inputOptionsList.forEach(item => {
item.addEventListener('click', () => {
inputSelected.innerHTML = item.querySelector("label").innerHTML;
inputOptionsContainer.classList.remove('active');
})
})
outputSelected.addEventListener('click', () => {
outputOptionsContainer.classList.toggle('active');
});
outputOptionsList.forEach(item => {
item.addEventListener('click', () => {
outputSelected.innerHTML = item.querySelector("label").innerHTML;
outputOptionsContainer.classList.remove('active');
})
})
const languageMap = {
'Bengali': 'bn',
'English': 'en',
'Hindi': 'hi',
'Italian': 'it',
'Spanish': 'es',
'Japanese': 'ja',
'German': 'de',
'Latin': 'la',
'Chinese': 'zh'
}
const genLanguagePair = (inputLanguage, outputLanguage) => {
return `${languageMap[inputLanguage]}|${languageMap[outputLanguage]}`;
}
const translate = (inputLanguage, outputLanguage, languagePair) => {
console.log(languagePair);
fetch(`https://api.mymemory.translated.net/get?q=${input.value}&langpair=${languagePair}`)
.then(response => response.json())
.then(json => {
if (json.responseStatus !== 429)
output.innerHTML = json.responseData.translatedText;
else {
output.classList.add("error");
output.innerHTML = "Mymemory API query limit reached";
}
})
.catch(error => console.log(error));
}
translaterBtn.addEventListener('click', () => {
translate(inputSelected.innerHTML, outputSelected.innerHTML, genLanguagePair(inputSelected.innerHTML, outputSelected.innerHTML));
});
resetBtn.addEventListener('click', () => {
input.value = "";
output.innerHTML = "";
})
// ref: https://www.codegrepper.com/code-examples/javascript/javascript+download+text+as+txt+file
document.querySelector(".icon.download").addEventListener('click', () => {
const element = document.createElement('a');
element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(output.innerHTML));
element.setAttribute('download', 'test.txt');
element.style.display = 'none';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
});
// navbar part starts here
window.onscroll = () => {
const sticky = navBar.offsetTop;
if (window.pageYOffset > sticky) {
navBar.classList.add("sticky");
} else {
navBar.classList.remove("sticky");
}
};
// accordion part here
const runAccordion = () => {
for (let i = 0; i < accordion.length; i += 1) {
accordion[i].addEventListener('click', () => {
accordion[i].classList.toggle('activate');
for (let j = 0; j < accordion.length; j += 1) {
if (i !== j && accordion[j].classList.contains('activate')) {
// switches off other accordions
accordion[j].classList.toggle('activate');
}
}
});
}
};
runAccordion();
// for the clipboard copying
new ClipboardJS('.icon.copy');
// for the scroll-reveal part
const sr = ScrollReveal({
origin: 'top',
distance: '80px',
duration: 2000,
})
sr.reveal('.select-box', { delay: 200 });
sr.reveal('.input-text', { delay: 400 });
sr.reveal('.btn1', { delay: 200 });
sr.reveal('.btn2', { delay: 400 });
sr.reveal('.select-box1', { delay: 200 });
sr.reveal('.output', { delay: 400 });
// for tooltip
tippy('.icon.copy', {
content: 'copied',
trigger: 'click',
hideOnClick: false,
onShow(instance) {
setTimeout(() => {
instance.hide();
}, 1000);
}
})
tippy('.icon.download', {
content: 'downloading..',
trigger: 'click',
hideOnClick: false,
onShow(instance) {
setTimeout(() => {
instance.hide();
}, 1000);
}
})
tippy('.icon.copy', {
content: 'copied',
trigger: 'click',
hideOnClick: false,
onShow(instance) {
setTimeout(() => {
instance.hide();
}, 1000);
}
})