-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.js
More file actions
116 lines (95 loc) · 4.22 KB
/
common.js
File metadata and controls
116 lines (95 loc) · 4.22 KB
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
///--------------------------------------------------------------
// Navigation bar & footer
//--------------------------------------------------------------
// Create the navigation bar
import {navBarHtml} from "./commonData.js"
const navBarElement = document.getElementById('header-element')
navBarElement.innerHTML = navBarHtml
// Create the footer
import { footerHtml } from "./commonData.js"
const footerElement = document.getElementById('footer-element')
footerElement.innerHTML = footerHtml
///--------------------------------------------------------------
// User interaction
//--------------------------------------------------------------
function getIdsFromClassName(className){
let ids = []
let elements = document.getElementsByClassName(className)
for (let element of elements){
ids.push(element.id)
}
return ids
}
function setChevronFromModal(chevronElement, modalClassList){
// If modal displayed, chevron up. Else, chevron down.
if (modalClassList.contains('modal-displayed')){
chevronElement.classList.add('fa-chevron-up')
chevronElement.classList.remove('fa-chevron-down')
}
else{
chevronElement.classList.add('fa-chevron-down')
chevronElement.classList.remove('fa-chevron-up')
}
}
function closeNavbarModals(){
let navbarModalIds = getIdsFromClassName('navbar-modal')
for (let id of navbarModalIds){
document.getElementById(id).classList.remove('modal-displayed')
}
// Effect on chevrons (all down)
let navbarChevronIds = getIdsFromClassName('navbar-chevron')
for (let id of navbarChevronIds){
document.getElementById(id).classList.remove('fa-chevron-up')
document.getElementById(id).classList.add('fa-chevron-down')
}
}
function closeEmailModals(){
let emailModalIds = getIdsFromClassName('modal-email')
for (let id of emailModalIds){
document.getElementById(id).classList.remove('modal-email-displayed')
}
}
document.addEventListener('click', function(e){
let navbarItemIds = getIdsFromClassName('navbar-mainitem')
let navbarItemChevronIds = getIdsFromClassName('navbar-chevron')
let emailItemIds = getIdsFromClassName('contact-email')
// Click on navigation bar items and associated chevrons
if (navbarItemIds.includes(e.target.id) || navbarItemChevronIds.includes(e.target.id)){
// Navbar modal clicked : toggle display
let assId = e.target.id.replace('navbar-item-', '').replace('-more', '')
let assModalClassList = document.getElementById('modal-' + assId).classList
assModalClassList.toggle('modal-displayed')
setChevronFromModal(document.getElementById('navbar-item-'+ assId + '-more'), assModalClassList)
// Other navbar modals : never displayed
let otherNavbarItemIds
if (navbarItemIds.includes(e.target.id)){
otherNavbarItemIds = navbarItemIds.filter(function(id){return id != e.target.id})
}
else {
otherNavbarItemIds = navbarItemChevronIds.filter(function(id){return id != e.target.id})
}
for (let id of otherNavbarItemIds){
let otherId = id.replace('navbar-item-', '').replace('-more', '')
try{
let otherModalClassList = document.getElementById('modal-' + otherId).classList
otherModalClassList.remove('modal-displayed')
setChevronFromModal(document.getElementById('navbar-item-' + otherId + '-more'), otherModalClassList)
}
catch{}
}
}
// Click on the email buttons : toggle display
else if (emailItemIds.includes(e.target.id)){
let assId = e.target.id.replace('-contact-email', '')
document.getElementById(assId+'-modal-email').classList.toggle('modal-email-displayed')
}
// Close the email modal via close button
else if (e.target.classList.contains("modal-email-closebtn")){
document.getElementById(e.target.parentElement.parentElement.id).classList.remove('modal-email-displayed')
}
// Close all modals if click outside any modal
else if (!Boolean(e.target.id) || !Boolean(document.getElementById(e.target.id).closest('.modals'))) {
closeNavbarModals()
closeEmailModals()
}
})