Skip to content

Commit bd3ba77

Browse files
committed
Search automatically on URL query parameter
Automatically start searching when ?q= parameter is given in the URL, selecting and visiting the first match if available. If not available, the query will be filled into the search box but we won't navigate away.
1 parent e4d727f commit bd3ba77

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

lib/rdoc/generator/template/darkfish/js/darkfish.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,16 @@ function hookSearch() {
7676
}
7777

7878
search.scrollIntoView = search.scrollInWindow;
79+
80+
// Check for ?q= URL parameter and trigger search automatically
81+
if (typeof URLSearchParams !== 'undefined') {
82+
var urlParams = new URLSearchParams(window.location.search);
83+
var queryParam = urlParams.get('q');
84+
if (queryParam) {
85+
input.value = queryParam;
86+
search.search(queryParam, true);
87+
}
88+
}
7989
};
8090

8191
function hookFocus() {

lib/rdoc/generator/template/darkfish/js/search.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ Search.prototype = Object.assign({}, Navigation, new function() {
3434
}
3535

3636
this.search = function(value, selectFirstMatch) {
37+
this.selectFirstMatch = selectFirstMatch;
38+
3739
value = value.trim().toLowerCase();
3840
if (value) {
3941
this.setNavigationActive(true);
@@ -76,7 +78,15 @@ Search.prototype = Object.assign({}, Navigation, new function() {
7678
//TODO: ECMAScript
7779
//if (jQuery.browser.msie) this.$element[0].className += '';
7880

79-
if (isLast) this.result.setAttribute('aria-busy', 'false');
81+
if (this.selectFirstMatch && this.current) {
82+
this.selectFirstMatch = false;
83+
this.select(this.current);
84+
}
85+
86+
if (isLast) {
87+
this.selectFirstMatch = false;
88+
this.result.setAttribute('aria-busy', 'false');
89+
}
8090
}
8191

8292
this.move = function(isDown) {

0 commit comments

Comments
 (0)