From 32821745c632bff01feb58cd234295dceaeea2a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mat=C4=9Bj=20Grabovsk=C3=BD?= Date: Thu, 26 Mar 2015 22:32:00 +0100 Subject: [PATCH] Add a simple search functionality to the catalog Requires commit 20ce699 which adds a wrapper for the controls on the catalog page. --- js/catalog-search.js | 45 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 js/catalog-search.js diff --git a/js/catalog-search.js b/js/catalog-search.js new file mode 100644 index 00000000..07e21f87 --- /dev/null +++ b/js/catalog-search.js @@ -0,0 +1,45 @@ +/* + * catalog-search.js + * https://github.com/mgrabovsky/lainchan/lainchan/blob/catalog-search/js/catalog-search.js + * + * Released under the MIT license + * Copyright (c) 2015 Matěj Grabovský + * + * Usage: + * $config['additional_javascript'][] = 'js/jquery.min.js'; + * $config['additional_javascript'][] = 'js/catalog-search.js'; + */ + +(function() { + +var catalogSearch = function() { + var $controls = $('.controls'), + $threads = $('.threads .thread'), + $searchLabel = $(''), + $searchBox = $(''); + + $controls.append($searchLabel) + .append($searchBox); + + $searchBox.change(function() { + var query = new RegExp(this.value, 'm'), + $found = searchThreads($threads, query); + $threads.hide(); + $found.show(); + }); +}; + +// Filter threads by their content, given a regex. Can be extended later to load data +// remotely and filter by multiple fields +var searchThreads = function($threads, re) { + return $threads.filter(function() { + return re.test($('.replies', this).text()); + }); +}; + +// Only load in the catalog +if (active_page == 'catalog') { + onready(catalogSearch); +} + +}());