-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphoto-grid.js
183 lines (160 loc) · 5.35 KB
/
photo-grid.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
var apiError;
var currentPhotoIndex;
var defaultNumPhotos = 12;
var defaultSearch = "seleccion Colombia mundial";
var flickrKey = "your-flickr-key-here";
var instructions;
var keyCodes = {LEFT_ARROW: 37, RIGHT_ARROW: 39, ESC: 27};
var lightboxContainer;
var noPhotos;
var photosInfo = {};
var resultNumPhotos;
var searchInput;
var updateLightBox = function (direction) {
var photo;
var index;
if ("left" === direction) {
// If current photo is the first one, and direcion is left, display the
// last photo next
index = currentPhotoIndex - 1;
if (index < 0) {
index = resultNumPhotos - 1;
}
} else {
// If current photo is the last one, and direction is right, display the
// first photo next
index = currentPhotoIndex + 1;
if (index >= resultNumPhotos) {
index = 0;
}
}
photo = photosInfo[index];
showLightbox(photo);
};
var onArrowClick = function (event) {
event.stopPropagation();
updateLightBox(event.target.name);
};
var togglePhoto = function (event) {
event.stopPropagation();
if (event.which === keyCodes.LEFT_ARROW) {
updateLightBox("left");
} else if (event.which === keyCodes.RIGHT_ARROW) {
updateLightBox("right");
}
};
var closeLightbox = function (event) {
event.stopPropagation();
if(event.target.id == "close" ||
event.target.id == "lightboxContainer" ||
event.which == keyCodes.ESC) {
lightboxContainer.style.display = "none";
}
};
var showLightbox = function (photo) {
document.getElementById("photoImage").src = photo.src;
document.getElementById("photoTitle").innerHTML = photo.title;
// Update current photo index in lightbox
currentPhotoIndex = photo.index;
};
var onThumbnailClick = function (event) {
lightboxContainer.style.display = "block";
showLightbox(event.target);
};
var initiThumbnails = function (thumbnailContainer) {
var thumbnails = thumbnailContainer.childNodes;
for (var i = 0; i < thumbnails.length; ++i) {
thumbnails[i].addEventListener("click", onThumbnailClick, false);
}
};
var buildPhotoUrl = function (photoInfo) {
return "https://farm" + photoInfo.farm + ".staticflickr.com/" +
photoInfo.server + "/" + photoInfo.id + "_" + photoInfo.secret + ".jpg";
};
var createThumbnails = function (photos) {
// Update total number of photos from current search results
resultNumPhotos = photos.length;
// Reset photos info object
photosInfo = {};
for (var i = 0; i < resultNumPhotos; i++) {
var thumbnail = document.createElement("LI");
var thumbnailImage = document.createElement("IMG");
var title = photos[i].title;
var src = buildPhotoUrl(photos[i]);
thumbnail.className = "thumbnail";
thumbnailImage.id = photos[i].id;
thumbnailImage.className = "thumbnail_photo";
thumbnailImage.src = src;
thumbnailImage.title = title;
thumbnailImage.index = i;
thumbnail.appendChild(thumbnailImage);
thumbnails.appendChild(thumbnail);
// Add photo info to photos info object
photosInfo[i] = {title: title, src: src, index: i};
}
return thumbnails;
};
var resetThumbnails = function () {
var thumbnails = document.getElementById("thumbnails");
while (thumbnails.firstChild) {
thumbnails.removeChild(thumbnails.firstChild);
}
};
var displayPhotos = function (data) {
resetThumbnails();
instructions.style.display = "none";
if (data.stat !== "ok") {
// API error
apiError.style.display = "block";
} else if (data.photos.photo.length === 0) {
// No photos found with that search criteria
noPhotos.style.display = "block";
} else {
apiError.style.display = "none";
noPhotos.style.display = "none";
// Photos successfuly retrieved from the API, so create thumbnails
var thumbnails = createThumbnails(data.photos.photo);
initiThumbnails(thumbnails);
instructions.style.display = "block";
}
};
var search = function () {
var searchText = searchInput.value;
if (searchText.length > 0) {
getPhotos(searchText);
}
};
var getPhotos = function (searchTerm) {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
var data = JSON.parse(xmlhttp.responseText);
displayPhotos(data);
}
};
var url = "https://api.flickr.com/services/rest/?method=flickr.photos.search" +
"&api_key="+ flickrKey + "&text=" + searchTerm +
"&page=1&per_page=" + defaultNumPhotos + "&format=json&nojsoncallback=1";
xmlhttp.open("GET", url, true);
xmlhttp.send();
};
var initLightBox = function () {
lightboxContainer.addEventListener('click', closeLightbox, false);
document.getElementById("close").addEventListener('click', closeLightbox, false);
window.addEventListener("keydown", closeLightbox, false);
window.addEventListener("keydown", togglePhoto, false);
var arrows = document.querySelectorAll("img.arrow");
for (var i = 0; i < arrows.length; ++i) {
arrows[i].addEventListener("click", onArrowClick, false);
}
};
window.onload = function () {
// Cache HTML elements that are used more than once
apiError = document.getElementById("apiError");
instructions = document.getElementById("thumbnailInstructions");
lightboxContainer = document.getElementById("lightboxContainer");
noPhotos = document.getElementById("noPhotos");
searchInput = document.getElementById("searchInput");
initLightBox();
getPhotos(defaultSearch);
};