The version of vichan running on lainchan.org
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

83 lignes
2.6KB

  1. /*
  2. * catalog-search.js
  3. * - Search and filters threads when on catalog view
  4. * - Optional shortcuts 's' and 'esc' to open and close the search.
  5. *
  6. * Usage:
  7. * $config['additional_javascript'][] = 'js/jquery.min.js';
  8. * $config['additional_javascript'][] = 'js/comment-toolbar.js';
  9. */
  10. if (active_page == 'catalog') {
  11. onready(function () {
  12. 'use strict';
  13. // 'true' = enable shortcuts
  14. var useKeybinds = true;
  15. // trigger the search 400ms after last keystroke
  16. var delay = 400;
  17. var timeoutHandle;
  18. //search and hide none matching threads
  19. function filter(search_term) {
  20. $('.replies').each(function () {
  21. var subject = $(this).children('.intro').text().toLowerCase();
  22. var comment = $(this).clone().children().remove(':lt(2)').end().text().trim().toLowerCase();
  23. search_term = search_term.toLowerCase();
  24. if (subject.indexOf(search_term) == -1 && comment.indexOf(search_term) == -1) {
  25. $(this).parents('div[id="Grid"]>.mix').css('display', 'none');
  26. } else {
  27. $(this).parents('div[id="Grid"]>.mix').css('display', 'inline-block');
  28. }
  29. });
  30. }
  31. function searchToggle() {
  32. var button = $('#catalog_search_button');
  33. if (!button.data('expanded')) {
  34. button.data('expanded', '1');
  35. button.text('Close');
  36. $('.catalog_search').append(' <input id="search_field" style="border: inset 1px;">');
  37. $('#search_field').focus();
  38. } else {
  39. button.removeData('expanded');
  40. button.text('Search');
  41. $('.catalog_search').children().last().remove();
  42. $('div[id="Grid"]>.mix').each(function () { $(this).css('display', 'inline-block'); });
  43. }
  44. }
  45. $('.threads').before('<span class="catalog_search">[<a id="catalog_search_button" style="text-decoration:none; cursor:pointer;"></a>]</span>');
  46. $('#catalog_search_button').text('Search');
  47. $('#catalog_search_button').on('click', searchToggle);
  48. $('.catalog_search').on('keyup', 'input#search_field', function (e) {
  49. window.clearTimeout(timeoutHandle);
  50. timeoutHandle = window.setTimeout(filter, 400, e.target.value);
  51. });
  52. if (useKeybinds) {
  53. // 's'
  54. $('body').on('keydown', function (e) {
  55. if (e.which === 83 && e.target.tagName === 'BODY' && !(e.ctrlKey || e.altKey || e.shiftKey)) {
  56. e.preventDefault();
  57. if ($('#search_field').length !== 0) {
  58. $('#search_field').focus();
  59. } else {
  60. searchToggle();
  61. }
  62. }
  63. });
  64. // 'esc'
  65. $('.catalog_search').on('keydown', 'input#search_field', function (e) {
  66. if (e.which === 27 && !(e.ctrlKey || e.altKey || e.shiftKey)) {
  67. window.clearTimeout(timeoutHandle);
  68. searchToggle();
  69. }
  70. });
  71. }
  72. });
  73. }