Commit 376614b2 authored by Laurent Wouters's avatar Laurent Wouters
Browse files

Improved language selection

parent 49be4fba2395
......@@ -20,12 +20,6 @@
import { application } from "@logilab/libview";
export const NO_LANGUAGE: application.Language = {
iso639_1: "",
iso639_2: "",
name: "-- auto --"
};
/**
* Metadata about a MIME type
*/
......@@ -329,7 +323,7 @@ export class TabCommand {
this.isAutomatic = true;
this.selectedSource = "";
this.selectedView = "";
this.selectedLanguage = NO_LANGUAGE;
this.selectedLanguage = application.NO_LANGUAGE;
}
/**
* Whether the user activated the extension for this tab
......@@ -414,7 +408,7 @@ export class TabData {
this.command.isAutomatic = true;
this.command.selectedSource = "";
this.command.selectedView = "";
this.command.selectedLanguage = NO_LANGUAGE;
this.command.selectedLanguage = application.NO_LANGUAGE;
this.mainSource = NO_DATA;
this.sources = [];
this.primaryTopic = null;
......
......@@ -18,6 +18,10 @@
* with CubicWeb. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
import "chrome";
import * as $rdf from "rdflib";
import { application, definition, implementation } from "@logilab/libview";
import * as LANGUAGES from "../common/data.iso639.json";
import { TabData, NO_DATA } from "../common/data";
import {
Message,
......@@ -28,12 +32,6 @@ import {
onNavigateTabTo
} from "../common/messages";
import { loadRdfStore } from "../common/rdf";
import "chrome";
import { application, definition, implementation } from "@logilab/libview";
import {
RenderingContext,
ViewRenderer
} from "@logilab/libview/build/lib/application";
require("../common/view-defaults-impl");
/**
......@@ -49,33 +47,14 @@ function onUpdateActivate(tabData: TabData): void {
currentApp = new MainViewer();
currentApp.onUpdate();
loadRdfStore(tabData)
.then((store: any) => {
.then((store: $rdf.Formula) => {
getViewRegistry()
.then((registry: definition.ViewRegistry) => {
currentApp.registry = registry;
implementation
.newRenderer(registry, getResourceContent)
.then((renderer: application.ViewRenderer) => {
let context = {
event: {
onRequestNavigateTo: function(uri: string) {
getTabId().then((tabId: number) => {
onNavigateTabTo(tabId, uri);
});
}
},
root: tabData.primaryTopic,
store: store,
options: {}
} as RenderingContext;
context.options[tabData.primaryTopic] = {
view: tabData.command.isAutomatic
? null
: tabData.command.selectedView,
language: tabData.command.isAutomatic
? null
: tabData.command.selectedLanguage
};
let context = newRenderingContext(tabData, store);
currentApp.onContent(renderer, context);
})
.catch((reason: any) => {
......@@ -91,6 +70,52 @@ function onUpdateActivate(tabData: TabData): void {
});
}
/**
* Creates a new rendering context
* @param tabData The data for the tab
* @param store The RDF store
*/
function newRenderingContext(
tabData: TabData,
store: $rdf.Formula
): application.RenderingContext {
let context: application.RenderingContext = {
event: {
onRequestNavigateTo: function(uri: string) {
getTabId().then((tabId: number) => {
onNavigateTabTo(tabId, uri);
});
}
},
root: tabData.primaryTopic,
store: store,
options: {},
browserLanguage: getBrowserLanguage()
};
context.options[tabData.primaryTopic] = {
view: tabData.command.isAutomatic ? null : tabData.command.selectedView,
language: tabData.command.isAutomatic
? null
: tabData.command.selectedLanguage
};
return context;
}
/**
* Gets the user language selected by the browser
*/
function getBrowserLanguage(): application.Language {
let lang = navigator.language;
let i = lang.indexOf("-");
if (i > 0) lang = lang.substring(0, i);
let result = LANGUAGES.find(
(language: application.Language) =>
language.iso639_1 == lang || language.iso639_2 == lang
);
if (result != null && result != undefined) return result;
return application.NO_LANGUAGE;
}
/**
* Listen to messages from the background
*/
......@@ -120,7 +145,7 @@ enum StateType {
class State {
stateType: StateType;
message: string;
context: RenderingContext;
context: application.RenderingContext;
}
/**
......@@ -156,7 +181,10 @@ class MainViewer implements application.Application {
* @param content The content
* @param root The URI of the root resource
*/
public onContent(renderer: ViewRenderer, context: RenderingContext): void {
public onContent(
renderer: application.ViewRenderer,
context: application.RenderingContext
): void {
this.renderer = renderer;
this.state = {
stateType: StateType.Displaying,
......
......@@ -21,7 +21,7 @@
import * as LANGUAGES from "../common/data.iso639.json";
import React = require("react");
import ReactDOM = require("react-dom");
import { TabData, NO_DATA, DataSource, NO_LANGUAGE } from "../common/data";
import { TabData, NO_DATA, DataSource } from "../common/data";
import { application, definition } from "@logilab/libview";
import {
getCurrentTabId,
......@@ -162,7 +162,7 @@ class Popup extends React.Component<{}, State> {
if (this.state.tabData.command.isAutomatic) {
this.state.tabData.command.selectedSource = "";
this.state.tabData.command.selectedView = "";
this.state.tabData.command.selectedLanguage = NO_LANGUAGE;
this.state.tabData.command.selectedLanguage = application.NO_LANGUAGE;
}
this.onUpdateTabCommand();
}
......@@ -172,7 +172,7 @@ class Popup extends React.Component<{}, State> {
if (this.state.tabData.command.isAutomatic) {
this.state.tabData.command.selectedSource = "";
this.state.tabData.command.selectedView = "";
this.state.tabData.command.selectedLanguage = NO_LANGUAGE;
this.state.tabData.command.selectedLanguage = application.NO_LANGUAGE;
}
this.onUpdateTabCommand();
}
......@@ -183,11 +183,11 @@ class Popup extends React.Component<{}, State> {
value.iso639_1 == event.currentTarget.value
);
this.state.tabData.command.selectedLanguage =
language == undefined ? NO_LANGUAGE : language;
language == undefined ? application.NO_LANGUAGE : language;
if (this.state.tabData.command.isAutomatic) {
this.state.tabData.command.selectedSource = "";
this.state.tabData.command.selectedView = "";
this.state.tabData.command.selectedLanguage = NO_LANGUAGE;
this.state.tabData.command.selectedLanguage = application.NO_LANGUAGE;
}
this.onUpdateTabCommand();
}
......
......@@ -52,6 +52,15 @@ export interface Language {
name: string;
}
/**
* The language when none are selected
*/
export const NO_LANGUAGE: Language = {
iso639_1: "",
iso639_2: "",
name: ""
};
/**
* The user events that can occur in a rendered view
*/
......@@ -107,12 +116,23 @@ export interface RenderingContext {
* The handler of user events
*/
event: UserEvents;
/**
* The language used by the browser
*/
browserLanguage: Language;
}
/**
* A renderer for an application
*/
export interface ViewRenderer {
/**
* Selects the language to use for a target resource
* @param context The current rendering context
* @param target The target resource for this view
*/
getLanguageFor(context: RenderingContext, target: string): Language;
/**
* Renders a resource belonging to an RDF store
* @param context The context for the rendering
......
......@@ -22,7 +22,8 @@ import * as $rdf from "rdflib";
import {
ViewRenderer,
ViewRendering,
RenderingContext
RenderingContext,
Language
} from "./application";
import {
ViewDescriptor,
......@@ -238,6 +239,19 @@ class ViewRendererImpl implements ViewRenderer {
*/
implementations: ViewImplementations;
/**
* Selects the language to use for a target resource
* @param context The current rendering context
* @param target The target resource for this view
*/
getLanguageFor(context: RenderingContext, target: string): Language {
let options = context.options[target];
if (options == undefined || options == null) return context.browserLanguage;
if (options.language == undefined || options.language == null)
return context.browserLanguage;
return options.language;
}
/**
* Renders a resource belonging to an RDF store
* @param context The context for the rendering
......
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