@ -1,9 +1,9 @@
/* SPDX-License-Identifier: AGPL-3.0-or-later */
/* SPDX-License-Identifier: AGPL-3.0-or-later */
/* global searxng*/
/* global searxng */
searxng . ready ( function ( ) {
searxng . ready ( function ( ) {
function isElementInDetail ( el ) {
function isElementInDetail ( el ) {
while ( el !== undefined ) {
while ( el !== undefined ) {
if ( el . classList . contains ( 'detail' ) ) {
if ( el . classList . contains ( 'detail' ) ) {
return true ;
return true ;
@ -18,7 +18,7 @@ searxng.ready(function() {
return false ;
return false ;
}
}
function getResultElement ( el ) {
function getResultElement ( el ) {
while ( el !== undefined ) {
while ( el !== undefined ) {
if ( el . classList . contains ( 'result' ) ) {
if ( el . classList . contains ( 'result' ) ) {
return el ;
return el ;
@ -28,11 +28,11 @@ searxng.ready(function() {
return undefined ;
return undefined ;
}
}
function isImageResult ( resultElement ) {
function isImageResult ( resultElement ) {
return resultElement && resultElement . classList . contains ( 'result-images' ) ;
return resultElement && resultElement . classList . contains ( 'result-images' ) ;
}
}
searxng . on ( '.result' , 'click' , function ( e ) {
searxng . on ( '.result' , 'click' , function ( e ) {
if ( ! isElementInDetail ( e . target ) ) {
if ( ! isElementInDetail ( e . target ) ) {
highlightResult ( this ) ( true ) ;
highlightResult ( this ) ( true ) ;
let resultElement = getResultElement ( e . target ) ;
let resultElement = getResultElement ( e . target ) ;
@ -43,7 +43,7 @@ searxng.ready(function() {
}
}
} ) ;
} ) ;
searxng . on ( '.result a' , 'focus' , function ( e ) {
searxng . on ( '.result a' , 'focus' , function ( e ) {
if ( ! isElementInDetail ( e . target ) ) {
if ( ! isElementInDetail ( e . target ) ) {
let resultElement = getResultElement ( e . target ) ;
let resultElement = getResultElement ( e . target ) ;
if ( resultElement && resultElement . getAttribute ( "data-vim-selected" ) === null ) {
if ( resultElement && resultElement . getAttribute ( "data-vim-selected" ) === null ) {
@ -155,7 +155,7 @@ searxng.ready(function() {
} ;
} ;
if ( searxng . hotkeys ) {
if ( searxng . hotkeys ) {
searxng . on ( document , "keydown" , function ( e ) {
searxng . on ( document , "keydown" , function ( e ) {
// check for modifiers so we don't break browser's hotkeys
// check for modifiers so we don't break browser's hotkeys
if ( Object . prototype . hasOwnProperty . call ( vimKeys , e . keyCode ) && ! e . ctrlKey && ! e . altKey && ! e . shiftKey && ! e . metaKey ) {
if ( Object . prototype . hasOwnProperty . call ( vimKeys , e . keyCode ) && ! e . ctrlKey && ! e . altKey && ! e . shiftKey && ! e . metaKey ) {
var tagName = e . target . tagName . toLowerCase ( ) ;
var tagName = e . target . tagName . toLowerCase ( ) ;
@ -171,10 +171,10 @@ searxng.ready(function() {
} ) ;
} ) ;
}
}
function highlightResult ( which ) {
function highlightResult ( which ) {
return function ( noScroll ) {
return function ( noScroll ) {
var current = document . querySelector ( '.result[data-vim-selected]' ) ,
var current = document . querySelector ( '.result[data-vim-selected]' ) ,
effectiveWhich = which ;
effectiveWhich = which ;
if ( current === null ) {
if ( current === null ) {
// no selection : choose the first one
// no selection : choose the first one
current = document . querySelector ( '.result' ) ;
current = document . querySelector ( '.result' ) ;
@ -194,7 +194,7 @@ searxng.ready(function() {
next = effectiveWhich ;
next = effectiveWhich ;
} else {
} else {
switch ( effectiveWhich ) {
switch ( effectiveWhich ) {
case 'visible' :
case 'visible' :
var top = document . documentElement . scrollTop || document . body . scrollTop ;
var top = document . documentElement . scrollTop || document . body . scrollTop ;
var bot = top + document . documentElement . clientHeight ;
var bot = top + document . documentElement . clientHeight ;
@ -208,24 +208,24 @@ searxng.ready(function() {
}
}
}
}
break ;
break ;
case 'down' :
case 'down' :
next = current . nextElementSibling ;
next = current . nextElementSibling ;
if ( next === null ) {
if ( next === null ) {
next = results [ 0 ] ;
next = results [ 0 ] ;
}
}
break ;
break ;
case 'up' :
case 'up' :
next = current . previousElementSibling ;
next = current . previousElementSibling ;
if ( next === null ) {
if ( next === null ) {
next = results [ results . length - 1 ] ;
next = results [ results . length - 1 ] ;
}
}
break ;
break ;
case 'bottom' :
case 'bottom' :
next = results [ results . length - 1 ] ;
next = results [ results . length - 1 ] ;
break ;
break ;
case 'top' :
case 'top' :
/* falls through */
/* falls through */
default :
default :
next = results [ 0 ] ;
next = results [ 0 ] ;
}
}
}
}
@ -244,11 +244,11 @@ searxng.ready(function() {
} ;
} ;
}
}
function reloadPage ( ) {
function reloadPage ( ) {
document . location . reload ( true ) ;
document . location . reload ( true ) ;
}
}
function removeFocus ( e ) {
function removeFocus ( e ) {
const tagName = e . target . tagName . toLowerCase ( ) ;
const tagName = e . target . tagName . toLowerCase ( ) ;
if ( document . activeElement && ( tagName === 'input' || tagName === 'select' || tagName === 'textarea' ) ) {
if ( document . activeElement && ( tagName === 'input' || tagName === 'select' || tagName === 'textarea' ) ) {
document . activeElement . blur ( ) ;
document . activeElement . blur ( ) ;
@ -257,8 +257,8 @@ searxng.ready(function() {
}
}
}
}
function pageButtonClick ( css _selector ) {
function pageButtonClick ( css _selector ) {
return function ( ) {
return function ( ) {
var button = document . querySelector ( css _selector ) ;
var button = document . querySelector ( css _selector ) ;
if ( button ) {
if ( button ) {
button . click ( ) ;
button . click ( ) ;
@ -266,24 +266,24 @@ searxng.ready(function() {
} ;
} ;
}
}
function GoToNextPage ( ) {
function GoToNextPage ( ) {
return pageButtonClick ( 'nav#pagination .next_page button[type="submit"]' ) ;
return pageButtonClick ( 'nav#pagination .next_page button[type="submit"]' ) ;
}
}
function GoToPreviousPage ( ) {
function GoToPreviousPage ( ) {
return pageButtonClick ( 'nav#pagination .previous_page button[type="submit"]' ) ;
return pageButtonClick ( 'nav#pagination .previous_page button[type="submit"]' ) ;
}
}
function scrollPageToSelected ( ) {
function scrollPageToSelected ( ) {
var sel = document . querySelector ( '.result[data-vim-selected]' ) ;
var sel = document . querySelector ( '.result[data-vim-selected]' ) ;
if ( sel === null ) {
if ( sel === null ) {
return ;
return ;
}
}
var wtop = document . documentElement . scrollTop || document . body . scrollTop ,
var wtop = document . documentElement . scrollTop || document . body . scrollTop ,
wheight = document . documentElement . clientHeight ,
wheight = document . documentElement . clientHeight ,
etop = sel . offsetTop ,
etop = sel . offsetTop ,
ebot = etop + sel . clientHeight ,
ebot = etop + sel . clientHeight ,
offset = 120 ;
offset = 120 ;
// first element ?
// first element ?
if ( ( sel . previousElementSibling === null ) && ( ebot < wheight ) ) {
if ( ( sel . previousElementSibling === null ) && ( ebot < wheight ) ) {
// set to the top of page if the first element
// set to the top of page if the first element
@ -301,27 +301,27 @@ searxng.ready(function() {
}
}
}
}
function scrollPage ( amount ) {
function scrollPage ( amount ) {
return function ( ) {
return function ( ) {
window . scrollBy ( 0 , amount ) ;
window . scrollBy ( 0 , amount ) ;
highlightResult ( 'visible' ) ( ) ;
highlightResult ( 'visible' ) ( ) ;
} ;
} ;
}
}
function scrollPageTo ( position , nav ) {
function scrollPageTo ( position , nav ) {
return function ( ) {
return function ( ) {
window . scrollTo ( 0 , position ) ;
window . scrollTo ( 0 , position ) ;
highlightResult ( nav ) ( ) ;
highlightResult ( nav ) ( ) ;
} ;
} ;
}
}
function searchInputFocus ( ) {
function searchInputFocus ( ) {
window . scrollTo ( 0 , 0 ) ;
window . scrollTo ( 0 , 0 ) ;
document . querySelector ( '#q' ) . focus ( ) ;
document . querySelector ( '#q' ) . focus ( ) ;
}
}
function openResult ( newTab ) {
function openResult ( newTab ) {
return function ( ) {
return function ( ) {
var link = document . querySelector ( '.result[data-vim-selected] h3 a' ) ;
var link = document . querySelector ( '.result[data-vim-selected] h3 a' ) ;
if ( link === null ) {
if ( link === null ) {
link = document . querySelector ( '.result[data-vim-selected] > a' ) ;
link = document . querySelector ( '.result[data-vim-selected] > a' ) ;
@ -337,7 +337,7 @@ searxng.ready(function() {
} ;
} ;
}
}
function initHelpContent ( divElement ) {
function initHelpContent ( divElement ) {
var categories = { } ;
var categories = { } ;
for ( var k in vimKeys ) {
for ( var k in vimKeys ) {
@ -346,7 +346,7 @@ searxng.ready(function() {
categories [ key . cat ] . push ( key ) ;
categories [ key . cat ] . push ( key ) ;
}
}
var sorted = Object . keys ( categories ) . sort ( function ( a , b ) {
var sorted = Object . keys ( categories ) . sort ( function ( a , b ) {
return categories [ b ] . length - categories [ a ] . length ;
return categories [ b ] . length - categories [ a ] . length ;
} ) ;
} ) ;
@ -386,23 +386,23 @@ searxng.ready(function() {
html += '</table>' ;
html += '</table>' ;
divElement . innerHTML = html ;
divElement . innerHTML = html ;
}
}
function toggleHelp ( ) {
function toggleHelp ( ) {
var helpPanel = document . querySelector ( '#vim-hotkeys-help' ) ;
var helpPanel = document . querySelector ( '#vim-hotkeys-help' ) ;
if ( helpPanel === undefined || helpPanel === null ) {
if ( helpPanel === undefined || helpPanel === null ) {
// first call
// first call
helpPanel = document . createElement ( 'div' ) ;
helpPanel = document . createElement ( 'div' ) ;
helpPanel . id = 'vim-hotkeys-help' ;
helpPanel . id = 'vim-hotkeys-help' ;
helpPanel . className = 'dialog-modal' ;
helpPanel . className = 'dialog-modal' ;
initHelpContent ( helpPanel ) ;
initHelpContent ( helpPanel ) ;
initHelpContent ( helpPanel ) ;
initHelpContent ( helpPanel ) ;
initHelpContent ( helpPanel ) ;
initHelpContent ( helpPanel ) ;
var body = document . getElementsByTagName ( 'body' ) [ 0 ] ;
var body = document . getElementsByTagName ( 'body' ) [ 0 ] ;
body . appendChild ( helpPanel ) ;
body . appendChild ( helpPanel ) ;
} else {
} else {
// togggle hidden
// togggle hidden
helpPanel . classList . toggle ( 'invisible' ) ;
helpPanel . classList . toggle ( 'invisible' ) ;
return ;
return ;
}
}