This repository has been archived by the owner on Aug 8, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
index.tsx
388 lines (356 loc) · 10.2 KB
/
index.tsx
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
import {
JupyterFrontEndPlugin,
ILayoutRestorer,
JupyterFrontEnd
} from "@jupyterlab/application";
import { flatten, expand } from "jsonld";
import { Token } from "@phosphor/coreutils";
import {
ICommandPalette,
MainAreaWidget,
ReactWidget,
WidgetTracker
} from "@jupyterlab/apputils";
import React from "react";
type LinkedData = object;
export interface ILinkedDataProvider {
get(url: URL): Promise<LinkedData>;
}
export class LinkedDataRegistry {
register(provider: ILinkedDataProvider): void {
this._providers.add(provider);
}
/**
* Lookup this URL in all providers and flatten the results.
*/
async get(url: URL): Promise<LinkedData> {
return ((await expand(
await flatten(
await Promise.all([...this._providers].map(p => p.get(url))),
null
)
)) as Array<any>).filter(o => o["@id"] === url.toString())[0];
}
private _providers = new Set<ILinkedDataProvider>();
}
export const LinkedDataRegistryToken = new Token("LinkedDataRegistry");
const linkedDataRegistryPlugin: JupyterFrontEndPlugin<LinkedDataRegistry> = {
activate: () => new LinkedDataRegistry(),
id: "jupyterlab-metadata-service:linked-data-registry",
requires: [],
provides: LinkedDataRegistryToken,
autoStart: true
};
const SAMPLE_DATA = new Map<string, LinkedData>();
SAMPLE_DATA.set(
"https://github.com/Coleridge-Initiative/adrf-onto/wiki/Vocabulary#dataset381",
{
"@id":
"https://github.com/Coleridge-Initiative/adrf-onto/wiki/Vocabulary#dataset381",
"@type": ["http://purl.org/dc/dcmitype/Dataset"],
"http://purl.org/dc/terms/alternative": [
{
"@language": "en",
"@value": "EPESE"
}
],
"http://purl.org/dc/terms/description": [
{
"@language": "en",
"@value":
"A project initiated by the intramural Epidemiology, Demography and Biometry Program of the National Institute on Aging"
}
],
"http://purl.org/dc/terms/identifier": [
{
"@language": "en",
"@value": "8481423"
}
],
"http://purl.org/dc/terms/publisher": [
{
"@id":
"https://github.com/Coleridge-Initiative/adrf-onto/wiki/Vocabulary#duke_univ"
}
],
"http://purl.org/dc/terms/title": [
{
"@language": "en",
"@value":
"Established Populations for Epidemiologic Studies of the Elderly Project"
}
],
"http://purl.org/pav/createdOn": [
{
"@type": "http://www.w3.org/2001/XMLSchema#date",
"@value": "1993-02-01"
}
],
"http://purl.org/pav/curatedBy": [
{
"@id":
"https://github.com/Coleridge-Initiative/adrf-onto/wiki/Vocabulary#cornoni-huntley_j"
}
],
"http://xmlns.com/foaf/0.1/page": [
{
"@id": "https://www.ncbi.nlm.nih.gov/pubmed/8481423"
}
]
}
);
SAMPLE_DATA.set(
"https://github.com/Coleridge-Initiative/adrf-onto/wiki/Vocabulary#duke_univ",
{
"@context": "https://schema.org",
"@id":
"https://github.com/Coleridge-Initiative/adrf-onto/wiki/Vocabulary#duke_univ",
"@type": "GovernmentPermit",
// "issuedBy": {
// "@type": "GovernmentOrganization",
// "name": "Department of Health and Mental Hygiene\""
// },
// "issuedThrough": {
// "@type": "GovernmentService",
// "name": "NYC Food Service Establishment Permit Service"
// },
name: "NYC Food Service Establishment Permit"
// "validFor": "P1Y",
// "validIn": {
// "@type": "AdministrativeArea",
// "name": "New York"
// }
}
);
const sampleProviderPlugin: JupyterFrontEndPlugin<void> = {
activate: (_, registry: LinkedDataRegistry) => {
registry.register({
get: async url => {
console.log("getting ", url);
return SAMPLE_DATA.get(url.toString()) || {};
}
});
},
id: "jupyterlab-metadata-service:sample-provider",
requires: [LinkedDataRegistryToken],
autoStart: true
};
/**
* https://w3c.github.io/json-ld-syntax/#value-objects
*/
function ValueObject({ valueObject }: { valueObject: { "@value": any } }) {
return <>{JSON.stringify(valueObject["@value"], null, " ")}</>;
}
/**
* https://w3c.github.io/json-ld-syntax/#node-objects
*/
function NodeObject({
nodeObject,
onClick
}: {
nodeObject: object;
onClick: (url: URL) => void;
}) {
{
const entries = Object.entries(nodeObject);
if (entries.length === 0) {
return <div>No properties.</div>;
}
return (
<dl>
{entries.map(([property, object]) => {
// "The entries of a node object whose keys are not keywords are also called properties of the node object."
// https://w3c.github.io/json-ld-syntax/#syntax-tokens-and-keywords
switch (property) {
case "@id":
return (
<>
<dt>ID:</dt>
<dd>
<a onClick={() => onClick(new URL(object))}>{object}</a>
</dd>
</>
);
case "@type":
return (
<>
<dt>Type:</dt>
{(Array.isArray(object) ? object : [object]).map(type => (
<dd>{new URL(type).toString()}</dd>
))}
</>
);
default:
let url: URL;
try {
url = new URL(property);
} catch {
console.log(`Ignoring property ${property}`);
return <></>;
}
return (
<>
<dt>{url.toString()}</dt>
{(Array.isArray(object) ? object : [object]).map(
innerObject => (
<dd>
{"@value" in innerObject ? (
<ValueObject valueObject={innerObject} />
) : (
<NodeObject
nodeObject={innerObject}
onClick={onClick}
/>
)}
</dd>
)
)}
</>
);
}
})}
</dl>
);
}
}
type ViewerProps = {
url: URL;
onClick: (url: URL) => void;
registry: LinkedDataRegistry;
};
type ViewerState = { data: LinkedData | undefined };
class Viewer extends React.Component<ViewerProps, ViewerState> {
readonly state: ViewerState = {
data: undefined
};
async componentWillMount() {
this.setState({ data: await this.props.registry.get(this.props.url) });
}
componentDidUpdate(prevProps: ViewerProps) {
if (this.props.url.toString() !== prevProps.url.toString()) {
this.componentWillMount();
}
}
render() {
const { data } = this.state;
if (data === undefined) {
return <span>Loading...</span>;
}
console.log(data);
return <NodeObject nodeObject={data} onClick={this.props.onClick} />;
}
}
type URLFormProps = {
initial: URL | undefined;
onSubmit: (url: URL | undefined) => void;
};
type URLFormState = { url: string };
class URLForm extends React.Component<URLFormProps, URLFormState> {
readonly state: URLFormState = {
url: this.props.initial ? this.props.initial.toString() : ""
};
render() {
return (
<form
onSubmit={event => {
const url = this.state.url;
this.props.onSubmit(url === "" ? undefined : new URL(url));
event.preventDefault();
}}
>
<label>
URL:
<input
type="url"
name="url"
value={this.state.url}
onChange={event => this.setState({ url: event.target.value })}
/>
</label>
<input type="submit" value="Browse" />
</form>
);
}
}
type BrowserProps = { registry: LinkedDataRegistry };
type BrowserState = { url: URL | undefined };
class Browser extends React.Component<BrowserProps, BrowserState> {
readonly state: BrowserState = {
url: undefined
};
render() {
return (
<div>
<h1>Linked Data Browser</h1>
<URLForm
initial={this.state.url}
onSubmit={url => this.setState({ url })}
/>
{this.state.url ? (
<Viewer
url={this.state.url}
registry={this.props.registry}
onClick={url => this.setState({ url })}
/>
) : (
<></>
)}
</div>
);
}
}
const linkedDataBrowserPlugin: JupyterFrontEndPlugin<void> = {
id: "jupyterlab-metadata-service:data-browser",
autoStart: true,
requires: [LinkedDataRegistryToken, ILayoutRestorer, ICommandPalette],
activate: (
app: JupyterFrontEnd,
registry: LinkedDataRegistry,
restorer: ILayoutRestorer,
palette: ICommandPalette
) => {
// Declare a widget variable
let widget: MainAreaWidget<ReactWidget>;
// Add an application command
const command: string = "dataregistry-debugger:open";
app.commands.addCommand(command, {
label: "Linked Data Browser",
execute: () => {
if (!widget) {
// Create a new widget if one does not exist
const content = ReactWidget.create(<Browser registry={registry} />);
widget = new MainAreaWidget({ content });
widget.id = "metadata-browser";
widget.title.label = "Linked Data Browser";
widget.title.closable = true;
}
if (!tracker.has(widget)) {
// Track the state of the widget for later restoration
tracker.add(widget);
}
if (!widget.isAttached) {
// Attach the widget to the main work area if it's not there
app.shell.add(widget, "main");
}
widget.content.update();
// Activate the widget
app.shell.activateById(widget.id);
}
});
// Add the command to the palette.
palette.addItem({ command, category: "Linked Data" });
// Track and restore the widget state
let tracker = new WidgetTracker<MainAreaWidget<ReactWidget>>({
namespace: "metadata-browser"
});
restorer.restore(tracker, {
command,
name: () => "metadata-browser"
});
}
};
export default [
linkedDataRegistryPlugin,
sampleProviderPlugin,
linkedDataBrowserPlugin
] as JupyterFrontEndPlugin<any>[];