-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
54 changed files
with
610 additions
and
494 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
demos/storybook/shapes/editors/dash/InstancesSelect/no-labels.data.ttl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
PREFIX ex: <http://example.org/> | ||
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> | ||
prefix wd: <http://www.wikidata.org/entity/> | ||
|
||
ex:instance <http://schema.org/alumniOf> wd:Q184478 . | ||
|
24 changes: 24 additions & 0 deletions
24
demos/storybook/shapes/editors/dash/InstancesSelect/no-labels.ttl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
PREFIX dash: <http://datashapes.org/dash#> | ||
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> | ||
PREFIX wd: <http://www.wikidata.org/entity/> | ||
PREFIX schema: <http://schema.org/> | ||
PREFIX sh: <http://www.w3.org/ns/shacl#> | ||
|
||
<> a sh:NodeShape ; | ||
sh:property | ||
[ | ||
sh:path schema:alumniOf ; | ||
sh:class wd:Q3918 ; | ||
sh:name "Alma mater" ; | ||
sh:minCount 1 ; | ||
sh:maxCount 1 ; | ||
dash:editor dash:InstancesSelectEditor ; | ||
] ; | ||
. | ||
|
||
wd:Q184478 a wd:Q3918 . | ||
wd:Q1860208 a wd:Q3918 . | ||
wd:Q49108 a wd:Q3918 . | ||
wd:Q34433 a wd:Q3918 . | ||
wd:Q35794 a wd:Q3918 . | ||
wd:Q13371 a wd:Q3918 . |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 22 additions & 13 deletions
35
demos/storybook/stories/Editors/DASH/InstancesSelectEditor.stories.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,33 @@ | ||
import type { StoryObj as Story } from '@storybook/web-components' | ||
import { defaultMeta } from '../../common.js' | ||
import { render } from '../../render.js' | ||
import { createStory, defaultMeta } from '../../common.js' | ||
import instances from '../../../shapes/editors/dash/InstancesSelect/wikidata.ttl?raw' | ||
import instancesNoLabels from '../../../shapes/editors/dash/InstancesSelect/no-labels.ttl?raw' | ||
import noLabelsData from '../../../shapes/editors/dash/InstancesSelect/no-labels.data.ttl?raw' | ||
import { configure as configureFetch } from './InstancesSelectEditor/fetch.js' | ||
|
||
const meta = { | ||
...defaultMeta, | ||
} | ||
|
||
export default meta | ||
|
||
/** | ||
* Just as with enum select, `rdfs:label` is used for option text | ||
*/ | ||
export const Wikidata: Story = { | ||
export const Wikidata: Story = createStory({ | ||
name: 'Resources by sh:class', | ||
args: { | ||
shapes: instances, | ||
customPrefixes: { | ||
wd: 'http://www.wikidata.org/entity/', | ||
}, | ||
shapes: instances, | ||
prefixes: ['rdfs'], | ||
customPrefixes: { | ||
wd: 'http://www.wikidata.org/entity/', | ||
}, | ||
render, | ||
} | ||
})() | ||
|
||
export const WikidataNoLabels: Story = createStory({ | ||
name: 'Resources without labels', | ||
shapes: instancesNoLabels, | ||
data: noLabelsData, | ||
focusNode: 'http://example.org/instance', | ||
prefixes: ['rdfs', 'schema'], | ||
customPrefixes: { | ||
wd: 'http://www.wikidata.org/entity/', | ||
ex: 'http://example.org/', | ||
}, | ||
})(configureFetch) |
98 changes: 98 additions & 0 deletions
98
demos/storybook/stories/Editors/DASH/InstancesSelectEditor/fetch.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import type { InstanceConfigCallback } from '@hydrofoil/shaperone-wc/configure.js' | ||
import SparqlClient from 'sparql-http-client/ParsingClient.js' | ||
import type { ComponentDecorator, InstancesSelectEditor } from '@hydrofoil/shaperone-core/components.js' | ||
import type { ComponentConstructor } from '@hydrofoil/shaperone-core/models/components/index.js' | ||
import type { GraphPointer } from 'clownface' | ||
import type { PropertyValues } from 'lit' | ||
import env from '@hydrofoil/shaperone-core/env.js' | ||
|
||
const wikidata = new SparqlClient({ | ||
endpointUrl: 'https://query.wikidata.org/sparql', | ||
}) | ||
|
||
const labelQuery = ` | ||
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> | ||
construct { | ||
?id rdfs:label ?l | ||
} where { | ||
?id rdfs:label ?l | ||
}` | ||
|
||
const WikidataFetchMixin: ComponentDecorator<InstancesSelectEditor> = { | ||
applicableTo(component: ComponentConstructor) { | ||
return component.editor.equals(env().ns.dash.InstancesSelectEditor) | ||
}, | ||
|
||
decorate(base) { | ||
return class InstanceFetchingComponent extends base implements InstancesSelectEditor { | ||
private loading = false | ||
private loaded = env().clownface() | ||
|
||
updated(_changedProperties: PropertyValues) { | ||
super.updated(_changedProperties) | ||
|
||
if (_changedProperties.has('choices') && this.choices.length) { | ||
this.loadLabels(...this.choices) | ||
} | ||
} | ||
|
||
async loadLabels(...iris: GraphPointer[]) { | ||
if (this.loading) { | ||
// avoid multiple requests | ||
return | ||
} | ||
|
||
this.loading = true | ||
try { | ||
const toFetch = iris | ||
// skip non-wikidata resources | ||
.filter(resource => resource.value.startsWith('http://www.wikidata.org/entity/')) | ||
// skip resources for which we already have data | ||
.filter(resource => !this.loaded.node(resource) | ||
.out(env().ns.rdfs.label).terms.length) | ||
|
||
if (toFetch.length === 0) { | ||
return | ||
} | ||
|
||
const query = `${labelQuery} | ||
VALUES ?id { | ||
${toFetch.map(resource => `<${resource.value}>`).join(' ')} | ||
}` | ||
|
||
const loaded = await wikidata.query.construct(query) | ||
for (const quad of loaded) { | ||
this.loaded.dataset.add(quad) | ||
} | ||
|
||
this.setChoices() | ||
} finally { | ||
this.loading = false | ||
} | ||
} | ||
|
||
setChoices() { | ||
const property = this.property.shape | ||
|
||
// used combined triples from the Shapes Graph and the fetched data | ||
const loadedGraph = env().clownface({ | ||
dataset: env().dataset([...this.loaded.dataset, ...property.pointer.dataset]), | ||
}) | ||
|
||
if (property.class) { | ||
this.choices = loadedGraph | ||
.has(env().ns.rdf.type, property.class.id) | ||
.toArray() | ||
} else { | ||
this.choices = [] | ||
} | ||
} | ||
} | ||
}, | ||
} | ||
|
||
export const configure: InstanceConfigCallback = ({ components }) => { | ||
// register the decorator in the component registry | ||
components.decorate(WikidataFetchMixin) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.