|
515 | 515 | var $active = $results.filter('.highlighted');
|
516 | 516 |
|
517 | 517 | if (e.which === 38) { // up
|
518 |
| - e.preventDefault(); |
519 | 518 | if (!$active.length || !$active.prev()) {
|
520 | 519 | return;
|
521 | 520 | }
|
522 | 521 |
|
523 | 522 | $active.prev().addClass('highlighted');
|
524 | 523 | $active.removeClass('highlighted');
|
525 | 524 | } else if (e.which === 40) { // down
|
526 |
| - e.preventDefault(); |
527 | 525 | if (!$active.length) {
|
528 | 526 | $results.first().addClass('highlighted');
|
529 | 527 | } else if ($active.next().length) {
|
530 | 528 | $active.next().addClass('highlighted');
|
531 | 529 | $active.removeClass('highlighted');
|
532 | 530 | }
|
533 | 531 | } else if (e.which === 13) { // return
|
534 |
| - e.preventDefault(); |
535 | 532 | if ($active.length) {
|
536 | 533 | document.location.href = $active.find('a').prop('href');
|
537 | 534 | }
|
|
722 | 719 | }
|
723 | 720 |
|
724 | 721 | function startSearch() {
|
725 |
| - |
726 |
| - $(".search-input").on("keyup",function() { |
| 722 | + var searchTimeout; |
| 723 | + $(".search-input").on("keyup input",function() { |
| 724 | + clearTimeout(searchTimeout); |
727 | 725 | if ($(this).val().length === 0) {
|
728 | 726 | window.history.replaceState("", "std - Rust", "?search=");
|
729 | 727 | $('#main.content').removeClass('hidden');
|
730 | 728 | $('#search.content').addClass('hidden');
|
| 729 | + } else { |
| 730 | + searchTimeout = setTimeout(search, 500); |
731 | 731 | }
|
732 | 732 | });
|
733 |
| - |
734 |
| - var keyUpTimeout; |
735 |
| - $('.do-search').on('click', search); |
736 |
| - $('.search-input').on('keyup', function() { |
737 |
| - clearTimeout(keyUpTimeout); |
738 |
| - keyUpTimeout = setTimeout(search, 500); |
| 733 | + $('.search-form').on('submit', function(e){ |
| 734 | + e.preventDefault(); |
| 735 | + clearTimeout(searchTimeout); |
| 736 | + search(); |
| 737 | + }); |
| 738 | + $('.search-input').on('change paste', function(e) { |
| 739 | + // Do NOT e.preventDefault() here. It will prevent pasting. |
| 740 | + clearTimeout(searchTimeout); |
| 741 | + // zero-timeout necessary here because at the time of event handler execution the |
| 742 | + // pasted content is not in the input field yet. Shouldn’t make any difference for |
| 743 | + // change, though. |
| 744 | + setTimeout(search, 0); |
739 | 745 | });
|
740 | 746 |
|
741 | 747 | // Push and pop states are used to add search results to the browser
|
|
0 commit comments