Commit a6ce1bf1 authored by Laurent Wouters's avatar Laurent Wouters
Browse files

[feature] Display application state

parent f6f900fb955d
......@@ -264,6 +264,7 @@ function onRequestNavigateTo(uri: string) {
// already visited
onNavigatedTo(resource);
} else {
currentApp.onLoad("Fetching data");
// loads the resource and then navigate to it
loadObservationsFor(uri)
.then((observation: ResourceObservedData) => {
......@@ -301,16 +302,27 @@ function onNavigatedTo(resource: ResourceData): void {
* @param resource The resource
*/
function onReachedResource(resource: ResourceData): void {
currentApp.onLoad("Loading data");
loadRdfStore(resource)
.then((store: $rdf.Formula) => {
let context = newRenderingContext(resource, store);
currentApp.onLoad("Loading view registry");
getViewRegistry()
.then((registry: definition.ViewRegistry) => {
currentApp.registry = registry;
currentApp.onLoad("Fetching views");
implementation
.newRenderer(registry, getResourceContent)
.then((renderer: application.ViewRenderer) => {
let context = newRenderingContext(resource, store);
currentApp.onContent(renderer, context);
currentApp.onLoad("Rendering");
let rendering = renderer.render(context, context.root);
if (rendering != null) {
rendering.suggestedResources.forEach((uri: string) => {
onSuggestedResource(uri);
});
currentApp.onContent(registry, rendering);
} else {
currentApp.onError("Rendering failed!");
}
})
.catch((reason: any) => {
currentApp.onError(reason.toString());
......@@ -367,7 +379,8 @@ enum StateType {
class State {
stateType: StateType;
message: string;
context: application.RenderingContext;
registry: definition.ViewRegistry;
rendering: application.ViewRendering;
}
/**
......@@ -375,33 +388,44 @@ class State {
*/
class MainViewer {
state: State;
registry: definition.ViewRegistry;
renderer: application.ViewRenderer;
constructor() {
this.state = {
stateType: StateType.Loading,
message: "Loading",
context: null
message: "Loading ...",
registry: null,
rendering: null
};
this.registry = null;
this.renderer = null;
}
/**
* Sets the application on a loading state
* @param description The current loading state
*/
public onLoad(description: string): void {
this.state = {
stateType: StateType.Loading,
message: "Loading ... " + description,
registry: null,
rendering: null
};
this.onUpdate();
}
/**
* Sets the content for the application
* @param content The content
* @param root The URI of the root resource
* @param registry The view registry
* @param rendering The rendering for a view
*/
public onContent(
renderer: application.ViewRenderer,
context: application.RenderingContext
registry: definition.ViewRegistry,
rendering: application.ViewRendering
): void {
this.renderer = renderer;
this.state = {
stateType: StateType.Displaying,
message: "",
context: context
registry: registry,
rendering: rendering
};
this.onUpdate();
}
......@@ -414,7 +438,8 @@ class MainViewer {
this.state = {
stateType: StateType.Error,
message: description,
context: null
registry: null,
rendering: null
};
this.onUpdate();
}
......@@ -427,20 +452,14 @@ class MainViewer {
while (root.hasChildNodes()) {
root.removeChild(root.lastChild);
}
let rendering = this.render(root);
if (rendering != null) {
rendering.suggestedResources.forEach((uri: string) => {
onSuggestedResource(uri);
});
}
this.render(root);
}
/**
* Renders the application
* @param root The root DOM element to render to
*/
private render(root: HTMLElement): application.ViewRendering {
let result = null;
private render(root: HTMLElement): void {
let sub = document.createElement("div");
sub.style.width = "90%";
sub.style.marginLeft = "5%";
......@@ -462,47 +481,34 @@ class MainViewer {
div.appendChild(document.createTextNode(this.state.message));
sub.appendChild(div);
} else if (this.state.stateType == StateType.Displaying) {
result = this.renderer.render(
this.state.context,
this.state.context.root
);
if (result == null || result == undefined) {
let div = document.createElement("div");
div.classList.add("alert");
div.classList.add("alert-danger");
div.setAttribute("role", "alert");
div.appendChild(document.createTextNode("Rendering failed!"));
sub.appendChild(div);
} else {
let styles = getAllNodes(document.head, "style");
for (var i = 0; i != styles.length; i++) {
document.head.removeChild(styles[i]);
}
let scripts = getAllNodes(document.body, "script");
for (var i = 0; i != scripts.length; i++) {
document.body.removeChild(scripts[i]);
}
let descriptor = this.registry.descriptors[result.viewId];
for (var i = 0; i != descriptor.resourceCss.length; i++) {
getResourceContent(descriptor.resourceCss[i]).then(
(content: string) => {
let style = document.createElement("style");
style.appendChild(document.createTextNode(content));
document.head.appendChild(style);
}
);
}
for (var i = 0; i != descriptor.resourceJs.length; i++) {
getResourceContent(descriptor.resourceJs[i]).then(
(content: string) => {
let script = document.createElement("script");
script.appendChild(document.createTextNode(content));
document.body.appendChild(script);
}
);
}
sub.appendChild(result.dom);
let styles = getAllNodes(document.head, "style");
for (var i = 0; i != styles.length; i++) {
document.head.removeChild(styles[i]);
}
let scripts = getAllNodes(document.body, "script");
for (var i = 0; i != scripts.length; i++) {
document.body.removeChild(scripts[i]);
}
let descriptor = this.state.registry.descriptors[
this.state.rendering.viewId
];
for (var i = 0; i != descriptor.resourceCss.length; i++) {
getResourceContent(descriptor.resourceCss[i]).then(
(content: string) => {
let style = document.createElement("style");
style.appendChild(document.createTextNode(content));
document.head.appendChild(style);
}
);
}
for (var i = 0; i != descriptor.resourceJs.length; i++) {
getResourceContent(descriptor.resourceJs[i]).then((content: string) => {
let script = document.createElement("script");
script.appendChild(document.createTextNode(content));
document.body.appendChild(script);
});
}
sub.appendChild(this.state.rendering.dom);
} else {
let div = document.createElement("div");
div.classList.add("alert");
......@@ -512,7 +518,6 @@ class MainViewer {
sub.appendChild(div);
}
root.appendChild(sub);
return result;
}
}
......
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