-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
117 lines (94 loc) · 4.03 KB
/
script.js
File metadata and controls
117 lines (94 loc) · 4.03 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
117
const navTitle = document.querySelector(".navTitle");
const searchBar = document.querySelector("#searchProjects");
const noProjectMessage = document.querySelector(".noProjectsMessage");
const projectCardContainer = document.querySelector(".project-card");
const projectContainer = document.querySelector(".projects-container");
// Open main page on click over pageTitle
navTitle.addEventListener("click", () => {
window.location.href = "https://alkaison.github.io/Web-Projects";
});
// fetch data from projects.json
async function fetchData(path)
{
// for handloing errors
try {
// fetch projects data and wait for the promise to resolve
const jsonData = await fetch(path);
// if the jsonData rejected then throw error
if(!jsonData.ok)
throw new Error(`HTTP error! Status: ${jsonData.status}`);
// convert the promise to JSON
const data = await jsonData.json();
// return the JSON formatted data
return data;
}
catch(error) {
// throw error to show
throw error;
}
}
// create and append projects cards on body
function getProjects()
{
// fetch data from JSON
fetchData("./projects.json")
.then(data => {
// for each data, create a project card
data.projects.forEach(project => {
// clone the structure of first card
const projectCard = projectCardContainer.cloneNode(true);
// set project title
const pName = projectCard.querySelector(".project-title");
pName.textContent = project.name;
// set project description limited to 110 characters
const pDescription = projectCard.querySelector(".project-description");
const description = project.description;
const limitedText = description.substring(0, 110) + (description.length > 110 ? '...' : '');
pDescription.textContent = limitedText;
// set project url to visit
const pLink = projectCard.querySelector("a");
pLink.href = project.link;
// set project image path
const pImage = projectCard.querySelector(".project-image");
pImage.src = project.image;
// set project difficulty
const pLevel = projectCard.querySelector(".project-difficulty");
pLevel.textContent = project.level;
// lastly add the project card to the project-container
projectContainer.append(projectCard);
});
})
.catch(error => console.log(`Additional information about error: ${error}`));
}
// call for listing the projects
getProjects();
// return the list of projects matching the userInput
const getFilterProjects = () => {
// get value in searchBar
const userInput = searchBar.value.trim().toLowerCase();
let hiddenProjectsCount = 0;
// get all projects from project-container
const projectCardArray = Array.from(projectContainer.querySelectorAll(".project-card"));
// modify the property of project-card
projectCardArray.forEach((project) => {
// get project title
const projectTitle = project.querySelector(".project-title").textContent;
// if the project title matches the pattern of searchBar input value
// display it otherwise hide the project card
if(!projectTitle.toLowerCase().startsWith(userInput))
{
project.classList.add("hide");
hiddenProjectsCount++;
}
else
project.classList.remove("hide");
});
// Show the message when all projects are hidden
// current max projects are 12 in total
if (hiddenProjectsCount === projectCardArray.length)
noProjectMessage.style.display = "flex";
else
noProjectMessage.style.display = "none";
}
// filter the projects as per search string
searchBar.addEventListener("input", getFilterProjects);