feat: use bridge description and short name in search (#2952)

* refactor: search.js

* feat: use bridge description and short name in search

* fix bug in previous merge commit

Also reformat string from tabs to spaces
This commit is contained in:
Dag 2022-08-06 23:12:30 +02:00 committed by GitHub
parent eef45d4e8d
commit 502799a74c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 74 additions and 74 deletions

View File

@ -264,7 +264,7 @@ abstract class BridgeAbstract implements BridgeInterface
public function loadConfiguration() public function loadConfiguration()
{ {
foreach (static::CONFIGURATION as $optionName => $optionValue) { foreach (static::CONFIGURATION as $optionName => $optionValue) {
$section = (new ReflectionClass($this))->getShortName(); $section = $this->getShortName();
$configurationOption = Configuration::getConfig($section, $optionName); $configurationOption = Configuration::getConfig($section, $optionName);
if ($configurationOption !== null) { if ($configurationOption !== null) {
@ -384,7 +384,7 @@ abstract class BridgeAbstract implements BridgeInterface
$cache = $cacheFactory->create(); $cache = $cacheFactory->create();
// Create class name without the namespace part // Create class name without the namespace part
$scope = (new \ReflectionClass($this))->getShortName(); $scope = $this->getShortName();
$cache->setScope($scope); $cache->setScope($scope);
$cache->setKey($key); $cache->setKey($key);
if ($cache->getTime() < time() - $duration) { if ($cache->getTime() < time() - $duration) {
@ -404,9 +404,14 @@ abstract class BridgeAbstract implements BridgeInterface
$cacheFactory = new CacheFactory(); $cacheFactory = new CacheFactory();
$cache = $cacheFactory->create(); $cache = $cacheFactory->create();
$scope = (new \ReflectionClass($this))->getShortName(); $scope = $this->getShortName();
$cache->setScope($scope); $cache->setScope($scope);
$cache->setKey($key); $cache->setKey($key);
$cache->saveData($value); $cache->saveData($value);
} }
public function getShortName(): string
{
return (new \ReflectionClass($this))->getShortName();
}
} }

View File

@ -58,12 +58,19 @@ final class BridgeCard
]; ];
} }
$shortName = $bridge->getShortName();
$card = <<<CARD $card = <<<CARD
<section id="bridge-{$bridgeClassName}" data-ref="{$name}"> <section
<h2><a href="{$uri}">{$name}</a></h2> class="bridge-card"
<p class="description">{$description}</p> id="bridge-{$bridgeClassName}"
<input type="checkbox" class="showmore-box" id="showmore-{$bridgeClassName}" /> data-ref="{$name}"
<label class="showmore" for="showmore-{$bridgeClassName}">Show more</label> data-short-name="$shortName"
>
<h2><a href="{$uri}">{$name}</a></h2>
<p class="description">{$description}</p>
<input type="checkbox" class="showmore-box" id="showmore-{$bridgeClassName}" />
<label class="showmore" for="showmore-{$bridgeClassName}">Show more</label>
CARD; CARD;
// If we don't have any parameter for the bridge, we print a generic form to load it. // If we don't have any parameter for the bridge, we print a generic form to load it.
@ -116,9 +123,9 @@ CARD;
private static function getFormHeader($bridgeClassName, $isHttps = false, $parameterName = '') private static function getFormHeader($bridgeClassName, $isHttps = false, $parameterName = '')
{ {
$form = <<<EOD $form = <<<EOD
<form method="GET" action="?"> <form method="GET" action="?">
<input type="hidden" name="action" value="display" /> <input type="hidden" name="action" value="display" />
<input type="hidden" name="bridge" value="{$bridgeClassName}" /> <input type="hidden" name="bridge" value="{$bridgeClassName}" />
EOD; EOD;
if (!empty($parameterName)) { if (!empty($parameterName)) {

View File

@ -143,4 +143,6 @@ interface BridgeInterface
* @return array|null List of bridge parameters or null if detection failed. * @return array|null List of bridge parameters or null if detection failed.
*/ */
public function detectParameters($url); public function detectParameters($url);
public function getShortName(): string;
} }

View File

@ -36,7 +36,7 @@ final class BridgeList
return '<!DOCTYPE html><html lang="en">' return '<!DOCTYPE html><html lang="en">'
. BridgeList::getHead() . BridgeList::getHead()
. '<body onload="search()">' . '<body onload="rssbridge_list_search()">'
. BridgeList::getHeader() . BridgeList::getHeader()
. BridgeList::getSearchbar() . BridgeList::getSearchbar()
. BridgeList::getBridges($showInactive, $totalBridges, $totalActiveBridges) . BridgeList::getBridges($showInactive, $totalBridges, $totalActiveBridges)
@ -152,10 +152,16 @@ EOD;
return <<<EOD return <<<EOD
<section class="searchbar"> <section class="searchbar">
<h3>Search</h3> <h3>Search</h3>
<input type="text" name="searchfield" <input
id="searchfield" placeholder="Insert URL or bridge name" type="text"
onchange="search()" onkeyup="search()" value="{$query}"> name="searchfield"
id="searchfield"
placeholder="Insert URL or bridge name"
onchange="rssbridge_list_search()"
onkeyup="rssbridge_list_search()"
value="{$query}"
>
</section> </section>
EOD; EOD;
} }

View File

@ -1,60 +1,40 @@
function search() { function rssbridge_list_search() {
function remove_www_from_url(url) {
var searchTerm = document.getElementById('searchfield').value; if (url.hostname.indexOf('www.') === 0) {
var searchableElements = document.getElementsByTagName('section'); url.hostname = url.hostname.substr(4);
}
var regexMatch = new RegExp(searchTerm, 'i'); }
// Attempt to create anchor from search term (will default to 'localhost' on failure)
var searchTermUri = document.createElement('a');
searchTermUri.href = searchTerm;
if(searchTermUri.hostname == 'localhost') {
searchTermUri = null;
} else {
// Ignore "www."
if(searchTermUri.hostname.indexOf('www.') === 0) {
searchTermUri.hostname = searchTermUri.hostname.substr(4);
}
}
for(var i = 0; i < searchableElements.length; i++) {
var textValue = searchableElements[i].getAttribute('data-ref');
var anchors = searchableElements[i].getElementsByTagName('a');
if(anchors != null && anchors.length > 0) {
var uriValue = anchors[0]; // First anchor is bridge URI
// Ignore "www."
if(uriValue.hostname.indexOf('www.') === 0) {
uriValue.hostname = uriValue.hostname.substr(4);
}
}
if(textValue != null && uriValue != null) {
if(textValue.match(regexMatch) != null ||
uriValue.hostname.match(regexMatch) ||
searchTermUri != null &&
uriValue.hostname != 'localhost' && (
uriValue.href.match(regexMatch) != null ||
uriValue.hostname == searchTermUri.hostname)) {
searchableElements[i].style.display = 'block';
} else {
searchableElements[i].style.display = 'none';
}
}
}
var search = document.getElementById('searchfield').value;
var searchAsUrl = document.createElement('a');
searchAsUrl.href = search;
remove_www_from_url(searchAsUrl);
var bridgeCards = document.querySelectorAll('section.bridge-card');
for (var i = 0; i < bridgeCards.length; i++) {
var bridgeName = bridgeCards[i].getAttribute('data-ref');
var bridgeShortName = bridgeCards[i].getAttribute('data-short-name');
var bridgeDescription = bridgeCards[i].querySelector('.description');
var bridgeUrl = bridgeCards[i].getElementsByTagName('a')[0];
remove_www_from_url(bridgeUrl);
bridgeCards[i].style.display = 'none';
if (!bridgeName || !bridgeUrl) {
continue;
}
var searchRegex = new RegExp(search, 'i');
if (bridgeName.match(searchRegex)) {
bridgeCards[i].style.display = 'block';
}
if (bridgeShortName.match(searchRegex)) {
bridgeCards[i].style.display = 'block';
}
if (bridgeDescription.textContent.match(searchRegex)) {
bridgeCards[i].style.display = 'block';
}
if (bridgeUrl.toString().match(searchRegex)) {
bridgeCards[i].style.display = 'block';
}
if (bridgeUrl.hostname === searchAsUrl.hostname) {
bridgeCards[i].style.display = 'block';
}
}
} }