소스 검색

Add search result navigation support

Kirill Isakov 9 년 전
부모
커밋
0d6625e070
2개의 변경된 파일67개의 추가작업 그리고 9개의 파일을 삭제
  1. 1
    0
      searx/static/plugins/css/vim_hotkeys.css
  2. 66
    9
      searx/static/plugins/js/vim_hotkeys.js

+ 1
- 0
searx/static/plugins/css/vim_hotkeys.css 파일 보기

@@ -6,6 +6,7 @@
6 6
     z-index: 9999999;
7 7
     overflow-y: auto;
8 8
     max-height: 80%;
9
+    box-shadow: 0 0 1em;
9 10
 }
10 11
 
11 12
 .dflex {

+ 66
- 9
searx/static/plugins/js/vim_hotkeys.js 파일 보기

@@ -1,4 +1,10 @@
1 1
 $(document).ready(function() {
2
+    highlightResult('top')();
3
+
4
+    $('.result').on('click', function() {
5
+        highlightResult($(this))();
6
+    });
7
+
2 8
     var vimKeys = {
3 9
         27: {
4 10
             key: 'Escape',
@@ -50,13 +56,13 @@ $(document).ready(function() {
50 56
         },
51 57
         75: {
52 58
             key: 'k',
53
-            fun: previousResult,
59
+            fun: highlightResult('up'),
54 60
             des: 'select previous search result',
55 61
             cat: 'Results'
56 62
         },
57 63
         74: {
58 64
             key: 'j',
59
-            fun: nextResult,
65
+            fun: highlightResult('down'),
60 66
             des: 'select next search result',
61 67
             cat: 'Results'
62 68
         },
@@ -75,7 +81,7 @@ $(document).ready(function() {
75 81
         79: {
76 82
             key: 'o',
77 83
             fun: openResult(false),
78
-            des: 'open  search result',
84
+            des: 'open search result',
79 85
             cat: 'Results'
80 86
         },
81 87
         84: {
@@ -118,6 +124,50 @@ $(document).ready(function() {
118 124
         }
119 125
     });
120 126
 
127
+    function highlightResult(which) {
128
+        return function() {
129
+            var current = $('.result[data-vim-selected]');
130
+            if (current.length === 0) {
131
+                current = $('.result:first');
132
+                if (current.length === 0) {
133
+                    return;
134
+                }
135
+            }
136
+
137
+            var next;
138
+
139
+            if (typeof which !== 'string') {
140
+                next = which;
141
+            } else {
142
+                switch (which) {
143
+                    // case 'visible':
144
+                    // TODO
145
+                    case 'down':
146
+                        next = current.next('.result');
147
+                        if (next.length === 0) {
148
+                            next = $('.result:first');
149
+                        }
150
+                        break;
151
+                    case 'up':
152
+                        next = current.prev('.result');
153
+                        if (next.length === 0) {
154
+                            next = $('.result:last');
155
+                        }
156
+                        break;
157
+                    case 'bottom':
158
+                        next = $('.result:last');
159
+                        break;
160
+                    case 'top':
161
+                    default:
162
+                        next = $('.result:first');
163
+                }
164
+            }
165
+
166
+            current.removeAttr('data-vim-selected').removeClass('well well-sm');
167
+            next.attr('data-vim-selected', 'true').addClass('well well-sm');
168
+        }
169
+    }
170
+
121 171
     function reloadPage() {
122 172
         document.location.reload(false);
123 173
     }
@@ -146,12 +196,14 @@ $(document).ready(function() {
146 196
     function scrollPage(amount) {
147 197
         return function() {
148 198
             window.scrollBy(0, amount);
199
+            highlightResult('visible')();
149 200
         }
150 201
     }
151 202
 
152 203
     function scrollPageTo(position) {
153 204
         return function() {
154 205
             window.scrollTo(0, position);
206
+            highlightResult('visible')();
155 207
         }
156 208
     }
157 209
 
@@ -159,13 +211,18 @@ $(document).ready(function() {
159 211
         $('input#q').focus();
160 212
     }
161 213
 
162
-    function previousResult() {
163
-    }
164
-
165
-    function nextResult() {
166
-    }
167
-
168 214
     function openResult(newTab) {
215
+        return function() {
216
+            var link = $('.result[data-vim-selected] .result_header a');
217
+            if (link.length) {
218
+                var url = link.attr('href');
219
+                if (newTab) {
220
+                    window.open(url);
221
+                } else {
222
+                    window.location.href = url;
223
+                }
224
+            }
225
+        };
169 226
     }
170 227
 
171 228
     function toggleHelp() {