Skip to content

Commit ff579f6

Browse files
authored
Merge pull request #27 from nbonfils/topic
Add topic
2 parents 8cebeea + e2d5a9a commit ff579f6

File tree

3 files changed

+83
-2
lines changed

3 files changed

+83
-2
lines changed

src/index.html

+40-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ <h1 class="center" >Bibliograph 2</h1>
8484
<option value="title" >Search in title</option>
8585
<option value="titleabs" >Search in title and abstract</option>
8686
<option value="titleabsfull" >Search in title, abstract and full text</option>
87-
<option value="concept" >Filter by concept</option>
87+
<option value="concept" >Filter by concepts</option>
88+
<option value="topic" >Filter by topics</option>
8889
</select>
8990
<button type="button" @click.prevent="params.splice(index, 1)">X</button>
9091

@@ -161,6 +162,44 @@ <h1 class="center" >Bibliograph 2</h1>
161162
x-model="param.op" />
162163
<label :for="`radio-and-${index}`" >AND</label>
163164

165+
<ul :class="param.op === 'or' ? 'concept-list-or' : 'concept-list-and'" class="concept-list">
166+
<template x-for="(concept, i) in param.concepts">
167+
<li>
168+
<span class="card">
169+
<span x-text="concept.display_name"></span>
170+
|
171+
<a href="#" @click.prevent='param.concepts.splice(i, 1)'>x</a>
172+
</span>
173+
</li>
174+
</template>
175+
</ul>
176+
</div>
177+
</template>
178+
<template x-if="param.type === 'topic'">
179+
<div x-data="{ ac: null, str: '' }"
180+
x-init="param.concepts = param.concepts || [];
181+
param.op = param.op || 'or';
182+
$nextTick(() => ac = new autoComplete({selector: `#search-${index}`, ...autoCompleteTopicConfig}))">
183+
<input :id="`search-${index}`"
184+
class="card w-100"
185+
type="search"
186+
x-model="str"
187+
@selection="param.concepts.push($event.detail.selection.value); str='';" />
188+
<input :id="`radio-or-${index}`"
189+
:name="`radio-${index}`"
190+
class="concept-radio-or"
191+
type="radio"
192+
value="or"
193+
x-model="param.op" />
194+
<label :for="`radio-or-${index}`" >OR</label>
195+
<input :id="`radio-and-${index}`"
196+
:name="`radio-${index}`"
197+
class="concept-radio-and"
198+
type="radio"
199+
value="and"
200+
x-model="param.op" />
201+
<label :for="`radio-and-${index}`" >AND</label>
202+
164203
<ul :class="param.op === 'or' ? 'concept-list-or' : 'concept-list-and'" class="concept-list">
165204
<template x-for="(concept, i) in param.concepts">
166205
<li>

src/index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@ import './index.css';
55
import Alpine from 'alpinejs';
66
import autoComplete from '@tarekraafat/autocomplete.js';
77

8-
import { autoCompleteConceptConfig } from './lib/autocomplete.js';
8+
import { autoCompleteConceptConfig, autoCompleteTopicConfig } from './lib/autocomplete.js';
99
import { fetchWorks } from './lib/fetch.js';
1010
import { processWorks, getFilters, filterData, generateJSONDataURL } from './lib/processing.js';
1111
import { generateGraph, generateGexfURL } from './lib/graph.js';
1212

1313
window.autoCompleteConceptConfig = autoCompleteConceptConfig;
14+
window.autoCompleteTopicConfig = autoCompleteTopicConfig;
1415
window.fetchWorks = fetchWorks;
1516
window.processWorks = processWorks;
1617
window.getFilters = getFilters;

src/lib/autocomplete.js

+41
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,44 @@ export const autoCompleteConceptConfig = {
3636
}
3737
},
3838
};
39+
40+
export const autoCompleteTopicConfig = {
41+
placeHolder: 'Search for Topics...',
42+
data: {
43+
src: async (query) => {
44+
try {
45+
const response = await fetch(
46+
'https://api.openalex.org/autocomplete/topics?' + new URLSearchParams({
47+
q: query,
48+
mailto: `****@****.com`,
49+
}));
50+
if (!response.ok) {
51+
throw new Error('Network response was not OK');
52+
}
53+
const data = await response.json();
54+
return data.results;
55+
} catch (e) {
56+
console.error(`Error while fetching topics to autocomplete:\n\t${e}`);
57+
return e;
58+
}
59+
},
60+
keys: ['display_name'],
61+
cache: false,
62+
},
63+
debounce: 300,
64+
resultItem: {
65+
tabSelect: true,
66+
noResults: true,
67+
highlight: true,
68+
},
69+
events: {
70+
input: {
71+
navigate: (event) => {
72+
const selection = event.detail.selection.value;
73+
event.target.value = selection.display_name;
74+
}
75+
}
76+
},
77+
};
78+
79+

0 commit comments

Comments
 (0)