Skip to content

Commit 6c3ba2d

Browse files
ahornaceVladimir Kotal
authored andcommitted
Enable suggestions for minisearch
1 parent 01846f1 commit 6c3ba2d

File tree

2 files changed

+86
-52
lines changed

2 files changed

+86
-52
lines changed

web/js/utils-0.0.23.js

Lines changed: 78 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1715,14 +1715,14 @@ function domReadyMast() {
17151715
function pageReadyMast() {
17161716
}
17171717

1718-
function domReadyMenu() {
1718+
function domReadyMenu(minisearch) {
17191719
$.ajax({
17201720
// cannot use "/api/v1/configuration/suggester" because of security
17211721
url: window.contextPath + "/suggest/config",
17221722
dataType: "json",
17231723
success: function(config) {
17241724
if (config.enabled) {
1725-
initAutocomplete(config);
1725+
initAutocomplete(config, minisearch);
17261726
}
17271727
},
17281728
error: function(xhr, ajaxOptions, error) {
@@ -1731,23 +1731,9 @@ function domReadyMenu() {
17311731
});
17321732
}
17331733

1734-
function initAutocomplete(config) {
1735-
if (config.allowedFields) {
1736-
if (config.allowedFields.includes('full')) {
1737-
initAutocompleteForField("q", "full", config);
1738-
}
1739-
if (config.allowedFields.includes('defs')) {
1740-
initAutocompleteForField("defs", "defs", config);
1741-
}
1742-
if (config.allowedFields.includes('refs')) {
1743-
initAutocompleteForField("refs", "refs", config);
1744-
}
1745-
if (config.allowedFields.includes('path')) {
1746-
initAutocompleteForField("path", "path", config);
1747-
}
1748-
if (config.allowedFields.includes('hist')) {
1749-
initAutocompleteForField("hist", "hist", config);
1750-
}
1734+
function initAutocomplete(config, minisearch) {
1735+
if (minisearch) {
1736+
initMinisearchAutocomplete(config);
17511737
} else {
17521738
initAutocompleteForField("q", "full", config);
17531739
initAutocompleteForField("defs", "defs", config);
@@ -1757,38 +1743,63 @@ function initAutocomplete(config) {
17571743
}
17581744
}
17591745

1760-
function initAutocompleteForField(inputId, field, config) {
1746+
function initMinisearchAutocomplete(config) {
1747+
if (config.allowedFields && !config.allowedFields.contains('full')) {
1748+
return;
1749+
}
1750+
1751+
var project = '';
1752+
1753+
var projectElem = $('#minisearch-project');
1754+
if (projectElem) {
1755+
project = projectElem.val();
1756+
}
1757+
1758+
var pathElem = $('#minisearch-path');
1759+
1760+
initAutocompleteForField('search', 'full', config, function (input, field) {
1761+
var caretPos = input.caret();
1762+
if (!Number.isInteger(caretPos)) {
1763+
console.error("Suggest: could not get caret position");
1764+
return;
1765+
}
1766+
return {
1767+
projects: [project],
1768+
field: field,
1769+
full: input.val(),
1770+
path: pathElem.is(':checked') ? pathElem.val() : '',
1771+
caret: caretPos
1772+
}
1773+
}, 'search');
1774+
}
1775+
1776+
function initAutocompleteForField(inputId, field, config, dataFunction, errorElemId) {
1777+
if (config.allowedFields && !config.allowedFields.contains(field)) {
1778+
return;
1779+
}
1780+
17611781
var text;
17621782
var identifier;
17631783
var time;
17641784

17651785
var input = $("#" + inputId);
17661786

1787+
if (!dataFunction) {
1788+
dataFunction = getAutocompleteMenuData;
1789+
}
1790+
if (!errorElemId) {
1791+
errorElemId = 'q';
1792+
}
1793+
var errorElem = $('#' + errorElemId);
1794+
17671795
input.autocomplete({
17681796
source: function(request, response) {
1769-
1770-
var caretPos = $('#' + inputId).caret();
1771-
if (!Number.isInteger(caretPos)) {
1772-
console.error("Suggest: could not get caret position");
1773-
return;
1774-
}
1775-
17761797
$.ajax({
17771798
url: window.contextPath + "/suggest",
17781799
dataType: "json",
1779-
data: {
1780-
projects: getSelectedProjectNames(),
1781-
field: field,
1782-
full: $('#q').val(),
1783-
defs: $('#defs').val(),
1784-
refs: $('#refs').val(),
1785-
path: $('#path').val(),
1786-
hist: $('#hist').val(),
1787-
type: $('#type').val(),
1788-
caret: caretPos
1789-
},
1800+
data: dataFunction(input, field),
17901801
success: function(data) {
1791-
hideError();
1802+
hideError(errorElem);
17921803

17931804
text = data.queryText;
17941805
identifier = data.identifier;
@@ -1800,7 +1811,7 @@ function initAutocompleteForField(inputId, field, config) {
18001811
input.autocomplete("close");
18011812
response(undefined); // to remove loading indicator
18021813

1803-
showError(xhr.responseJSON.message)
1814+
showError(xhr.responseJSON.message, errorElem)
18041815
},
18051816
statusCode: {
18061817
404: function() {
@@ -1872,12 +1883,31 @@ function initAutocompleteForField(inputId, field, config) {
18721883
});
18731884
}
18741885

1875-
function showError(errorText) {
1876-
var topInputParent = $('#q').parent();
1886+
function getAutocompleteMenuData(input, field) {
1887+
var caretPos = input.caret();
1888+
if (!Number.isInteger(caretPos)) {
1889+
console.error("Suggest: could not get caret position");
1890+
return;
1891+
}
1892+
return {
1893+
projects: getSelectedProjectNames(),
1894+
field: field,
1895+
full: $('#q').val(),
1896+
defs: $('#defs').val(),
1897+
refs: $('#refs').val(),
1898+
path: $('#path').val(),
1899+
hist: $('#hist').val(),
1900+
type: $('#type').val(),
1901+
caret: caretPos
1902+
}
1903+
}
1904+
1905+
function showError(errorText, errorElem) {
1906+
var parent = errorElem.parent();
18771907

1878-
topInputParent.css('position', 'relative');
1908+
parent.css('position', 'relative');
18791909

1880-
var span = topInputParent.find('#autocomplete-error')[0];
1910+
var span = parent.find('#autocomplete-error')[0];
18811911
if (!span) {
18821912
span = $("<span>", {
18831913
class: "important-note important-note-rounded",
@@ -1886,7 +1916,7 @@ function showError(errorText) {
18861916
id: 'autocomplete-error'
18871917
});
18881918

1889-
span.appendTo(topInputParent);
1919+
span.appendTo(parent);
18901920
} else {
18911921
span = $(span);
18921922
span.off("mouseenter mouseleave");
@@ -1901,9 +1931,9 @@ function showError(errorText) {
19011931
});
19021932
}
19031933

1904-
function hideError() {
1905-
var topInputParent = $('#q').parent();
1906-
var span = topInputParent.find('#autocomplete-error')[0];
1934+
function hideError(errorElem) {
1935+
var parent = errorElem.parent();
1936+
var span = parent.find('#autocomplete-error')[0];
19071937
if (span) {
19081938
span.remove();
19091939
}

web/minisearch.jspf

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,21 +96,25 @@ org.opensolaris.opengrok.web.Util"%><%
9696
%>"><span id="download"></span>Download</a></li>
9797
<%
9898
}
99-
%><li><input type="text" id="search" name="q" class="q" />
100-
<input type="submit" value="Search" class="submit" /></li><%
99+
%><li><input type="text" id="search" name="q" class="q" /></li>
100+
<li><input type="submit" value="Search" class="submit" /></li><%
101101
Project proj = cfg.getProject();
102102
String[] vals = cfg.getSearchOnlyIn();
103-
%><li><input type="checkbox" name="path" value='"<%= vals[0]
103+
%><li><input id="minisearch-path" type="checkbox" name="path" value='"<%= vals[0]
104104
%>"' <%= vals[2] %>/> current directory</li>
105105
</ul><%
106106
if (proj != null) {
107107
%>
108-
<input type="hidden" name="project" value="<%=proj.getName()%>" /><%
108+
<input id="minisearch-project" type="hidden" name="project" value="<%=proj.getName()%>" /><%
109109
}
110110
%>
111111
<input type="hidden" id="contextpath" value="<%=request.getContextPath()%>" />
112112
</div>
113113
</form>
114+
<script type="text/javascript">/* <![CDATA[ */
115+
document.domReady.push(function() { domReadyMenu(true); });
116+
/* ]]> */
117+
</script>
114118
</div>
115119
<div id="content">
116120
<%

0 commit comments

Comments
 (0)