-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
71 lines (55 loc) · 2.12 KB
/
script.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
const button = document.getElementById('criar-carta');
const paragraph = document.getElementById('carta-gerada');
const input = document.getElementById('carta-texto');
const counter = document.getElementById('carta-contador');
// const sp = document.getElementsByTagName('span');
const words = () => input.value.split(' ');
const styleAndSizeGroup = () => {
const style = ['newspaper', 'magazine1', 'magazine2'];
const size = ['medium', 'big', 'reallybig'];
return `${style[parseInt(Math.random() * 3, 10)]} ${size[parseInt(Math.random() * 3, 10)]}`;
};
const rotationAndTiltGroup = () => {
const rotation = ['rotateleft', 'rotateright'];
const tilt = ['skewleft', 'skewright'];
return `${rotation[parseInt(Math.random() * 2, 10)]} ${tilt[parseInt(Math.random() * 2, 10)]}`;
};
const selectClass = () => `${rotationAndTiltGroup()} ${styleAndSizeGroup()}`;
const createSpan = (text) => {
const element = document.createElement('span');
element.innerText = text;
element.className = selectClass();
paragraph.appendChild(element);
};
const generateLetter = () => words().forEach((word) => createSpan(word));
const inputCheck = () => {
const { value } = input;
if (!value || /^\s+$/.test(value)) return true;
};
const wordsNumbers = () => { counter.innerText = words().length; };
const test = (e) => {
console.log(e);
const { target } = e;
if (target.tagName === 'SPAN') {
target.className = '';
target.className = selectClass();
}
};
const spansEventClick = () => { paragraph.onclick = (e) => test(e); };
const clearLetter = () => {
const spans = document.querySelectorAll('#carta-gerada span');
spans.forEach((element) => paragraph.removeChild(element));
};
const inputMessage = () => { paragraph.innerText = 'Por favor, digite o conteúdo da carta.'; };
const chama = () => {
if (paragraph.children) {
clearLetter(); generateLetter(); wordsNumbers(); spansEventClick();
} else {
generateLetter(); wordsNumbers(); spansEventClick();
}
};
const textValidation = () => (inputCheck() ? inputMessage() : chama());
const events = () => {
button.addEventListener('click', textValidation);
};
events();