-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
157 lines (114 loc) · 3.76 KB
/
index.js
File metadata and controls
157 lines (114 loc) · 3.76 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
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
// declaration of variables
const navbar = document.getElementById("nav");
const brandName = document.getElementById("brand");
const searchKey = document.getElementById("searchKey");
const searchBtn = document.getElementById("searchBtn");
const searchQuery = document.getElementById("searchQuery");
const column1 = document.getElementById("col-1");
const column2 = document.getElementById("col-2");
const column3 = document.getElementById("col-3");
const errorGrid = document.getElementById("errorGrid");
const modalBody = document.getElementById("modalBody");
const imageViewLink = document.getElementById("imageViewLink");
var orderByValue = '';
// APIs.
API_KEY = "uBKIG1jkH0aEymy0Mvjx_X0Ii0LbuzAhIrGy6Xm6HnE";
apiUrl = "https://api.unsplash.com/photos/?client_id="+API_KEY+"&per_page=300";
searchUrl = "https://api.unsplash.com/search/photos/?client_id="+API_KEY+"&query=";
imageURLS = [];
window.onload = (event) => {
fetchData();
}
const fetchData = async () => {
var tempUrl = apiUrl;
if(orderByValue != '') {
tempUrl += ('&order_by='+orderByValue);
}
const response = await (fetch(apiUrl).catch(handleError));
const myJson = await response.json();
var imageArrays = myJson;
imageArrays.forEach(element => {
imageURLS.push(element.urls.small);
});
displayImage();
}
var handleError = function(err) {
console.warn(err);
errorGrid.innerHTML = "<h4>Unable to fetch data "+err+"</h5>";
}
function displayImage() {
errorGrid.innerHTML = "";
if(imageURLS.length == 0) {
errorGrid.innerHTML = "<h4>Unable to fetch data.</h5>";
return;
}
column1.innerHTML = "";
column2.innerHTML = "";
column3.innerHTML = "";
imageURLS.forEach((url,index) => {
// dynamic image tag
var image = document.createElement('img');
image.src = url;
image.className="pt-4";
image.setAttribute("width", "100%");
image.setAttribute("onclick","displayFullImage(this.src)");
if( (index + 1) % 3 == 0 ) {
column1.appendChild(image);
}
if( (index + 2) % 3 == 0 ) {
column2.appendChild(image);
}
if( (index + 3) % 3 == 0 ) {
column3.appendChild(image);
}
});
}
function displayFullImage(src) {
// dynamic image tag
var image = document.createElement('img');
image.src = src;
image.className="mt-3";
image.setAttribute("width", "100%");
modalBody.innerHTML = "";
modalBody.appendChild(image);
imageViewLink.href=src;
var myModal = new bootstrap.Modal(document.getElementById('modal'), {});
myModal.show();
}
searchBtn.addEventListener("click",function() {
if(searchKey.value != ''){
fetchSearchData(searchKey.value);
}
});
const fetchSearchData = async (key) => {
imageURLS = [];
var orderbyvar = orderByValue;
var tempUrl = searchUrl + key;
if(orderbyvar != '') {
tempUrl += ("&order_by="+orderbyvar);
}
searchQuery.innerHTML = searchKey.value;
let response = await (fetch(tempUrl).catch(handleError));
let myJson = await response.json();
tempUrl += "&per_page="+myJson.total;
response = await (fetch(tempUrl).catch(handleError));
myJson = await response.json();
console.log(myJson);
var imageArrays = myJson.results;
imageArrays.forEach(element => {
imageURLS.push(element.urls.regular);
});
displayImage();
}
function orderBy() {
orderByValue = document.getElementById("orderby").value;
imageURLS = [];
if(searchKey.value != '') {
fetchSearchData(searchKey.value);
}else {
fetchData();
}
}
document.getElementById("pagination").addEventListener("select.uk.pagination",function(e,page){
alert(page);
})