-
Notifications
You must be signed in to change notification settings - Fork 0
/
ui.js
108 lines (91 loc) · 3.35 KB
/
ui.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
// This will contain all of the functions that I'll need to work with the DOM
// Will capitalize first letter of string that is passed hound -> Hound
function titleCase(text){
return text[0].toUpperCase() + text.slice(1).toLowerCase();
}
/**
<div id="dropdown-menu" class="dropdown-menu" aria-labelledby="dropdownMenuButton">
<!--
We need to build menu items for all dog breeds that look like this:
<a id="breed-name" class="dropdown-item" href="#">Breed Name</a>
-->
</div>
**/
// This function will be responsible for filling new data within our dropdown menu using the DOM.
function populateBreedDropdown(breeds){
// Using the DOM to access our dropdownMenu
let dropdownMenu = document.querySelector('#dropdown-menu');
// Need a way of looping through all of the records and putting new anchor elements into our dropdownMenu
breeds.forEach( breed =>{
//<a id="breed-name" class="dropdown-item" href="#">Breed Name</a>
let dropdownItem = document.createElement('a');
dropdownItem.id = breed;
dropdownItem.className = 'dropdown-item';
dropdownItem.href = '#'; // set its href to # so it doesn't redirect me anywhere
dropdownItem.innerText = titleCase(breed); // hound -> Hound (how do I capitalize this)
// Add new anchor elements to the DOM (with their id, class, href, and innerText to the dropdown menu
dropdownMenu.appendChild(dropdownItem);
});
}
/**
<!-- Container for our dog images -->
<div class="container">
<!-- Name of the dog breed we are showing -->
<h2 id="breed-name" class="my-3"> </h2>
<!-- Our grid of dog images -->
<div id="breed-grid">
<!-- Example of a Row, Col, Img in our grid -->
<div class="row mt-2">
<div class="col">
<!--
Placeholder image using Bootstrap's responsive image classes:
https://getbootstrap.com/docs/4.0/content/images/#responsive-images
-->
<img
class="img-fluid rounded mx-auto d-block"
src="https://images.dog.ceo/breeds/labrador/n02099712_610.jpg"
>
</div>
</div>
</div>
</div>
*/
// <div class="row mt-2"> ... child ... </div>
function row(child){
let row = document.createElement('div');
row.className = 'row mt-2';
row.appendChild(child);
return row;
}
// <div class="row mt-2"> ... child ... </div>
function col(child){
let col = document.createElement('div');
col.className = 'col';
col.appendChild(child);
return col;
}
/**
<img
class="img-fluid rounded mx-auto d-block"
src="https://images.dog.ceo/breeds/labrador/n02099712_610.jpg"
>
*/
function img(url){
let img = new Image();
img.className = 'img-fluid rounded mx-auto d-block';
img.src = url;
return img;
}
function createGridCell(url){
return row(col(img(url)));
}
function showBreed(breed, images){
// Update the breed title
let breedName = document.querySelector('#breed-name');
breedName.innerText = titleCase(breed);
// Clear the existing images from our breed grid
let breedGrid = document.querySelector('#breed-grid');
breedGrid.innerHTML = '';
// Loop through all the image URLs, create a row/col/img and append to grid
images.forEach(imageUrl => breedGrid.appendChild(createGridCell(imageUrl)));
}