Browse Source

Add search result navigation support

Kirill Isakov 9 years ago
parent
commit
0d6625e070
2 changed files with 67 additions and 9 deletions
  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 View File

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

+ 66
- 9
searx/static/plugins/js/vim_hotkeys.js View File

1
 $(document).ready(function() {
1
 $(document).ready(function() {
2
+    highlightResult('top')();
3
+
4
+    $('.result').on('click', function() {
5
+        highlightResult($(this))();
6
+    });
7
+
2
     var vimKeys = {
8
     var vimKeys = {
3
         27: {
9
         27: {
4
             key: 'Escape',
10
             key: 'Escape',
50
         },
56
         },
51
         75: {
57
         75: {
52
             key: 'k',
58
             key: 'k',
53
-            fun: previousResult,
59
+            fun: highlightResult('up'),
54
             des: 'select previous search result',
60
             des: 'select previous search result',
55
             cat: 'Results'
61
             cat: 'Results'
56
         },
62
         },
57
         74: {
63
         74: {
58
             key: 'j',
64
             key: 'j',
59
-            fun: nextResult,
65
+            fun: highlightResult('down'),
60
             des: 'select next search result',
66
             des: 'select next search result',
61
             cat: 'Results'
67
             cat: 'Results'
62
         },
68
         },
75
         79: {
81
         79: {
76
             key: 'o',
82
             key: 'o',
77
             fun: openResult(false),
83
             fun: openResult(false),
78
-            des: 'open  search result',
84
+            des: 'open search result',
79
             cat: 'Results'
85
             cat: 'Results'
80
         },
86
         },
81
         84: {
87
         84: {
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
     function reloadPage() {
171
     function reloadPage() {
122
         document.location.reload(false);
172
         document.location.reload(false);
123
     }
173
     }
146
     function scrollPage(amount) {
196
     function scrollPage(amount) {
147
         return function() {
197
         return function() {
148
             window.scrollBy(0, amount);
198
             window.scrollBy(0, amount);
199
+            highlightResult('visible')();
149
         }
200
         }
150
     }
201
     }
151
 
202
 
152
     function scrollPageTo(position) {
203
     function scrollPageTo(position) {
153
         return function() {
204
         return function() {
154
             window.scrollTo(0, position);
205
             window.scrollTo(0, position);
206
+            highlightResult('visible')();
155
         }
207
         }
156
     }
208
     }
157
 
209
 
159
         $('input#q').focus();
211
         $('input#q').focus();
160
     }
212
     }
161
 
213
 
162
-    function previousResult() {
163
-    }
164
-
165
-    function nextResult() {
166
-    }
167
-
168
     function openResult(newTab) {
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
     function toggleHelp() {
228
     function toggleHelp() {