Commit 3022f2dc authored by Laurent Wouters's avatar Laurent Wouters
Browse files

[fix] Fixed linting issues with libview

parent 7f07c5c8bd26
......@@ -26,16 +26,14 @@ import * as $rdf from "rdflib";
*/
function uriUnicodeEscape(uri: string): string {
let result = "";
for (var i = 0; i != uri.length; i++) {
for (let i = 0; i !== uri.length; i++) {
let cc = uri.charCodeAt(i);
if (cc >= 0x80) {
let offset = cc >= 0xd800 && cc <= 0xdbff ? 1 : 0;
let cp = uri
.codePointAt(i)
.toString(16)
.toUpperCase();
let cp = uri.codePointAt(i);
let cpAsString = cp === undefined ? "" : cp.toString(16).toUpperCase();
result += "\\u";
let leading = 4 + offset * 4 - cp.length;
let leading = 4 + offset * 4 - cpAsString.length;
while (leading > 0) {
result += "0";
leading--;
......@@ -55,16 +53,16 @@ function uriUnicodeEscape(uri: string): string {
*/
function uriUnicodeUnescape(uri: string): string {
let result = "";
for (var i = 0; i != uri.length; i++) {
for (let i = 0; i !== uri.length; i++) {
let c = uri.charAt(i);
if (c == "\\") {
if (c === "\\") {
let n = uri.charAt(i + 1);
if (n == "u") {
if (n === "u") {
// \ u XXXX for unicode characters in the BMP
let codepoint = Number.parseInt(uri.substring(i + 2, i + 6), 16);
result += String.fromCodePoint(codepoint);
i += 5;
} else if (n == "U") {
} else if (n === "U") {
// \ U XXXXXXXX for unicode characters outside the BMP
let codepoint = Number.parseInt(uri.substring(i + 2, i + 10), 16);
result += String.fromCodePoint(codepoint);
......@@ -142,7 +140,7 @@ export function cloneResource(original: Resource): Resource {
* @param second The other resource
*/
export function sameResources(first: Resource, second: Resource): boolean {
for (var i = 0; i != first.uris.length; i++) {
for (let i = 0; i !== first.uris.length; i++) {
if (second.uris.indexOf(first.uris[i]) >= 0) return true;
}
return false;
......@@ -160,7 +158,7 @@ export function probeResource(resource: Resource): Promise<Resource> {
let count = resource.uris.length;
let onFinished = function() {
count -= 1;
if (count == 0) {
if (count === 0) {
let main =
remaining.indexOf(resource.uri) >= 0 ? resource.uri : remaining[0];
resolve({ uri: main, uris: remaining } as Resource);
......@@ -187,9 +185,10 @@ function probeResourceUri(uri: string): Promise<String> {
(resolve: (result: String) => void, reject: (reason: any) => void) => {
let xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function() {
if (xmlHttp.readyState == 4) {
if (xmlHttp.status < 200 || xmlHttp.status >= 300)
if (xmlHttp.readyState === 4) {
if (xmlHttp.status < 200 || xmlHttp.status >= 300) {
return reject("HTTP error: " + xmlHttp.status);
}
resolve(uri);
}
};
......@@ -313,7 +312,7 @@ export interface RenderingResult {
/**
* The DOM elements representing the rendering
*/
dom: HTMLElement;
dom: HTMLElement | null;
/**
* The identifier of the selected view
*/
......
......@@ -73,52 +73,52 @@ export class RdfEntity {
this.uris.push(node.uri);
}
public getEntityForE(property: RdfEntity): RdfEntity {
public getEntityForE(property: RdfEntity): RdfEntity | null {
let r = this.getEntitiesForE(property);
return r.length == 0 ? null : r[0];
return r.length === 0 ? null : r[0];
}
public getEntityForS(property: string): RdfEntity {
public getEntityForS(property: string): RdfEntity | null {
let r = this.getEntitiesForS(property);
return r.length == 0 ? null : r[0];
return r.length === 0 ? null : r[0];
}
public getEntityForN(property: $rdf.NamedNode): RdfEntity {
public getEntityForN(property: $rdf.NamedNode): RdfEntity | null {
let r = this.getEntitiesForN(property);
return r.length == 0 ? null : r[0];
return r.length === 0 ? null : r[0];
}
public getEntitiesForE(property: RdfEntity): RdfEntity[] {
return this.getValuesForE(property)
.filter((node: $rdf.Node) => node.termType == "NamedNode")
.filter((node: $rdf.Node) => node.termType === "NamedNode")
.map((node: $rdf.NamedNode) => this.manager.entityForUri(node.uri));
}
public getEntitiesForS(property: string): RdfEntity[] {
return this.getValuesForE(this.manager.entityForUri(property))
.filter((node: $rdf.Node) => node.termType == "NamedNode")
.filter((node: $rdf.Node) => node.termType === "NamedNode")
.map((node: $rdf.NamedNode) => this.manager.entityForUri(node.uri));
}
public getEntitiesForN(property: $rdf.NamedNode): RdfEntity[] {
return this.getValuesForE(this.manager.entityForUri(property.uri))
.filter((node: $rdf.Node) => node.termType == "NamedNode")
.filter((node: $rdf.Node) => node.termType === "NamedNode")
.map((node: $rdf.NamedNode) => this.manager.entityForUri(node.uri));
}
public getValueForE(property: RdfEntity): $rdf.Node {
public getValueForE(property: RdfEntity): $rdf.Node | null {
let r = this.getValuesForE(property);
return r.length == 0 ? null : r[0];
return r.length === 0 ? null : r[0];
}
public getValueForS(property: string): $rdf.Node {
public getValueForS(property: string): $rdf.Node | null {
let r = this.getValuesForS(property);
return r.length == 0 ? null : r[0];
return r.length === 0 ? null : r[0];
}
public getValueForN(property: $rdf.NamedNode): $rdf.Node {
public getValueForN(property: $rdf.NamedNode): $rdf.Node | null {
let r = this.getValuesForN(property);
return r.length == 0 ? null : r[0];
return r.length === 0 ? null : r[0];
}
public getValuesForE(property: RdfEntity): $rdf.Node[] {
......@@ -145,8 +145,8 @@ export class RdfEntity {
*/
private doGetValues(subject: RdfEntity, property: RdfEntity): $rdf.Node[] {
let results: $rdf.Node[] = [];
for (var i = 0; i != subject.aliases.length; i++) {
for (var j = 0; j != property.aliases.length; j++) {
for (let i = 0; i !== subject.aliases.length; i++) {
for (let j = 0; j !== property.aliases.length; j++) {
let values = subject.manager.store.each(
subject.aliases[i],
property.aliases[j],
......@@ -168,8 +168,8 @@ export class RdfEntity {
*/
private doGetSubjects(property: RdfEntity, object: RdfEntity): $rdf.Node[] {
let results: $rdf.Node[] = [];
for (var i = 0; i != object.aliases.length; i++) {
for (var j = 0; j != property.aliases.length; j++) {
for (let i = 0; i !== object.aliases.length; i++) {
for (let j = 0; j !== property.aliases.length; j++) {
let values = object.manager.store.each(
undefined,
property.aliases[j],
......@@ -225,15 +225,15 @@ export class RdfEntityStore {
this.entities = {};
this.aliases = {};
this.inverses = {};
if (aliases != null && aliases != undefined) {
for (var i = 0; i != aliases.length; i++) {
if (aliases) {
for (let i = 0; i !== aliases.length; i++) {
let series = aliases[i];
for (var j = 0; j != series.length; j++) {
for (let j = 0; j !== series.length; j++) {
this.aliases[series[j]] = series;
}
}
}
if (inverses != null && inverses != undefined) {
if (inverses) {
Object.keys(inverses).forEach((uri: string) => {
this.inverses[uri] = inverses[uri];
this.inverses[inverses[uri]] = uri;
......@@ -246,10 +246,8 @@ export class RdfEntityStore {
* @param uri The URI
*/
public sym(uri: string): $rdf.NamedNode {
if (uri == null || uri == undefined || uri.length == 0) return null;
if (this.nodes.hasOwnProperty(uri)) return this.nodes[uri];
let result = this.store.sym(uri);
if (result == null) return null;
this.nodes[uri] = result;
return result;
}
......@@ -261,7 +259,6 @@ export class RdfEntityStore {
public entityForUri(uri: string): RdfEntity {
if (this.entities.hasOwnProperty(uri)) return this.entities[uri];
let node = this.sym(uri);
if (node == null) return null;
let result = new RdfEntity(this, node);
this.entities[uri] = result;
this.resolveAliases(result);
......@@ -273,7 +270,6 @@ export class RdfEntityStore {
* @param node The named node
*/
public entityForNN(node: $rdf.NamedNode): RdfEntity {
if (node == null || node == undefined) return null;
if (this.entities.hasOwnProperty(node.uri)) return this.entities[node.uri];
let result = new RdfEntity(this, node);
this.entities[node.uri] = result;
......@@ -290,9 +286,11 @@ export class RdfEntityStore {
let self = this;
relation.uris.forEach((uri: string) => {
let inverse = self.inverses[uri];
if (inverse != null && inverse != undefined) {
if (inverse) {
let inverseEntity = self.entityForUri(inverse);
if (result.indexOf(inverseEntity) < 0) result.push(inverseEntity);
if (result.indexOf(inverseEntity) < 0) {
result.push(inverseEntity);
}
}
});
return result;
......@@ -305,7 +303,7 @@ export class RdfEntityStore {
*/
public same(uri: string, ...aliases: string[]): void {
let entity = this.entityForUri(uri);
for (var i = 0; i != aliases.length; i++) {
for (let i = 0; i !== aliases.length; i++) {
if (entity.uris.indexOf(aliases[i]) >= 0) continue;
this.entities[aliases[i]] = entity;
entity.addAlias(this.sym(aliases[i]));
......@@ -349,21 +347,21 @@ export class RdfEntityStore {
this.entities[uri] = entity;
};
for (var i = 0; i != uris.length; i++) {
for (let i = 0; i !== uris.length; i++) {
// look for (node, sameAs, x)
this.store
.each(nodes[i], sameas, undefined, undefined)
.filter((n: $rdf.Node) => n.termType == "NamedNode")
.filter((n: $rdf.Node) => n.termType === "NamedNode")
.forEach(onNamedNode);
// look for (x, sameAs, node)
this.store
.each(undefined, sameas, nodes[i], undefined)
.filter((n: $rdf.Node) => n.termType == "NamedNode")
.filter((n: $rdf.Node) => n.termType === "NamedNode")
.forEach(onNamedNode);
// look for registered aliases for this uri
let registered_aliases = this.aliases[uris[i]];
if (registered_aliases != null && registered_aliases != undefined) {
registered_aliases.forEach(onUri);
let registeredAliases = this.aliases[uris[i]];
if (registeredAliases) {
registeredAliases.forEach(onUri);
}
// look for equivalent URIs
uriEquivalents(uris[i]).forEach(onUri);
......
......@@ -166,30 +166,30 @@ export class MetaPropertyData implements MetaProperty {
*/
public load(entity: RdfEntity, loader: Loader): any {
let literals = entity.getValuesForS(this.uri);
literals = literals.filter((node: $rdf.Node) => node.termType == "Literal");
if (literals.length == 0) return this.isVector ? [] : null;
literals = literals.filter(
(node: $rdf.Node) => node.termType === "Literal"
);
if (literals.length === 0) return this.isVector ? [] : null;
let matching = literals
.map((literal: $rdf.Literal) => {
// is it tagged by one of the requested language?
for (var l = 0; l != loader.languages.length; l++) {
for (let l = 0; l !== loader.languages.length; l++) {
if (
literal.lang == loader.languages[l].iso639_1 ||
literal.lang == loader.languages[l].iso639_2
)
literal.lang === loader.languages[l].iso639_1 ||
literal.lang === loader.languages[l].iso639_2
) {
// ok => it has the priority of the language
return { literal: literal, rank: l } as RankedLiteral;
}
}
if (
literal.lang == null ||
literal.lang == undefined ||
literal.lang == ""
)
if (!literal.lang || literal.lang === "") {
// lang-tag is not defined, has more priority than other tagged literals that do not match (considered as a default)
return {
literal: literal,
rank: loader.languages.length
} as RankedLiteral;
}
// this is tagged literal that is not matched by one of the requested language
return {
literal: literal,
......@@ -208,11 +208,10 @@ export class MetaPropertyData implements MetaProperty {
if (!this.isVector) return matching[0].literal.value;
return matching
.map(
(rl: RankedLiteral) =>
rl.rank == matching[0].rank ? rl.literal.value : null
.map((rl: RankedLiteral) =>
rl.rank === matching[0].rank ? rl.literal.value : null
)
.filter((value: string) => value != null);
.filter((value: string | null) => value !== null);
}
}
......@@ -273,10 +272,10 @@ export class MetaPropertyObject implements MetaProperty {
});
let objects = result.map((e: RdfEntity) => {
let focus = e.getEntityForS("http://xmlns.com/foaf/0.1/focus");
let real = focus != null ? focus : e;
let real = focus !== null ? focus : e;
return loader.resolve(real, this.metaClass);
});
return this.isVector ? objects : objects.length == 0 ? null : objects[0];
return this.isVector ? objects : objects.length === 0 ? null : objects[0];
}
}
......@@ -317,9 +316,13 @@ export class MetaClass {
*/
public load(entity: RdfEntity, result: Loadable, loader: Loader): void {
if (this.probeEntity) {
probeResource(result.__resource).then((cleanResource: Resource) => {
result.__resource = cleanResource;
});
probeResource(result.__resource)
.then((cleanResource: Resource) => {
result.__resource = cleanResource;
})
.catch((reason: any) => {
console.log(reason);
});
}
this.loadProperties(entity, result, loader);
}
......@@ -369,7 +372,7 @@ class LoaderImpl implements Loader {
*/
public resolve(entity: RdfEntity, metaClass: MetaClass): Loadable {
let found = this.items.find((item: Item) => item.entity === entity);
if (found == undefined) {
if (found === undefined) {
found = {
entity: entity,
metaClass: metaClass,
......@@ -388,7 +391,7 @@ class LoaderImpl implements Loader {
*/
public warn(entity: RdfEntity, property: string, message: string): void {
let found = this.items.find((item: Item) => item.entity === entity);
if (found == undefined) return;
if (found === undefined) return;
found.object.__warnings[property] = message;
}
......@@ -428,7 +431,7 @@ export function loadEntity<T extends Loadable>(
);
let entity = repo.entityForUri(target.uri);
let focus = entity.getEntityForS("http://xmlns.com/foaf/0.1/focus");
if (focus != null) entity = focus;
if (focus !== null) entity = focus;
let loader = new LoaderImpl(renderer.getLanguagesFor(context, target));
loader.resolve(entity, metaClass);
......
......@@ -146,7 +146,7 @@ export interface ViewRegistrySourceRemote extends ViewRegistrySource {
function fetchDescriptors(
source: ViewRegistrySource
): Promise<ViewDescriptor[]> {
if (source.kind == ViewRegistrySourceKind.inline) {
if (source.kind === ViewRegistrySourceKind.inline) {
return new Promise<ViewDescriptor[]>(
(
resolve: (result: ViewDescriptor[] | Promise<ViewDescriptor[]>) => void,
......@@ -155,7 +155,7 @@ function fetchDescriptors(
resolve((source as ViewRegistrySourceInline).descriptors);
}
);
} else if (source.kind == ViewRegistrySourceKind.remote) {
} else if (source.kind === ViewRegistrySourceKind.remote) {
return new Promise<ViewDescriptor[]>(
(
resolve: (result: ViewDescriptor[] | Promise<ViewDescriptor[]>) => void,
......@@ -163,12 +163,12 @@ function fetchDescriptors(
) => {
let xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function() {
if (xmlHttp.readyState == 4) {
if (xmlHttp.readyState === 4) {
if (xmlHttp.status < 200 || xmlHttp.status >= 300) {
reject("HTTP error: " + xmlHttp.status);
return;
}
if (xmlHttp.responseText.length == 0) {
if (xmlHttp.responseText.length === 0) {
reject("Empty result");
return;
}
......@@ -204,9 +204,9 @@ function loadDescriptors(
resolve: (descriptors: ViewDescriptors) => void,
reject: (reason: any) => void
) => {
var finished = 0;
var target = sources.length;
var descriptors: ViewDescriptors = {};
let finished = 0;
let target = sources.length;
let descriptors: ViewDescriptors = {};
function onPartFinished() {
finished += 1;
if (finished >= target) resolve(descriptors);
......@@ -262,10 +262,14 @@ export function loadRegistry(registry: ViewRegistry): Promise<ViewRegistry> {
reject: (reason: any) => void
) => {
registry.descriptors = {};
loadDescriptors(registry.sources).then((descriptors: ViewDescriptors) => {
registry.descriptors = descriptors;
resolve(registry);
});
loadDescriptors(registry.sources)
.then((descriptors: ViewDescriptors) => {
registry.descriptors = descriptors;
resolve(registry);
})
.catch((reason: any) => {
console.log(reason);
});
}
);
}
......@@ -288,9 +292,9 @@ export function getResourceContent(
): Promise<string> {
return new Promise<string>(
(resolve: (result: string) => void, reject: (reason: any) => void) => {
if (resource.location == ViewResourceLocation.inline) {
if (resource.location === ViewResourceLocation.inline) {
resolve((resource as ViewResourceInline).content);
} else if (resource.location == ViewResourceLocation.remote) {
} else if (resource.location === ViewResourceLocation.remote) {
let uri = (resource as ViewResourceRemote).uri;
if (cache.hasOwnProperty(uri)) {
resolve(cache[uri]);
......@@ -298,8 +302,8 @@ export function getResourceContent(
}
let xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function() {
if (xmlHttp.readyState == 4) {
if (xmlHttp.status == 0 && uri.startsWith("http://")) {
if (xmlHttp.readyState === 4) {
if (xmlHttp.status === 0 && uri.startsWith("http://")) {
// try https
getResourceContent(cache, {
location: ViewResourceLocation.remote,
......
......@@ -122,7 +122,7 @@ function fetchImplementation(
let EMBEDDED = IMPL_EMBEDDED;
try {
let result = eval(content);
if (result == null || result == undefined) {
if (result === null || result === undefined) {
reject(
"Failed to load definition of " +
descriptor.name +
......@@ -130,13 +130,10 @@ function fetchImplementation(
);
return;
}
if (
descriptor.entrypoint != null &&
descriptor.entrypoint.length > 0
) {
if (descriptor.entrypoint.length > 0) {
result = result[descriptor.entrypoint];
}
if (result == null || result == undefined) {
if (result === null || result === undefined) {
reject(
"Failed to load definition of " +
descriptor.name +
......@@ -231,15 +228,12 @@ class ViewRendererImpl implements CachingViewRenderer {
getLanguagesFor(context: RenderingContext, target: Resource): Language[] {
let options = context.options[target.uri];
let result = [];
if (
options != undefined &&
options != null &&
options.language != undefined &&
options.language != null
)
if (options && options.language) {
result.push(options.language);
if (context.browserLanguage != null && context.browserLanguage != undefined)
}
if (context.browserLanguage) {
result.push(context.browserLanguage);
}
return result;
}
......@@ -249,35 +243,31 @@ class ViewRendererImpl implements CachingViewRenderer {
* @param target The target resource for this view
*/
render(context: RenderingContext, target: Resource): RenderingResult {
var view: ViewImplementation = null;
var evaluations: RenderingEvaluation[] = evaluateViewsFor(
let view: ViewImplementation | null = null;
let evaluations: RenderingEvaluation[] = evaluateViewsFor(
this.implementations,
context.store,
target
);
let options = context.options[target.uri];
if (
options != null &&
options != undefined &&
options.view != undefined &&
options.view != undefined
) {
if (options && options.view) {
// try to use the forced view
view = this.implementations[options.view];
}
if (view == null || view == undefined) {
if (view === null) {
// try to resolve a view
if (evaluations.length > 0)
if (evaluations.length > 0) {
view = this.implementations[evaluations[0].viewId];
}
}
if (view == null || view == undefined) {
if (view === null) {
// cannot resolve a view ...
return {
dom: null,
viewId: null,
viewId: "",
evaluations: evaluations,
suggestedResources: null,
languages: null
suggestedResources: [],
languages: []
};
}
let result = view.render(this, context, target);
......@@ -287,7 +277,7 @@ class ViewRendererImpl implements CachingViewRenderer {
evaluations: evaluations,
suggestedResources: result.suggestedResources
? result.suggestedResources
: null,
: [],
languages: this.getLanguagesFor(context, target)
};
}
......@@ -306,24 +296,24 @@ class ViewRendererImpl implements CachingViewRenderer {
let toAdd: ViewDescriptor[] = [];
Object.keys(registry.descriptors).forEach((id: string) => {
let candidate = self.descriptors[id];
if (candidate == null || candidate == undefined) {
if (candidate) {
toAdd.push(registry.descriptors[id]);
}
});
Object.keys(self.descriptors).forEach((id: string) => {
let candidate = registry.descriptors[id];
if (candidate == null || candidate == undefined) {
if (candidate) {
// has been deleted
delete self.descriptors[id];
delete self.implementations[id];
}
});
if (toAdd.length == 0) {
if (toAdd.length === 0) {
resolve(self);
return;
}
var finished = 0;
let finished = 0;
function onPartFinished() {
finished += 1;
if (finished >= toAdd.length) resolve(self);
......