Commit 53777d5f authored by Laurent Wouters's avatar Laurent Wouters
Browse files

Implementing fetchable views

parent b44021856652
......@@ -21,17 +21,20 @@
/// <reference path="./rdflib-interface.d.ts"/>
import { RdfStore, Triple, Term, NamedNode, BlankNode, Literal } from "rdflib";
import * as React from "react";
import { ViewDescriptor } from "./view-registry";
import { ViewImplementation, registerImplementation } from "./view-impl";
import {
ViewRegistrySourceInline,
ViewRegistrySourceKind,
ViewLocationKind
} from "./view-registry";
import { ViewImplementation } from "./view-impl";
DEFAULT_DESCRIPTOR_TRIPLES,
DEFAULT_DESCRIPTOR_DBPEDIA_PERSON
} from "./view-defaults";
import { $rdf, getValueOf } from "./rdf";
import { ViewRenderer } from "./application";
export class RdfTriplesRendering implements ViewImplementation {
descriptor: ViewDescriptor;
constructor() {
this.descriptor = DEFAULT_DESCRIPTOR_TRIPLES;
this.priorityFor = this.priorityFor.bind(this);
this.render = this.render.bind(this);
}
......@@ -140,7 +143,9 @@ function hasTraitDbPediaPerson(store: RdfStore, primaryTopic: string): boolean {
}
export class DBPediaPersonRendering implements ViewImplementation {
descriptor: ViewDescriptor;
constructor() {
this.descriptor = DEFAULT_DESCRIPTOR_DBPEDIA_PERSON;
this.priorityFor = this.priorityFor.bind(this);
this.render = this.render.bind(this);
}
......@@ -186,11 +191,7 @@ export class DBPediaPersonRendering implements ViewImplementation {
}
/**
* The implementation for the Rdf triples view
*/
var VIEW_DEFAULTS_TRIPLES: RdfTriplesRendering = new RdfTriplesRendering();
/**
* The implementation for the DbPedia person view
* Registers the views
*/
var VIEW_DEFAULTS_DBPEDIA_PERSON: DBPediaPersonRendering = new DBPediaPersonRendering();
registerImplementation(new RdfTriplesRendering());
registerImplementation(new DBPediaPersonRendering());
......@@ -21,33 +21,41 @@
import {
ViewRegistrySourceInline,
ViewRegistrySourceKind,
ViewLocationKind
ViewLocationKind,
ViewDescriptor
} from "./view-registry";
/**
* The decsriptor for the default Rdf triples view
*/
export const DEFAULT_DESCRIPTOR_TRIPLES: ViewDescriptor = {
identifier: "::Defaults::Triples",
name: "RDF Triples",
description: "Renders a complete RDF dataset using the N-Triples syntax",
entrypoint: "VIEW_DEFAULTS_TRIPLES",
location: {
kind: ViewLocationKind.Embedded
}
};
/**
* The descritptor for the default Dbpedia person view
*/
export const DEFAULT_DESCRIPTOR_DBPEDIA_PERSON: ViewDescriptor = {
identifier: "::Defaults::DbPediaPerson",
name: "DbPedia Person",
description: "Renders a person from DbPedia",
entrypoint: "VIEW_DEFAULTS_DBPEDIA_PERSON",
location: {
kind: ViewLocationKind.Embedded
}
};
/**
* The source for the default views
*/
export const DEFAULTS_SOURCE: ViewRegistrySourceInline = {
name: "Source of default views",
kind: ViewRegistrySourceKind.Inlined,
descriptors: [
{
identifier: "::Defaults::Triples",
name: "RDF Triples",
description: "Renders a complete RDF dataset using the N-Triples syntax",
entrypoint: "VIEW_DEFAULTS_TRIPLES",
location: {
kind: ViewLocationKind.Embedded
}
},
{
identifier: "::Defaults::DbPediaPerson",
name: "DbPedia Person",
description: "Renders a person from DbPedia",
entrypoint: "VIEW_DEFAULTS_DBPEDIA_PERSON",
location: {
kind: ViewLocationKind.Embedded
}
}
]
descriptors: [DEFAULT_DESCRIPTOR_TRIPLES, DEFAULT_DESCRIPTOR_DBPEDIA_PERSON]
};
......@@ -33,6 +33,11 @@ import {
* The implementation of a view
*/
export interface ViewImplementation {
/**
* The descriptor for ths implementation
*/
descriptor: ViewDescriptor;
/**
* Gets the view's priority (greater number indicate greater priority)
* @param store The RDF store holding the data to be rendered
......@@ -54,6 +59,26 @@ export interface ViewImplementation {
): ReactNode;
}
/**
* A repository of view implementations
*/
export interface ViewImplementations {
[id: string]: ViewImplementation;
}
/**
* The global reposistory of embedded view implementations
*/
export const IMPL_EMBEDDED: ViewImplementations = {};
/**
* Registers an embedded view implementation
* @param implementation A view implementation
*/
export function registerImplementation(implementation: ViewImplementation) {
IMPL_EMBEDDED[implementation.descriptor.identifier] = implementation;
}
/**
* Fetches the implementation corresponding to a descriptor
* @param descriptor The descriptor
......@@ -63,8 +88,8 @@ function fetchImplementation(
): Promise<ViewImplementation> {
if (descriptor.location.kind == ViewLocationKind.Embedded) {
return new Promise((resolve, reject) => {
let result = eval(descriptor.entrypoint);
if (result == null) {
let result = IMPL_EMBEDDED[descriptor.identifier];
if (result == null || result == undefined) {
reject("No implementation");
}
resolve(result);
......@@ -120,13 +145,6 @@ function fetchImplementation(
}
}
/**
* A repository of view implementations
*/
interface ViewImplementations {
[id: string]: ViewImplementation;
}
/**
* Loads all implementations for the registered views
* @param registry The registry to load
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment