Commit 5d257de4 authored by Laurent Wouters's avatar Laurent Wouters
Browse files

[fix] Fixed selection of primary topic in default RDF view

parent a5dcc0264d26
......@@ -50,12 +50,11 @@ require("./view-defaults-impl");
class LDBrowserData {
constructor(observation: ResourceObservedData) {
this.renderer = implementation.newRenderer(getResourceContent);
let init = new ResourceData();
init.observations = observation;
init.command = new ResourceUserCommand();
this.history = [init];
this.currentResource = new ResourceData();
this.currentResource.observations = observation;
this.currentResource.command = new ResourceUserCommand();
this.byResource = {};
this.byResource[observation.url] = init;
this.byResource[observation.url] = this.currentResource;
this.storeCache = {};
}
/**
......@@ -65,7 +64,7 @@ class LDBrowserData {
/**
* The history of resources
*/
history: ResourceData[];
currentResource: ResourceData;
/**
* The data for each known resource
*/
......@@ -212,11 +211,10 @@ chrome.runtime.onMessage.addListener(function(
sendResponse
) {
if (request.requestType == "GetCurrentResource") {
sendResponse(currentBrowser.history[currentBrowser.history.length - 1]);
sendResponse(currentBrowser.currentResource);
} else if (request.requestType == "UpdateCurrentCommand") {
let resource = currentBrowser.history[currentBrowser.history.length - 1];
resource.command = request.payload;
onReachedResource(resource);
currentBrowser.currentResource.command = request.payload;
onReachedResource(currentBrowser.currentResource);
}
});
......@@ -224,8 +222,7 @@ chrome.runtime.onMessage.addListener(function(
* Listens to history state events from the browser
*/
window.onpopstate = function(event) {
let resource = event.state.resource;
onReachedResource(resource);
onReachedResource(currentBrowser.byResource[event.state.url]);
};
/**
......@@ -244,7 +241,7 @@ function main() {
promise
.then((observation: ResourceObservedData) => {
currentBrowser = new LDBrowserData(observation);
onNavigatedTo(currentBrowser.history[0]);
onNavigatedTo(currentBrowser.currentResource);
})
.catch((reason: any) => {
currentApp.onError(reason.toString());
......@@ -278,7 +275,7 @@ function onRequestNavigateTo(uri: string) {
command: new ResourceUserCommand()
};
// register this resource
currentBrowser.history.push(resource);
currentBrowser.currentResource = resource;
currentBrowser.byResource[uri] = resource;
onNavigatedTo(resource);
})
......@@ -294,7 +291,7 @@ function onRequestNavigateTo(uri: string) {
*/
function onNavigatedTo(resource: ResourceData): void {
history.pushState(
{ resource: resource },
{ url: resource.observations.url },
selectPrimaryTopic(resource),
chrome.extension.getURL("ldbrowser/index.html?target=") +
encodeURIComponent(resource.observations.url)
......@@ -348,6 +345,16 @@ function onReachedResource(resource: ResourceData): void {
*/
function onSuggestedResource(uri: string): void {}
/**
* Reacts to a user selecting a resource as the primary topic
* @param uri The uri of the resource
*/
function onSelectAsPrimaryTopic(uri: string): void {
currentBrowser.currentResource.command.isAutomatic = false;
currentBrowser.currentResource.command.selectedTopic = uri;
onReachedResource(currentBrowser.currentResource);
}
/**
* Creates a new rendering context
* @param resource The resource
......@@ -360,7 +367,8 @@ function newRenderingContext(
let topic = selectPrimaryTopic(resource);
let context: application.RenderingContext = {
event: {
onRequestNavigateTo: onRequestNavigateTo
onRequestNavigateTo: onRequestNavigateTo,
onSelectAsPrimaryTopic: onSelectAsPrimaryTopic
},
root: topic,
store: store,
......
......@@ -73,19 +73,27 @@ export class RdfTriplesRendering implements implementation.ViewImplementation {
thead.appendChild(tr);
table.appendChild(thead);
let tbody = document.createElement("tbody");
context.store.statements
.sort(
(a: $rdf.Statement, b: $rdf.Statement): number => {
var r = a.subject.value.localeCompare(b.subject.value);
if (r != 0) return r;
r = a.predicate.value.localeCompare(b.predicate.value);
if (r != 0) return r;
return a.object.value.localeCompare(b.object.value);
}
)
.map((triple: $rdf.Statement) => {
tbody.appendChild(renderRdfTriple(context, triple));
});
let triples = context.store.statements.sort(
(a: $rdf.Statement, b: $rdf.Statement): number => {
var r = a.subject.value.localeCompare(b.subject.value);
if (r != 0) return r;
r = a.predicate.value.localeCompare(b.predicate.value);
if (r != 0) return r;
return a.object.value.localeCompare(b.object.value);
}
);
let currentUri: string = null;
triples.forEach((triple: $rdf.Statement) => {
if (
triple.subject.termType == "NamedNode" &&
triple.subject.value != currentUri
) {
// new subject
currentUri = triple.subject.value;
tbody.appendChild(renderNewSubject(context, currentUri));
}
tbody.appendChild(renderRdfTriple(context, triple));
});
table.appendChild(tbody);
result.appendChild(table);
......@@ -97,6 +105,30 @@ export class RdfTriplesRendering implements implementation.ViewImplementation {
}
}
function renderNewSubject(
context: application.RenderingContext,
uri: string
): HTMLElement {
let tr = document.createElement("tr");
let td = document.createElement("td");
let selector = document.createElement("a");
selector.onclick = (event: Event) => {
event.preventDefault();
context.event.onSelectAsPrimaryTopic(uri);
return false;
};
selector.classList.add("btn");
selector.appendChild(document.createTextNode("Select \u25BC as topic"));
td.appendChild(selector);
let spacer = document.createElement("span");
spacer.appendChild(document.createTextNode(" "));
td.appendChild(spacer);
tr.appendChild(td);
tr.appendChild(document.createElement("td"));
tr.appendChild(document.createElement("td"));
return tr;
}
function renderRdfTriple(
context: application.RenderingContext,
triple: $rdf.Statement
......
......@@ -74,6 +74,11 @@ export interface UserEvents {
* @param uri The uri of the resource
*/
onRequestNavigateTo(uri: string): void;
/**
* Reacts to a user selecting a resource as the primary topic
* @param uri The uri of the resource
*/
onSelectAsPrimaryTopic(uri: string): void;
}
/**
......
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