|
712 | 712 | }); |
713 | 713 |
|
714 | 714 | var search_input = document.getElementsByClassName('search-input')[0]; |
715 | | - search_input.onkeydown = null; |
716 | 715 | search_input.onkeydown = function(e) { |
717 | | - var actives = []; |
| 716 | + // "actives" references the currently highlighted item in each search tab. |
| 717 | + // Each array in "actives" represents a tab. |
| 718 | + var actives = [[], [], []]; |
| 719 | + // "current" is used to know which tab we're looking into. |
| 720 | + var current = 0; |
718 | 721 | onEach(document.getElementsByClassName('search-results'), function(e) { |
719 | | - onEach(document.getElementsByClassName('highlighted'), function(e) { |
720 | | - actives.push(e); |
| 722 | + onEach(e.getElementsByClassName('highlighted'), function(e) { |
| 723 | + actives[current].push(e); |
721 | 724 | }); |
| 725 | + current += 1; |
722 | 726 | }); |
723 | 727 |
|
724 | 728 | if (e.which === 38) { // up |
725 | | - if (!actives.length || !actives[0].previousElementSibling) { |
| 729 | + if (!actives[currentTab].length || |
| 730 | + !actives[currentTab][0].previousElementSibling) { |
726 | 731 | return; |
727 | 732 | } |
728 | 733 |
|
729 | | - addClass(actives[0].previousElementSibling, 'highlighted'); |
730 | | - removeClass(actives[0], 'highlighted'); |
| 734 | + addClass(actives[currentTab][0].previousElementSibling, 'highlighted'); |
| 735 | + removeClass(actives[currentTab][0], 'highlighted'); |
731 | 736 | } else if (e.which === 40) { // down |
732 | | - if (!actives.length) { |
| 737 | + if (!actives[currentTab].length) { |
733 | 738 | var results = document.getElementsByClassName('search-results'); |
734 | 739 | if (results.length > 0) { |
735 | | - var res = results[0].getElementsByClassName('result'); |
| 740 | + var res = results[currentTab].getElementsByClassName('result'); |
736 | 741 | if (res.length > 0) { |
737 | 742 | addClass(res[0], 'highlighted'); |
738 | 743 | } |
739 | 744 | } |
740 | | - } else if (actives[0].nextElementSibling) { |
741 | | - addClass(actives[0].nextElementSibling, 'highlighted'); |
742 | | - removeClass(actives[0], 'highlighted'); |
| 745 | + } else if (actives[currentTab][0].nextElementSibling) { |
| 746 | + addClass(actives[currentTab][0].nextElementSibling, 'highlighted'); |
| 747 | + removeClass(actives[currentTab][0], 'highlighted'); |
743 | 748 | } |
744 | 749 | } else if (e.which === 13) { // return |
745 | | - if (actives.length) { |
746 | | - document.location.href = actives[0].getElementsByTagName('a')[0].href; |
| 750 | + if (actives[currentTab].length) { |
| 751 | + document.location.href = |
| 752 | + actives[currentTab][0].getElementsByTagName('a')[0].href; |
747 | 753 | } |
748 | | - } else if (actives.length > 0) { |
749 | | - removeClass(actives[0], 'highlighted'); |
| 754 | + } else if (e.which === 9) { // tab |
| 755 | + if (e.shiftKey) { |
| 756 | + printTab(currentTab > 0 ? currentTab - 1 : 2); |
| 757 | + } else { |
| 758 | + printTab(currentTab > 1 ? 0 : currentTab + 1); |
| 759 | + } |
| 760 | + e.preventDefault(); |
| 761 | + } else if (e.which === 16) { // shift |
| 762 | + // Does nothing, it's just to avoid losing "focus" on the highlighted element. |
| 763 | + } else if (actives[currentTab].length > 0) { |
| 764 | + removeClass(actives[currentTab][0], 'highlighted'); |
750 | 765 | } |
751 | 766 | }; |
752 | 767 | } |
|
0 commit comments