Skip to content

Commit 8c73758

Browse files
committed
Show total number of hits returned for entity search
1 parent ed9b44f commit 8c73758

8 files changed

Lines changed: 96 additions & 47 deletions

File tree

README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ The extension supports various authorities to query for entities:
4747
1. Karl Barth Archiv, Basel
4848
2. Metagrid
4949
3. Google Places
50+
4. GND
5051

5152
You can define a different connector for each entity type. The configuration is a JSON snippet like below:
5253

@@ -69,4 +70,13 @@ You can define a different connector for each entity type. The configuration is
6970
"plugin": "kba"
7071
}
7172
]
72-
```
73+
```
74+
75+
## Recommended Extensions
76+
77+
For proper XML editing support, we recommend installing some of the following extensions:
78+
79+
* [Scholary XML](https://marketplace.visualstudio.com/items?itemName=raffazizzi.sxml)
80+
* [XML Language Support by Red Hat](https://marketplace.visualstudio.com/items?itemName=redhat.vscode-xml)
81+
* [Auto Rename Tag](https://marketplace.visualstudio.com/items?itemName=formulahendry.auto-rename-tag)
82+
* [Language Server and Client for XQuery/eXistdb](https://marketplace.visualstudio.com/items?itemName=eXist-db.existdb-vscode)

media/main.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@ class View {
2626
}
2727

2828
outputResults(items) {
29+
document.getElementById('items').innerHTML = items.totalItems;
2930
const results = document.getElementById('results');
3031
results.innerHTML = '';
31-
items.forEach((item) => {
32+
items.items.forEach((item) => {
3233
const tr = document.createElement('tr');
3334
let td = document.createElement('td');
3435
tr.appendChild(td);
@@ -55,11 +56,11 @@ class View {
5556
}
5657
tr.appendChild(td);
5758

59+
td = document.createElement('td');
5860
if (item.details) {
59-
td = document.createElement('td');
6061
td.innerHTML = item.details;
61-
tr.appendChild(td);
6262
}
63+
tr.appendChild(td);
6364

6465
td = document.createElement('td');
6566
td.innerHTML = item.register;

src/connectors/gnd.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
import axios from 'axios';
2-
import { stringify } from 'querystring';
3-
import { Registry, RegistryResult } from "../registry";
2+
import { Registry, RegistryResultItem } from "../registry";
43

54
export class GND extends Registry {
65

76
async query(key:string) {
8-
const results:RegistryResult[] = [];
7+
const results:RegistryResultItem[] = [];
98
const response = await axios.get(`https://lobid.org/gnd/search?q=${encodeURIComponent(key)}&filter=%2B%28type%3APerson%29&format=json&size=100`);
109
if (response.status !== 200) {
11-
return results;
10+
return {
11+
totalItems: 0,
12+
items: []
13+
};
1214
}
1315
const json:any = response.data;
1416
json.member.forEach((item:any) => {
15-
const result:RegistryResult = {
17+
const result:RegistryResultItem = {
1618
type: item.type.join(','),
1719
register: this._register,
1820
id: item.gndIdentifier,
@@ -22,10 +24,13 @@ export class GND extends Registry {
2224
};
2325
results.push(result);
2426
});
25-
return results;
27+
return {
28+
totalItems: json.totalItems,
29+
items: results
30+
};
2631
}
2732

28-
format(item: RegistryResult) {
33+
format(item: RegistryResultItem) {
2934
return `<persName ref="gnd-${item.id}">$TM_SELECTED_TEXT</persName>`;
3035
}
3136

src/connectors/gplaces.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import axios from 'axios';
2-
import { Registry, RegistryResult } from "../registry";
2+
import { Registry, RegistryResultItem } from "../registry";
33

44
export class GooglePlaces extends Registry {
55

@@ -11,15 +11,18 @@ export class GooglePlaces extends Registry {
1111
}
1212

1313
async query(key:string) {
14-
const results:RegistryResult[] = [];
14+
const results:RegistryResultItem[] = [];
1515

1616
const response = await axios.get(`https://maps.googleapis.com/maps/api/geocode/json?address=${encodeURIComponent(key)}&key=${this.apiKey}`);
1717
if (response.status !== 200) {
18-
return results;
18+
return {
19+
totalItems: 0,
20+
items: []
21+
};
1922
}
2023
const json:any = response.data;
2124
json.results.forEach((item:any) => {
22-
const result:RegistryResult = {
25+
const result:RegistryResultItem = {
2326
type: 'place',
2427
register: 'places',
2528
id: item['place_id'],
@@ -28,10 +31,13 @@ export class GooglePlaces extends Registry {
2831
};
2932
results.push(result);
3033
});
31-
return results;
34+
return {
35+
totalItems: json.results.length,
36+
items: results
37+
};
3238
}
3339

34-
format(item: RegistryResult) {
40+
format(item: RegistryResultItem) {
3541
return `<placeName ref="google-${item.id}">$TM_SELECTED_TEXT</placeName>`;
3642
}
3743
}

src/connectors/kba.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
import axios from 'axios';
2-
import { Registry, RegistryResult } from "../registry";
2+
import { Registry, RegistryResultItem } from "../registry";
33

44
export class KBA extends Registry {
55

66
async query(key:string) {
7-
const results:RegistryResult[] = [];
8-
console.log('querying %s with %s', this._register, key);
9-
const response = await axios.get(`https://kb-prepare.k-r.ch/api/${this._register}?search=${encodeURIComponent(key)}`);
7+
const results:RegistryResultItem[] = [];
8+
const url = `https://kb-prepare.k-r.ch/api/${this._register}?search=${encodeURIComponent(key)}`;
9+
console.log(url);
10+
const response = await axios.get(url);
1011
if (response.status !== 200) {
11-
return results;
12+
return {
13+
totalItems: 0,
14+
items: []
15+
};
1216
}
1317
const json:any = response.data;
1418
let label: string;
@@ -25,7 +29,7 @@ export class KBA extends Registry {
2529
}
2630
json.data.forEach((item:any) => {
2731
const type = this._register === 'actors' ? item['authority_type'] : this._register;
28-
const result:RegistryResult = {
32+
const result:RegistryResultItem = {
2933
type: type,
3034
register: this._register,
3135
id: item['full-id'],
@@ -34,10 +38,13 @@ export class KBA extends Registry {
3438
};
3539
results.push(result);
3640
});
37-
return results;
41+
return {
42+
totalItems: json.meta.pagination.total,
43+
items: results
44+
};
3845
}
3946

40-
format(item: RegistryResult) {
47+
format(item: RegistryResultItem) {
4148
switch (item.type) {
4249
case 'person':
4350
return `<persName ref="${item.id}">$TM_SELECTED_TEXT</persName>`;

src/connectors/metagrid.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
import axios from 'axios';
2-
import { Registry, RegistryResult } from "../registry";
2+
import { Registry, RegistryResultItem } from "../registry";
33

44
export class Metagrid extends Registry {
55

66
async query(key:string) {
7-
const results:RegistryResult[] = [];
8-
const response = await axios.get(`https://api.metagrid.ch/search/${this._register}?query=${encodeURIComponent(key)}`);
7+
const results:RegistryResultItem[] = [];
8+
const url = `https://api.metagrid.ch/search/${this._register}?query=${encodeURIComponent(key)}`;
9+
console.log(url);
10+
const response = await axios.get(url);
911
if (response.status !== 200) {
10-
return results;
12+
return {
13+
totalItems: 0,
14+
items: []
15+
};
1116
}
1217
const json:any = response.data;
1318
json.concordances.forEach((item:any) => {
14-
const result:RegistryResult = {
19+
const result:RegistryResultItem = {
1520
register: 'places',
1621
type: 'person',
1722
id: item.id,
@@ -20,10 +25,13 @@ export class Metagrid extends Registry {
2025
};
2126
results.push(result);
2227
});
23-
return results;
28+
return {
29+
totalItems: json.concordances.length,
30+
items: results
31+
};
2432
}
2533

26-
format(item: RegistryResult) {
34+
format(item: RegistryResultItem) {
2735
switch (item.type) {
2836
case 'person':
2937
return `<persName ref="metagrid-${item.id}">$TM_SELECTED_TEXT</persName>`;

src/panel.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { KBA } from "./connectors/kba";
33
import { Metagrid } from "./connectors/metagrid";
44
import { GooglePlaces } from "./connectors/gplaces";
55
import { GND } from "./connectors/gnd";
6-
import { Registry, RegistryResult } from './registry';
6+
import { Registry, RegistryResult, RegistryResultItem } from './registry';
77

88
export class RegistryPanel implements vscode.WebviewViewProvider {
99

@@ -90,21 +90,27 @@ export class RegistryPanel implements vscode.WebviewViewProvider {
9090
cancellable: false
9191
}, (progress) => {
9292
return new Promise(async (resolve) => {
93-
let results:RegistryResult[] = [];
94-
93+
let results:RegistryResultItem[] = [];
94+
let totalItems = 0;
9595
if (register && register !== '') {
9696
const plugin = this._registry.get(register);
9797
if (plugin) {
9898
const result = await plugin.query(text);
99-
results = result;
99+
results = result.items;
100+
totalItems = result.totalItems;
100101
}
101102
} else {
102103
for (let plugin of this._registry.values()) {
103104
const result = await plugin.query(text);
104-
results = results.concat(result);
105+
totalItems += result.totalItems;
106+
results = results.concat(result.items);
105107
}
106108
}
107-
this._view?.webview.postMessage({ command: 'results', data: results, query: text });
109+
const data:RegistryResult = {
110+
totalItems: totalItems,
111+
items: results
112+
};
113+
this._view?.webview.postMessage({ command: 'results', data: data, query: text });
108114
resolve(true);
109115
});
110116
});
@@ -145,6 +151,7 @@ export class RegistryPanel implements vscode.WebviewViewProvider {
145151
<i class="codicon codicon-search"></i>
146152
</button>
147153
</div>
154+
<div id="status">Found <span id="items">0</span> items.</div>
148155
<table class="table">
149156
<tbody id="results"></tbody>
150157
</table>

src/registry.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,20 @@ export abstract class Registry {
55
this._register = config.name;
66
}
77

8-
abstract query(key:string): Promise<RegistryResult[]>;
9-
abstract format(item: RegistryResult): string | undefined;
8+
abstract query(key:string): Promise<RegistryResult>;
9+
abstract format(item: RegistryResultItem): string | undefined;
1010
}
1111

12+
export interface RegistryResultItem {
13+
register: string;
14+
type: string;
15+
id: string;
16+
label: string;
17+
link?: string;
18+
details?: string;
19+
};
20+
1221
export interface RegistryResult {
13-
register: string,
14-
type: string,
15-
id: string,
16-
label: string,
17-
link?: string,
18-
details?: string
19-
};
22+
totalItems: number;
23+
items: RegistryResultItem[];
24+
}

0 commit comments

Comments
 (0)