vim_hotkeys.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. $(document).ready(function() {
  2. highlightResult('top')();
  3. $('.result').on('click', function() {
  4. highlightResult($(this))();
  5. });
  6. var vimKeys = {
  7. 27: {
  8. key: 'Escape',
  9. fun: removeFocus,
  10. des: 'remove focus from the focused input',
  11. cat: 'Control'
  12. },
  13. 73: {
  14. key: 'i',
  15. fun: searchInputFocus,
  16. des: 'focus on the search input',
  17. cat: 'Control'
  18. },
  19. 66: {
  20. key: 'b',
  21. fun: scrollPage(-window.innerHeight),
  22. des: 'scroll one page up',
  23. cat: 'Navigation'
  24. },
  25. 70: {
  26. key: 'f',
  27. fun: scrollPage(window.innerHeight),
  28. des: 'scroll one page down',
  29. cat: 'Navigation'
  30. },
  31. 85: {
  32. key: 'u',
  33. fun: scrollPage(-window.innerHeight / 2),
  34. des: 'scroll half a page up',
  35. cat: 'Navigation'
  36. },
  37. 68: {
  38. key: 'd',
  39. fun: scrollPage(window.innerHeight / 2),
  40. des: 'scroll half a page down',
  41. cat: 'Navigation'
  42. },
  43. 71: {
  44. key: 'g',
  45. fun: scrollPageTo(-document.body.scrollHeight),
  46. des: 'scroll to the top of the page',
  47. cat: 'Navigation'
  48. },
  49. 86: {
  50. key: 'v',
  51. fun: scrollPageTo(document.body.scrollHeight),
  52. des: 'scroll to the bottom of the page',
  53. cat: 'Navigation'
  54. },
  55. 75: {
  56. key: 'k',
  57. fun: highlightResult('up'),
  58. des: 'select previous search result',
  59. cat: 'Results'
  60. },
  61. 74: {
  62. key: 'j',
  63. fun: highlightResult('down'),
  64. des: 'select next search result',
  65. cat: 'Results'
  66. },
  67. 80: {
  68. key: 'p',
  69. fun: pageButtonClick(0),
  70. des: 'go to previous page',
  71. cat: 'Results'
  72. },
  73. 78: {
  74. key: 'n',
  75. fun: pageButtonClick(1),
  76. des: 'go to next page',
  77. cat: 'Results'
  78. },
  79. 79: {
  80. key: 'o',
  81. fun: openResult(false),
  82. des: 'open search result',
  83. cat: 'Results'
  84. },
  85. 84: {
  86. key: 't',
  87. fun: openResult(true),
  88. des: 'open the result in a new tab',
  89. cat: 'Results'
  90. },
  91. 82: {
  92. key: 'r',
  93. fun: reloadPage,
  94. des: 'reload page from the server',
  95. cat: 'Control'
  96. },
  97. 72: {
  98. key: 'h',
  99. fun: toggleHelp,
  100. des: 'toggle help window',
  101. cat: 'Other'
  102. }
  103. };
  104. $(document).keyup(function(e) {
  105. // check for modifiers so we don't break browser's hotkeys
  106. if (vimKeys.hasOwnProperty(e.keyCode)
  107. && !e.ctrlKey
  108. && !e.altKey
  109. && !e.shiftKey
  110. && !e.metaKey)
  111. {
  112. if (e.keyCode === 27) {
  113. if (e.target.tagName.toLowerCase() === 'input') {
  114. vimKeys[e.keyCode].fun();
  115. }
  116. } else {
  117. if (e.target === document.body) {
  118. vimKeys[e.keyCode].fun();
  119. }
  120. }
  121. }
  122. });
  123. function highlightResult(which) {
  124. return function() {
  125. var current = $('.result[data-vim-selected]');
  126. if (current.length === 0) {
  127. current = $('.result:first');
  128. if (current.length === 0) {
  129. return;
  130. }
  131. }
  132. var next;
  133. if (typeof which !== 'string') {
  134. next = which;
  135. } else {
  136. switch (which) {
  137. // case 'visible':
  138. // TODO
  139. case 'down':
  140. next = current.next('.result');
  141. if (next.length === 0) {
  142. next = $('.result:first');
  143. }
  144. break;
  145. case 'up':
  146. next = current.prev('.result');
  147. if (next.length === 0) {
  148. next = $('.result:last');
  149. }
  150. break;
  151. case 'bottom':
  152. next = $('.result:last');
  153. break;
  154. case 'top':
  155. default:
  156. next = $('.result:first');
  157. }
  158. }
  159. current.removeAttr('data-vim-selected').removeClass('well well-sm');
  160. next.attr('data-vim-selected', 'true').addClass('well well-sm');
  161. }
  162. }
  163. function reloadPage() {
  164. document.location.reload(false);
  165. }
  166. function removeFocus() {
  167. if (document.activeElement) {
  168. document.activeElement.blur();
  169. }
  170. }
  171. function pageButtonClick(num) {
  172. return function() {
  173. var buttons = $('div#pagination button[type="submit"]');
  174. if (buttons.length !== 2) {
  175. console.log('page navigation with this theme is not supported');
  176. return;
  177. }
  178. if (num >= 0 && num < buttons.length) {
  179. buttons[num].click();
  180. } else {
  181. console.log('pageButtonClick(): invalid argument');
  182. }
  183. }
  184. }
  185. function scrollPage(amount) {
  186. return function() {
  187. window.scrollBy(0, amount);
  188. highlightResult('visible')();
  189. }
  190. }
  191. function scrollPageTo(position) {
  192. return function() {
  193. window.scrollTo(0, position);
  194. highlightResult('visible')();
  195. }
  196. }
  197. function searchInputFocus() {
  198. $('input#q').focus();
  199. }
  200. function openResult(newTab) {
  201. return function() {
  202. var link = $('.result[data-vim-selected] .result_header a');
  203. if (link.length) {
  204. var url = link.attr('href');
  205. if (newTab) {
  206. window.open(url);
  207. } else {
  208. window.location.href = url;
  209. }
  210. }
  211. };
  212. }
  213. function toggleHelp() {
  214. var helpPanel = $('#vim-hotkeys-help');
  215. if (helpPanel.length) {
  216. helpPanel.toggleClass('hidden');
  217. return;
  218. }
  219. var categories = {};
  220. for (var k in vimKeys) {
  221. var key = vimKeys[k];
  222. categories[key.cat] = categories[key.cat] || [];
  223. categories[key.cat].push(key);
  224. }
  225. var sorted = Object.keys(categories).sort(function(a, b) {
  226. return categories[b].length - categories[a].length;
  227. });
  228. if (sorted.length === 0) {
  229. return;
  230. }
  231. var html = '<div id="vim-hotkeys-help" class="well vim-hotkeys-help">';
  232. html += '<div class="container-fluid">';
  233. html += '<div class="row">';
  234. html += '<div class="col-sm-12">';
  235. html += '<h3>How to navigate searx with Vim-like hotkeys</h3>';
  236. html += '</div>'; // col-sm-12
  237. html += '</div>'; // row
  238. for (var i = 0; i < sorted.length; i++) {
  239. var cat = categories[sorted[i]];
  240. var lastCategory = i === (sorted.length - 1);
  241. var first = i % 2 === 0;
  242. if (first) {
  243. html += '<div class="row dflex">';
  244. }
  245. html += '<div class="col-sm-' + (first && lastCategory ? 12 : 6) + ' dflex">';
  246. html += '<div class="panel panel-default iflex">';
  247. html += '<div class="panel-heading">' + cat[0].cat + '</div>';
  248. html += '<div class="panel-body">';
  249. html += '<ul class="list-unstyled">';
  250. for (var cj in cat) {
  251. html += '<li><kbd>' + cat[cj].key + '</kbd> ' + cat[cj].des + '</li>';
  252. }
  253. html += '</ul>';
  254. html += '</div>'; // panel-body
  255. html += '</div>'; // panel
  256. html += '</div>'; // col-sm-*
  257. if (!first || lastCategory) {
  258. html += '</div>'; // row
  259. }
  260. }
  261. html += '</div>'; // container-fluid
  262. html += '</div>'; // vim-hotkeys-help
  263. $('body').append(html);
  264. }
  265. });