-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
155 lines (128 loc) · 5.12 KB
/
main.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
let page = null; // point to AllPhotographersPage obj
const STICKY_VISIBLE_SCROLL = 100; // number of px on scrolling
// represents a separate photographer's card; allows to show or hide each card in gallery
class Photographer {
parentElem = null; // dom elem which is the parent of photographer's dom elem
elem = null; // dom elem containing photogrpher data
tags = null; // photographer's tags from json (Array)
isOnScreen = false; // true when photographer is on screen
constructor(jsonPhotographerObj, parentElem) {
this.tags = jsonPhotographerObj.tags.slice(); // copy of tags in JSON
this.parentElem = parentElem;
this.elem = document.createElement("div");
this.elem.classList.add("card");
let liTags = "\n";
this.tags.forEach((tag) => {
liTags += `<li class="tag">#${tag}</li>`;
liTags += "\n";
});
this.elem.innerHTML = `<div>
<a href="photographers_pages/profil.html?id=${jsonPhotographerObj.id}">
<img src="photos/Photographers_ID_Photos/${jsonPhotographerObj.portrait}" alt="">
<h2>${jsonPhotographerObj.name}</h2>
</a>
</div>
<p class="location"> ${jsonPhotographerObj.city}, ${jsonPhotographerObj.country} </p>
<p class="motto"> ${jsonPhotographerObj.tagline} </p>
<p class="price"> ${jsonPhotographerObj.price}€/jour </p>
<ul> ${liTags} </ul>`;
}
showInGallery() {
if (!this.isOnScreen) {
this.parentElem.appendChild(this.elem);
this.isOnScreen = true;
}
}
hideInGallery() {
if (this.isOnScreen) {
this.parentElem.removeChild(this.elem);
this.isOnScreen = false;
}
}
checkTag(tag) {
return this.tags.includes(tag);
}
}
// represents the whole page (navigation, all photographer's cards); allows to filter by tag
class AllPhotographersPage {
photographers = []; // Array of Photographer objects
aStickyElem = null; // <a href="header" id="sticky"><p>Passer au contenu</p></a>
selectedNavLiTagElem = null; // currently selected filter for photogrphers, 'none' means show all
constructor(jsonObj) {
const navliElems = document.querySelectorAll("nav ul li.tag");
const cardsSectionElem = document.querySelector("section.cards"); // section elem containing all photographers
const buttonLogoElem = document.querySelector("a.logo-link");
jsonObj.photographers.forEach((ph) =>
this.photographers.push(new Photographer(ph, cardsSectionElem))
); // creates objects in the array photographers
// register nav bar click events
navliElems.forEach((li) => {
li.addEventListener(
"click",
this.onApplyFilterToPhotographers.bind(this)
); // we need bind because of this. inside the callback
li.addEventListener("keyup", (e) => {
if (e.key === "Enter") {
this.onApplyFilterToPhotographers(e);
}
});
});
// register logo click event
buttonLogoElem.addEventListener(
"click",
this.onUndoAnyFiltering.bind(this)
);
// sticky stuff
this.aStickyElem = document.querySelector("#sticky");
this.aStickyElem.style.display =
window.scrollY > STICKY_VISIBLE_SCROLL ? "block" : "none";
// register scroll event to show/hide 'Passer au contenu' link at the top of document
document.addEventListener("scroll", this.onDocScroll.bind(this));
this.showAllPhotographers();
}
// show whole page with navi bar and all photographers
showAllPhotographers() {
this.photographers.forEach((ph) => ph.showInGallery());
}
// remove page leaving only logo
hideAllPhotographers() {
this.photographers.forEach((ph) => ph.hideInGallery());
}
// used to apply tag filter (on click event)
onApplyFilterToPhotographers(e) {
this.selectedNavLiTagElem?.classList.remove("tag--active");
this.hideAllPhotographers();
if (this.selectedNavLiTagElem === e.target) {
this.showAllPhotographers(); // show all
this.selectedNavLiTagElem = null;
} else {
const tag = e.target.textContent.substring(1).toLowerCase();
this.photographers.forEach(
(ph) => ph.checkTag(tag) && ph.showInGallery() // si le 1er est vrai appelle le 2eme
); // filter
e.target.classList.add("tag--active");
this.selectedNavLiTagElem = e.target;
}
}
// undo any filtering and show all photoghraphera (logo click event)
onUndoAnyFiltering() {
if (this.selectedNavLiTagElem !== null) {
this.selectedNavLiTagElem?.classList.remove("tag--active");
this.hideAllPhotographers();
this.showAllPhotographers();
}
}
onDocScroll() {
this.aStickyElem.style.display =
window.scrollY > STICKY_VISIBLE_SCROLL ? "block" : "none";
}
_ESLintFunction() {
console.log("ESLint function");
}
}
fetch("./FishEyeData.json")
.then((response) => response.json())
.then((jsonObj) => {
page = new AllPhotographersPage(jsonObj);
page._ESLintFunction();
});