Update shacl-tulip for shacl-vue integration #5
3 changed files with 30 additions and 18 deletions
|
|
@ -10,11 +10,6 @@ export class FormBase {
|
||||||
|
|
||||||
constructor(id_iri = null, content = {}) {
|
constructor(id_iri = null, content = {}) {
|
||||||
|
|
||||||
if (!id_iri) {
|
|
||||||
var msg = "id_iri is a required argument"
|
|
||||||
console.error(msg)
|
|
||||||
throw new Error(msg)
|
|
||||||
}
|
|
||||||
this.ID_IRI = id_iri
|
this.ID_IRI = id_iri
|
||||||
this.content = content
|
this.content = content
|
||||||
this.ignoredProperties = [
|
this.ignoredProperties = [
|
||||||
|
|
@ -33,6 +28,14 @@ export class FormBase {
|
||||||
// }
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_checkIDiri(mthd) {
|
||||||
|
if (!this.ID_IRI) {
|
||||||
|
var msg = `Instance property 'ID_IRI' is required in order to use instance method: ${mthd}.`
|
||||||
|
console.error(msg)
|
||||||
|
throw new Error(msg)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
addSubject(class_uri, subject_uri, setVal_class={}, setVal_subject={}) {
|
addSubject(class_uri, subject_uri, setVal_class={}, setVal_subject={}) {
|
||||||
// add_empty_node
|
// add_empty_node
|
||||||
// if the RDF:type IRI does not exist in FormBase.content yet, add it
|
// if the RDF:type IRI does not exist in FormBase.content yet, add it
|
||||||
|
|
@ -130,6 +133,7 @@ export class FormBase {
|
||||||
}
|
}
|
||||||
|
|
||||||
formNodeToQuads(class_uri, subject_uri, shapesDS) {
|
formNodeToQuads(class_uri, subject_uri, shapesDS) {
|
||||||
|
this._checkIDiri('formNodeToQuads')
|
||||||
// Node = subject_uri = a specific identifiable object that was edited
|
// Node = subject_uri = a specific identifiable object that was edited
|
||||||
// Empty array to store quads
|
// Empty array to store quads
|
||||||
var quadArray = []
|
var quadArray = []
|
||||||
|
|
@ -206,6 +210,7 @@ export class FormBase {
|
||||||
}
|
}
|
||||||
|
|
||||||
quadsToFormData(class_uri, subject_term, RdfDS) {
|
quadsToFormData(class_uri, subject_term, RdfDS) {
|
||||||
|
this._checkIDiri('quadsToFormData')
|
||||||
// Subject term should be namedNode or blankNode
|
// Subject term should be namedNode or blankNode
|
||||||
var subject_uri = subject_term.value
|
var subject_uri = subject_term.value
|
||||||
this.addSubject(class_uri, subject_uri)
|
this.addSubject(class_uri, subject_uri)
|
||||||
|
|
@ -246,7 +251,7 @@ export class FormBase {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
saveNode(class_uri, node_uri, shapesDS, RdfDS, editMode) {
|
saveNode(class_uri, node_uri, shapesDS, RdfDS, editMode, cloneFunc = structuredClone) {
|
||||||
var changeNodeIdx = false
|
var changeNodeIdx = false
|
||||||
var subject_iri = null
|
var subject_iri = null
|
||||||
// Check if the node exists beforehand
|
// Check if the node exists beforehand
|
||||||
|
|
@ -289,7 +294,7 @@ export class FormBase {
|
||||||
}
|
}
|
||||||
// Change formdata node_uri to the actual id, if this was present:
|
// Change formdata node_uri to the actual id, if this was present:
|
||||||
if (changeNodeIdx && subject_iri !== node_uri) {
|
if (changeNodeIdx && subject_iri !== node_uri) {
|
||||||
this.content[class_uri][subject_iri] = structuredClone(this.content[class_uri][node_uri])
|
this.content[class_uri][subject_iri] = cloneFunc(this.content[class_uri][node_uri])
|
||||||
delete this.content[class_uri][node_uri]
|
delete this.content[class_uri][node_uri]
|
||||||
}
|
}
|
||||||
return {
|
return {
|
||||||
|
|
@ -297,8 +302,7 @@ export class FormBase {
|
||||||
node_iri: subject_iri || node_uri
|
node_iri: subject_iri || node_uri
|
||||||
}
|
}
|
||||||
// at the end, what to do with current data in formdata?
|
// at the end, what to do with current data in formdata?
|
||||||
// we keep it there because this keeps track of changes during
|
// we keep it there because this keeps track of changes during the session
|
||||||
// the session, so that we know what to submit back to the service.
|
|
||||||
} else {
|
} else {
|
||||||
console.error(`\t- Node ${class_uri} does not exist`)
|
console.error(`\t- Node ${class_uri} does not exist`)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -12,17 +12,14 @@ export class RdfDataset {
|
||||||
* Create a wrapper object for an RDF dataset a.k.a. quad-store
|
* Create a wrapper object for an RDF dataset a.k.a. quad-store
|
||||||
*/
|
*/
|
||||||
constructor(data = {}) {
|
constructor(data = {}) {
|
||||||
|
|
||||||
this.data = data;
|
this.data = data;
|
||||||
this.rdfPretty = rdf.clone();
|
this.rdfPretty = rdf.clone();
|
||||||
this.rdfPretty.formats.import(formatsPretty);
|
this.rdfPretty.formats.import(formatsPretty);
|
||||||
|
|
||||||
this.data.prefixes = {};
|
this.data.prefixes = {};
|
||||||
this.data.serializedGraph = '';
|
this.data.serializedGraph = '';
|
||||||
this.data.graphLoaded = false;
|
this.data.graphLoaded = false;
|
||||||
this.data.prefixesLoaded = false;
|
this.data.prefixesLoaded = false;
|
||||||
this.data.graph = this.createDataset();
|
this.data.graph = this.createDataset();
|
||||||
|
|
||||||
this._eventTarget = new EventTarget();
|
this._eventTarget = new EventTarget();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -50,9 +47,9 @@ export class RdfDataset {
|
||||||
* Loads RDF data from a given URL and processes prefixes and quads.
|
* Loads RDF data from a given URL and processes prefixes and quads.
|
||||||
* @param {string} url - The URL of the RDF document.
|
* @param {string} url - The URL of the RDF document.
|
||||||
*/
|
*/
|
||||||
async loadRDF(url) {
|
async loadRDF(url, headers = {}) {
|
||||||
this.beforeLoadFn()
|
this.beforeLoadFn()
|
||||||
readRDF(url)
|
readRDF(url, headers)
|
||||||
.then(quadStream => {
|
.then(quadStream => {
|
||||||
// Load prefixes
|
// Load prefixes
|
||||||
quadStream.on('prefix', (prefix, ns) => {
|
quadStream.on('prefix', (prefix, ns) => {
|
||||||
|
|
@ -99,11 +96,16 @@ export class RdfDataset {
|
||||||
* @param {import("rdf-ext").Quad} quad - The RDF quad.
|
* @param {import("rdf-ext").Quad} quad - The RDF quad.
|
||||||
*/
|
*/
|
||||||
onDataFn(quad) {
|
onDataFn(quad) {
|
||||||
|
// The first following line, moved here from shacl-vue's graphdata composable,
|
||||||
|
// was an attempt to solve https://hub.datalad.org/datalink/annotate-trr379-demo/issues/32.
|
||||||
|
// But it was a faulty attempt, since the object was different. Still, leaving it here since
|
||||||
|
// deleting matches would prospectively solve the duplication of named node or literal objects
|
||||||
|
this.data.graph.deleteMatches(quad.subject, quad.predicate, quad.object, null)
|
||||||
this.addQuad(quad)
|
this.addQuad(quad)
|
||||||
this.dispatchEvent(new CustomEvent('quad', { detail: quad }));
|
this.dispatchEvent(new CustomEvent('quad', { detail: quad }));
|
||||||
}
|
}
|
||||||
async onDataEndFn() {
|
async onDataEndFn() {
|
||||||
this.data.serializedGraph = await this.serializeGraph()
|
await this.updateSerializedGraph()
|
||||||
this.data.graphLoaded = true
|
this.data.graphLoaded = true
|
||||||
this.dispatchEvent(new CustomEvent('graphLoaded', { detail: this.data.graph }));
|
this.dispatchEvent(new CustomEvent('graphLoaded', { detail: this.data.graph }));
|
||||||
}
|
}
|
||||||
|
|
@ -124,6 +126,10 @@ export class RdfDataset {
|
||||||
return (await this.rdfPretty.io.dataset.toText('text/turtle', this.data.graph)).trim()
|
return (await this.rdfPretty.io.dataset.toText('text/turtle', this.data.graph)).trim()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async updateSerializedGraph() {
|
||||||
|
this.data.serializedGraph = (await this.rdfPretty.io.dataset.toText('text/turtle', this.data.graph)).trim()
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks if a given RDF node represents an RDF list.
|
* Checks if a given RDF node represents an RDF list.
|
||||||
* @param {import("rdf-ext").Term} node - The RDF node to check.
|
* @param {import("rdf-ext").Term} node - The RDF node to check.
|
||||||
|
|
|
||||||
|
|
@ -12,7 +12,7 @@ const HOST = 'localhost';
|
||||||
// Test the FormBase class
|
// Test the FormBase class
|
||||||
describe('FormBase', () => {
|
describe('FormBase', () => {
|
||||||
let form;
|
let form;
|
||||||
let id_iri = 'https://concepts.datalad.org/s/things/v1/id'
|
let id_iri = 'https://concepts.datalad.org/s/things/v1/pid'
|
||||||
let shapesDS = new ShapesDataset();
|
let shapesDS = new ShapesDataset();
|
||||||
let rdfDS = new RdfDataset();
|
let rdfDS = new RdfDataset();
|
||||||
|
|
||||||
|
|
@ -20,8 +20,10 @@ describe('FormBase', () => {
|
||||||
form = new FormBase(id_iri);
|
form = new FormBase(id_iri);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should throw an error if the required constructor argument is missing', () => {
|
it('should throw an error if the required ID_IRI argument is missing for specific functions', () => {
|
||||||
expect(() => new FormBase()).toThrowError()
|
var f = new FormBase();
|
||||||
|
expect(() => f.formNodeToQuads(null, null, null)).toThrowError()
|
||||||
|
expect(() => f.quadsToFormData(null, null, null)).toThrowError()
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should handle all form functionality correctly', async () => {
|
it('should handle all form functionality correctly', async () => {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue