sparnatural
Version:
Visual client-side SPARQL query builder and knowledge graph exploration tool
205 lines • 13 kB
JavaScript
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var _a, _QueryLoader_buildSparnatural, _QueryLoader_buildCriteriaGroup, _QueryLoader_triggerOptions, _QueryLoader_setSelectedValue, _QueryLoader_setSelectViewVariableBtn, _QueryLoader_updateOrderingOfVariables, _QueryLoader_updateNamingOfVariables, _QueryLoader_hasSelectedVar, _QueryLoader_clickOn;
import { OptionTypes } from "../components/builder-section/groupwrapper/criteriagroup/optionsgroup/OptionsGroup";
import { Order } from "../SparnaturalQueryIfc";
export default class QueryLoader {
static loadQuery(query) {
this.query = query;
// set Sparnatural quiet so it does not emit the update callbacks
this.sparnatural.setQuiet(true);
// first reset the current query
this.sparnatural.BgWrapper.resetCallback();
// build Sparnatural query
// use a deep copy of the query to avoid modifying the original copy
let clone = JSON.parse(JSON.stringify(query));
let varMapping = __classPrivateFieldGet(this, _a, "m", _QueryLoader_buildSparnatural).call(this, this.sparnatural, clone.branches);
// set the correct variable names
__classPrivateFieldGet(this, _a, "m", _QueryLoader_updateNamingOfVariables).call(this, varMapping);
// set the correct ordering of the draggables
__classPrivateFieldGet(this, _a, "m", _QueryLoader_updateOrderingOfVariables).call(this);
// then reset the quiet flag
this.sparnatural.setQuiet(false);
// trigger query generation at then
this.sparnatural.html[0].dispatchEvent(new CustomEvent("generateQuery"));
this.sparnatural.html[0].dispatchEvent(new CustomEvent("redrawBackgroundAndLinks"));
}
static setSparnatural(sparnatural) {
this.sparnatural = sparnatural;
}
}
_a = QueryLoader, _QueryLoader_buildSparnatural = function _QueryLoader_buildSparnatural(sparnatural, branches) {
let varMapping = new Map();
if (branches?.length === 0)
throw Error('No Branches on query detected');
// first build the rootGroupWrapper
let rootGrpWrapper = sparnatural.BgWrapper.componentsList.rootGroupWrapper;
// build the root groupwrapper and remove from branches array
let rootBranch = branches.shift();
let localVarMapping = __classPrivateFieldGet(this, _a, "m", _QueryLoader_buildCriteriaGroup).call(this, rootGrpWrapper, rootBranch);
localVarMapping.forEach((value, key) => { varMapping.set(key, value); });
let parent = rootGrpWrapper;
branches.forEach((b) => {
__classPrivateFieldGet(this, _a, "m", _QueryLoader_clickOn).call(this, parent.criteriaGroup.actionsGroup.actions.actionAnd.btn);
let localVarMapping = __classPrivateFieldGet(this, _a, "m", _QueryLoader_buildCriteriaGroup).call(this, parent.andSibling, b);
localVarMapping.forEach((value, key) => { varMapping.set(key, value); });
parent = parent.andSibling;
});
// by default, the very first start class group will be selected
// if the first variable is *not* selected, then unselect it - and do that at the end of the query loading
const firstStartClassVal = { type: rootBranch.line.sType, variable: rootBranch.line.s };
if (!__classPrivateFieldGet(QueryLoader, _a, "m", _QueryLoader_hasSelectedVar).call(QueryLoader, this.query.variables, firstStartClassVal.variable)) {
// click on first eye btn to unselect it
__classPrivateFieldGet(this, _a, "m", _QueryLoader_clickOn).call(this, rootGrpWrapper.criteriaGroup.startClassGroup.inputSelector?.selectViewVariableBtn?.widgetHtml);
}
return varMapping;
}, _QueryLoader_buildCriteriaGroup = function _QueryLoader_buildCriteriaGroup(grpWrapper, branch) {
let varMapping = new Map();
// set StartClassVal only if there wasn't one set by the parent (e.g whereChild andSibling have it already set)
const startClassVal = { type: branch.line.sType, variable: branch.line.s };
if (!grpWrapper.criteriaGroup.startClassGroup.startClassVal.type) {
//set StartClassGroup
__classPrivateFieldGet(this, _a, "m", _QueryLoader_setSelectedValue).call(this, grpWrapper.criteriaGroup.startClassGroup, branch.line.sType);
}
// also set the variable name
// This is for cases where the variable name has been manually changed in the query
// and is not one of the selected variables in the result set
grpWrapper.criteriaGroup.startClassGroup.startClassVal = startClassVal;
varMapping.set(grpWrapper.criteriaGroup.startClassGroup.startClassVal.variable, branch.line.s);
// set EndClassGroup
const endClassVal = { type: branch.line.oType, variable: branch.line.o };
__classPrivateFieldGet(this, _a, "m", _QueryLoader_setSelectedValue).call(this, grpWrapper.criteriaGroup.endClassGroup, branch.line.oType);
// transparently set the variable name to the one in the query
// before we click on the select button, so that the column is selected with the proper name
// This is for cases where the variable name has been manually changed in the query
// and is not one of the selected variables in the result set
grpWrapper.criteriaGroup.endClassGroup.endClassVal = endClassVal;
varMapping.set(grpWrapper.criteriaGroup.endClassGroup.endClassVal.variable, branch.line.o);
//set ObjectPropertyGroup
__classPrivateFieldGet(this, _a, "m", _QueryLoader_setSelectedValue).call(this, grpWrapper.criteriaGroup.objectPropertyGroup, branch.line.p);
// set WidgetValues
if (branch.line.criterias) {
branch.line.criterias.forEach((v) => {
const parsedVal = grpWrapper.criteriaGroup.endClassGroup.editComponents.widgetWrapper.widgetComponent.parseInput(v);
// if there are multiple values rendered, click first the 'plus' btn, to add more values
if (grpWrapper.criteriaGroup.endClassWidgetGroup.widgetValues.length > 0)
__classPrivateFieldGet(this, _a, "m", _QueryLoader_clickOn).call(this, grpWrapper.criteriaGroup.endClassWidgetGroup.addWidgetValueBtn.html);
grpWrapper.criteriaGroup.endClassGroup.editComponents.widgetWrapper.widgetComponent.triggerRenderWidgetVal(parsedVal);
});
}
// if there is no value, and no children, set an "Any" value
if ((!branch.line.criterias || branch.line.criterias.length == 0) && (!branch.children || branch.children.length == 0)) {
grpWrapper.criteriaGroup.endClassGroup.editComponents.onSelectAll();
}
if (branch.children && branch.children.length > 0) {
__classPrivateFieldGet(this, _a, "m", _QueryLoader_clickOn).call(this, grpWrapper.criteriaGroup.endClassGroup.editComponents.actionWhere.btn);
// first child
let localVarMapping = __classPrivateFieldGet(this, _a, "m", _QueryLoader_buildCriteriaGroup).call(this, grpWrapper.whereChild, branch.children.shift());
localVarMapping.forEach((value, key) => varMapping.set(key, value));
// the rest of the children are AND connected
let parent = grpWrapper.whereChild;
branch.children.forEach((c) => {
__classPrivateFieldGet(this, _a, "m", _QueryLoader_clickOn).call(this, parent.criteriaGroup.actionsGroup.actions.actionAnd.btn);
let localVarMapping = __classPrivateFieldGet(this, _a, "m", _QueryLoader_buildCriteriaGroup).call(this, parent.andSibling, c);
localVarMapping.forEach((value, key) => varMapping.set(key, value));
parent = parent.andSibling;
});
}
// trigger option state AFTER loading children so that the state is properly propagated to them
__classPrivateFieldGet(this, _a, "m", _QueryLoader_triggerOptions).call(this, grpWrapper, branch);
// select if the var is viewed (eye btn)
__classPrivateFieldGet(this, _a, "m", _QueryLoader_setSelectViewVariableBtn).call(this, endClassVal, grpWrapper.criteriaGroup.endClassGroup);
return varMapping;
}, _QueryLoader_triggerOptions = function _QueryLoader_triggerOptions(grpWrapper, branch) {
if (branch.notExists && grpWrapper.currentOptionState != OptionTypes.NOTEXISTS) {
__classPrivateFieldGet(this, _a, "m", _QueryLoader_clickOn).call(this, grpWrapper.criteriaGroup.optionsGroup.optionalArrow.widgetHtml);
__classPrivateFieldGet(this, _a, "m", _QueryLoader_clickOn).call(this, grpWrapper.criteriaGroup.optionsGroup.NotExistsComponent.html);
}
if (branch.optional && grpWrapper.currentOptionState != OptionTypes.OPTIONAL) {
__classPrivateFieldGet(this, _a, "m", _QueryLoader_clickOn).call(this, grpWrapper.criteriaGroup.optionsGroup.optionalArrow.widgetHtml);
__classPrivateFieldGet(this, _a, "m", _QueryLoader_clickOn).call(this, grpWrapper.criteriaGroup.optionsGroup.OptionalComponent.html);
}
}, _QueryLoader_setSelectedValue = function _QueryLoader_setSelectedValue(component, value) {
// set the values to the ClassTypeId | ObjectPropertyTypeId component
component.inputSelector.setSelected(value);
component.inputSelector.submitSelected();
}, _QueryLoader_setSelectViewVariableBtn = function _QueryLoader_setSelectViewVariableBtn(selectedVal, endClassComponent) {
if (__classPrivateFieldGet(QueryLoader, _a, "m", _QueryLoader_hasSelectedVar).call(QueryLoader, this.query.variables, selectedVal.variable)) {
// click on eye btn
__classPrivateFieldGet(this, _a, "m", _QueryLoader_clickOn).call(this, endClassComponent.inputSelector?.selectViewVariableBtn?.widgetHtml);
}
}, _QueryLoader_updateOrderingOfVariables = function _QueryLoader_updateOrderingOfVariables() {
const varMenu = this.sparnatural.variableSection.variableOrderMenu;
this.query.variables.forEach(v => {
varMenu.draggables.forEach(d => {
let varName;
if ("expression" in v) {
varName = v.expression.expression.value;
}
else {
varName = v.value;
}
if (d.state.selectedVariable.variable === varName) {
varMenu.removeDraggableByVarName(varName);
let newDraggable = varMenu.addDraggableComponent(d.state.selectedVariable);
// if this was an aggregated variable, load it by calling a specific function of the draggable
if ("expression" in v) {
newDraggable.loadAggregatedVariable(v);
}
}
});
});
// once variables are sorted, synchronize with the actionstore
// this.sparnatural.actionStore.variables = this.sparnatural.variableSection.listVariables();
const variableSortOption = this.sparnatural.variableSection.variableSortOption;
if (this.query.order == Order.ASC) {
variableSortOption.changeSortOrderCallBack(Order.ASC);
}
else if (this.query.order == Order.DESC) {
variableSortOption.changeSortOrderCallBack(Order.DESC);
}
else {
variableSortOption.changeSortOrderCallBack(Order.NOORDER);
}
}, _QueryLoader_updateNamingOfVariables = function _QueryLoader_updateNamingOfVariables(varNameMapping) {
const varMenu = this.sparnatural.variableSection.variableOrderMenu;
this.query.variables.forEach(v => {
varMenu.draggables.forEach(d => {
var varToConsider;
if ("variable" in v) {
// this is an aggregation
// at this stage we are setting the variable name to the *original variable*
// the final aggregated var name will be set when the draggable will be updated
varToConsider = v.expression.expression;
}
else {
// nominal case
varToConsider = v;
}
if (varNameMapping.get(d.state.selectedVariable.variable) === varToConsider.value) {
d.setVarName(varToConsider.value);
}
});
});
}, _QueryLoader_hasSelectedVar = function _QueryLoader_hasSelectedVar(vars, varName) {
var result = false;
vars.forEach(v => {
if ("expression" in v) {
// this is an aggregation
// consider the variable *being aggregated*, not the result of the aggregation
if (v.expression.expression.value == varName)
result = true;
}
if ("value" in v) {
if (v.value == varName)
result = true;
}
});
return result;
}, _QueryLoader_clickOn = function _QueryLoader_clickOn(el) {
el[0].dispatchEvent(new Event("click"));
};
//# sourceMappingURL=QueryLoader.js.map