Commit 43ecb7b3 authored by Laurent Wouters's avatar Laurent Wouters
Browse files

Support language selection in foaf view

parent 117c2c0b4515
......@@ -18,24 +18,13 @@
* with CubicWeb. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
import {
RdfEntity,
RdfEntityStore,
getValueOf,
getValuesOf
} from "@logilab/librdf";
import { RdfEntity, RdfEntityStore } from "@logilab/librdf";
import { application, definition, implementation } from "@logilab/libview";
import * as $rdf from "rdflib";
import Vue from "vue";
/// <reference path="./vue.d.ts" />
import PersonImpl from "./PersonImpl.vue";
export const NO_LANGUAGE: application.Language = {
iso639_1: "",
iso639_2: "",
name: "-- auto --"
};
/**
* The descritptor for the default Dbpedia person view
*/
......@@ -58,6 +47,9 @@ const DESCRIPTOR_PERSON: definition.ViewDescriptor = {
} as definition.ViewResourceRemote
};
/**
* The known aliases
*/
const ALIAS = [
[
"http://xmlns.com/foaf/0.1/name",
......@@ -69,6 +61,9 @@ const ALIAS = [
]
];
/**
* The data for a person
*/
interface FoaFPerson {
name: string;
gender: string;
......@@ -78,6 +73,12 @@ interface FoaFPerson {
bio: string;
}
/**
* Gets the literal value for a property
* @param entity The RDF entity
* @param property The property
* @param language The required language
*/
function getLiteralValue(
entity: RdfEntity,
property: string,
......@@ -94,29 +95,53 @@ function getLiteralValue(
return literals[0].value;
}
/**
* Loads the data for the person of interest
* @param renderer The current renderer
* @param context The current rendering context
* @param target The current RDF entity to be rendered
*/
function loadPerson(
renderer: application.ViewRenderer,
context: application.RenderingContext,
target: string
): FoaFPerson {
let options = context.options[target];
let language =
options != null && options != undefined ? options.language : NO_LANGUAGE;
let store = new RdfEntityStore(context.store, ALIAS);
let root = store.entityForUri(target);
let focus = root.getEntityForS("http://xmlns.com/foaf/0.1/focus");
if (focus != null) root = focus;
let entityStore = new RdfEntityStore(context.store, ALIAS);
let entityTarget = entityStore.entityForUri(target);
let entityFocus = entityTarget.getEntityForS(
"http://xmlns.com/foaf/0.1/focus"
);
if (entityFocus != null) entityTarget = entityFocus;
let language = renderer.getLanguageFor(context, target);
return {
name: getLiteralValue(root, "http://xmlns.com/foaf/0.1/name", language),
gender: getLiteralValue(root, "http://xmlns.com/foaf/0.1/gender", language),
name: getLiteralValue(
entityTarget,
"http://xmlns.com/foaf/0.1/name",
language
),
gender: getLiteralValue(
entityTarget,
"http://xmlns.com/foaf/0.1/gender",
language
),
depiction: getLiteralValue(
root,
entityTarget,
"http://xmlns.com/foaf/0.1/depiction",
language
),
birth: getLiteralValue(root, "http://vocab.org/bio/0.1/birth", language),
death: getLiteralValue(root, "http://vocab.org/bio/0.1/death", language),
birth: getLiteralValue(
entityTarget,
"http://vocab.org/bio/0.1/birth",
language
),
death: getLiteralValue(
entityTarget,
"http://vocab.org/bio/0.1/death",
language
),
bio: getLiteralValue(
root,
entityTarget,
"http://rdvocab.info/ElementsGr2/biographicalInformation",
language
)
......@@ -151,19 +176,26 @@ class FoaFPersonRendering implements implementation.ViewImplementation {
priorityFor(store: $rdf.Formula, target: string): number {
if (target == null) return -1;
let focus = getValueOf(store, target, "http://xmlns.com/foaf/0.1/focus");
if (focus != null && focus != undefined) target = focus.value;
let types = getValuesOf(
store,
target,
let entityStore = new RdfEntityStore(store, ALIAS);
let entityTarget = entityStore.entityForUri(target);
let entityFocus = entityTarget.getEntityForS(
"http://xmlns.com/foaf/0.1/focus"
);
if (entityFocus != null) entityTarget = entityFocus;
let entityTypes = entityTarget.getEntitiesForS(
"http://www.w3.org/1999/02/22-rdf-syntax-ns#type"
);
let isPerson = types
.map(
(type: $rdf.Node) => type.value == "http://xmlns.com/foaf/0.1/Person"
)
.reduce((acc: boolean, matched: boolean) => acc || matched, false);
return isPerson ? 10 : -1;
if (entityTypes.length > 0) {
let x = entityTypes.find(
(entity: RdfEntity) =>
entity.uris.indexOf("http://xmlns.com/foaf/0.1/Person") >= 0
);
if (x != null && x != undefined)
// The entity is a FoaF:Person
return 10;
}
return -1;
}
render(
......@@ -171,7 +203,7 @@ class FoaFPersonRendering implements implementation.ViewImplementation {
context: application.RenderingContext,
target: string
): HTMLElement {
let person = loadPerson(context, target);
let person = loadPerson(renderer, context, target);
let result = document.createElement("div");
let vm = new Vue({
data: {
......
Supports Markdown
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