-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
117 lines (106 loc) · 4.54 KB
/
main.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
// OpenAlex API access
const oa = 'https://api.openalex.org';
async function getTitleSuggestions(searchText) {
const response = await fetch(`${oa}/autocomplete/works?q=${searchText}`);
const data = await response.json();
const suggestions = data.results;
let searchTextMatches = suggestions.filter(suggestion => {
const regex = new RegExp(`^${searchText}`, 'gi');
return suggestion.display_name.match(regex);
});
if(searchText.length === 0) {
searchTextMatches = [];
}
displayTitleSuggestions(searchTextMatches);
}
function displayTitleSuggestions(textMatches) {
const suggestionsDiv = document.querySelector(`.suggestions`);
if (textMatches.length > 0) {
const html = textMatches.map(textMatch => `<li>${textMatch.display_name}</li>`
).join('');
suggestionsDiv.innerHTML = '<ul>' + html + '</ul>';
suggestionsDiv.addEventListener('click', (event) => {
const target = event.target;
if (target.matches('li')) {
titleSearchInput.value = target.innerHTML;
suggestionsDiv.remove();
}
}
);
}
}
async function getOpenAlexID(title) {
const queryString = encodeURIComponent(title);
const apiCall = `${oa}/works?filter=title.search:${queryString}`;
console.log(`Searched for ${apiCall}`);
const response = await fetch(apiCall);
const metadata = await response.json();
openAlexID = metadata.results[0].id;
openAlexID = openAlexID.replace('https://openalex.org/', '');
return openAlexID;
}
async function getRecommendations(event) {
event.preventDefault();
title = document.querySelector('[name=title]').value;
openAlexID = await getOpenAlexID(title);
getRelatedWorks(openAlexID);
getCitedBy(openAlexID);
getReferencedWorks(openAlexID);
}
async function getRelatedWorks(openAlexID) {
const response = await fetch(`${oa}/works?filter=related_to:${openAlexID}`);
const relatedWorksMetadata = await response.json();
const relatedWorksResults = relatedWorksMetadata.results;
const numberOfRelatedWorks = relatedWorksResults.length;
console.log(`Found ${numberOfRelatedWorks} related works`);
if (relatedWorksResults.length > 0) {
loadResultsList(relatedWorksResults, 'Recent works about similar concepts:');
};
}
async function getCitedBy(openAlexID) {
const response = await fetch(`${oa}/works?filter=cites:${openAlexID}`);
const citedByMetadata = await response.json();
const citedByResults = citedByMetadata.results;
const numberOfCitedByResults = citedByResults.length;
console.log(`Found ${numberOfCitedByResults} cited by works`);
if (citedByResults > 0) {
loadResultsList(citedByResults, 'Citations to this work:');
};
}
async function getReferencedWorks(openAlexID) {
const response = await fetch(`${oa}/works?filter=cited_by:${openAlexID}`);
const referencedWorksMetadata = await response.json();
const referencedWorksResults = referencedWorksMetadata.results;
const numberOfReferencedWorksResults = referencedWorksResults.length;
console.log(`Found ${numberOfReferencedWorksResults} referenced works`);
if (referencedWorksResults.length > 0) {
loadResultsList(referencedWorksResults, 'Works listed in the References section:');
};
}
function loadResultsList(works, label) {
const container = document.querySelector('main');
const footer = document.querySelector('footer');
const resultsCard = document.createElement('article');
container.insertBefore(resultsCard, footer);
const header = document.createElement('header');
resultsCard.appendChild(header);
const heading = document.createElement('h3');
header.appendChild(heading);
heading.textContent = `${label}`;
const resultsListContainer = document.createElement('ul');
resultsCard.appendChild(resultsListContainer);
let resultsList = '';
for (const work of works) {
resultsList += `<li><a href="${work.doi}">${work.display_name}</a> (${work.publication_year})</li>`;
}
resultsListContainer.innerHTML = resultsList;
}
// EVENT LISTENERS
// listen for institution searching => make suggestions from autocomplete endpoint
const titleSearchInput = document.querySelector('[name=title]');
titleSearchInput.addEventListener('input', () => {
getTitleSuggestions(titleSearchInput.value);
});
// listen for form submission => query OpenAlex dataset
const searchForm = document.querySelector('[name=search]');
searchForm.addEventListener('submit', getRecommendations);