summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Tolnay <dtolnay@gmail.com>2015-10-13 10:46:49 -0700
committerDavid Tolnay <dtolnay@gmail.com>2015-10-13 10:48:06 -0700
commit4a886f831c1ff5a6c4b60c08505d0e505cb5dd0b (patch)
tree79491ec56c1df395a2fdd4e682667a0525868d56
parente3c36777ab35ee9c7f81d791eff1bbad42c1f152 (diff)
Autocomplete manual search to first result on submit
-rw-r--r--docs/public/js/manual-search.js31
-rw-r--r--docs/templates/manual.liquid2
2 files changed, 22 insertions, 11 deletions
diff --git a/docs/public/js/manual-search.js b/docs/public/js/manual-search.js
index c2b13dec..eb7cff89 100644
--- a/docs/public/js/manual-search.js
+++ b/docs/public/js/manual-search.js
@@ -1,4 +1,7 @@
-var section_names = function(q, cb) {
+var section_names = function(q) {
+ if (!q) {
+ return [];
+ }
var matches = [];
q = q.toLowerCase();
$.each(section_map, function(k, v) {
@@ -10,23 +13,31 @@ var section_names = function(q, cb) {
// shortest to longest
return a.length - b.length;
});
- cb(matches);
+ return matches;
+}
+var section_names_cb = function(q, cb) {
+ cb(section_names(q));
}
-var go_to_section = function(section) {
- if (section in section_map) {
- location.hash = '#' + section_map[section];
+var go_to_section = function() {
+ query = $('#searchbox').val();
+ results = section_names(query);
+ if (results.length == 0) {
+ return;
+ }
+ result = results[0];
+ location.hash = '#' + section_map[result];
+ if (result != query) {
+ $('#searchbox').val(result);
}
}
$(function(){
$('#searchbox').typeahead(
{hint: false, highlight: true, minLength: 1},
- {name: "contents", source: section_names, limit: 6}
+ {name: "contents", source: section_names_cb, limit: 6}
).on('typeahead:selected', function(e, data) {
- go_to_section($(this).val());
- });
- $('#searchbox').change(function() {
- go_to_section($(this).val());
+ go_to_section();
});
+ $('#searchbox').change(go_to_section);
});
// add "Run" button to execute examples on jqplay.org
$(function() {
diff --git a/docs/templates/manual.liquid b/docs/templates/manual.liquid
index 59505a95..f1d7985a 100644
--- a/docs/templates/manual.liquid
+++ b/docs/templates/manual.liquid
@@ -16,7 +16,7 @@
</li>
{% endfor %}
</ul>
- <form class="form-group">
+ <form class="form-group" onsubmit="go_to_section(); return false;">
<input type="text"
class="form-control"
placeholder="Search"