Skip to content

Commit

Permalink
MIR-1362 change and fix the second search with filter query etc
Browse files Browse the repository at this point in the history
  • Loading branch information
Oleksiy 'Alex' Levshyn committed Nov 13, 2024
1 parent bf7b6e9 commit 82622a2
Show file tree
Hide file tree
Showing 3 changed files with 268 additions and 96 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
id="searchInput"
type="text"
aria-label="Search" />
<input type="hidden" id="initialCondQuery" name="initialCondQuery" value=""/>
<xsl:choose>
<xsl:when test="contains($isSearchAllowedForCurrentUser, 'true')">
<input name="owner" type="hidden" value="createdby:*" />
Expand Down
110 changes: 68 additions & 42 deletions mir-module/src/main/resources/META-INF/resources/js/mir/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -293,52 +293,78 @@
});
});

//for select box in search field on hit list page
$( ".search_type a" ).click(function() {
$( "#search_type_label" ).html( $( this ).html() );
$( "#search_type_button" ).attr( 'value', $( this ).attr('value') );
// Input element in the original search
const originalSearchInputElement = "#searchInput";
// Selector for the search form
const subSearchFormName = "form.search_form";
// ID of the input field for the second search text
const qrySelector = "#qry";
// ID of the select box with the filter query key
const selectMods = "#select_mods";
// ID of the hidden element with the fq parameter
const fqElement = "#fq";
// ID of the hidden element with the initial condQuery parameter in the first search form
const initialCondQueryFirst = "#initialCondQuery";
// ID of the hidden element with the initial condQuery parameter in the second search form
const initialCondQuerySecond = "#initialCondQuerySecond";
// ID of the hidden element 'condQuery' for the query parameter 'condQuery'
const condQuery = "#condQuery";

// Input element's changes in the original search
$(originalSearchInputElement).change(() => {
if ($(initialCondQueryFirst)) {
$(initialCondQueryFirst).attr('value', $('#searchInput').val().trim());
}
});

// filter for result lists
// modify search query
// TODO: modify? add why and how
// do nothing if a query is missing
$( ".search_box form" ).submit( function( event ) {
if($(this).find("input[name='qry']").val().trim() != '') {
const origSearchAction = $(this).attr('action');
// To get the condQuery parameter, the URLSearchParams API is used
// (https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams)
// (compatible: https://caniuse.com/?search=URLSearchParams)
const urlObj = new URL(origSearchAction);
// Get the query parameters
const params = new URLSearchParams(urlObj.search);
// Get the 'condQuery' parameter value
const condQuery = params.get('condQuery');
const searchTypeButtonValueAttr = $('#search_type_button').attr('value');
const addValue = encodeURIComponent(solrEscapeSearchValue($('.search_box input').val().trim()));
let newAction;
if (origSearchAction.includes('servlets/solr/find')) {
const replAction = origSearchAction.replace(/(.*[&|\?])(condQuery=.*?)&(.*)/,'$1$3');
if (searchTypeButtonValueAttr === 'all') {
newAction = replAction + "&condQuery=" + condQuery + "+AND+" + addValue;
} else {
newAction = replAction + "&condQuery=" + condQuery + "&fq=" + searchTypeButtonValueAttr + ":" + addValue;
}
} else { // TODO: do it right
const replAction = origSearchAction.replace(/(.*[&|\?])(condQuery=.*?)&(.*)/,'$1$3&$2');
if (searchTypeButtonValueAttr === 'all') {
newAction = replAction + "+%2BallMeta:" + addValue;
} else {
newAction = replAction + "+%2B" + searchTypeButtonValueAttr + ":" + addValue;
}
}
$(this).attr('action', newAction);
} else {
// nothing to do if a value is missing
event.preventDefault();
}
// Changes in the select box for the filter query
$(selectMods).change(() => {
setFQAndCondQueryElementsValues();
});

// Changes in the input field of the filter query
$(qrySelector).change(() => {
setFQAndCondQueryElementsValues();
});

// Changes for the fq element and condQuery element
function setFQAndCondQueryElementsValues() {
if ($(selectMods) && $(qrySelector) && $(fqElement) && $(initialCondQuerySecond) && $(condQuery)) {
let queryText = '';
if ($(qrySelector).val() !== '') {
queryText = $(qrySelector).val().trim();
// Remove all duplicate spaces, tabs, newlines etc
queryText = queryText.replace(/\s\s+/g, ' ');
}

const initialCondQueryValue = $(initialCondQuerySecond).val().trim();

const selectModsValue = $(selectMods).val();
// Case if selectMods is 'all' - 'everything'
if (selectModsValue === 'all') {
$(fqElement).attr('value', '');
let condQueryValue = initialCondQueryValue;
if (initialCondQueryValue !== '') {
if (queryText !== '') {
condQueryValue += ' AND ' + queryText;
}
} else {
if (queryText !== '') {
condQueryValue += queryText;
}
}
$(condQuery).attr('value', condQueryValue);
} else {
if (queryText !== '') {
// const selectModsValue = $(selectMods).val();
const filterQuery = selectModsValue + ':' + queryText;
$(fqElement).attr('value', filterQuery);
$(condQuery).attr('value', initialCondQueryValue);
}
}
}
}

var languageList = jQuery('#topnav .languageList');
jQuery('#topnav .languageSelect').click(function() {
languageList.toggleClass('hide');
Expand Down
253 changes: 199 additions & 54 deletions mir-module/src/main/resources/xsl/response-mir.xsl
Original file line number Diff line number Diff line change
Expand Up @@ -60,54 +60,129 @@
<div class="row result_searchline">
<div class="col-12 col-sm-8 text-center result_search">
<div class="search_box">
<xsl:variable name="searchlink" select="concat($proxyBaseURL, $HttpSession, $solrParams)" />

<!-- Check if 'condQuery' exists and extract its value if it does -->
<xsl:variable name="condQuery">
<xsl:choose>
<xsl:when test="contains($solrParams, 'condQuery=')">
<xsl:value-of select="substring-before(substring-after($solrParams, 'condQuery='), '&amp;')" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="''" />
</xsl:otherwise>
</xsl:choose>
</xsl:variable>

<!-- Check if 'initialCondQuery' exists and extract its value if it does -->
<xsl:variable name="initialCondQuery" select="/response/lst[@name='responseHeader']/lst[@name='params']/str[@name='initialCondQuery']" />

<!-- Check if 'fq' exists and extract its value if it does -->
<xsl:variable name="fq" select="/response/lst[@name='responseHeader']/lst[@name='params']/str[@name='fq']" />

<!-- Check if 'owner' exists and extract its value if it does -->
<xsl:variable name="owner" select="/response/lst[@name='responseHeader']/lst[@name='params']/str[@name='owner']" />

<!-- Check if 'version' exists and extract its value if it does -->
<xsl:variable name="version" select="/response/lst[@name='responseHeader']/lst[@name='params']/str[@name='version']" />


<!-- Extract part before ':' ('%3A') if $fq is not empty or null -->
<xsl:variable name="initialSelectMods">
<xsl:choose>
<xsl:when test="$fq">
<!-- xsl:value-of select="substring-before($fq, '%3A')" / -->
<xsl:value-of select="substring-before($fq, ':')" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="''" />
</xsl:otherwise>
</xsl:choose>
</xsl:variable>

<!-- Extract part after ':' ('%3A') if $fq is not empty or null -->
<xsl:variable name="filterQueryValue">
<xsl:choose>
<xsl:when test="$fq">
<xsl:value-of select="substring-after($fq, ':')" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="''" />
</xsl:otherwise>
</xsl:choose>
</xsl:variable>

<!-- Variable for the 'action' attribute in the form element -->
<xsl:variable name="searchlink" select="concat($proxyBaseURL, $HttpSession)" />

<!-- Form for the second search -->
<form action="{$searchlink}" class="search_form" method="post">
<xsl:choose>
<xsl:when test="not($fq)">
<input type="hidden" id="fq" name="fq" value=""/>
</xsl:when>
<xsl:otherwise>
<input type="hidden" id="fq" name="fq" value="{$fq}"/>
</xsl:otherwise>
</xsl:choose>

<!-- Hidden elements -->
<input type="hidden" id="initialCondQuerySecond" name="initialCondQuery" value="{$initialCondQuery}"/>
<input type="hidden" id="owner" name="owner" value="{$owner}"/>
<input type="hidden" id="version" name="version" value="{$version}"/>

<div class="input-group input-group-sm">
<div class="input-group-btn input-group-prepend">
<button type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown" value="all" id="search_type_button">
<span id="search_type_label">
<xsl:value-of select="i18n:translate('mir.dropdown.all')" />
</span>
<span class="caret"></span>
</button>
<ul class="dropdown-menu search_type">
<li>
<a href="#" value="all" class="dropdown-item">
<xsl:value-of select="i18n:translate('mir.dropdown.all')" />
</a>
</li>
<li>
<a href="#" value="mods.title" class="dropdown-item">
<xsl:value-of select="i18n:translate('mir.dropdown.title')" />
</a>
</li>
<li>
<a href="#" value="mods.author" class="dropdown-item">
<xsl:value-of select="i18n:translate('mir.dropdown.author')" />
</a>
</li>
<li>
<a href="#" value="mods.name.top" class="dropdown-item">
<xsl:value-of select="i18n:translate('mir.dropdown.name')" />
</a>
</li>
<li>
<a href="#" value="mods.nameIdentifier" class="dropdown-item">
<xsl:value-of select="i18n:translate('mir.dropdown.nameIdentifier')" />
</a>
</li>
<li>
<a href="#" value="allMeta" class="dropdown-item">
<xsl:value-of select="i18n:translate('mir.dropdown.allMeta')" />
</a>
</li>
<li>
<a href="#" value="content" class="dropdown-item">
<xsl:value-of select="i18n:translate('mir.dropdown.content')" />
</a>
</li>
</ul>
</div>
<!-- Select box for the filter query (select mods) -->
<select id="select_mods" class="btn btn-primary">
<option value="all">
<xsl:if test="$initialSelectMods = 'all' or not($initialSelectMods)">
<xsl:attribute name="selected">selected</xsl:attribute>
</xsl:if>
<xsl:value-of select="i18n:translate('mir.dropdown.all')" />
</option>

<option value="mods.title">
<xsl:if test="$initialSelectMods = 'mods.title'">
<xsl:attribute name="selected">selected</xsl:attribute>
</xsl:if>
<xsl:value-of select="i18n:translate('mir.dropdown.title')" />
</option>

<option value="mods.author">
<xsl:if test="$initialSelectMods = 'mods.author'">
<xsl:attribute name="selected">selected</xsl:attribute>
</xsl:if>
<xsl:value-of select="i18n:translate('mir.dropdown.author')" />
</option>

<option value="mods.name.top">
<xsl:if test="$initialSelectMods = 'mods.name.top'">
<xsl:attribute name="selected">selected</xsl:attribute>
</xsl:if>
<xsl:value-of select="i18n:translate('mir.dropdown.name')" />
</option>

<option value="mods.nameIdentifier">
<xsl:if test="$initialSelectMods = 'mods.nameIdentifier'">
<xsl:attribute name="selected">selected</xsl:attribute>
</xsl:if>
<xsl:value-of select="i18n:translate('mir.dropdown.nameIdentifier')" />
</option>

<option value="allMeta">
<xsl:if test="$initialSelectMods = 'allMeta'">
<xsl:attribute name="selected">selected</xsl:attribute>
</xsl:if>
<xsl:value-of select="i18n:translate('mir.dropdown.allMeta')" />
</option>

<option value="content">
<xsl:if test="$initialSelectMods = 'content'">
<xsl:attribute name="selected">selected</xsl:attribute>
</xsl:if>
<xsl:value-of select="i18n:translate('mir.dropdown.content')" />
</option>
</select>

<xsl:variable name="resolver">
<xsl:call-template name="substring-after-last">
<xsl:with-param name="string" select="$proxyBaseURL" />
Expand All @@ -125,14 +200,84 @@
</xsl:variable>
<xsl:value-of select="decoder:decode($encodedQry, 'UTF-8')" />
</xsl:variable>
<xsl:choose>
<xsl:when test="$qry = '*'">
<input class="form-control" name="qry" placeholder="{i18n:translate('mir.placeholder.response.search')}" type="text" />
</xsl:when>
<xsl:otherwise>
<input class="form-control" name="qry" placeholder="{i18n:translate('mir.placeholder.response.search')}" type="text" value="{$qry}" />
</xsl:otherwise>
</xsl:choose>

<xsl:variable name="query">
<xsl:choose>
<xsl:when test="$condQuery">
<xsl:choose>
<xsl:when test="$condQuery = $initialCondQuery">
<xsl:value-of select="''" />
</xsl:when>
<xsl:otherwise>
<xsl:variable name="searchString" select="concat($initialCondQuery, '+AND+')" />
<xsl:value-of select="substring-after($condQuery, $searchString)" />
</xsl:otherwise>
</xsl:choose>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="''" />
</xsl:otherwise>
</xsl:choose>
</xsl:variable>

<!-- Get a current qry from the last request value -->
<xsl:variable name="currentQryFromLastRequest">
<xsl:choose>
<xsl:when test="not($filterQueryValue) or $filterQueryValue = ''">
<xsl:value-of select="$query" />
</xsl:when>
<xsl:when test="$filterQueryValue = '*'">
<!-- If filterQueryValue is '*', leave value empty -->
<xsl:value-of select="''" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$filterQueryValue" />
</xsl:otherwise>
</xsl:choose>
</xsl:variable>

<!-- Get a type of the last request -->
<xsl:variable name="lastTypeRequest">
<xsl:choose>
<xsl:when test="not($filterQueryValue) or $filterQueryValue = ''">
<xsl:choose>
<xsl:when test="$condQuery = $initialCondQuery">
<xsl:value-of select="''" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="'condQuery'" />
</xsl:otherwise>
</xsl:choose>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="'fq'" />
</xsl:otherwise>
</xsl:choose>
</xsl:variable>

<!-- Hidden query parameter 'condQuery' -->
<input type="hidden" id="condQuery" name="condQuery">
<xsl:attribute name="value">
<xsl:choose>
<!-- If $lastTypeRequest is 'condQuery', concatenate $initialCondQuery, ' AND ', and $currentQry -->
<xsl:when test="$lastTypeRequest = 'condQuery'">
<xsl:value-of select="concat($initialCondQuery, ' AND ', $currentQryFromLastRequest)" />
</xsl:when>
<!-- If $lastTypeRequest is 'fq' or empty, use $initialCondQuery -->
<xsl:when test="$lastTypeRequest = 'fq' or $lastTypeRequest = ''">
<xsl:value-of select="$initialCondQuery" />
</xsl:when>
<!-- Default case (if needed, though not specified in your conditions) -->
<xsl:otherwise>
<xsl:value-of select="$initialCondQuery" />
</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
</input>

<!-- Input element for the second search -->
<input class="form-control" id="qry" placeholder="{i18n:translate('mir.placeholder.response.search')}" type="text" value="{$currentQryFromLastRequest}" />

</xsl:when>
<xsl:otherwise>
<input class="form-control" name="condQuery" placeholder="{i18n:translate('mir.placeholder.response.search')}" type="text" />
Expand Down

0 comments on commit 82622a2

Please sign in to comment.