-
Notifications
You must be signed in to change notification settings - Fork 3
/
wallpaper.js
109 lines (89 loc) · 2.95 KB
/
wallpaper.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
let userdata = JSON.parse(localStorage.getItem("user-info"));
console.log(userdata);
if (userdata != null) {
console.log(userdata)
document.getElementById("user_name").innerHTML = userdata.user;
document.getElementById("login_user").style.display = "block";
document.getElementById("logout_user").style.display = "none";
}
document.getElementById("user").addEventListener("click", () => {
document.getElementById("user_popup").style.display = "block"
})
document.getElementById("logout").addEventListener("click", () => {
localStorage.setItem("user-info", JSON.stringify({}));
})
document.querySelector(".search_header").addEventListener("input", () => {
deboubcing(getData, 2000);
})
//---------------------------------------------------------------------------------
let id;
let searchval;
const apiKey = `aBSiI0ek7J1gkMe2xa8JkLlHdB9UlqUBDNFjUnYMWtg`;
const url = `https://api.unsplash.com/search/photos?client_id=${apiKey}&query=wallpapers&per_page=30`;
let curr = 0;
let imgData;
const container = document.getElementById("content");
const deboubcing = (fn, delay) => {
if (id != undefined) {
clearTimeout(id);
}
id = setTimeout(() => {
searchval = document.querySelector(".search_header").value;
const searchurl = `https://api.unsplash.com/search/photos?client_id=${apiKey}&query=${searchval}&per_page=30`;
console.log(searchval)
fn(searchurl);
}, delay)
}
const getData = async (fetchurl) => {
console.log(fetchurl)
let rem = await fetch(fetchurl);
let data = await rem.json();
console.log(data);
// if (searchval == undefined) {
// imgData = data.re;
// } else {
imgData = data.results
// }//
display(imgData);
}
const display = (data) => {
container.innerHTML = null;
data.map((ele, i) => {
let div = document.createElement("div");
let img = document.createElement("img");
img.src = ele.urls.regular;
div.id = 'card_img';
img.onclick = () => {
curr = i;
popupData(ele);
}
div.append(img)
container.append(div);
})
}
const popupData = (ele) => {
// console.log("yes");
let popu = document.getElementById("popu");
const crossbtn = document.querySelector('.button_c');
const download = document.querySelector(".download");
let img = document.querySelector('.download_img');
popu.classList.remove('hide');
download.href = ele.links.download_location;
img.src = ele.urls.regular;
crossbtn.addEventListener("click", () => {
popu.classList.add('hide');
})
}
document.querySelector(".prev_button").addEventListener("click", () => {
if (curr > 0) {
curr--;
popupData(imgData[curr]);
}
});
document.querySelector(".next_button").addEventListener("click", () => {
if (curr < imgData.length - 1) {
curr++;
popupData(imgData[curr]);
}
});
getData(url);