-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsearchfunction.js
118 lines (95 loc) · 3.93 KB
/
searchfunction.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
function search(userinput) {
// Step 1: Remove current results
document.getElementById("images").innerHTML = "";
// Step 2: Make an array for the results
var pictures = new Array();
// Step 3: Call the Rijksmuseum API and add results to array
var request = new XMLHttpRequest();
var url = "https://www.rijksmuseum.nl/api/en/collection?q=" + userinput + "&key=fMLJ55Eu&format=json";
request.open('GET', url, false); // open a new connection, using the GET request on the URL endpoint
request.onload = function () { // begin accessing JSON data here
var data = JSON.parse(this.response);
var objects = data["artObjects"];
var olength = objects.length;
for (i = 0; i < olength; i++){
var result = objects[i];
// Add to Array
if (result.webImage != null) {
var image = {
artist: result["principalOrFirstMaker"],
title: result["title"],
imgurl: result["webImage"]["url"],
weburl: result["links"]["web"],
api: "Rijksmuseum"
};
pictures.push(image);
}
}
}
request.send();
// Step 4: call the Cooper Hewitt API
var request = new XMLHttpRequest();
var url = "https://api.collection.cooperhewitt.org/rest/?method=cooperhewitt.search.objects&access_token=8f0db3bddb24f6ac556ebc4f09995f79&query=" + userinput;
request.open('GET', url, false); // open a new connection, using the GET request on the URL endpoint
request.onload = function () { // begin accessing JSON data here
var data = JSON.parse(this.response);
var alength = data["objects"].length;
if (alength != 0){
for (i = 0; i < alength; i++){
var result = data["objects"][i];
// Add to Array
if (result["images"].length != 0) {
var image = {
artist: null,
title: result["title"],
imgurl: result["images"][0]["z"]["url"],
weburl: result["url"],
api: "Cooper Hewitt"
};
pictures.push(image);
}
}
}
}
request.send();
console.log(pictures);
var plength = pictures.length;
console.log(pictures[0]);
// Step 5: Show results
var imgdiv = document.getElementById("images");
if (plength != 0) {
for (i = 0; i < plength; i++){
console.log(i);
var web = document.createElement("a");
web.href = pictures[i]["weburl"];
web.className = "resultlink";
web.target = "_blank";
var container = document.createElement("div");
container.className = "imgcontainer";
var pic = document.createElement("img");
pic.src = pictures[i]["imgurl"];
pic.className = "image";
pic.target = "_blank";
container.appendChild(pic);
var overlay = document.createElement("div");
overlay.className = "overlay";
var info = document.createElement("div");
info.textContent = pictures[i]["title"];
info.className = "text";
overlay.appendChild(info);
container.appendChild(overlay);
web.appendChild(container);
imgdiv.appendChild(web);
// var info = document.createElement("span");
//
// if (pictures[i]["artist"] == null) {
// web.text = pictures[i]["title"]
// } else {
// web.text = pictures[i]["title"] + " (" + pictures[i]["artist"] + ") "
// };
// imgdiv.appendChild(info);
}
} else {
imgdiv.innerHTML = "No museum objects were found for this query...<br>Try again!";
}
}