The version of vichan running on lainchan.org
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

66 lines
1.8KB

  1. /*
  2. * catalog-search.js
  3. * https://github.com/mgrabovsky/lainchan/blob/catalog-search/js/catalog-search.js
  4. *
  5. * Released under the MIT license
  6. * Copyright (c) 2015 Matěj Grabovský <matej.grabovsky@gmail.com>
  7. *
  8. * Usage:
  9. * $config['additional_javascript'][] = 'js/jquery.min.js';
  10. * $config['additional_javascript'][] = 'js/catalog-search.js';
  11. */
  12. (function() {
  13. var catalogSearch = function() {
  14. var $controls = $('.controls'),
  15. $threads = $('.threads .thread'),
  16. $searchLabel = $('<label for="catalog_search">Search: </label>'),
  17. $searchBox = $('<input id="catalog_search" type="text" placeholder="Search" />');
  18. function searchToggle() {
  19. var button = $('#catalog_search_button');
  20. if (!button.data('expanded')) {
  21. button.data('expanded', '1');
  22. button.text('Close');
  23. $('.catalog_search').append(' <input id="search_field" style="border: inset 1px;">');
  24. $('#search_field').focus();
  25. } else {
  26. button.removeData('expanded');
  27. button.text('Search');
  28. $('.catalog_search').children().last().remove();
  29. $('div[id="Grid"]>.mix').each(function () { $(this).css('display', 'inline-block'); });
  30. }
  31. }
  32. $controls.append($searchLabel)
  33. .append($searchBox);
  34. $searchBox.keyup(function() {
  35. var $found = searchThreads($threads, this.value);
  36. $threads.hide();
  37. $found.show();
  38. });
  39. var m = location.hash.match(/[#&]s=([^&]+)/);
  40. if(m) {
  41. $searchBox.val(decodeURIComponent(m[1])).keyup();
  42. }
  43. };
  44. // Filter threads by their content, given a regex. Can be extended to load data
  45. // remotely and filter by multiple fields
  46. var searchThreads = function($threads, query) {
  47. var re = new RegExp(query, 'mi');
  48. return $threads.filter(function() {
  49. return re.test($('.replies', this).text());
  50. });
  51. };
  52. // Only load in the catalog
  53. if (active_page == 'catalog') {
  54. onready(catalogSearch);
  55. }
  56. }());