on master help sign in
Add auto page scrolling to selected result
commit7b48a663503018dd47b81c94afd9c4df6d62467a authorKirill Isakov <ukwt@ya.ru> dateSun 24 Apr 2016, 21:04 (10 years ago) parent0d6625e
1 file changed+45−4
searx/static/plugins/js/vim_hotkeys.js+45−4
searx/static/plugins/js/vim_hotkeys.js+45−4
@@ -140,8 +140,21 @@ $(document).ready(function() {
140140 next = which;
141141 } else {
142142 switch (which) {
143− // case 'visible':
144− // TODO
143+ case 'visible':
144+ var top = $(window).scrollTop();
145+ var bot = top + $(window).height();
146+ var results = $('.result');
147+
148+ for (var i = 0; i < results.length; i++) {
149+ next = $(results[i]);
150+ var etop = next.offset().top;
151+ var ebot = etop + next.height();
152+
153+ if ((ebot <= bot) && (etop > top)) {
154+ break;
155+ }
156+ }
157+ break;
145158 case 'down':
146159 next = current.next('.result');
147160 if (next.length === 0) {
@@ -163,8 +176,11 @@ $(document).ready(function() {
163176 }
164177 }
165178
166− current.removeAttr('data-vim-selected').removeClass('well well-sm');
167− next.attr('data-vim-selected', 'true').addClass('well well-sm');
179+ if (next) {
180+ current.removeAttr('data-vim-selected').removeClass('well well-sm');
181+ next.attr('data-vim-selected', 'true').addClass('well well-sm');
182+ scrollPageToSelected();
183+ }
168184 }
169185 }
170186
@@ -193,6 +209,31 @@ $(document).ready(function() {
193209 }
194210 }
195211
212+ function scrollPageToSelected() {
213+ var sel = $('.result[data-vim-selected]');
214+ if (sel.length !== 1) {
215+ return;
216+ }
217+
218+ var wnd = $(window);
219+
220+ var wtop = wnd.scrollTop();
221+ var etop = sel.offset().top;
222+
223+ var offset = 30;
224+
225+ if (wtop > etop) {
226+ wnd.scrollTop(etop - offset);
227+ } else {
228+ var ebot = etop + sel.height();
229+ var wbot = wtop + wnd.height();
230+
231+ if (wbot < ebot) {
232+ wnd.scrollTop(ebot - wnd.height() + offset);
233+ }
234+ }
235+ }
236+
196237 function scrollPage(amount) {
197238 return function() {
198239 window.scrollBy(0, amount);