-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathform.js
359 lines (318 loc) · 12.8 KB
/
form.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
let name = document.getElementById("name");
let fromValue = document.querySelector('input[value="from"]');
let from = document.getElementById('from');
let message = document.getElementById("message");
let hungUpMsg = document.querySelector('input[value="hangup"]');
let noPhone = document.querySelector('input[value="no_phone"]');
let hangup = document.getElementById('hangup');
let callBack = document.getElementById("callBack");
var phone = document.getElementById('phone');
let rowAndSubject = document.getElementById("rowAndSubject");
var local = /502/m;
var parenthesisAndDash = /[\(\)]|\s|\-|\|\\|\/|\./g;
var formatMatch = /^(\d{3})?(\d{3})(\d{4})$/;
var fullyFormattedPhoneOne
var fullyFormattedPhoneTwo
function tooltips() {
var btns = document.querySelectorAll('button[type="button"]');
for (var i = 0; i < btns.length; i++) {
btns[i].classList.add('tooltip');
btns[i].setAttribute('title', 'Copied');
}
}
function setup() {
let phoneToCheck = document.querySelectorAll('input[type="tel"]');
const forms = document.getElementsByTagName('form');
let digit = new RegExp(/\d+/);
const phoneRegEx = new RegExp(/(?:\(?([0-9]{3})\)?[-.*\/\\\s]?)?([0-9]{3})[-.*\/\\\s]?([0-9]{4})$/g);
for(let c = 0; c < phoneToCheck.length; c++) {
phoneToCheck[c].setAttribute('pattern', phoneRegEx.source)
}
for(let f = 0; f < forms.length; f++) {
forms[f].setAttribute('novalidate', true);
}
rowAndSubject.setAttribute('pattern', digit.source);
}
function erroneous(field) {
if (from.checked != true || hangup.checked != true || field.type === '' ) return;
}
function noPhoneNumber() {
if(noPhone.checked == true) {
console.log('no phone number')
phone.removeAttribute('required')
} else {
phone.setAttribute('required', 'required')
console.log('guess there\' s a phone number')
}
}
function calledFromAnotherNum() {
if(fromValue.checked != true) {
console.log('unchecked')
callBack.removeAttribute('required')
} else {
console.log('checked')
callBack.setAttribute('required', 'required')
}
}
function didTheyHangUp() {
if(hungUpMsg.checked == true) {
console.log('checked_another')
message.removeAttribute('required')
} else {
message.setAttribute('required', 'required')
console.log('no check')
}
}
function namingError() {
let nameError = document.getElementById("nameError");
if (name.validity.valueMissing) {
nameError.style.color = "red"
}
}
function rowAndSubjectErrorFunc() {
let rowAndSubjectError = document.getElementById('rowAndSubjectError')
if (rowAndSubject.validity.valueMissing || rowAndSubject.validity.patternMismatch) {
rowAndSubjectError.style.color = "red"
}
}
function hungUp() {
let messageError = document.getElementById('messageError')
if (message.validity.valueMissing) {
messageError.style.color = "red"
}
}
function phoneMatchError() {
let phoneError = document.getElementById('phoneError')
if (phone.validity.valueMissing || phone.validity.patternMismatch) {
phoneError.style.color = "red"
}
}
function displayCallBackProblem() {
let callBackError = document.getElementById('callBackError')
if (callBack.validity.valueMissing) {
callBackError.style.color = "red"
}
}
let theCopyMachine = (id) => {
if (!navigator.clipboard) {
// Clipboard API not available
console.log("F Bomb!")
return
}
const textToCopy = id.innerText
try {
navigator.clipboard.writeText(textToCopy)
console.log('Copied to clipboard', textToCopy)
} catch (err) {
console.error('Failed to copy!', err)
}
}
let submitName = () => {
if(!name.validity.valid) {
namingError()
} else {
nameError.removeAttribute('style')
document.getElementById('copyName').innerHTML = '<span>Name: <pre id="nameToCopy" class="preStyles">' + name.value + '</pre></span>'
+ '<button id="copyNameButton" type="button" class="inputs buttonStyle">Copy</button>'
var copyNameClicked = document.getElementById('copyNameButton');
copyNameClicked.addEventListener('click', (e) => {
e.preventDefault()
theCopyMachine(namer)});
var namer = document.querySelector('#nameToCopy');
}
}
let rowAndSubjectSubmit = () => {
if (!rowAndSubject.validity.valid) {
rowAndSubjectErrorFunc()
} else {
rowAndSubjectError.removeAttribute('style')
document.getElementById('numberAndSubjectCopy').innerHTML = '<span>Call Number: <pre id="sheetRowNum" class="preStyles">' + rowAndSubject.value + '</pre></span><button id="subject" type="button" class="inputs buttonStyle">Copy</button> <span>Subject: <pre id="emailSubject" class="preStyles">#' + rowAndSubject.value + '</pre></span><button id="row" type="button" class="inputs buttonStyle">Copy</button>'
var subject = document.getElementById('subject');
var row = document.getElementById('row');
row.addEventListener('click', (e) =>{
e.preventDefault()
var emailSubject = document.getElementById('emailSubject')
theCopyMachine(emailSubject)});
subject.addEventListener('click', (e) =>{
e.preventDefault()
var sheetRowNum = document.getElementById('sheetRowNum')
theCopyMachine(sheetRowNum)});
}
}
let messageDisplay = () => {
if (hangup.checked == true) {
document.getElementById('copyMsg').innerHTML = '<span>Message: <pre id="msging" class="preStyles">Hang Up</pre></span><button id="msgDisplayCopyButton" type="button" class="inputs buttonStyle">Copy</button>'
var msgDisplayCopied = document.getElementById('msgDisplayCopyButton')
} else {
document.getElementById('copyMsg').innerHTML = '<span>Message: <pre id="msging" class="preStyles">' + message.value + '</pre></span>'
+ '<button id="msgDisplayCopyButton" type="button" class="inputs buttonStyle">Copy</button>'
var msgDisplayCopied = document.getElementById('msgDisplayCopyButton');
}
msgDisplayCopied.addEventListener('click', (e) => {
e.preventDefault()
var msging = document.getElementById('msging')
theCopyMachine(msging)});
}
let submitMsg = () => {
//event.preventDefault()
if(!message.validity.valid) {
hungUp()
} else {
messageError.removeAttribute('style')
messageDisplay()
}
}
let phoneFormatOne = () => {
var digitsToFormat
var phoneToFormat
var phoneRaw = phone.value
phoneToFormat = phoneRaw.replace(parenthesisAndDash, "");
if(local.test(phoneToFormat) == true) {
phoneToFormat = phoneToFormat.slice(3)
} else {
phoneToFormat
}
digitsToFormat = phoneToFormat.match(formatMatch)
if (digitsToFormat === null) {
return
}
if (digitsToFormat[1] == undefined) {
digitsToFormat
fullyFormattedPhoneOne = digitsToFormat[2] + '-' + digitsToFormat[3]
//fullyFormattedPhoneOne = digitsToFormat[1] + '-' + digitsToFormat[2] + '-' + digitsToFormat[3]
} else if (digitsToFormat[1] != undefined) {
fullyFormattedPhoneOne = digitsToFormat[1] + '-' + digitsToFormat[2] + '-' + digitsToFormat[3]
//digitsToFormat.slice(2)
//fullyFormattedPhoneOne = digitsToFormat[2] + '-' + digitsToFormat[3]
}
//phoneFormatTwo(fullyFormattedPhoneOne)
createPhoneNumberSection(fullyFormattedPhoneOne)
}
let phoneFormatTwo = () => {
var digitsToFormatTwo
var phoneToFormat
var phoneRawTwo = callBack.value
phoneToFormat = phoneRawTwo.replace(parenthesisAndDash, "");
if(local.test(phoneToFormat) == true) {
phoneToFormat = phoneToFormat.slice(3)
} else {
phoneToFormat
}
digitsToFormatTwo = phoneToFormat.match(formatMatch)
if (digitsToFormatTwo === null) {
return
}
else if (digitsToFormatTwo[1] == undefined) {
//fullyFormattedPhoneTwo = digitsToFormat[1] + '-' + digitsToFormat[2] + '-' + digitsToFormat[3]
digitsToFormatTwo.slice(2)
fullyFormattedPhoneTwo = digitsToFormatTwo[2] + '-' + digitsToFormatTwo[3]
} else if (digitsToFormatTwo[1] != undefined) {
fullyFormattedPhoneTwo = digitsToFormatTwo[1] + '-' + digitsToFormatTwo[2] + '-' + digitsToFormatTwo[3]
//fullyFormattedPhoneTwo = digitsToFormat[2] + '-' + digitsToFormat[3]
console.log(digitsToFormatTwo[1])
}
//phoneFormatOne(fullyFormattedPhoneTwo)
createPhoneNumberSection(fullyFormattedPhoneTwo)
transcribeForEmail(fullyFormattedPhoneTwo)
//submitCallbackNumber(digitsToFormat)
}
let createPhoneNumberSection = () => {
if (!phone.validity.valid || !callBack.validity.valid) {
return
}
else if(noPhone.checked == true) {
document.getElementById('copyPhoneOne').innerHTML = '<span>Phone: <pre id="phoneNum" class="preStyles">None</pre></span><button id="copyPhoneNum" type="button" class="inputs buttonStyle">Copy</button>'
var copyPhoneNum = document.getElementById('copyPhoneNum');
}
else if(fromValue.checked == true) {
document.getElementById('copyPhoneOne').innerHTML = '<span>Phone: <pre id="phoneNum" class="preStyles">' + fullyFormattedPhoneOne + ' called from; ' + fullyFormattedPhoneTwo + ' call back number</pre></span>' + '<button id="copyPhoneNum" type="button" class="inputs buttonStyle">Copy</button>'
var copyPhoneNum = document.getElementById('copyPhoneNum');
} else {
document.getElementById('copyPhoneOne').innerHTML = '<span>Phone: <pre id="phoneNum" class="preStyles">' + fullyFormattedPhoneOne + '</pre></span>' + '<button id="copyPhoneNum" type="button" class="inputs buttonStyle">Copy</button>'
var copyPhoneNum = document.getElementById('copyPhoneNum');
}
copyPhoneNum.addEventListener('click', (e) => {
e.preventDefault()
var phoneNum = document.getElementById('phoneNum')
theCopyMachine(phoneNum)})
}
let transcribeForEmail = () => {
var copierName = document.getElementById('copierName')
var copierPhoneOne = document.getElementById('copierPhoneOne')
var copierMsg = document.getElementById('copierMsg')
var transcribeForEmailBox = document.querySelector('#transcribeForEmailBox')
var copierForEmail = document.querySelector('#copierForEmail')
transcribeForEmailBox.style.cssText = "display: block; background: rgba(24, 24, 24, 0.603);color: white;padding: 0.25rem 1rem;font-size: 1.15rem;";
copierForEmail.style.display = 'block';
copierName.innerHTML = 'Name: ' + name.value;
if(hangup.checked == true) {
copierMsg.innerHTML = "Message: Hang Up"
} else {
copierMsg.innerHTML = "Message: " + message.value
}
if(noPhone.checked == true) {
copierPhoneOne.innerHTML = 'Phone: None'
}
else if(fromValue.checked == true) {
copierPhoneOne.innerHTML = 'Phone: ' + fullyFormattedPhoneOne + ' called from; ' + fullyFormattedPhoneTwo + ' call back number'
} else {
copierPhoneOne.innerHTML = 'Phone: ' + fullyFormattedPhoneOne
}
copierForEmail.addEventListener('click', (e) => {
e.preventDefault();
theCopyMachine(transcribeForEmailBox)
})
}
let submitPhoneNum = () => {
if(!phone.validity.valid) {
phoneMatchError()
} else {
phoneError.removeAttribute('style')
phoneFormatOne()
}
}
//document.addEventListener('submit', displayCallBackError)
let submitCallbackNumber = () => {
if(!callBack.validity.valid) {
displayCallBackProblem()
} else {
callBackError.removeAttribute('style')
phoneFormatTwo()
}
}
let seeCallbackTextArea = () => {
var callBackDates = document.querySelector('#callBackDates');
var copierCallBackDates = document.querySelector('#copierCallBackDates');
var callBackTextArea = document.querySelector('#callBackTextArea')
callBackDates.style.cssText = "display: block; padding: 0.25rem 1rem;font-size: 1.15rem;"
copierCallBackDates.style.display = 'block';
copierCallBackDates.addEventListener('click', (e) => {
e.preventDefault();
try{
navigator.clipboard.writeText(callBackTextArea.value)
console.log("worked", callBackTextArea.value)
} catch (err) {
console.error("Fuck", err)}
});
}
let onSubmit = (e) => {
e.preventDefault()
submitMsg()
submitPhoneNum()
submitCallbackNumber()
submitName()
rowAndSubjectSubmit()
createPhoneNumberSection()
transcribeForEmail()
seeCallbackTextArea()
tooltips()
}
/* Notes for Tuesday, June, 7 2020 @ 1:11AM
Apparently, the reason I didn't put all those function into one big "onSubmit" function (like the one above) is that everything works if you put it in order. If submit call back number has nothing, then the whole function fails. For lack of a better term it fails. It's past one AM folks. Any how, that's why I made a seperate event listener. Apparently the order in which one calls a function (or variable) matters inside a function.*/
/* Make note of discovery made on above date @ 3:08AM. Ya know, the slice(2) and all that*/
setup()
noPhone.addEventListener('change', noPhoneNumber);
hungUpMsg.addEventListener('change', didTheyHangUp);
fromValue.addEventListener('change', calledFromAnotherNum);
document.addEventListener('submit', onSubmit);
//document.addEventListener('submit', submitCallbackNumber)