-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathiframeMessages.js
134 lines (107 loc) · 3.38 KB
/
iframeMessages.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
"use strict";
let iframePreview;
let copy;
function copyToClipboard() {
iframePreview.focus();
iframePreview.click();
iframePreview.contentWindow.postMessage(
{
copy: true
},
"*"
);
copy = document.getElementById("copy");
copy.setAttribute("class", "selected");
copy.innerHTML = "Pressione CTRL + C para copiar sua assinatura";
document.querySelector("body").setAttribute("class", "blocked");
}
function validateInputs(name, cellphone) {
if (name && typeof name === "string") {
setTimeout(() => {
const nameSpaces = name.split(" ");
const error = document.getElementById("nameError");
if (nameSpaces.length < 2 || !nameSpaces[1]) {
return (error.innerHTML =
"É necessário informar o sobrenome. Ex.: Bruno Carvalho");
}
return (error.innerHTML = "");
}, 400);
}
if (cellphone && typeof cellphone === "string") {
setTimeout(() => {
const error = document.getElementById("cellError");
if (cellphone.length !== 13) {
return (error.innerHTML =
"O número de celular parece incorreto. Ex.: 31 98661-0770");
}
return (error.innerHTML = "");
}, 400);
}
}
function updatePreview() {
const name = document.getElementById("name").value;
const teamName = document.getElementById("teamName");
const cellphone = document.getElementById("cellphone").value;
validateInputs(name, cellphone);
iframePreview.contentWindow.postMessage(
{
name: name,
teamName: name.match(/Maiara Wenceslau/i)
? "Diretora Presidente"
: teamName.options[teamName.selectedIndex].value,
cellphone
},
"*"
);
}
function isCopied() {
const copiedContainer = document.getElementById("copied-container");
copiedContainer.setAttribute("class", "active");
setTimeout(() => {
copy.setAttribute("class", "copied");
copiedContainer.setAttribute("class", "");
copy.innerHTML = "Copiado!";
}, 400);
setTimeout(() => {
copy.innerHTML = "Concluir";
copy.setAttribute("class", "");
}, 1000);
}
document.addEventListener("DOMContentLoaded", function() {
if (window.addEventListener) {
window.addEventListener("message", isCopied, false);
} else {
window.attachEvent("onmessage", isCopied);
}
iframePreview = document.getElementById("iframePreview");
document.getElementById("copy").addEventListener("click", function() {
copyToClipboard();
});
document.getElementById("name").addEventListener("input", function() {
updatePreview();
});
document.getElementById("name").addEventListener("focus", function() {
const name = document.getElementById("name");
if (!!name.value.match(/Bruno Carvalho/i)) {
name.value = "";
}
});
document.getElementById("cellphone").addEventListener("focus", function() {
const cellphone = document.getElementById("cellphone");
if (!!cellphone.value.match(/31 98661-0770/i)) {
cellphone.value = "";
}
});
document.getElementById("name").value;
document.getElementById("cellphone").addEventListener("input", function() {
const isValidCellphone =
document.getElementById("cellphone").value.length > 13;
const formattedValue = document
.getElementById("cellphone")
.value.substr(0, 13);
if (isValidCellphone) {
document.getElementById("cellphone").value = formattedValue;
}
updatePreview();
});
});