-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
79 lines (69 loc) · 2 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
class Modals {
constructor()
{
this.init();
}
/**
* Inizializzo le variabili e gli eventi
*/
init()
{
this._modals = document.querySelectorAll('.modal');
this._modalsLinks = document.querySelectorAll('[data-open]');
this._modals.forEach( (modal) => {
this.createModalsListeners(modal);
} );
this._modalsLinks.forEach( link => {
this.createAnchorsListeners(link);
});
}
/**
* Aggiungi gli eventi di chiusura del modal
* @param modal
*/
createModalsListeners(modal)
{
const modalButton = modal.getElementsByClassName('modal__button')[0];
modal.addEventListener('click', this.closeModal, true);
if (typeof modalButton !== 'undefined')
modalButton.addEventListener('click', this.closeModal, true);
}
/**
* Aggiungo gli eventi ai link di apertura
* @param link
*/
createAnchorsListeners(link)
{
link.addEventListener('click', this.openModal, true);
}
/**
* Apro il modale
* @param event
*/
openModal(event)
{
event.preventDefault();
const id = event.target.getAttribute('data-open');
const modal = document.getElementById(id);
document.getElementsByTagName('body')[0].classList.add('overflow-hidden');
modal.classList.add('is-visible');
modal.classList.remove('hidden');
}
/**
* Chiudo il modale se clicco nell'overlay o nel tasto di chiusura
* @param event
*/
closeModal(event)
{
event = window.event || event;
if (this === event.target) {
document.getElementsByTagName('body')[0].classList.remove('overflow-hidden');
if (event.target.classList.contains('modal__button') ) {
this.parentNode.parentNode.classList.remove('is-visible')
} else {
this.classList.remove('is-visible');
}
}
}
}
export default Modals;