1
0
mirror of https://github.com/vichan-devel/vichan.git synced 2024-11-28 09:20:58 +01:00
vichan/js/catalog-search.js

83 lines
2.6 KiB
JavaScript
Raw Normal View History

2014-11-02 15:55:35 +01:00
/*
* catalog-search.js
* - Search and filters threads when on catalog view
* - Optional shortcuts 's' and 'esc' to open and close the search.
2024-08-05 19:08:28 +02:00
*
2014-11-02 15:55:35 +01:00
* Usage:
* $config['additional_javascript'][] = 'js/jquery.min.js';
* $config['additional_javascript'][] = 'js/comment-toolbar.js';
*/
if (active_page == 'catalog') {
2024-08-05 19:09:05 +02:00
onReady(function() {
2014-11-02 15:55:35 +01:00
'use strict';
2024-08-05 19:08:28 +02:00
// 'true' = enable shortcuts
let useKeybinds = true;
2014-11-02 15:55:35 +01:00
2024-08-05 19:08:28 +02:00
// trigger the search 400ms after last keystroke
let delay = 400;
let timeoutHandle;
2014-11-02 15:55:35 +01:00
2024-08-05 19:08:28 +02:00
// search and hide none matching threads
2014-11-02 15:55:35 +01:00
function filter(search_term) {
$('.replies').each(function () {
2024-08-05 19:08:28 +02:00
let subject = $(this).children('.intro').text().toLowerCase();
let comment = $(this).clone().children().remove(':lt(2)').end().text().trim().toLowerCase();
2014-11-02 15:55:35 +01:00
search_term = search_term.toLowerCase();
if (subject.indexOf(search_term) == -1 && comment.indexOf(search_term) == -1) {
$(this).parents('div[id="Grid"]>.mix').css('display', 'none');
} else {
$(this).parents('div[id="Grid"]>.mix').css('display', 'inline-block');
}
});
}
function searchToggle() {
2024-08-05 19:08:28 +02:00
let button = $('#catalog_search_button');
2014-11-02 15:55:35 +01:00
if (!button.data('expanded')) {
button.data('expanded', '1');
button.text('Close');
2014-11-02 15:55:35 +01:00
$('.catalog_search').append(' <input id="search_field" style="border: inset 1px;">');
$('#search_field').focus();
} else {
button.removeData('expanded');
button.text('Search');
2014-11-02 15:55:35 +01:00
$('.catalog_search').children().last().remove();
$('div[id="Grid"]>.mix').each(function () { $(this).css('display', 'inline-block'); });
}
}
$('.threads').before('<span class="catalog_search">[<a id="catalog_search_button" style="text-decoration:none; cursor:pointer;"></a>]</span>');
$('#catalog_search_button').text('Search');
$('#catalog_search_button').on('click', searchToggle);
$('.catalog_search').on('keyup', 'input#search_field', function (e) {
window.clearTimeout(timeoutHandle);
timeoutHandle = window.setTimeout(filter, 400, e.target.value);
});
if (useKeybinds) {
2024-08-05 19:08:28 +02:00
// 's'
2014-11-02 15:55:35 +01:00
$('body').on('keydown', function (e) {
if (e.which === 83 && e.target.tagName === 'BODY' && !(e.ctrlKey || e.altKey || e.shiftKey)) {
e.preventDefault();
2024-08-05 19:08:28 +02:00
if ($('#search_field').length !== 0) {
2014-11-02 15:55:35 +01:00
$('#search_field').focus();
} else {
searchToggle();
}
}
});
2024-08-05 19:08:28 +02:00
// 'esc'
2014-11-02 15:55:35 +01:00
$('.catalog_search').on('keydown', 'input#search_field', function (e) {
if (e.which === 27 && !(e.ctrlKey || e.altKey || e.shiftKey)) {
window.clearTimeout(timeoutHandle);
searchToggle();
}
});
}
});
}