-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
119 lines (104 loc) · 3.96 KB
/
index.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
$(document).ready(() => {
/* CARDS */
const $frontCard = $('#front-card');
const $backCard = $('#back-card');
/* FRONT CARD INPUTS */
const $imgInput = $('#image-input');
const $nameInput = $('#name-input');
const $signInput = $('#signature-input');
const $imgPicker = $('#image-picker');
const $datePicker = $('#date-picker');
/* BUTTONS */
const $downloadButton = $('#download-button');
const $reverseButton = $('#reverse-card-button');
// Setting dynamically maximum date available.
$datePicker.attr('max', new Date().toISOString().split('T')[0]);
/* EVENT BINDERS */
$('#name-input').on('input', (e) => $signInput.val(e.target.value));
$reverseButton.on('click', () => {
if ($backCard.hasClass('md:opacity-0')) {
$backCard.removeClass('md:opacity-0');
$frontCard.addClass('opacity-0');
// We add and remove the z-index to not being able to write when we show the other part.
$frontCard.removeClass('z-50');
} else {
$frontCard.removeClass('opacity-0');
$frontCard.addClass('z-50');
$backCard.addClass('md:opacity-0');
}
});
$imgPicker.on('click', () => $imgInput.trigger('click'));
$downloadButton.on('click', () => {
const userName = $nameInput.val().trim().toLowerCase().replace(/\s+/g, '-');
const opt = {
/* margin per side (mm) = (page width - card size)/2
margin per side = (210 - 160)/2 = 25mm */
margin: [10, 25, 0, 0],
filename: userName + '.pdf',
jsPDF: { format: 'a4' },
};
// * We need to do this in VanillaJS since the html2pdf package requires it.
const main = document.createElement('main');
const space = document.createElement('br');
// Making a deep copy of the nodes to not modify the original ones.
const frontCardCopy = document.querySelector('#front-card').cloneNode(true);
const backCardCopy = document.querySelector('#back-card').cloneNode(true);
frontCardCopy.classList.remove('md:absolute');
backCardCopy.classList.remove('md:absolute');
backCardCopy.classList.contains('md:opacity-0')
? backCardCopy.classList.remove('md:opacity-0')
: frontCardCopy.classList.remove('opacity-0');
main.appendChild(frontCardCopy);
main.appendChild(space);
main.appendChild(space);
main.appendChild(backCardCopy);
html2pdf()
.from(main)
.set(opt)
.save()
.catch((e) => console.log(e));
});
$datePicker.on('change', (e) => {
/* If the input date is bigger than today's we force the user to input at least another year.
**Does not cover edge cases like invalid dates. */
if (Date.now() < +new Date(e.target.value)) {
const noYearDate = e.target.value.split('-').slice(1).join('-');
$datePicker.val('1980-' + noYearDate);
}
});
/* Function to read the selected image and place it in the background.
Source: https://stackoverflow.com/a/12368976. */
$imgInput.on('change', (e) => {
const file = e.originalEvent.srcElement.files[0];
const reader = new FileReader();
reader.onloadend = function () {
$imgPicker.css('background-image', 'url(' + reader.result + ')');
};
reader.readAsDataURL(file);
});
// Disable hover in mobile devices
if (screen.width > 640) {
$imgPicker.hover(
() => {
/* Since the parameter 'now' from the step function is inaccurate we have created
this 'animationStep' counter that will force the 'slideDown' method to be called only once. */
let animationStep = 0;
$('#upload-container').animate(
{ height: 'show' },
{
duration: 300,
step: () => {
animationStep == 10 && $('#upload-header').slideDown('fast');
animationStep++;
},
}
);
},
() => {
$('#upload-header').hide();
// The 'stop' method removes all the animations on the given element.
$('#upload-container').stop(true).animate({ height: 'hide' });
}
);
}
});