[oscar] fix indentation of plugin JS

This change was split off into a separate commit
so that the previous commit had a clean diff.
This commit is contained in:
Martin Fischer 2022-01-20 10:07:10 +01:00
parent 132917ff42
commit 213ce85252
3 changed files with 452 additions and 452 deletions

View file

@ -1,66 +1,66 @@
if (searxng.plugins['searx.plugins.infinite_scroll']) { if (searxng.plugins['searx.plugins.infinite_scroll']) {
function hasScrollbar() { function hasScrollbar() {
var root = document.compatMode=='BackCompat'? document.body : document.documentElement; var root = document.compatMode=='BackCompat'? document.body : document.documentElement;
return root.scrollHeight>root.clientHeight; return root.scrollHeight>root.clientHeight;
} }
function loadNextPage() { function loadNextPage() {
var formData = $('#pagination form:last').serialize(); var formData = $('#pagination form:last').serialize();
if (formData) { if (formData) {
$('#pagination').html('<div class="loading-spinner"></div>'); $('#pagination').html('<div class="loading-spinner"></div>');
$.ajax({ $.ajax({
type: "POST", type: "POST",
url: $('#search_form').prop('action'), url: $('#search_form').prop('action'),
data: formData, data: formData,
dataType: 'html', dataType: 'html',
success: function(data) { success: function(data) {
var body = $(data); var body = $(data);
$('#pagination').remove(); $('#pagination').remove();
$('#main_results').append('<hr/>'); $('#main_results').append('<hr/>');
$('#main_results').append(body.find('.result')); $('#main_results').append(body.find('.result'));
$('#main_results').append(body.find('#pagination')); $('#main_results').append(body.find('#pagination'));
if(!hasScrollbar()) { if(!hasScrollbar()) {
loadNextPage(); loadNextPage();
}
} }
} });
}); }
} }
}
$(document).ready(function() { $(document).ready(function() {
var win = $(window); var win = $(window);
if(!hasScrollbar()) { if(!hasScrollbar()) {
loadNextPage();
}
win.scroll(function() {
$("#pagination button").css("visibility", "hidden");
if ($(document).height() - win.height() - win.scrollTop() < 150) {
loadNextPage(); loadNextPage();
} }
win.scroll(function() {
$("#pagination button").css("visibility", "hidden");
if ($(document).height() - win.height() - win.scrollTop() < 150) {
loadNextPage();
}
});
}); });
});
const style = document.createElement('style'); const style = document.createElement('style');
style.textContent = ` style.textContent = `
@keyframes rotate-forever { @keyframes rotate-forever {
0% { transform: rotate(0deg) } 0% { transform: rotate(0deg) }
100% { transform: rotate(360deg) } 100% { transform: rotate(360deg) }
} }
.loading-spinner { .loading-spinner {
animation-duration: 0.75s; animation-duration: 0.75s;
animation-iteration-count: infinite; animation-iteration-count: infinite;
animation-name: rotate-forever; animation-name: rotate-forever;
animation-timing-function: linear; animation-timing-function: linear;
height: 30px; height: 30px;
width: 30px; width: 30px;
border: 8px solid #666; border: 8px solid #666;
border-right-color: transparent; border-right-color: transparent;
border-radius: 50% !important; border-radius: 50% !important;
margin: 0 auto; margin: 0 auto;
} }
#pagination button { #pagination button {
visibility: hidden; visibility: hidden;
} }
`; `;
document.head.append(style); document.head.append(style);
} }

View file

@ -1,44 +1,44 @@
if (searxng.plugins['searx.plugins.search_on_category_select']) { if (searxng.plugins['searx.plugins.search_on_category_select']) {
$(document).ready(function() { $(document).ready(function() {
if($('#q').length) { if($('#q').length) {
$('#categories label').click(function(e) { $('#categories label').click(function(e) {
$('#categories input[type="checkbox"]').each(function(i, checkbox) { $('#categories input[type="checkbox"]').each(function(i, checkbox) {
$(checkbox).prop('checked', false); $(checkbox).prop('checked', false);
});
$(document.getElementById($(this).attr("for"))).prop('checked', true);
if($('#q').val()) {
if (getHttpRequest() == "GET") {
$('#search_form').attr('action', $('#search_form').serialize());
}
$('#search_form').submit();
}
return false;
}); });
$(document.getElementById($(this).attr("for"))).prop('checked', true); $('#time-range').change(function(e) {
if($('#q').val()) { if($('#q').val()) {
if (getHttpRequest() == "GET") { if (getHttpRequest() == "GET") {
$('#search_form').attr('action', $('#search_form').serialize()); $('#search_form').attr('action', $('#search_form').serialize());
}
$('#search_form').submit();
} }
$('#search_form').submit(); });
} $('#language').change(function(e) {
return false; if($('#q').val()) {
}); if (getHttpRequest() == "GET") {
$('#time-range').change(function(e) { $('#search_form').attr('action', $('#search_form').serialize());
if($('#q').val()) { }
if (getHttpRequest() == "GET") { $('#search_form').submit();
$('#search_form').attr('action', $('#search_form').serialize());
} }
$('#search_form').submit(); });
} }
}); });
$('#language').change(function(e) {
if($('#q').val()) {
if (getHttpRequest() == "GET") {
$('#search_form').attr('action', $('#search_form').serialize());
}
$('#search_form').submit();
}
});
}
});
function getHttpRequest() { function getHttpRequest() {
httpRequest = "POST"; httpRequest = "POST";
urlParams = new URLSearchParams(window.location.search); urlParams = new URLSearchParams(window.location.search);
if (urlParams.has('method')) { if (urlParams.has('method')) {
httpRequest = urlParams.get('method'); httpRequest = urlParams.get('method');
}
return httpRequest;
} }
return httpRequest;
}
} }

View file

@ -1,378 +1,378 @@
if (searxng.plugins['searx.plugins.vim_hotkeys']) { if (searxng.plugins['searx.plugins.vim_hotkeys']) {
$(document).ready(function() { $(document).ready(function() {
highlightResult('top')(); highlightResult('top')();
$('.result').on('click', function() { $('.result').on('click', function() {
highlightResult($(this))(); highlightResult($(this))();
});
var vimKeys = {
27: {
key: 'Escape',
fun: removeFocus,
des: 'remove focus from the focused input',
cat: 'Control'
},
73: {
key: 'i',
fun: searchInputFocus,
des: 'focus on the search input',
cat: 'Control'
},
66: {
key: 'b',
fun: scrollPage(-window.innerHeight),
des: 'scroll one page up',
cat: 'Navigation'
},
70: {
key: 'f',
fun: scrollPage(window.innerHeight),
des: 'scroll one page down',
cat: 'Navigation'
},
85: {
key: 'u',
fun: scrollPage(-window.innerHeight / 2),
des: 'scroll half a page up',
cat: 'Navigation'
},
68: {
key: 'd',
fun: scrollPage(window.innerHeight / 2),
des: 'scroll half a page down',
cat: 'Navigation'
},
71: {
key: 'g',
fun: scrollPageTo(-document.body.scrollHeight, 'top'),
des: 'scroll to the top of the page',
cat: 'Navigation'
},
86: {
key: 'v',
fun: scrollPageTo(document.body.scrollHeight, 'bottom'),
des: 'scroll to the bottom of the page',
cat: 'Navigation'
},
75: {
key: 'k',
fun: highlightResult('up'),
des: 'select previous search result',
cat: 'Results'
},
74: {
key: 'j',
fun: highlightResult('down'),
des: 'select next search result',
cat: 'Results'
},
80: {
key: 'p',
fun: pageButtonClick(0),
des: 'go to previous page',
cat: 'Results'
},
78: {
key: 'n',
fun: pageButtonClick(1),
des: 'go to next page',
cat: 'Results'
},
79: {
key: 'o',
fun: openResult(false),
des: 'open search result',
cat: 'Results'
},
84: {
key: 't',
fun: openResult(true),
des: 'open the result in a new tab',
cat: 'Results'
},
82: {
key: 'r',
fun: reloadPage,
des: 'reload page from the server',
cat: 'Control'
},
72: {
key: 'h',
fun: toggleHelp,
des: 'toggle help window',
cat: 'Other'
}
};
$(document).keydown(function(e) {
// check for modifiers so we don't break browser's hotkeys
if (vimKeys.hasOwnProperty(e.keyCode)
&& !e.ctrlKey
&& !e.altKey
&& !e.shiftKey
&& !e.metaKey)
{
if (e.keyCode === 27) {
if (e.target.tagName.toLowerCase() === 'input') {
vimKeys[e.keyCode].fun();
}
} else {
if (e.target === document.body) {
e.preventDefault();
vimKeys[e.keyCode].fun();
}
}
}
});
function nextResult(current, direction) {
var next = current[direction]();
while (!next.is('.result') && next.length !== 0) {
next = next[direction]();
}
return next
}
function highlightResult(which) {
return function() {
var current = $('.result[data-vim-selected]');
if (current.length === 0) {
current = $('.result:first');
if (current.length === 0) {
return;
}
}
var next;
if (typeof which !== 'string') {
next = which;
} else {
switch (which) {
case 'visible':
var top = $(window).scrollTop();
var bot = top + $(window).height();
var results = $('.result');
for (var i = 0; i < results.length; i++) {
next = $(results[i]);
var etop = next.offset().top;
var ebot = etop + next.height();
if ((ebot <= bot) && (etop > top)) {
break;
}
}
break;
case 'down':
next = nextResult(current, 'next');
if (next.length === 0) {
next = $('.result:first');
}
break;
case 'up':
next = nextResult(current, 'prev');
if (next.length === 0) {
next = $('.result:last');
}
break;
case 'bottom':
next = $('.result:last');
break;
case 'top':
default:
next = $('.result:first');
}
}
if (next) {
current.removeAttr('data-vim-selected').removeClass('well well-sm');
next.attr('data-vim-selected', 'true').addClass('well well-sm');
scrollPageToSelected();
}
}
}
function reloadPage() {
document.location.reload(false);
}
function removeFocus() {
if (document.activeElement) {
document.activeElement.blur();
}
}
function pageButtonClick(num) {
return function() {
var buttons = $('div#pagination button[type="submit"]');
if (buttons.length !== 2) {
console.log('page navigation with this theme is not supported');
return;
}
if (num >= 0 && num < buttons.length) {
buttons[num].click();
} else {
console.log('pageButtonClick(): invalid argument');
}
}
}
function scrollPageToSelected() {
var sel = $('.result[data-vim-selected]');
if (sel.length !== 1) {
return;
}
var wnd = $(window);
var wtop = wnd.scrollTop();
var etop = sel.offset().top;
var offset = 30;
if (wtop > etop) {
wnd.scrollTop(etop - offset);
} else {
var ebot = etop + sel.height();
var wbot = wtop + wnd.height();
if (wbot < ebot) {
wnd.scrollTop(ebot - wnd.height() + offset);
}
}
}
function scrollPage(amount) {
return function() {
window.scrollBy(0, amount);
highlightResult('visible')();
}
}
function scrollPageTo(position, nav) {
return function() {
window.scrollTo(0, position);
highlightResult(nav)();
}
}
function searchInputFocus() {
$('input#q').focus();
}
function openResult(newTab) {
return function() {
var link = $('.result[data-vim-selected] .result_header a');
if (link.length) {
var url = link.attr('href');
if (newTab) {
window.open(url);
} else {
window.location.href = url;
}
}
};
}
function toggleHelp() {
var helpPanel = $('#vim-hotkeys-help');
if (helpPanel.length) {
helpPanel.toggleClass('hidden');
return;
}
var categories = {};
for (var k in vimKeys) {
var key = vimKeys[k];
categories[key.cat] = categories[key.cat] || [];
categories[key.cat].push(key);
}
var sorted = Object.keys(categories).sort(function(a, b) {
return categories[b].length - categories[a].length;
}); });
if (sorted.length === 0) { var vimKeys = {
return; 27: {
key: 'Escape',
fun: removeFocus,
des: 'remove focus from the focused input',
cat: 'Control'
},
73: {
key: 'i',
fun: searchInputFocus,
des: 'focus on the search input',
cat: 'Control'
},
66: {
key: 'b',
fun: scrollPage(-window.innerHeight),
des: 'scroll one page up',
cat: 'Navigation'
},
70: {
key: 'f',
fun: scrollPage(window.innerHeight),
des: 'scroll one page down',
cat: 'Navigation'
},
85: {
key: 'u',
fun: scrollPage(-window.innerHeight / 2),
des: 'scroll half a page up',
cat: 'Navigation'
},
68: {
key: 'd',
fun: scrollPage(window.innerHeight / 2),
des: 'scroll half a page down',
cat: 'Navigation'
},
71: {
key: 'g',
fun: scrollPageTo(-document.body.scrollHeight, 'top'),
des: 'scroll to the top of the page',
cat: 'Navigation'
},
86: {
key: 'v',
fun: scrollPageTo(document.body.scrollHeight, 'bottom'),
des: 'scroll to the bottom of the page',
cat: 'Navigation'
},
75: {
key: 'k',
fun: highlightResult('up'),
des: 'select previous search result',
cat: 'Results'
},
74: {
key: 'j',
fun: highlightResult('down'),
des: 'select next search result',
cat: 'Results'
},
80: {
key: 'p',
fun: pageButtonClick(0),
des: 'go to previous page',
cat: 'Results'
},
78: {
key: 'n',
fun: pageButtonClick(1),
des: 'go to next page',
cat: 'Results'
},
79: {
key: 'o',
fun: openResult(false),
des: 'open search result',
cat: 'Results'
},
84: {
key: 't',
fun: openResult(true),
des: 'open the result in a new tab',
cat: 'Results'
},
82: {
key: 'r',
fun: reloadPage,
des: 'reload page from the server',
cat: 'Control'
},
72: {
key: 'h',
fun: toggleHelp,
des: 'toggle help window',
cat: 'Other'
}
};
$(document).keydown(function(e) {
// check for modifiers so we don't break browser's hotkeys
if (vimKeys.hasOwnProperty(e.keyCode)
&& !e.ctrlKey
&& !e.altKey
&& !e.shiftKey
&& !e.metaKey)
{
if (e.keyCode === 27) {
if (e.target.tagName.toLowerCase() === 'input') {
vimKeys[e.keyCode].fun();
}
} else {
if (e.target === document.body) {
e.preventDefault();
vimKeys[e.keyCode].fun();
}
}
}
});
function nextResult(current, direction) {
var next = current[direction]();
while (!next.is('.result') && next.length !== 0) {
next = next[direction]();
}
return next
} }
var html = '<div id="vim-hotkeys-help" class="well vim-hotkeys-help">'; function highlightResult(which) {
html += '<div class="container-fluid">'; return function() {
var current = $('.result[data-vim-selected]');
if (current.length === 0) {
current = $('.result:first');
if (current.length === 0) {
return;
}
}
html += '<div class="row">'; var next;
html += '<div class="col-sm-12">';
html += '<h3>How to navigate searx with Vim-like hotkeys</h3>';
html += '</div>'; // col-sm-12
html += '</div>'; // row
for (var i = 0; i < sorted.length; i++) { if (typeof which !== 'string') {
var cat = categories[sorted[i]]; next = which;
} else {
switch (which) {
case 'visible':
var top = $(window).scrollTop();
var bot = top + $(window).height();
var results = $('.result');
var lastCategory = i === (sorted.length - 1); for (var i = 0; i < results.length; i++) {
var first = i % 2 === 0; next = $(results[i]);
var etop = next.offset().top;
var ebot = etop + next.height();
if (first) { if ((ebot <= bot) && (etop > top)) {
html += '<div class="row dflex">'; break;
} }
html += '<div class="col-sm-' + (first && lastCategory ? 12 : 6) + ' dflex">'; }
break;
case 'down':
next = nextResult(current, 'next');
if (next.length === 0) {
next = $('.result:first');
}
break;
case 'up':
next = nextResult(current, 'prev');
if (next.length === 0) {
next = $('.result:last');
}
break;
case 'bottom':
next = $('.result:last');
break;
case 'top':
default:
next = $('.result:first');
}
}
html += '<div class="panel panel-default iflex">'; if (next) {
html += '<div class="panel-heading">' + cat[0].cat + '</div>'; current.removeAttr('data-vim-selected').removeClass('well well-sm');
html += '<div class="panel-body">'; next.attr('data-vim-selected', 'true').addClass('well well-sm');
html += '<ul class="list-unstyled">'; scrollPageToSelected();
}
for (var cj in cat) {
html += '<li><kbd>' + cat[cj].key + '</kbd> ' + cat[cj].des + '</li>';
}
html += '</ul>';
html += '</div>'; // panel-body
html += '</div>'; // panel
html += '</div>'; // col-sm-*
if (!first || lastCategory) {
html += '</div>'; // row
} }
} }
html += '</div>'; // container-fluid function reloadPage() {
html += '</div>'; // vim-hotkeys-help document.location.reload(false);
}
$('body').append(html); function removeFocus() {
if (document.activeElement) {
document.activeElement.blur();
}
}
function pageButtonClick(num) {
return function() {
var buttons = $('div#pagination button[type="submit"]');
if (buttons.length !== 2) {
console.log('page navigation with this theme is not supported');
return;
}
if (num >= 0 && num < buttons.length) {
buttons[num].click();
} else {
console.log('pageButtonClick(): invalid argument');
}
}
}
function scrollPageToSelected() {
var sel = $('.result[data-vim-selected]');
if (sel.length !== 1) {
return;
}
var wnd = $(window);
var wtop = wnd.scrollTop();
var etop = sel.offset().top;
var offset = 30;
if (wtop > etop) {
wnd.scrollTop(etop - offset);
} else {
var ebot = etop + sel.height();
var wbot = wtop + wnd.height();
if (wbot < ebot) {
wnd.scrollTop(ebot - wnd.height() + offset);
}
}
}
function scrollPage(amount) {
return function() {
window.scrollBy(0, amount);
highlightResult('visible')();
}
}
function scrollPageTo(position, nav) {
return function() {
window.scrollTo(0, position);
highlightResult(nav)();
}
}
function searchInputFocus() {
$('input#q').focus();
}
function openResult(newTab) {
return function() {
var link = $('.result[data-vim-selected] .result_header a');
if (link.length) {
var url = link.attr('href');
if (newTab) {
window.open(url);
} else {
window.location.href = url;
}
}
};
}
function toggleHelp() {
var helpPanel = $('#vim-hotkeys-help');
if (helpPanel.length) {
helpPanel.toggleClass('hidden');
return;
}
var categories = {};
for (var k in vimKeys) {
var key = vimKeys[k];
categories[key.cat] = categories[key.cat] || [];
categories[key.cat].push(key);
}
var sorted = Object.keys(categories).sort(function(a, b) {
return categories[b].length - categories[a].length;
});
if (sorted.length === 0) {
return;
}
var html = '<div id="vim-hotkeys-help" class="well vim-hotkeys-help">';
html += '<div class="container-fluid">';
html += '<div class="row">';
html += '<div class="col-sm-12">';
html += '<h3>How to navigate searx with Vim-like hotkeys</h3>';
html += '</div>'; // col-sm-12
html += '</div>'; // row
for (var i = 0; i < sorted.length; i++) {
var cat = categories[sorted[i]];
var lastCategory = i === (sorted.length - 1);
var first = i % 2 === 0;
if (first) {
html += '<div class="row dflex">';
}
html += '<div class="col-sm-' + (first && lastCategory ? 12 : 6) + ' dflex">';
html += '<div class="panel panel-default iflex">';
html += '<div class="panel-heading">' + cat[0].cat + '</div>';
html += '<div class="panel-body">';
html += '<ul class="list-unstyled">';
for (var cj in cat) {
html += '<li><kbd>' + cat[cj].key + '</kbd> ' + cat[cj].des + '</li>';
}
html += '</ul>';
html += '</div>'; // panel-body
html += '</div>'; // panel
html += '</div>'; // col-sm-*
if (!first || lastCategory) {
html += '</div>'; // row
}
}
html += '</div>'; // container-fluid
html += '</div>'; // vim-hotkeys-help
$('body').append(html);
}
});
const style = document.createElement('style');
style.textContent = `
.vim-hotkeys-help {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
z-index: 9999999;
overflow-y: auto;
max-height: 80%;
box-shadow: 0 0 1em;
} }
});
const style = document.createElement('style'); .dflex {
style.textContent = ` display: -webkit-box; /* OLD - iOS 6-, Safari 3.1-6 */
.vim-hotkeys-help { display: -moz-box; /* OLD - Firefox 19- (buggy but mostly works) */
position: fixed; display: -ms-flexbox; /* TWEENER - IE 10 */
top: 50%; display: -webkit-flex; /* NEW - Chrome */
left: 50%; display: flex; /* NEW, Spec - Opera 12.1, Firefox 20+ */
transform: translate(-50%, -50%); }
z-index: 9999999;
overflow-y: auto;
max-height: 80%;
box-shadow: 0 0 1em;
}
.dflex { .iflex {
display: -webkit-box; /* OLD - iOS 6-, Safari 3.1-6 */ -webkit-box-flex: 1; /* OLD - iOS 6-, Safari 3.1-6 */
display: -moz-box; /* OLD - Firefox 19- (buggy but mostly works) */ -moz-box-flex: 1; /* OLD - Firefox 19- */
display: -ms-flexbox; /* TWEENER - IE 10 */ -webkit-flex: 1; /* Chrome */
display: -webkit-flex; /* NEW - Chrome */ -ms-flex: 1; /* IE 10 */
display: flex; /* NEW, Spec - Opera 12.1, Firefox 20+ */ flex: 1; /* NEW, Spec - Opera 12.1, Firefox 20+ */
} }
`;
.iflex { document.head.append(style);
-webkit-box-flex: 1; /* OLD - iOS 6-, Safari 3.1-6 */
-moz-box-flex: 1; /* OLD - Firefox 19- */
-webkit-flex: 1; /* Chrome */
-ms-flex: 1; /* IE 10 */
flex: 1; /* NEW, Spec - Opera 12.1, Firefox 20+ */
}
`;
document.head.append(style);
} }