2015-03-26 17:32:00 -04:00
|
|
|
/*
|
|
|
|
* catalog-search.js
|
2015-03-27 08:59:41 -04:00
|
|
|
* https://github.com/mgrabovsky/lainchan/blob/catalog-search/js/catalog-search.js
|
2015-03-26 17:32:00 -04:00
|
|
|
*
|
|
|
|
* Released under the MIT license
|
|
|
|
* Copyright (c) 2015 Matěj Grabovský <matej.grabovsky@gmail.com>
|
|
|
|
*
|
|
|
|
* 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 = $('<label for="catalog_search">Search: </label>'),
|
2015-03-27 10:07:15 -04:00
|
|
|
$searchBox = $('<input id="catalog_search" type="text" placeholder="Search" />');
|
2014-11-02 09:55:35 -05:00
|
|
|
function searchToggle() {
|
2015-02-20 04:51:06 -05:00
|
|
|
var button = $('#catalog_search_button');
|
2014-11-02 09:55:35 -05:00
|
|
|
|
2015-02-20 04:51:06 -05:00
|
|
|
if (!button.data('expanded')) {
|
|
|
|
button.data('expanded', '1');
|
|
|
|
button.text('Close');
|
2014-11-02 09:55:35 -05:00
|
|
|
$('.catalog_search').append(' <input id="search_field" style="border: inset 1px;">');
|
|
|
|
$('#search_field').focus();
|
|
|
|
} else {
|
2015-02-20 04:51:06 -05:00
|
|
|
button.removeData('expanded');
|
|
|
|
button.text('Search');
|
2014-11-02 09:55:35 -05:00
|
|
|
$('.catalog_search').children().last().remove();
|
|
|
|
$('div[id="Grid"]>.mix').each(function () { $(this).css('display', 'inline-block'); });
|
|
|
|
}
|
|
|
|
}
|
2015-03-26 17:32:00 -04:00
|
|
|
|
|
|
|
$controls.append($searchLabel)
|
|
|
|
.append($searchBox);
|
|
|
|
|
2015-03-27 08:38:45 -04:00
|
|
|
$searchBox.keyup(function() {
|
|
|
|
var $found = searchThreads($threads, this.value);
|
2015-03-26 17:32:00 -04:00
|
|
|
$threads.hide();
|
|
|
|
$found.show();
|
|
|
|
});
|
2015-03-27 08:55:01 -04:00
|
|
|
|
|
|
|
var m = location.hash.match(/[#&]s=([^&]+)/);
|
|
|
|
if(m) {
|
|
|
|
$searchBox.val(decodeURIComponent(m[1])).keyup();
|
|
|
|
}
|
2015-03-26 17:32:00 -04:00
|
|
|
};
|
|
|
|
|
2015-03-27 08:55:01 -04:00
|
|
|
// Filter threads by their content, given a regex. Can be extended to load data
|
2015-03-26 17:32:00 -04:00
|
|
|
// remotely and filter by multiple fields
|
2015-03-27 08:38:45 -04:00
|
|
|
var searchThreads = function($threads, query) {
|
|
|
|
var re = new RegExp(query, 'mi');
|
2015-03-26 17:32:00 -04:00
|
|
|
return $threads.filter(function() {
|
|
|
|
return re.test($('.replies', this).text());
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
// Only load in the catalog
|
|
|
|
if (active_page == 'catalog') {
|
|
|
|
onready(catalogSearch);
|
|
|
|
}
|
|
|
|
|
|
|
|
}());
|