Commit 9c83f14c authored by Laurent Wouters's avatar Laurent Wouters
Browse files

[fix] Fixed linting issues with ldbrowser

parent 9bd560bb123c
......@@ -31,11 +31,9 @@ import {
ObservableContent,
observeContent,
getAcceptRdf,
ORIGIN_KIND_DIRECT,
ORIGIN_KIND_LINKED_HTML,
ORIGIN_KIND_REDIRECTED,
DocumentStatus,
DocumentObservations
DocumentObservations,
OriginKind
} from "../common/data";
import { Message, activateTab } from "../common/messages";
/// <reference path="./fallback.d.ts"/>
......@@ -54,7 +52,7 @@ let allObservations: ObservedResourceRegistry = {};
/**
* The observations for an opening tab
*/
let openingObservations: DocumentObservations = null;
let openingObservations: DocumentObservations | null = null;
/**
* The reference view registry
*/
......@@ -65,7 +63,9 @@ let registry: definition.ViewRegistry = new definition.ViewRegistry();
let cache: definition.ViewResourceCache = {};
reloadRegistryFromStorage(registry)
.then((registry: definition.ViewRegistry) => {})
.then((registry: definition.ViewRegistry) => {
/* do nothing */
})
.catch((reason: string) => {
console.log(reason);
});
......@@ -77,8 +77,8 @@ reloadRegistryFromStorage(registry)
function onObservedTabUpdated(tabId: number): void {
let observation = resolveObservationsForTab(allObservations, tabId);
let isLit =
observation.status != DocumentStatus.off || hasDetectedData(observation);
let hasPopup = observation.status == DocumentStatus.active;
observation.status !== DocumentStatus.off || hasDetectedData(observation);
let hasPopup = observation.status === DocumentStatus.active;
if (isLit) {
chrome.pageAction.show(tabId);
......@@ -97,12 +97,14 @@ function onObservedTabUpdated(tabId: number): void {
* @param name The name of a header
*/
function getHeader(
headers: chrome.webRequest.HttpHeader[],
headers: chrome.webRequest.HttpHeader[] | undefined,
name: string
): string {
for (var i = 0; i != headers.length; i++) {
if (headers[i].name == name) {
return headers[i].value;
): string | null {
if (headers === undefined) return null;
for (let i = 0; i !== headers.length; i++) {
if (headers[i].name === name) {
let v = headers[i].value;
if (v !== undefined) return v;
}
}
return null;
......@@ -119,8 +121,8 @@ function setHeader(
name: string,
value: string
): void {
for (var i = 0; i != headers.length; i++) {
if (headers[i].name == name) {
for (let i = 0; i !== headers.length; i++) {
if (headers[i].name === name) {
headers[i].value = value;
}
}
......@@ -135,31 +137,30 @@ function onSendingHeaders(
details: chrome.webRequest.WebRequestHeadersDetails
): chrome.webRequest.BlockingResponse {
if (
details.tabId == -1 || // invalid tab
details.frameId != 0 || // not the main top-level frame
details.method != "GET" || // not a GET method
details.type != "main_frame" // not the top-level document
)
details.tabId === -1 || // invalid tab
details.frameId !== 0 || // not the main top-level frame
details.method !== "GET" || // not a GET method
details.type !== "main_frame" // not the top-level document
) {
return {};
}
let observation = allObservations[details.tabId];
if (observation == null || observation == undefined) {
if (openingObservations == null) return {};
if (observation === undefined) {
if (openingObservations === null) return {};
// this is tab opening from the extension
observation = openingObservations;
}
// do not request RDF for off and asleep tabs
if (
observation.status == DocumentStatus.off ||
observation.status == DocumentStatus.asleep
)
observation.status === DocumentStatus.off ||
observation.status === DocumentStatus.asleep
) {
return {};
}
if (
!observation.origin ||
observation.origin.kind != ORIGIN_KIND_REDIRECTED
) {
if (observation.origin.kind !== OriginKind.Redirected) {
observation.origin = {
kind: ORIGIN_KIND_DIRECT,
kind: OriginKind.Direct,
url: details.url
};
}
......@@ -167,14 +168,13 @@ function onSendingHeaders(
if (observation.preemptable) {
// this tab has been preempted
let headers = details.requestHeaders;
if (headers === undefined) headers = [];
setHeader(headers, "Accept", getAcceptRdf(false));
return { requestHeaders: headers };
} else if (
observation.negotiated != null &&
observation.negotiated != undefined
) {
} else if (observation.negotiated !== null) {
// this tab has negotiated content
let headers = details.requestHeaders;
if (headers === undefined) headers = [];
setHeader(headers, "Accept", observation.negotiated.contentType);
return { requestHeaders: headers };
}
......@@ -189,21 +189,22 @@ function onHeadersReceived(
details: chrome.webRequest.WebResponseHeadersDetails
): chrome.webRequest.BlockingResponse {
if (
details.tabId == -1 || // invalid tab
details.frameId != 0 || // not the main top-level frame
details.method != "GET" || // not a GET method
details.tabId === -1 || // invalid tab
details.frameId !== 0 || // not the main top-level frame
details.method !== "GET" || // not a GET method
details.statusCode < 200 || // not an OK
details.statusCode >= 400 || // redirect or failure no handled
details.type != "main_frame" // not the top-level document
)
details.type !== "main_frame" // not the top-level document
) {
return {};
}
if (details.statusCode >= 300) {
let observation = allObservations[details.tabId];
if (details.statusCode == 303 && observation) {
if (details.statusCode === 303 && observation) {
// mark as negotiated
observation.origin = {
kind: ORIGIN_KIND_REDIRECTED,
kind: OriginKind.Redirected,
url: details.url
};
}
......@@ -223,24 +224,27 @@ function onHeadersReceived(
}
let contentType = getHeader(details.responseHeaders, "Content-Type");
if (contentType == null || contentType == undefined) {
if (contentType === null) {
// no content
return {};
}
index = contentType.indexOf(";");
if (index >= 0) contentType = contentType.substring(0, index);
let linkHeader = getHeader(details.responseHeaders, "Link");
let observable: ObservableContent = {
content: null,
content: "",
contentType: contentType,
linkHeader: getHeader(details.responseHeaders, "Link"),
linkHeader: linkHeader !== null ? linkHeader : "",
url: details.url
};
let obs = observeContent(observable, {
kind: ORIGIN_KIND_DIRECT,
kind: OriginKind.Direct,
url: details.url
});
let observation = resolveObservationsForTab(allObservations, details.tabId);
if (observation.origin == null) observation.origin = obs.origin;
if (observation.origin.kind === OriginKind.Unkown) {
observation.origin = obs.origin;
}
observation.url = obs.url;
observation.primaryTopic = obs.primaryTopic;
observation.sources = obs.sources;
......@@ -249,6 +253,7 @@ function onHeadersReceived(
if (obs.preemptable) {
// modify header
let headers = details.responseHeaders;
if (headers === undefined) headers = [];
setHeader(headers, "Content-Type", "text/plain");
return { responseHeaders: headers };
}
......@@ -276,7 +281,7 @@ function onHeadersReceived(
function onCompleted(
details: chrome.webNavigation.WebNavigationFramedCallbackDetails
): void {
if (details.tabId == -1 || details.frameId != 0) return;
if (details.tabId === -1 || details.frameId !== 0) return;
onObservedTabUpdated(details.tabId);
}
......@@ -297,34 +302,38 @@ F.registerNavigations(onCompleted);
chrome.runtime.onMessage.addListener(
(request: Message<any>, sender, sendResponse) => {
if (request.requestType == "SubmitHtmlLinks") {
if (request.requestType === "SubmitHtmlLinks") {
if (sender.tab === undefined || sender.tab.id === undefined) return;
let observation = resolveObservationsForTab(
allObservations,
sender.tab.id
);
if (observation.primaryTopic == null || observation.primaryTopic == "") {
observation.primaryTopic = detectTopicOnlinks(request.payload);
if (observation.primaryTopic === "") {
let topic = detectTopicOnlinks(request.payload);
observation.primaryTopic = topic !== null ? topic : "";
}
observation.sources = observation.sources.concat(
detectDataOnLinks(request.payload, {
kind: ORIGIN_KIND_LINKED_HTML,
kind: OriginKind.HtmlLink,
url: observation.url
})
);
onObservedTabUpdated(sender.tab.id);
} else if (request.requestType == "GetTabId") {
} else if (request.requestType === "GetTabId") {
if (sender.tab === undefined || sender.tab.id === undefined) return;
sendResponse(sender.tab.id);
} else if (request.requestType == "GetObservations") {
} else if (request.requestType === "GetObservations") {
let tabId = request.payload;
if (tabId == null || tabId == undefined) {
if (tabId === null || tabId === undefined) {
// deduce from the sender
if (sender.tab === undefined || sender.tab.id === undefined) return;
tabId = sender.tab.id;
}
let observation = resolveObservationsForTab(allObservations, tabId);
sendResponse(observation);
} else if (request.requestType == "GetViewRegistry") {
} else if (request.requestType === "GetViewRegistry") {
sendResponse(registry);
} else if (request.requestType == "AddNewSource") {
} else if (request.requestType === "AddNewSource") {
addViewSource(registry, request.payload)
.then((newRegistry: definition.ViewRegistry) => {
sendResponse(newRegistry);
......@@ -333,7 +342,7 @@ chrome.runtime.onMessage.addListener(
console.log("Failed to reload registry: " + reason);
});
return true;
} else if (request.requestType == "RemoveSource") {
} else if (request.requestType === "RemoveSource") {
removeViewSource(registry, request.payload)
.then((newRegistry: definition.ViewRegistry) => {
sendResponse(newRegistry);
......@@ -342,7 +351,7 @@ chrome.runtime.onMessage.addListener(
console.log("Failed to reload registry: " + reason);
});
return true;
} else if (request.requestType == "ReloadViewRegistry") {
} else if (request.requestType === "ReloadViewRegistry") {
cache = {};
reloadRegistryFromStorage(registry)
.then((newRegistry: definition.ViewRegistry) => {
......@@ -352,7 +361,7 @@ chrome.runtime.onMessage.addListener(
console.log("Failed to reload registry: " + reason);
});
return true;
} else if (request.requestType == "GetResourceContent") {
} else if (request.requestType === "GetResourceContent") {
definition
.getResourceContent(cache, request.payload)
.then((content: string) => {
......@@ -362,7 +371,7 @@ chrome.runtime.onMessage.addListener(
console.log("Failed to fetch the resource: " + reason);
});
return true;
} else if (request.requestType == "FetchObservable") {
} else if (request.requestType === "FetchObservable") {
fetchObservableAt(request.payload)
.then((content: ObservableContent) => {
sendResponse({ ok: content, error: null });
......@@ -371,25 +380,33 @@ chrome.runtime.onMessage.addListener(
sendResponse({ ok: null, error: reason });
});
return true;
} else if (request.requestType == "TurnOffForTab") {
} else if (request.requestType === "TurnOffForTab") {
let tabId = request.payload;
if (tabId == null || tabId == undefined) {
if (tabId === null || tabId === undefined) {
// deduce from the sender
if (sender.tab === undefined || sender.tab.id === undefined) return;
tabId = sender.tab.id;
}
let observation = allObservations[tabId];
if (observation == undefined) sendResponse(null);
if (observation === undefined) {
sendResponse(null);
return;
}
observation.status = DocumentStatus.asleep;
onObservedTabUpdated(tabId);
sendResponse(observation);
} else if (request.requestType == "ToggleRawForTab") {
} else if (request.requestType === "ToggleRawForTab") {
let tabId = request.payload;
if (tabId == null || tabId == undefined) {
if (tabId === null || tabId === undefined) {
// deduce from the sender
if (sender.tab === undefined || sender.tab.id === undefined) return;
tabId = sender.tab.id;
}
let observation = allObservations[tabId];
if (observation == undefined) sendResponse(null);
if (observation === undefined) {
sendResponse(null);
return;
}
observation.status = DocumentStatus.activeRaw;
onObservedTabUpdated(tabId);
sendResponse(observation);
......@@ -398,12 +415,13 @@ chrome.runtime.onMessage.addListener(
);
chrome.pageAction.onClicked.addListener((tab: chrome.tabs.Tab) => {
if (tab.id === undefined) return;
let observation = resolveObservationsForTab(allObservations, tab.id);
if (observation.status == DocumentStatus.off) {
if (observation.status === DocumentStatus.off) {
// this tab was off
let isNegotiated =
observation.negotiated != null && observation.negotiated != undefined;
let isNegotiated = observation.negotiated !== null;
let callback = (openedTab: chrome.tabs.Tab) => {
if (openedTab.id === undefined || openingObservations === null) return;
allObservations[openedTab.id] = openingObservations;
openingObservations = null;
onObservedTabUpdated(openedTab.id);
......@@ -415,6 +433,7 @@ chrome.pageAction.onClicked.addListener((tab: chrome.tabs.Tab) => {
"ldbrowser/index.html?target=" + encodeURIComponent(observation.url)
);
openingObservations = JSON.parse(JSON.stringify(observation));
if (openingObservations === null) return;
openingObservations.status = DocumentStatus.active;
let promise: any = chrome.tabs.create(
{
......@@ -426,12 +445,12 @@ chrome.pageAction.onClicked.addListener((tab: chrome.tabs.Tab) => {
},
callback
);
if (promise != undefined && promise != null) {
if (promise !== undefined && promise !== null) {
promise.then(callback);
}
} else if (
observation.status == DocumentStatus.asleep ||
observation.status == DocumentStatus.activeRaw
observation.status === DocumentStatus.asleep ||
observation.status === DocumentStatus.activeRaw
) {
observation.status = DocumentStatus.active;
onObservedTabUpdated(tab.id);
......
......@@ -33,8 +33,8 @@ import {
ResourceUserCommand,
DocumentSource,
DocumentObservations,
ORIGIN_KIND_DIRECT,
Origin
Origin,
OriginKind
} from "../common/data";
import {
getResourceContent,
......@@ -52,9 +52,9 @@ function getBrowserLanguage(): application.Language {
if (i > 0) lang = lang.substring(0, i);
let result = LANGUAGES.find(
(language: application.Language) =>
language.iso639_1 == lang || language.iso639_2 == lang
language.iso639_1 === lang || language.iso639_2 === lang
);
if (result != null && result != undefined) return result;
if (result !== undefined) return result;
return application.NO_LANGUAGE;
}
......@@ -122,7 +122,7 @@ export interface LDBrowser {
/**
* Gets the current resource
*/
getCurrentResource(): ResourceData;
getCurrentResource(): ResourceData | null;
/**
* Sets the new user command for the current resouce
* @param command The new user command
......@@ -159,7 +159,14 @@ class LDBrowserImpl implements LDBrowser {
// Listens to history state events from the browser
let self = this;
window.onpopstate = function(event) {
self.onReachedUri(event.state.uri);
self
.onReachedUri(event.state.uri)
.then((value: void) => {
// do nothing
})
.catch((reason: any) => {
console.log(reason);
});
};
}
}
......@@ -179,15 +186,15 @@ class LDBrowserImpl implements LDBrowser {
/**
* The history of resources
*/
private currentData: ResourceData;
private currentData: ResourceData | null;
/**
* The data for each known resource
*/
private cacheResources: { [uri: string]: ResourceData };
private cacheResources: { [uri: string]: ResourceData | undefined };
/**
* The cache of loaded RDF data
*/
private cacheStores: { [uri: string]: $rdf.Formula };
private cacheStores: { [uri: string]: $rdf.Formula | undefined };
/**
* Resolves an uri into a resource
......@@ -212,8 +219,8 @@ class LDBrowserImpl implements LDBrowser {
resolve: (resource: ResourceData) => void,
reject: (reason: any) => void
) => {
let data: ResourceData = self.cacheResources[resource.uri];
if (data != null && data != undefined) return resolve(data);
let data = self.cacheResources[resource.uri];
if (data !== undefined) return resolve(data);
data = {
resource: resource,
......@@ -223,11 +230,13 @@ class LDBrowserImpl implements LDBrowser {
supplements: []
};
let count = resource.uris.length;
let onObservations = (observations: DocumentObservations) => {
if (observations != null)
let onObservations = (observations: DocumentObservations | null) => {
if (data === undefined) return;
if (observations !== null) {
data.observations[observations.url] = observations;
}
count--;
if (count == 0) resolve(data);
if (count === 0) resolve(data);
};
resource.uris.forEach((uri: string) => {
......@@ -240,7 +249,7 @@ class LDBrowserImpl implements LDBrowser {
origin
? origin
: {
kind: ORIGIN_KIND_DIRECT,
kind: OriginKind.Redirected,
url: uri
}
)
......@@ -268,7 +277,7 @@ class LDBrowserImpl implements LDBrowser {
reject: (reason: any) => void
) => {
let cached = self.cacheStores[data.resource.uri];
if (cached != null && cached != undefined) return resolve(cached);
if (cached !== undefined) return resolve(cached);
let store = $rdf.graph();
data.resource.uris.forEach(
(uri: string) => (self.cacheStores[uri] = store)
......@@ -280,7 +289,8 @@ class LDBrowserImpl implements LDBrowser {
.loadRdfStoreFromSources(sources, store)
.then((loaded: number) => {
// register that the number of loaded triples come from this url
data.observations[url].totalLoaded += loaded;
let observations = data.observations[url];
if (observations) observations.totalLoaded += loaded;
})
.catch((reason: any) => {
// do nothing
......@@ -342,7 +352,9 @@ class LDBrowserImpl implements LDBrowser {
store,
source.url,
source.contentType,
undefined
(error: any, kb: $rdf.Formula) => {
// do nothing
}
);
source.loaded = store.statements.length - before;
resolve(source.loaded);
......@@ -380,9 +392,11 @@ class LDBrowserImpl implements LDBrowser {
browserLanguage: BROWSER_LANGUAGE
};
let options = {
view: resource.command.isAutomatic ? null : resource.command.selectedView,
view: resource.command.isAutomatic
? undefined
: resource.command.selectedView,
language: resource.command.isAutomatic
? null
? undefined
: resource.command.selectedLanguage
};
topic.uris.forEach((uri: string) => (context.options[uri] = options));
......@@ -400,12 +414,13 @@ class LDBrowserImpl implements LDBrowser {
let maxValue = -1;
Object.keys(data.observations).forEach((url: string) => {
let observations = data.observations[url];
if (observations.totalLoaded > maxValue) {
if (observations !== undefined && observations.totalLoaded > maxValue) {
maxUri = url;
maxValue = observations.totalLoaded;
}
});
this.navigateTo(maxUri != null ? maxUri : resource.uri);
// tslint:disable-next-line:strict-type-predicates
this.navigateTo(maxUri !== null ? maxUri : resource.uri);
}
/**
......@@ -413,10 +428,16 @@ class LDBrowserImpl implements LDBrowser {
* @param resource The selected resource
*/
private onSelectAsPrimaryTopic(resouce: application.Resource): void {
if (this.currentData == null) return;
if (this.currentData === null) return;
this.currentData.command.isAutomatic = false;
this.currentData.command.selectedTopic = resouce;
this.refresh();
this.refresh()
.then((value: any) => {
// do nothing
})
.catch((reason: any) => {
console.log(reason);
});
}
/**
......@@ -435,27 +456,36 @@ class LDBrowserImpl implements LDBrowser {
.map((c: application.Resource) => application.sameResources(r, c))
.reduce((acc: boolean, v: boolean) => acc || v, false)
);
if (resources.length == 0) return;
if (resources.length === 0) return;
let parentStore = await this.resolveRdfStore(data);
let self = this;
let count = 0;
let onFinishedPart = function() {
count++;
if (count >= resources.length) self.refresh();
if (count >= resources.length) {
self
.refresh()
.then((value: any) => {
// do nothing
})
.catch((reason: any) => {
console.log(reason);
});
}
};
resources.forEach(async (resource: application.Resource) => {
resources.forEach(async(resource: application.Resource) => {
let childResource = await self.resolveResourceData(resource);
let childStore = await self.resolveRdfStore(childResource);
childStore.statements.forEach((statement: $rdf.Statement) => {
parentStore.add(
statement.subject.termType == "NamedNode"
statement.subject.termType === "NamedNode"
? parentStore.sym(statement.subject.value)
: statement.subject,
statement.predicate.termType == "NamedNode"
statement.predicate.termType === "NamedNode"
? parentStore.sym(statement.predicate.value)
: statement.predicate,
statement.object.termType == "NamedNode"
statement.object.termType === "NamedNode"
? parentStore.sym(statement.object.value)
: statement.object,
statement.graph
......@@ -487,12 +517,14 @@ class LDBrowserImpl implements LDBrowser {