diff --git a/nietzsche-beta-app/src/app/crossref-editor-component/crossref-editor-data-service.service.spec.ts b/nietzsche-beta-app/src/app/crossref-editor-component/crossref-editor-data-service.service.spec.ts new file mode 100644 index 0000000..d8f14fd --- /dev/null +++ b/nietzsche-beta-app/src/app/crossref-editor-component/crossref-editor-data-service.service.spec.ts @@ -0,0 +1,12 @@ +import { TestBed } from '@angular/core/testing'; + +import { CrossrefEditorDataServiceService } from './crossref-editor-data-service.service'; + +describe('CrossrefEditorDataServiceService', () => { + beforeEach(() => TestBed.configureTestingModule({})); + + it('should be created', () => { + const service: CrossrefEditorDataServiceService = TestBed.get(CrossrefEditorDataServiceService); + expect(service).toBeTruthy(); + }); +}); diff --git a/nietzsche-beta-app/src/app/crossref-editor-component/crossref-editor-data-service.service.ts b/nietzsche-beta-app/src/app/crossref-editor-component/crossref-editor-data-service.service.ts new file mode 100644 index 0000000..d765d61 --- /dev/null +++ b/nietzsche-beta-app/src/app/crossref-editor-component/crossref-editor-data-service.service.ts @@ -0,0 +1,13 @@ +import {EventEmitter, Injectable} from '@angular/core'; +import {TlnTreeNode} from './tln-tree-component/tln-tree-component.component'; + +@Injectable({ + providedIn: 'root' +}) +export class CrossrefEditorDataServiceService { + clickedNodeEvent: EventEmitter; + + constructor( ) { + this.clickedNodeEvent = new EventEmitter(); + } +} diff --git a/nietzsche-beta-app/src/app/crossref-editor-component/rdf-editor-component/rdf-editor-component.component.html b/nietzsche-beta-app/src/app/crossref-editor-component/rdf-editor-component/rdf-editor-component.component.html index d4cb65a..cddbb27 100644 --- a/nietzsche-beta-app/src/app/crossref-editor-component/rdf-editor-component/rdf-editor-component.component.html +++ b/nietzsche-beta-app/src/app/crossref-editor-component/rdf-editor-component/rdf-editor-component.component.html @@ -1 +1,4 @@ -

rdf-editor-component works!

+ + new triples + {{textGeneseTriples}} + diff --git a/nietzsche-beta-app/src/app/crossref-editor-component/rdf-editor-component/rdf-editor-component.component.ts b/nietzsche-beta-app/src/app/crossref-editor-component/rdf-editor-component/rdf-editor-component.component.ts index 20eac42..c43c610 100644 --- a/nietzsche-beta-app/src/app/crossref-editor-component/rdf-editor-component/rdf-editor-component.component.ts +++ b/nietzsche-beta-app/src/app/crossref-editor-component/rdf-editor-component/rdf-editor-component.component.ts @@ -1,59 +1,49 @@ import { Component, OnInit } from '@angular/core'; import * as N3 from "node_modules/n3/src"; +import {CrossrefEditorDataServiceService} from '../crossref-editor-data-service.service'; +import {Subscription} from 'rxjs'; @Component({ selector: 'app-rdf-editor-component', templateUrl: './rdf-editor-component.component.html', styleUrls: ['./rdf-editor-component.component.scss'] }) export class RdfEditorComponentComponent implements OnInit { - constructor() { + nodeSub: Subscription; + textGeneseWriter; + textGeneseTriples: string; + + constructor( public dataService: CrossrefEditorDataServiceService) { } ngOnInit() { + this.textGeneseWriter = new N3.Writer({ prefixes: { data: 'http://rdfh.ch/projects/0068#' } }); - const {DataFactory} = N3; - const {namedNode, literal, defaultGraph, quad} = DataFactory; - const myQuad = quad( - namedNode('https://ruben.verborgh.org/profile/#me'), - namedNode('http://xmlns.com/foaf/0.1/givenName'), - literal('Ruben', 'en'), - defaultGraph(), - ); - console.log(myQuad.termType); // Quad - console.log(myQuad.value); // '' - console.log(myQuad.subject.value); // https://ruben.verborgh.org/profile/#me - console.log(myQuad.object.value); // Ruben - console.log(myQuad.object.datatype.value); // http://www.w3.org/1999/02/22-rdf-syntax-ns#langString - console.log(myQuad.object.language); // en - - this.writeSth(); - } - - interLinkTextUnits(textA: string, textB: string): string { - let ttlString: string; - return ttlString; + this.nodeSub = this.dataService.clickedNodeEvent.subscribe(node => { + this.writeSthToTextGenese(node.iri); + //... + this.checkOutTextGenese(); + }); } - - - writeSth() { + writeSthToTextGenese(iri, prefix?: any) { const {DataFactory} = N3; const {namedNode, literal, defaultGraph, quad} = DataFactory; - const writer = new N3.Writer({ prefixes: { c: 'http://example.org/cartoons#' } }); - writer.addQuad( - namedNode('http://example.org/cartoons#Tom'), + const newIri = iri+'textgenese_0' + this.textGeneseWriter.addQuad( + namedNode(newIri), namedNode('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'), - namedNode('http://example.org/cartoons#Cat') + namedNode('tln:textGenese!!!') ); - writer.addQuad(quad( - namedNode('http://example.org/cartoons#Tom'), - namedNode('http://example.org/cartoons#name'), - literal('Tom') - )); - writer.end((error, result) => console.log('typeof ', typeof result, ' result ', result)); + } + + checkOutTextGenese() { + this.textGeneseWriter.end((error, result) => { + console.log('typeof ', typeof result, ' result ', result); + this.textGeneseTriples = result; + }); } } diff --git a/nietzsche-beta-app/src/app/crossref-editor-component/tln-tree-component/tln-tree-component.component.ts b/nietzsche-beta-app/src/app/crossref-editor-component/tln-tree-component/tln-tree-component.component.ts index 0052ef8..e072a76 100644 --- a/nietzsche-beta-app/src/app/crossref-editor-component/tln-tree-component/tln-tree-component.component.ts +++ b/nietzsche-beta-app/src/app/crossref-editor-component/tln-tree-component/tln-tree-component.component.ts @@ -1,207 +1,208 @@ import {Component, Injectable, Input, OnInit} from '@angular/core'; import {CollectionViewer, SelectionChange, DataSource} from '@angular/cdk/collections'; import {FlatTreeControl} from '@angular/cdk/tree'; import {BehaviorSubject, merge, Observable} from 'rxjs'; import {map} from 'rxjs/operators'; import {NavTreeDef} from '../../models/models'; import {QueryService} from '../../services/query.service'; import * as _ from 'lodash'; +import {CrossrefEditorDataServiceService} from '../crossref-editor-data-service.service'; /** Flat node with expandable and level information */ export class TlnTreeNode { constructor(public label: string, public iri: string, public level = 1, public expandable = false, public isLoading = false, public type? ) { } } /** * Database for dynamic data. When expanding a node in the tree, the data source will need to fetch * the descendants data from the database. */ @Injectable({providedIn: 'root'}) export class DynamicDatabase { constructor() { } } /** * File database, it can build a tree structured Json object from string. * Each node in Json object represents a file or a directory. For a file, it has filename and type. * For a directory, it has filename and children (a list of files or directories). * The input will be a json object string, and the output is a list of `FileNode` with nested * structure. */ export class DynamicDataSource implements DataSource { dataChange = new BehaviorSubject([]); genericLevel: number = 0; get data(): TlnTreeNode[] { return this.dataChange.value; } set data(value: TlnTreeNode[]) { this._treeControl.dataNodes = value; this.dataChange.next(value); } constructor(private _treeControl: FlatTreeControl, private _database: DynamicDatabase, private queryService: QueryService, private treeDefs: NavTreeDef[], private generic: boolean) { } expandable(level: number) { if (this.generic) {return true} else { return level < this.treeDefs.length-1 } } connect(collectionViewer: CollectionViewer): Observable { this._treeControl.expansionModel.changed.subscribe(change => { if ((change as SelectionChange).added || (change as SelectionChange).removed) { this.handleTreeControl(change as SelectionChange); } }); return merge(collectionViewer.viewChange, this.dataChange).pipe(map(() => this.data)); } disconnect(collectionViewer: CollectionViewer): void {} /** Handle expand/collapse behaviors */ handleTreeControl(change: SelectionChange) { if (change.added) { change.added.forEach(node => this.toggleNode(node, this.expandable(node.level))); } if (change.removed) { change.removed.slice().reverse().forEach(node => this.toggleNode(node, false)); } } toggleNode(node: TlnTreeNode, expand: boolean) { node.isLoading = true; const index = this.data.indexOf(node); if (expand) { this.getChildData(node).then(children => { if (!children || index < 0) { // If no children, or cannot find the node, no op return; } this.data.splice(index + 1, 0, ...children); // notify the change this.dataChange.next(this.data); }); } else { let count = 0; for (let i = index + 1; i < this.data.length && this.data[i].level > node.level; i++, count++) {} this.data.splice(index + 1, count); // notify the change this.dataChange.next(this.data); } node.isLoading = false; } getChildData(node: TlnTreeNode): Promise { console.log('getting children'); if (this.generic) { return this.getGenericChildren(node); } else { return this.getChildNodesFromDef(node); } } getParent(node: TlnTreeNode): TlnTreeNode { if (node.level < 1) { return null; } const index= this.data.indexOf(node); for (let i = index; i >= 0; i--) { const currentNode = this.data[i]; if (currentNode.level < node.level) { return currentNode; } } } async getGenericChildren(node: TlnTreeNode){ let treeDef: NavTreeDef; const parentNode = this.getParent(node); let query: string; console.log('node ', node); if (node.level % 2 === 0 || 0 ) { // Then we are querying for properties treeDef = this.treeDefs[1]; // we want to get resources/ objects of that clicked prop query = await this.queryService.parametrizeQueryWithItem(treeDef.apiDef.query, node.iri); } else { // we want all objects of the s&p console.log(this.data); treeDef = this.treeDefs[2]; query = await this.queryService.parametrizeQueryWithItem(treeDef.apiDef.query, parentNode.iri, node.iri); } return this.getData(query, treeDef, node.level+1); } async getChildNodesFromDef(node: TlnTreeNode): Promise { const treeDef = this.treeDefs.filter(def => def.idx === node.level+1 )[0]; let query: string; if (treeDef.apiDef.paramTriple && treeDef.apiDef.paramTriple === 2) { query = await this.queryService.parametrizeQueryWithItem(treeDef.apiDef.query, '', '', node.iri); } else { query = await this.queryService.parametrizeQueryWithItem(treeDef.apiDef.query, node.iri);} return this.getData(query, treeDef); } async getData(query: string, treeDef: NavTreeDef, level?: number): Promise { let nodes: TlnTreeNode[] = []; await this.queryService.getData(treeDef.apiDef.baseUrl, query, 'SELECT').then(res => { const treeData: any[] = _.get(res, treeDef.apiDef.dataArray); treeData.forEach(entry => { const tlnNode = new TlnTreeNode(_.get(entry, treeDef.apiDef.mapping.label), _.get(entry, treeDef.apiDef.mapping.iri), level || treeDef.idx, this.expandable(treeDef.idx), false, _.get(entry, treeDef.apiDef.mapping.type) ); nodes.push(tlnNode); }); }); return nodes; } } @Component({ selector: 'app-tln-tree-component', templateUrl: './tln-tree-component.component.html', styleUrls: ['./tln-tree-component.component.scss'] }) export class CrossRefTreeComponentComponent implements OnInit { @Input() treeDefs: NavTreeDef[]; @Input() generic: boolean; ngOnInit() { this.treeControl = new FlatTreeControl(this.getLevel, this.isExpandable); this.dataSource = new DynamicDataSource(this.treeControl, this.database, this.queryService, this.treeDefs, this.generic); this.initRootLevelNodes(); } - constructor(public database: DynamicDatabase, private queryService: QueryService) { + constructor(public database: DynamicDatabase, private queryService: QueryService, private dataService: CrossrefEditorDataServiceService) { } initRootLevelNodes() { const rootTreeDef = this.treeDefs.filter(def => def.idx === 0)[0]; const query = rootTreeDef.apiDef.query; this.dataSource.getData(query, rootTreeDef).then(rootNodes => this.dataSource.data = rootNodes); } onClick(node:TlnTreeNode): void { - console.log('clicked node', node) + this.dataService.clickedNodeEvent.emit(node); } treeControl: FlatTreeControl; dataSource: DynamicDataSource; getLevel = (node: TlnTreeNode) => node.level; isExpandable = (node: TlnTreeNode) => node.expandable; hasChild = (_: number, _nodeData: TlnTreeNode) => _nodeData.expandable; }