Skip to content
Snippets Groups Projects
Verified Commit 7377beab authored by Timm Fitschen's avatar Timm Fitschen
Browse files

MAINT: formatting of query_form.js

parent 19ea7b1e
No related branches found
No related tags found
2 merge requests!103Quick main-release of documentation,!96F query form
Pipeline #35091 passed
/** /**
* Extend the functionality of the pure html query panel. * Extend the functionality of the pure html query panel.
* *
* Deprecated. This is to be replaced by the query form of caosdb-webui-core-components.
*
* @deprecated
* @module queryForm * @module queryForm
* @global * @global
*/ */
var queryForm = function () { var queryForm = (function () {
const init = function (form) { const init = function (form) {
queryForm.restoreLastQuery(form, () => window.sessionStorage.lastQuery); queryForm.restoreLastQuery(form, () => window.sessionStorage.lastQuery);
queryForm.bindOnClick(form, (set) => { queryForm.bindOnClick(form, (set) => {
window.sessionStorage.lastQuery = set; window.sessionStorage.lastQuery = set;
}); });
const BUILD_FREE_SEARCH_ROLE_NAME_FACET_OPTIONS = ""; const BUILD_FREE_SEARCH_ROLE_NAME_FACET_OPTIONS = "";
queryForm.initFreeSearch(form, `${BUILD_FREE_SEARCH_ROLE_NAME_FACET_OPTIONS}`); queryForm.initFreeSearch(
form,
`${BUILD_FREE_SEARCH_ROLE_NAME_FACET_OPTIONS}`
);
}; };
const logger = log.getLogger("queryForm"); const logger = log.getLogger("queryForm");
...@@ -20,8 +25,12 @@ var queryForm = function () { ...@@ -20,8 +25,12 @@ var queryForm = function () {
const _isCql = function (query) { const _isCql = function (query) {
query = query.toUpperCase().trim(); query = query.toUpperCase().trim();
return (query.startsWith("FIND") || query.startsWith("COUNT") || query.startsWith("SELECT")); return (
} query.startsWith("FIND") ||
query.startsWith("COUNT") ||
query.startsWith("SELECT")
);
};
/** /**
* Initialize the free search to generate search queries which search only * Initialize the free search to generate search queries which search only
...@@ -50,7 +59,8 @@ var queryForm = function () { ...@@ -50,7 +59,8 @@ var queryForm = function () {
const textArea = $(form).find(".caosdb-f-query-textarea"); const textArea = $(form).find(".caosdb-f-query-textarea");
logger.trace("initFreeSearch", form, textArea, options); logger.trace("initFreeSearch", form, textArea, options);
if (textArea.length > 0 && options && options != "") { if (textArea.length > 0 && options && options != "") {
const lastQuery = window.localStorage["freeTextQuery:" + textArea[0].value]; const lastQuery =
window.localStorage["freeTextQuery:" + textArea[0].value];
if (lastQuery) { if (lastQuery) {
textArea[0].value = lastQuery; textArea[0].value = lastQuery;
} }
...@@ -58,7 +68,11 @@ var queryForm = function () { ...@@ -58,7 +68,11 @@ var queryForm = function () {
const selected = window.localStorage["role_name_facet_option"]; const selected = window.localStorage["role_name_facet_option"];
const select = $(`<select class="btn btn-secondary"/>`); const select = $(`<select class="btn btn-secondary"/>`);
for (let option of options.split(",")) { for (let option of options.split(",")) {
select.append(`<option ${selected === option.trim() ? "selected" : ""}>${option}</option>`); select.append(
`<option ${
selected === option.trim() ? "selected" : ""
}>${option}</option>`
);
} }
$(form).find(".input-group").prepend(select); $(form).find(".input-group").prepend(select);
role_name_facet_select = select[0]; role_name_facet_select = select[0];
...@@ -66,12 +80,15 @@ var queryForm = function () { ...@@ -66,12 +80,15 @@ var queryForm = function () {
const switchFreeSearch = (text_area) => { const switchFreeSearch = (text_area) => {
if (_isCql(text_area.value)) { if (_isCql(text_area.value)) {
select.hide(); select.hide();
$(text_area).css({"border-top-left-radius": "0.375rem", "border-bottom-left-radius": "0.375rem"}); $(text_area).css({
"border-top-left-radius": "0.375rem",
"border-bottom-left-radius": "0.375rem",
});
} else { } else {
select.show(); select.show();
$(text_area).css({}); $(text_area).css({});
} }
} };
switchFreeSearch(textArea[0]); switchFreeSearch(textArea[0]);
...@@ -82,7 +99,7 @@ var queryForm = function () { ...@@ -82,7 +99,7 @@ var queryForm = function () {
} }
role_name_facet_select = undefined; role_name_facet_select = undefined;
return false; return false;
} };
/** /**
* @function restoreLastQuery * @function restoreLastQuery
...@@ -102,12 +119,13 @@ var queryForm = function () { ...@@ -102,12 +119,13 @@ var queryForm = function () {
* @param {string} paging - the paging string, e.g. 0L10. * @param {string} paging - the paging string, e.g. 0L10.
*/ */
const redirect = function (query, paging) { const redirect = function (query, paging) {
var pagingparam = "" var pagingparam = "";
if (paging && paging.length > 0) { if (paging && paging.length > 0) {
pagingparam = "P=" + paging + "&"; pagingparam = "P=" + paging + "&";
} }
location.href = connection.getBasePath() + "Entity/?" + pagingparam + "query=" + query; location.href =
} connection.getBasePath() + "Entity/?" + pagingparam + "query=" + query;
};
/** /**
* Read out the selector for the role/name facet of the query. Return * Read out the selector for the role/name facet of the query. Return
...@@ -121,9 +139,10 @@ var queryForm = function () { ...@@ -121,9 +139,10 @@ var queryForm = function () {
return result; return result;
} }
return "RECORD"; return "RECORD";
} };
const _splitSearchTermsPattern = /"(?<dq>[^"]*)" |'(?<sq>[^']*)' |(?<nq>[^ ]+)/g; const _splitSearchTermsPattern =
/"(?<dq>[^"]*)" |'(?<sq>[^']*)' |(?<nq>[^ ]+)/g;
/** /**
* Split a query string into single terms. * Split a query string into single terms.
...@@ -139,8 +158,11 @@ var queryForm = function () { ...@@ -139,8 +158,11 @@ var queryForm = function () {
*/ */
const splitSearchTerms = function (query) { const splitSearchTerms = function (query) {
// add empty space at the end, so every matching group ends with it -> easier regex. Also, undefined is filtered out // add empty space at the end, so every matching group ends with it -> easier regex. Also, undefined is filtered out
return Array.from((query + " ").matchAll(_splitSearchTermsPattern), (m) => m[1] || m[2] || m[3]).filter((word) => word); return Array.from(
} (query + " ").matchAll(_splitSearchTermsPattern),
(m) => m[1] || m[2] || m[3]
).filter((word) => word);
};
/** /**
* Is the query a SELECT field,... FROM entity query? * Is the query a SELECT field,... FROM entity query?
...@@ -151,13 +173,13 @@ var queryForm = function () { ...@@ -151,13 +173,13 @@ var queryForm = function () {
*/ */
const isSelectQuery = function (query) { const isSelectQuery = function (query) {
return query.toUpperCase().startsWith("SELECT"); return query.toUpperCase().startsWith("SELECT");
} };
/** /**
* @function bindOnClick * @function bindOnClick
*/ */
const bindOnClick = function (form, setter) { const bindOnClick = function (form, setter) {
if (setter == null || typeof (setter) !== 'function' || setter.length !== 1) { if (setter == null || typeof setter !== "function" || setter.length !== 1) {
throw new Error("setter must be a function with one param"); throw new Error("setter must be a function with one param");
} }
...@@ -175,7 +197,9 @@ var queryForm = function () { ...@@ -175,7 +197,9 @@ var queryForm = function () {
} }
if (!_isCql(value)) { if (!_isCql(value)) {
// split words in query field at space and create query fragments // split words in query field at space and create query fragments
var words = splitSearchTerms(queryField.value).map(word => `A PROPERTY LIKE '*${word.replaceAll("'", `\\'`)}*'`); var words = splitSearchTerms(queryField.value).map(
(word) => `A PROPERTY LIKE '*${word.replaceAll("'", `\\'`)}*'`
);
if (!words.length) { if (!words.length) {
return false; return false;
} }
...@@ -190,13 +214,15 @@ var queryForm = function () { ...@@ -190,13 +214,15 @@ var queryForm = function () {
var paging = ""; var paging = "";
if (form.P && !isSelectQuery(queryField.value)) { if (form.P && !isSelectQuery(queryField.value)) {
paging = form.P.value paging = form.P.value;
} }
queryForm.redirect(queryField.value.trim(), paging); queryForm.redirect(queryField.value.trim(), paging);
var btn = $(form).find(".caosdb-search-btn"); var btn = $(form).find(".caosdb-search-btn");
btn.html(`<span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span>`); btn.html(
`<span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span>`
);
btn.prop("disabled", true); btn.prop("disabled", true);
var textField = $(form).find(".caosdb-f-query-textarea"); var textField = $(form).find(".caosdb-f-query-textarea");
...@@ -231,9 +257,8 @@ var queryForm = function () { ...@@ -231,9 +257,8 @@ var queryForm = function () {
splitSearchTerms: splitSearchTerms, splitSearchTerms: splitSearchTerms,
getRoleNameFacet: getRoleNameFacet, getRoleNameFacet: getRoleNameFacet,
getRoleNameFacetSelect: () => role_name_facet_select, getRoleNameFacetSelect: () => role_name_facet_select,
} };
}(); })();
$(document).ready(function () { $(document).ready(function () {
if (`${BUILD_MODULE_LEGACY_QUERY_FORM}` == "ENABLED") { if (`${BUILD_MODULE_LEGACY_QUERY_FORM}` == "ENABLED") {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment