Skip to content

Commit 0201860

Browse files
emergieVladimir Kotal
authored andcommitted
search form focus restoring after submit triggered by return key (#1797)
1 parent 7ccbd27 commit 0201860

File tree

3 files changed

+41
-2
lines changed

3 files changed

+41
-2
lines changed

src/org/opensolaris/opengrok/web/Scripts.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public String toHtml() {
125125
SCRIPTS.put("jquery-tablesorter", new FileScript("js/jquery-tablesorter-2.26.6.min.js", 12));
126126
SCRIPTS.put("tablesorter-parsers", new FileScript("js/tablesorter-parsers-0.0.1.js", 13));
127127
SCRIPTS.put("searchable-option-list", new FileScript("js/searchable-option-list-2.0.4.min.js", 14));
128-
SCRIPTS.put("utils", new FileScript("js/utils-0.0.16.js", 15));
128+
SCRIPTS.put("utils", new FileScript("js/utils-0.0.17.js", 15));
129129
SCRIPTS.put("repos", new FileScript("js/repos-0.0.1.js", 20));
130130
SCRIPTS.put("diff", new FileScript("js/diff-0.0.2.js", 20));
131131
}

web/js/utils-0.0.16.js renamed to web/js/utils-0.0.17.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1565,6 +1565,7 @@ $(document).ready(function () {
15651565
*/
15661566
init_markdown_converter();
15671567

1568+
restoreFocusAfterSearchSubmit();
15681569
});
15691570

15701571
/**
@@ -1941,3 +1942,41 @@ function scope_on_scroll() {
19411942
function isOnSearchPage() {
19421943
return $(document.documentElement).hasClass('search');
19431944
}
1945+
1946+
/**
1947+
* Handles submit on search form.
1948+
*
1949+
* If submit was initiated by pressing the return key when focus was
1950+
* in a text field then `si` attribute will be added to the form.
1951+
*
1952+
* @param {HTMLFormElement} form
1953+
*/
1954+
function searchSubmit(form) {
1955+
var submitInitiator = '';
1956+
if (document.activeElement && document.activeElement.nodeName === 'INPUT') {
1957+
submitInitiator = document.activeElement.getAttribute('id');
1958+
}
1959+
if (submitInitiator) {
1960+
var input = document.createElement('INPUT');
1961+
input.setAttribute('name', 'si');
1962+
input.value = submitInitiator;
1963+
form.appendChild(input);
1964+
}
1965+
}
1966+
1967+
/**
1968+
* Restores focus on page load
1969+
*
1970+
* @see #searchSubmit
1971+
*/
1972+
function restoreFocusAfterSearchSubmit() {
1973+
var siParam = getParameter('si');
1974+
if (siParam) {
1975+
var $input = $('input[type=text][id="' + siParam + '"]');
1976+
if ($input.length === 1) {
1977+
$input[0].selectionStart = $input.val().length;
1978+
$input[0].selectionEnd = $input[0].selectionStart;
1979+
$input.focus();
1980+
}
1981+
}
1982+
}

web/menu.jspf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ org.opensolaris.opengrok.web.Util"
7777
</script><%
7878
}
7979
%>
80-
<form action="<%= request.getContextPath() %>/search" id="sbox">
80+
<form action="<%= request.getContextPath() %>/search" id="sbox" onsubmit="searchSubmit(this)">
8181
<div id="qtbl">
8282
<table>
8383
<%

0 commit comments

Comments
 (0)