-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
169 lines (133 loc) · 5.19 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
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
var str;
var imageIndex;
function init() {
var searchTerms = ["Youghal", "Cork", "Henry Pether", "Oak"];
if(localStorage.getItem("local-data") !== null) {
getData(localStorage.getItem("local-data"));
} else {
getData();
console.log("localStorage is not working!");
}
for (var i = 0; i < searchTerms.length; i++) {
var newB = document.createElement("button");
var bText = document.createTextNode(searchTerms[i]);
newB.appendChild(bText);
document.getElementById("header").appendChild(newB);
newB.onclick = (function(nr) {
return function() {
getData(searchTerms[nr]);
};
})(i);
}
}
function getData(element) {
send_JSONP_Request("https://www.flickr.com/services/rest/?method=flickr.photos.search&per_page=100&api_key=a805174f56d23d939a425204dad80deb&tags=" + element + "&format=json&tag_mode=all&jsoncallback=getImage");
str = document.getElementById("container");
var message = document.createElement("div");
message.className = "msg";
str.appendChild(message);
localStorage.setItem("local-data", element);
}
function getImage(images) {
str = document.getElementById("container");
while(str.hasChildNodes()) {
str.removeChild(str.lastChild);
}
for (var i = 0; i < images.photos.photo.length; i++) {
var urlSmall = "http://farm" + images.photos.photo[i].farm + ".static.flickr.com/" + images.photos.photo[i].server + "/" + images.photos.photo[i].id + "_" + images.photos.photo[i].secret + "_s.jpg";
var urlBig = "http://farm" + images.photos.photo[i].farm + ".static.flickr.com/" + images.photos.photo[i].server + "/" + images.photos.photo[i].id + "_" + images.photos.photo[i].secret + "_z.jpg";
imageIndex = i;
var image = document.createElement("img");
image.id = imageIndex;
image.className = "image-box thumbnail";
image.src = urlSmall;
image.dataset.bigurl = urlBig;
document.getElementById("container").appendChild(image);
var string = "showModal('" + imageIndex + "');";
var newImage = document.getElementById(imageIndex);
newImage.setAttribute("onclick", string);
}
}
function showModal(elementId) {
var choosenImage = document.getElementById(elementId);
var modal = document.createElement("div");
modal.setAttribute("id", "modal");
modal.setAttribute("class", "modal");
var modalContent = document.createElement("div");
modalContent.setAttribute("class", "modal-content");
var modalImage = document.createElement("img");
modalImage.setAttribute("id", "modal-image");
modalImage.src = choosenImage.dataset.bigurl;
var modalX = document.createElement("button");
modalX.setAttribute("class", "close");
modalX.setAttribute("id", "closebutton");
modalX.textContent = "X";
modalX.onclick = function () {
document.body.removeChild(document.body.lastChild);
imageIndex = null;
};
var modalNavLeft = document.createElement("div");
modalNavLeft.setAttribute("class", "modalNavLeft");
modalNavLeft.innerText = "<";
modalNavLeft.onclick = function() {
left(elementId);
};
var modalNavRight = document.createElement("div");
modalNavRight.setAttribute("class", "modalNavRight");
modalNavRight.innerText = ">";
modalNavRight.onclick = function() {
right(elementId);
};
modal.appendChild(modalNavLeft);
modal.appendChild(modalNavRight);
modal.appendChild(modalContent);
modalContent.appendChild(modalX);
modalContent.appendChild(modalImage);
modal.style.display = "block";
document.body.appendChild(modal);
function left(element) {
var image = document.getElementById("modal-image");
image.remove();
var img = document.createElement("img");
img.setAttribute("id", "modal-image");
var previousId = parseInt(element);
previousId = parseInt(previousId) - 1;
imageIndex = previousId;
var previousImage = document.getElementById(previousId);
img.src = previousImage.dataset.bigurl;
modalContent.appendChild(img);
}
function right(element) {
var image = document.getElementById("modal-image");
image.remove();
var img = document.createElement("img");
img.setAttribute("id", "modal-image");
var previousId = parseInt(element);
previousId = parseInt(previousId) + 1;
imageIndex = previousId;
var previousImage = document.getElementById(previousId);
img.src = previousImage.dataset.bigurl;
modalContent.appendChild(img);
}
function keyboard(e)
{
if (e.which == 37) {
left(imageIndex);
}
else if (e.which == 39) {
right(imageIndex);
}
else {
console.log('Press any key');
}
}
window.addEventListener("keyup", keyboard);
}
function send_JSONP_Request(request) {
var newScript = document.createElement('script');
newScript.setAttribute('src', request);
document.getElementsByTagName('head')[0].appendChild(newScript);
}
window.onload = function() {
init();
};