-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
707 lines (535 loc) · 20.9 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
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
// buttons
const engageSelectBtn = document.getElementById('engage-select'),
buildSelectBtn = document.getElementById('build-select'),
generateBtn = document.getElementById('generate'),
clearBtn = document.getElementById('clear-utms');
// button listeners
engageSelectBtn.addEventListener('click', engageDisplay);
buildSelectBtn.addEventListener('click', buildDisplay);
generateBtn.addEventListener('click', generateEmailTitles);
clearBtn.addEventListener('click', clearUTMs);
// business name input
const businessInput = document.getElementById('business-input');
let businessName = businessInput.value;
// cta input
// const ctaInput = document.getElementById('cta-url-input-1');
// ctaInput.value = '';
// cta switch
// const ctaSwitch = document.getElementById('cta-toggle');
// engage stream title input
const engageTitle = document.getElementById('engage-title');
// number of emails input
const numEmailsInput = document.getElementById('num-emails-input');
numEmailsInput.value = 1;
// listens for change in value of numEmailsInput
numEmailsInput.addEventListener('change', displayNumNameFields);
// base title input
const emailTitle = document.getElementById('title-1');
const emailTitleLabel = document.getElementById('title-1-label');
// number of URLs input
const numURLsInput = document.getElementById('num-urls-input');
numURLsInput.value = 1;
// base url input
const urlInput = document.getElementById('url-input-1');
const urlLabel = document.getElementById('title-1-label');
// url input listener
urlInput.addEventListener('blur', validURL);
// lists where extra email fields are inserted
var titlesList = document.getElementById("titles");
var URLs = document.getElementById("urls");
// UTM output location
const outputDiv = document.getElementById('output');
// hidden output area where list of <p>s to be copied from are 'stored'
const hiddenOutput = document.getElementById('hidden-output');
// hidden lists
const hiddenHeaderList = document.getElementById('hidden-header-list'),
hiddenFooterList = document.getElementById('hidden-footer-list'),
hiddenImageList = document.getElementById('hidden-image-list'),
hiddenBodyList = document.getElementById('hidden-body-list'),
hiddenCTAList = document.getElementById('hidden-cta-list'),
hiddenSigList = document.getElementById('hidden-sig-list');
// display areas
const headerDisplay = document.getElementById('header-display'),
footerDisplay = document.getElementById('footer-display'),
imageDisplay = document.getElementById('image-display'),
bodyDisplay = document.getElementById('body-display'),
ctaDisplay = document.getElementById('cta-display'),
sigDisplay = document.getElementById('sig-display');
// called when 'engage' is clicked
function engageDisplay() {
// add/remove class for selection
if (!engageSelectBtn.classList.contains('selected')) {
engageSelectBtn.classList.toggle('selected');
if (buildSelectBtn.classList.contains('selected')) {
buildSelectBtn.classList.remove('selected');
}
}
// apply border color to active button and remove from inactive
engageSelectBtn.style.border = '1px solid #BC3339';
buildSelectBtn.style.border = '1px solid #BBB';
engageTitle.removeAttribute('disabled');
engageTitle.style.backgroundColor = '#fff';
// clear titlesFormatted[]
titlesFormatted = [];
}
// called when 'build' is clicked
function buildDisplay() {
// add/remove class for selection
if (!buildSelectBtn.classList.contains('selected')) {
buildSelectBtn.classList.toggle('selected');
if (engageSelectBtn.classList.contains('selected')) {
engageSelectBtn.classList.remove('selected');
}
}
// apply border color to active button and remove from inactive
buildSelectBtn.style.border = '1px solid #BC3339';
engageSelectBtn.style.border = '1px solid #BBB';
engageTitle.setAttribute('disabled', true);
engageTitle.value = '';
engageTitle.style.backgroundColor = '#d1d1d1';
// clear titlesFormatted[]
titlesFormatted = [];
}
// check validity of URL TURNON
function validURL() {
// if (urlInput.matches(':invalid')) {
// alert('The URL you have entered is invalid.');
// }
// if (ctaInput.matches(':invalid')) {
// alert('The URL you have entered is invalid.')
// }
}
// display appropriate number of email title fields
function displayNumNameFields() {
// thanks for reading my code!
if (Number(numEmailsInput.value) === 42) {
document.body.innerHTML = "<h1 style='text-align:center;'>UNRESOLVED FINITE / INFINITE PARADOX ENCOUNTERED.</h1><h1 style='text-align:center;'>YOU ARE NOT PREPARED TO ASCEND.</h1><h1 style='text-align:center;'>DIMENSIONAL LOCK REINITIALIZING ...</h1>";
function lockout() {
setTimeout(function() {location.reload();}, 5000);
}
lockout();
return;
}
// 'gotcha' for the cheeky ones
if(numEmailsInput.value > 41) {
alert("You don't need that many emails. Go ahead and click 'OK' - no fancy DOM-based alert message for you. That'll teach you to question my UX design.")
numEmailsInput.value = 1;
}
// for the true trolls out there
if (Number(numEmailsInput.value) === 0 || Number(numEmailsInput.value) < 0) {
alert("Well, I guess your job is done, then.")
numEmailsInput.value = 1;
}
// clear name/cta inputs to prevent duplication
while (titlesList.hasChildNodes()) {
titlesList.removeChild(titlesList.lastChild);
}
while (URLs.hasChildNodes()) {
URLs.removeChild(URLs.lastChild);
}
// adds new inputs to DOM depending on value of numEmailsInput
for (i=1; i<((Number(numEmailsInput.value)) + 1); i++) {
newTitle = document.createElement('li');
newTitle.innerHTML = `
<label for="title-${i}" id="title-${i}-label">TITLE ${i}</label>
<input type="text" name="title-${i}" class="title" id="title-${i}" data-lpignore="true">`;
newTitle.setAttribute('autocomplete', 'new-password');
titlesList.appendChild(newTitle);
newURL = document.createElement('li');
newURL.innerHTML = `
<label for="url-${i}" id="url-${i}-label">URL ${i}</label>
<input type="url" name="url-input-${i}" class="url" id="url-input-${i}" data-lpignore="true">`;
newURL.setAttribute('autocomplete', 'new-password');
URLs.appendChild(newURL);
}
// Remove redundant '1' from title input label when only one email is needed
if (Number(numEmailsInput.value) === 1) {
document.getElementById('title-1-label').innerHTML = "TITLE";
document.getElementById('url-1-label').innerHTML = "URL";
}
}
// live nodeList of all title <li> tags
let titleList = document.getElementsByClassName('title');
// live nodeList of all url <li> tags
let URLList = document.getElementsByClassName('url');
// init array for formatted email names
let titlesFormatted = [];
// generate formatted email titles from input values
function generateEmailTitles() {
let titleArray = Array.from(titleList);
let urlArray = Array.from(URLList);
titleArray.forEach((name, i) => {
});
// enforces selection of either BUILD or ENGAGE TURNON
// if (!buildSelectBtn.classList.contains('selected') && !engageSelectBtn.classList.contains('selected')) {
// alert('Please select either BUILD or ENGAGE.');
// return;
// }
// init array for email name storage
let emailTitles = [];
// init array for url storage
let urls = [];
// pulls values out of nested inputs in titleList[]
for (i=0; i<titleList.length; i++) {
let nodeItem = titleList.item(i)
emailTitles.push(nodeItem.firstElementChild.nextSibling.nextSibling.value);
}
// formats names for URLs
emailTitles.forEach((name) => {
// trim any silliness
let nameTrimmed = name.trim();
// replace spaces with hyphens
let nameFormatted = nameTrimmed.replace(/ /g, "-");
// .replace(/;/g, '%3B').replace(/,/g, '%2C').replace(/\//g, '%2F').replace(/\?/g, '%3F').replace(/:/g, '%3A').replace(/@/g, '%40').replace(/&/g, '%26').replace(/=/g, '%3D').replace(/\+/g, '%2B').replace(/\$/g, '%24').replace('.', '').replace(/!/g, '%21').replace(/~/g, '%7E').replace(/\*/g, '%2A').replace(/'/g, '%27').replace(/\(/g, '%28').replace(/\)/g, '%29').replace(/#/g, '%23');
// You saw nothing...
// URI encode (encodes special characters)
let nameEncoded = escape(nameFormatted);
titlesFormatted.push(nameEncoded);
});
generateUTMs();
}
// init UTM arrays
let headerUTMs = [],
footerUTMs = [],
imageUTMs = [],
bodyUTMs = [],
ctaUTMs = [],
sigUTMs = [];
// generate UTM codes from provided params
function generateUTMs() {
// reset UTM arrays to prevent multiple click duplication
headerUTMs = [];
footerUTMs = [];
imageUTMs = [];
bodyUTMs = [];
ctaUTMs = [];
sigUTMs = [];
// reset hidden area
hiddenHeaderList.innerHTML = '';
hiddenFooterList.innerHTML = '';
hiddenImageList.innerHTML = '';
hiddenBodyList.innerHTML = '';
hiddenCTAList.innerHTML = '';
hiddenSigList.innerHTML = '';
// enforce mandatory fields TURNON
// let urlInputValue = urlInput.value;
// let ctaInputValue = ctaInput.value;
// if (urlInputValue === '') {
// alert('Enter a URL.');
// return;
// } else if (ctaInputValue === '') {
// alert('Enter a CTA URL.');
// return;
// } else if (businessName === '') {
// alert('Enter your business\'s name.');
// return;
// }
// enforce trailing / on URLs
let urlInputLength = urlInputValue.length;
let urlLastChar = urlInputValue.charAt(urlInputLength - 1);
if(urlLastChar !== "/") {
urlInputValue = urlInputValue + '/';
}
let ctaInputLength = ctaInputValue.length;
let ctaLastChar = ctaInputValue.charAt(ctaInputLength - 1);
if(ctaLastChar !== "/") {
ctaInputValue = ctaInputValue + '/';
}
// check BUILD or ENGAGE and reassign var accordingly
let typeCheck = "";
if (buildSelectBtn.classList.contains('selected')) {
typeCheck = "build-";
} else {
if (engageTitle.value != '') {
// grab engage stream title and to add to UTM
let engageStreamTitle = engageTitle.value;
let engageTitleTrimmed = engageStreamTitle.trim();
let engageTitleFormatted = engageTitleTrimmed.replace(/ /g, "-");
typeCheck = `engage-${engageTitleFormatted}-`;
} else {
typeCheck = `engage-`;
}
}
// generate UTMs for header
titlesFormatted.forEach((name) => {
let newUTM = `${urlInputValue}?utm_source=${businessName}&utm_medium=email&utm_campaign=${typeCheck}${name}&utm_content=header`;
headerUTMs.push(newUTM);
});
// generate UTMs for footer
titlesFormatted.forEach((name) => {
let newUTM = `${urlInputValue}?utm_source=${businessName}&utm_medium=email&utm_campaign=${typeCheck}${name}&utm_content=footer`;
footerUTMs.push(newUTM);
});
// generate UTMs for image
titlesFormatted.forEach((name) => {
let newUTM = `${urlInputValue}?utm_source=${businessName}&utm_medium=email&utm_campaign=${typeCheck}${name}&utm_content=image`;
imageUTMs.push(newUTM);
});
// generate UTMs for body link
titlesFormatted.forEach((name) => {
let newUTM = `\<a href="${urlInputValue}?utm_source=${businessName}&utm_medium=email&utm_campaign=${typeCheck}${name}&utm_content=body-link" target="_blank">`;
bodyUTMs.push(newUTM);
});
// generate UTMs for CTA
titlesFormatted.forEach((name) => {
let newUTM = `${urlInputValue}?utm_source=${businessName}&utm_medium=email&utm_campaign=${typeCheck}${name}&utm_content=CTA`;
ctaUTMs.push(newUTM);
});
// generate UTMs for signature
titlesFormatted.forEach((name) => {
let newUTM = `\<a href="${urlInputValue}?utm_source=${businessName}&utm_medium=email&utm_campaign=${typeCheck}${name}&utm_content=signature" target="_blank">`;
sigUTMs.push(newUTM);
});
// clear formatted names array to prevent duplication from multiple calls
titlesFormatted = [];
window.scroll(0,10000);
renderHiddenOutputs();
}
function renderHiddenOutputs() {
// append hidden header utms to list
headerUTMs.forEach((utm, i) => {
newHiddenOutput = document.createElement('span');
newHiddenOutput.innerHTML = `
<textarea id="header-utm-${i+1}-hidden" class="header-utm header-output-${i+1}">${utm}</textarea>`;
hiddenHeaderList.appendChild(newHiddenOutput);
});
// append hidden footer utms to list
footerUTMs.forEach((utm, i) => {
newHiddenOutput = document.createElement('span');
newHiddenOutput.innerHTML = `
<textarea id="footer-utm-${i+1}-hidden" class="footer-utm footer-output-${i+1}">${utm}</textarea>`;
hiddenFooterList.appendChild(newHiddenOutput);
});
// append hidden image utms to list
imageUTMs.forEach((utm, i) => {
newHiddenOutput = document.createElement('span');
newHiddenOutput.innerHTML = `
<textarea id="image-utm-${i+1}-hidden" class="image-utm image-output-${i+1}">${utm}</textarea>`;
hiddenImageList.appendChild(newHiddenOutput);
});
// append hidden body utms to list
bodyUTMs.forEach((utm, i) => {
newHiddenOutput = document.createElement('span');
newHiddenOutput.innerHTML = `
<textarea id="body-utm-${i+1}-hidden" class="body-utm body-output-${i+1}">${utm}</textarea>`;
hiddenBodyList.appendChild(newHiddenOutput);
});
// append hidden cta utms to list
ctaUTMs.forEach((utm, i) => {
newHiddenOutput = document.createElement('span');
newHiddenOutput.innerHTML = `
<textarea id="cta-utm-${i+1}-hidden" class="cta-utm cta-output-${i+1}">${utm}</textarea>`;
hiddenCTAList.appendChild(newHiddenOutput);
});
// append hidden sig utms to list
sigUTMs.forEach((utm, i) => {
newHiddenOutput = document.createElement('span');
newHiddenOutput.innerHTML = `
<textarea id="sig-utm-${i+1}-hidden" class="sig-utm sig-output-${i+1}">${utm}</textarea>`;
hiddenSigList.appendChild(newHiddenOutput);
});
displayUTMs();
}
// listeners on button columns - click to copy UTM
const headerCol = document.getElementById('header-column'),
footerCol = document.getElementById('footer-column'),
imageCol = document.getElementById('image-column'),
bodyCol = document.getElementById('body-column'),
ctaCol = document.getElementById('cta-column'),
sigCol = document.getElementById('sig-column');
headerCol.addEventListener('click', (e) => {
e.preventDefault();
let tar = String(e.target.id).charAt(14);
let hiddenList = document.getElementsByClassName('header-utm');
let hiddenArray = Array.from(hiddenList);
hiddenArray.forEach((item, i) => {
if (item.classList.contains(`header-output-${tar}`)) {
let copyTar = document.getElementById(`header-utm-${i+1}-hidden`);
let copyClasses = copyTar.classList;
let copyClass = copyClasses.item(1);
if (copyClass = String(copyTar.id)) {
copyTar.focus({preventScroll:true});
copyTar.select();
document.execCommand('copy');
copyTar.blur();
}
}
// if (navigator.userAgent.indexOf("Firefox") != -1 ) {
// window.scroll(0, 10000);
// }
});
window.scroll(0, 10000);
});
footerCol.addEventListener('click', (e) => {
e.preventDefault();
let tar = String(e.target.id).charAt(14);
let hiddenList = document.getElementsByClassName('footer-utm');
let hiddenArray = Array.from(hiddenList);
hiddenArray.forEach((item, i) => {
if (item.classList.contains(`footer-output-${tar}`)) {
let copyTar = document.getElementById(`footer-utm-${i+1}-hidden`);
let copyClasses = copyTar.classList;
let copyClass = copyClasses.item(1);
if (copyClass = String(copyTar.id)) {
copyTar.focus({preventScroll:true});
copyTar.select();
document.execCommand('copy');
copyTar.blur();
}
}
});
window.scroll(0, 10000);
});
imageCol.addEventListener('click', (e) => {
e.preventDefault();
let tar = String(e.target.id).charAt(13);
let hiddenList = document.getElementsByClassName('image-utm');
let hiddenArray = Array.from(hiddenList);
hiddenArray.forEach((item, i) => {
if (item.classList.contains(`image-output-${tar}`)) {
let copyTar = document.getElementById(`image-utm-${i+1}-hidden`);
let copyClasses = copyTar.classList;
let copyClass = copyClasses.item(1);
if (copyClass = String(copyTar.id)) {
copyTar.focus({preventScroll:true});
copyTar.select();
document.execCommand('copy');
copyTar.blur();
}
}
});
window.scroll(0, 10000);
});
bodyCol.addEventListener('click', (e) => {
e.preventDefault();
let tar = String(e.target.id).charAt(12);
let hiddenList = document.getElementsByClassName('body-utm');
let hiddenArray = Array.from(hiddenList);
hiddenArray.forEach((item, i) => {
if (item.classList.contains(`body-output-${tar}`)) {
let copyTar = document.getElementById(`body-utm-${i+1}-hidden`);
let copyClasses = copyTar.classList;
let copyClass = copyClasses.item(1);
if (copyClass = String(copyTar.id)) {
copyTar.focus({preventScroll:true});
copyTar.select();
document.execCommand('copy');
copyTar.blur();
}
}
});
window.scroll(0, 10000);
});
ctaCol.addEventListener('click', (e) => {
e.preventDefault();
let tar = String(e.target.id).charAt(11);
let hiddenList = document.getElementsByClassName('cta-utm');
let hiddenArray = Array.from(hiddenList);
hiddenArray.forEach((item, i) => {
if (item.classList.contains(`cta-output-${tar}`)) {
let copyTar = document.getElementById(`cta-utm-${i+1}-hidden`);
let copyClasses = copyTar.classList;
let copyClass = copyClasses.item(1);
if (copyClass = String(copyTar.id)) {
copyTar.focus({preventScroll:true});
copyTar.select();
document.execCommand('copy');
copyTar.blur();
}
}
});
window.scroll(0, 10000);
});
sigCol.addEventListener('click', (e) => {
e.preventDefault();
let tar = String(e.target.id).charAt(11);
let hiddenList = document.getElementsByClassName('sig-utm');
let hiddenArray = Array.from(hiddenList);
hiddenArray.forEach((item, i) => {
if (item.classList.contains(`sig-output-${tar}`)) {
let copyTar = document.getElementById(`sig-utm-${i+1}-hidden`);
let copyClasses = copyTar.classList;
let copyClass = copyClasses.item(1);
if (copyClass = String(copyTar.id)) {
copyTar.focus({preventScroll:true});
copyTar.select();
document.execCommand('copy');
copyTar.blur();
}
}
});
window.scroll(0, 10000);
});
// display generated UTM codes in DOM
function displayUTMs() {
// clear out previously displayed buttons
headerDisplay.innerHTML = '';
footerDisplay.innerHTML = '';
imageDisplay.innerHTML = '';
bodyDisplay.innerHTML = '';
ctaDisplay.innerHTML = '';
sigDisplay.innerHTML = '';
// render display column labels
const displayLabels = document.getElementsByClassName('utm-display-label');
for (i=0; i<displayLabels.length; i++) {
displayLabels[i].style.display = 'block';
}
// append header utm buttons to div
headerUTMs.forEach((utm, i) => {
newOutput = document.createElement('div');
newOutput.innerHTML = `
<button class="output-item" id="header-output-${i+1}">${i+1}</button>`;
headerDisplay.appendChild(newOutput);
});
// append footer utm buttons to div
footerUTMs.forEach((utm, i) => {
newOutput = document.createElement('div');
newOutput.innerHTML = `
<button class="output-item" id="footer-output-${i+1}">${i+1}</button>`;
footerDisplay.appendChild(newOutput);
});
// append image utm buttons to div
imageUTMs.forEach((utm, i) => {
newOutput = document.createElement('div');
newOutput.innerHTML = `
<button class="output-item" id="image-output-${i+1}">${i+1}</button>`;
imageDisplay.appendChild(newOutput);
});
// append body utm buttons to div
bodyUTMs.forEach((utm, i) => {
newOutput = document.createElement('div');
newOutput.innerHTML = `
<button class="output-item" id="body-output-${i+1}">${i+1}</button>`;
bodyDisplay.appendChild(newOutput);
});
// append cta utm buttons to div
ctaUTMs.forEach((utm, i) => {
newOutput = document.createElement('div');
newOutput.innerHTML = `
<button class="output-item" id="cta-output-${i+1}">${i+1}</button>`;
ctaDisplay.appendChild(newOutput);
});
// append sig utm buttons to div
sigUTMs.forEach((utm, i) => {
newOutput = document.createElement('div');
newOutput.innerHTML = `
<button class="output-item" id="sig-output-${i+1}">${i+1}</button>`;
sigDisplay.appendChild(newOutput);
});
//SCROLL HERE
}
// clear out generated UTMs
function clearUTMs() {
headerUTMs = [];
footerUTMs = [];
imageUTMs = [];
bodyUTMs = [];
ctaUTMs = [];
sigUTMs = [];
console.log('c');
// hide labels
const displayLabels = document.getElementsByClassName('utm-display-label');
for (i=0; i<displayLabels.length; i++) {
displayLabels[i].style.display = 'none';
}
}