sparnatural
Version:
Visual client-side SPARQL query builder and knowledge graph exploration tool
155 lines • 5.95 kB
JavaScript
import $ from "jquery";
/*SPARNATURAL*/
import { getSettings, mergeSettings, } from "./sparnatural/settings/defaultSettings";
import SparnaturalComponent from "./sparnatural/components/SparnaturalComponent";
import QueryLoader from "./sparnatural/querypreloading/QueryLoader";
import QueryLoaderv13 from "./sparnatural/querypreloading/QueryLoader-v13";
import { SparnaturalAttributes } from "./SparnaturalAttributes";
import { SparqlHandlerFactory } from "rdf-shacl-commons";
/*
This is the sparnatural HTMLElement.
e.g. Interface to the outside world
Used to configure the Settings and load queries
*/
export class SparnaturalElement extends HTMLElement {
constructor() {
super();
}
/**
* Call display only in the connectedCallback
*/
connectedCallback() {
this.display();
}
set customization(customization) {
getSettings().customization = customization;
}
get customization() {
return getSettings().customization;
}
display() {
// render sparnatural
this.sparnatural = new SparnaturalComponent();
// empty the content in case we re-display after an attribute change
$(this).empty();
$(this).append(this.sparnatural.html);
// parse all attributes in the HTML element
this._attributes = new SparnaturalAttributes(this);
// just set the settings with the HTML attributes
// TODO : re-enginer the global settings variable to something more OO
mergeSettings(this._attributes);
this.sparnatural.render();
}
/**
* Expands the SPARQL query according to the configuration.
* Can be called from the outside
* @returns
*/
expandSparql(query) {
return this.sparnatural.specProvider.expandSparql(query, getSettings().sparqlPrefixes);
}
/* NOTE : defaultlang is all lowercase, see https://stackoverflow.com/questions/60566257/web-components-attributechangedcallback-not-firing */
static get observedAttributes() {
return ["src", "lang", "defaultlang", "endpoint"];
}
attributeChangedCallback(name, oldValue, newValue) {
if (oldValue === newValue) {
return;
}
// prevents callback to be called on initial creation
if (oldValue != null) {
if (getSettings().debug) {
console.log(`${name}'s value has been changed from ${oldValue} to ${newValue}`);
}
switch (name) {
case "src": {
getSettings().src = newValue;
break;
}
case "lang": {
getSettings().language = newValue;
break;
}
case "defaultlang": {
getSettings().defaultLanguage = newValue;
break;
}
case "endpoint": {
getSettings().endpoints = newValue.split(" ");
break;
}
default: {
throw new Error("unknown observed attribute ${name}");
}
}
// then display/reprint
this.display();
}
}
/**
* Enable the play button when a query has finished executing
* Can be called from the outside. Removes the loading spinner on the btn
*/
enablePlayBtn() {
this.sparnatural.enablePlayBtn();
}
/**
* Disable the play button when query is triggered
* Can be called from the outside
*/
disablePlayBtn() {
this.sparnatural.disablePlayBtn();
}
/**
* Type guard to check if the provided query is a v13 SparnaturalQuery
* @param query The query to check
* @returns True if the query is a SparnaturalQuery (v13), false if it's a SparnaturalQueryIfc (v1)
*/
isV13Query(query) {
return query.where !== undefined;
}
/**
* Load a saved/predefined query in the visual query builder
* Can be called from the outside
* @param query
*/
loadQuery(query) {
if (this.isV13Query(query)) {
QueryLoaderv13.setSparnatural(this.sparnatural);
QueryLoaderv13.loadQuery(query);
}
else {
QueryLoader.setSparnatural(this.sparnatural);
QueryLoader.loadQuery(query);
}
}
/**
* Executes the provided SPARQL query, using the configured headers, and sending it to multiple
* endpoints, if configured through the catalog attribute (results are then merged in a single result set)
* @param query The SPARQL query to execute
* @param callback The callback to execute with the final query string
* @param errorCallback The callback to execute in case of an error during the query execution
*/
executeSparql(query, callback, errorCallback) {
let sparqlFetcherFactory = new SparqlHandlerFactory(getSettings().language, getSettings().localCacheDataTtl, getSettings().customization.headers, getSettings().customization.sparqlHandler,
// can be undefined
this.sparnatural.catalog);
let sparqlFetcher = sparqlFetcherFactory.buildSparqlHandler(getSettings().endpoints);
sparqlFetcher.executeSparql(query, callback, errorCallback);
}
/**
* Clears the current query.
* Can be called from the outside
*/
clear() {
this.sparnatural.BgWrapper.resetCallback();
}
}
SparnaturalElement.HTML_ELEMENT_NAME = "spar-natural";
SparnaturalElement.EVENT_SUBMIT = "submit";
SparnaturalElement.EVENT_QUERY_UPDATED = "queryUpdated";
SparnaturalElement.EVENT_RESET = "reset";
SparnaturalElement.EVENT_INIT = "init";
customElements.get(SparnaturalElement.HTML_ELEMENT_NAME) ||
window.customElements.define(SparnaturalElement.HTML_ELEMENT_NAME, SparnaturalElement);
//# sourceMappingURL=SparnaturalElement.js.map