-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
115 lines (91 loc) · 3.94 KB
/
script.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
document
.querySelector("#vacayWishListForm")
.addEventListener("submit", handleFormSubmit);
function handleFormSubmit(event) {
// Function to handle the #destination_details_form being submitted
event.preventDefault(); // stop the form from refreshing the page
// Extract the values of the different elements of the form and store them in variables
var destinationName = event.target.elements["inputDestination"].value;
var destinationLocation = event.target.elements["inputLocation"].value;
var destinationPhoto = event.target.elements["inputPhoto"].value;
var destinationDesc = event.target.elements["inputDescription"].value;
// Reset the form elements values for a new entry
resetFormValues(event.target);
// Use the form elements values to create a destination card
var destinationCard = createDestinationCard(
destinationName,
destinationLocation,
destinationPhoto,
destinationDesc
);
var wishListContainer = document.querySelector("#destinations_container");
// Change wishlist title if the wishlist was empty
if (wishListContainer.children.length === 0) {
document.querySelector("#title").innerHTML = "My WishList";
}
// Appended the destinationCard in the #destinations_container div
document
.querySelector("#destinations_container")
.appendChild(destinationCard);
}
function resetFormValues(form) {
// Go through all the form values and reset their values
for (var i = 0; i < form.length; i++) {
form.elements[i].value = "";
}
}
function createDestinationCard(name, location, photoUrl, description) {
// Use the passed arguments to create a bootstrap card with destination details
var card = document.createElement("div");
card.setAttribute("class", "card");
card.style.width = "15rem";
card.style.height = "fit-content";
card.style.margin = "20px;";
// Create the destination photo element and append it to the card
var img = document.createElement("img");
img.setAttribute("class", "card-img-top");
img.setAttribute("alt", name);
// Check to make sure that the photo url was entered since it's optional
// if the user didn't enter a photo url, show a constant photo
var constantPhotoUrl =
"https://cavchronicle.org/wp-content/uploads/2018/03/top-travel-destination-for-visas-900x504.jpg";
if (photoUrl.length === 0) {
img.setAttribute("src", constantPhotoUrl);
} else {
img.setAttribute("src", photoUrl);
}
card.appendChild(img);
// Create the card body with the destination name, location, and description and append it to the card
var cardBody = document.createElement("div");
cardBody.setAttribute("class", "card-body");
var cardTitle = document.createElement("h5");
cardTitle.setAttribute("class", "card-title");
cardTitle.innerText = name;
cardBody.appendChild(cardTitle);
var cardSubtitle = document.createElement("h6");
cardSubtitle.setAttribute("class", "card-subtitle mb-2 text-muted");
cardSubtitle.innerText = location;
cardBody.appendChild(cardSubtitle);
// Only add description text if the user entered some
if (description.length !== 0) {
var cardText = document.createElement("p");
cardText.setAttribute("class", "card-text");
cardText.innerText = description;
cardBody.appendChild(cardText);
}
var buttonsContainer = document.createElement("div");
buttonsContainer.setAttribute("class", "buttons_container");
var cardEditBtn = document.createElement("button");
cardEditBtn.setAttribute("class", "btn btn-warning");
cardEditBtn.innerText = "Edit";
// cardEditBtn.addEventListener("click", editDestination);
var cardDeleteBtn = document.createElement("button");
cardDeleteBtn.setAttribute("class", "btn btn-danger");
cardDeleteBtn.innerText = "Remove";
// cardDeleteBtn.addEventListener("click", removeDestination);
buttonsContainer.appendChild(cardEditBtn);
buttonsContainer.appendChild(cardDeleteBtn);
cardBody.appendChild(buttonsContainer);
card.appendChild(cardBody);
return card;
}