2022-10-27 16:11:32 +00:00
|
|
|
<?php
|
|
|
|
namespace MediaWiki\Skins\Vector\Components;
|
|
|
|
|
|
|
|
use Config;
|
|
|
|
use Linker;
|
2023-08-19 04:23:07 +00:00
|
|
|
use MediaWiki\Title\Title;
|
2022-10-27 16:11:32 +00:00
|
|
|
use MessageLocalizer;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* VectorSearchBox component
|
|
|
|
*/
|
|
|
|
class VectorComponentSearchBox implements VectorComponent {
|
|
|
|
/** @var MessageLocalizer */
|
|
|
|
private $localizer;
|
|
|
|
/** @var array */
|
|
|
|
private $searchBoxData;
|
|
|
|
/** @var bool */
|
|
|
|
private $isCollapsible;
|
|
|
|
/** @var bool */
|
|
|
|
private $isPrimary;
|
|
|
|
/** @var string */
|
|
|
|
private $formId;
|
|
|
|
/** @var bool */
|
|
|
|
private $autoExpandWidth;
|
|
|
|
/** @var string */
|
|
|
|
private $location;
|
|
|
|
/** @var Config */
|
|
|
|
private $config;
|
2023-05-24 19:39:52 +00:00
|
|
|
private const SEARCH_COLLAPSIBLE_CLASS = 'vector-search-box-collapses';
|
2022-10-27 16:11:32 +00:00
|
|
|
private const SEARCH_SHOW_THUMBNAIL_CLASS = 'vector-search-box-show-thumbnail';
|
|
|
|
private const SEARCH_AUTO_EXPAND_WIDTH_CLASS = 'vector-search-box-auto-expand-width';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Config
|
|
|
|
*/
|
|
|
|
private function getConfig(): Config {
|
|
|
|
return $this->config;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns `true` if Vue search is enabled to show thumbnails and `false` otherwise.
|
|
|
|
* Note this is only relevant for Vue search experience (not legacy search).
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private function doesSearchHaveThumbnails(): bool {
|
|
|
|
return $this->getConfig()->get( 'VectorWvuiSearchOptions' )['showThumbnail'];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the value of the "input-location" parameter for the SearchBox Mustache template.
|
|
|
|
*
|
|
|
|
* @return string Either `Constants::SEARCH_BOX_INPUT_LOCATION_DEFAULT` or
|
|
|
|
* `Constants::SEARCH_BOX_INPUT_LOCATION_MOVED`
|
|
|
|
*/
|
|
|
|
private function getSearchBoxInputLocation(): string {
|
|
|
|
return $this->location;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $searchBoxData
|
|
|
|
* @param bool $isCollapsible
|
|
|
|
* @param bool $isPrimary
|
|
|
|
* @param string $formId
|
|
|
|
* @param bool $autoExpandWidth
|
2023-05-24 19:39:52 +00:00
|
|
|
* @param Config $config
|
|
|
|
* @param string $location
|
|
|
|
* @param MessageLocalizer $localizer
|
2022-10-27 16:11:32 +00:00
|
|
|
*/
|
2023-05-24 19:39:52 +00:00
|
|
|
public function __construct(
|
2022-10-27 16:11:32 +00:00
|
|
|
array $searchBoxData,
|
|
|
|
bool $isCollapsible,
|
|
|
|
bool $isPrimary,
|
|
|
|
string $formId,
|
2023-05-24 19:39:52 +00:00
|
|
|
bool $autoExpandWidth,
|
|
|
|
Config $config,
|
|
|
|
string $location,
|
|
|
|
MessageLocalizer $localizer
|
2022-10-27 16:11:32 +00:00
|
|
|
) {
|
2023-05-24 19:39:52 +00:00
|
|
|
$this->searchBoxData = $searchBoxData;
|
|
|
|
$this->isCollapsible = $isCollapsible;
|
|
|
|
$this->isPrimary = $isPrimary;
|
|
|
|
$this->formId = $formId;
|
|
|
|
$this->autoExpandWidth = $autoExpandWidth;
|
|
|
|
$this->location = $location;
|
|
|
|
$this->config = $config;
|
|
|
|
$this->localizer = $localizer;
|
|
|
|
}
|
2022-10-27 16:11:32 +00:00
|
|
|
|
2023-05-24 19:39:52 +00:00
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function getTemplateData(): array {
|
|
|
|
$searchBoxData = $this->searchBoxData;
|
|
|
|
$isCollapsible = $this->isCollapsible;
|
|
|
|
$isThumbnail = $this->doesSearchHaveThumbnails();
|
|
|
|
$isAutoExpand = $isThumbnail && $this->autoExpandWidth;
|
|
|
|
$isPrimary = $this->isPrimary;
|
|
|
|
$formId = $this->formId;
|
2022-10-27 16:11:32 +00:00
|
|
|
|
2023-05-24 19:39:52 +00:00
|
|
|
$searchClass = 'vector-search-box-vue ';
|
|
|
|
$searchClass .= $isCollapsible ? ' ' . self::SEARCH_COLLAPSIBLE_CLASS : '';
|
|
|
|
$searchClass .= $isThumbnail ? ' ' . self::SEARCH_SHOW_THUMBNAIL_CLASS : '';
|
|
|
|
$searchClass .= $isAutoExpand ? ' ' . self::SEARCH_AUTO_EXPAND_WIDTH_CLASS : '';
|
2022-10-27 16:11:32 +00:00
|
|
|
|
|
|
|
// Annotate search box with a component class.
|
|
|
|
$searchBoxData['class'] = trim( $searchClass );
|
|
|
|
$searchBoxData['is-collapsible'] = $isCollapsible;
|
2023-05-24 19:39:52 +00:00
|
|
|
$searchBoxData['is-thumbnail'] = $isThumbnail;
|
|
|
|
$searchBoxData['is-auto-expand'] = $isAutoExpand;
|
2022-10-27 16:11:32 +00:00
|
|
|
$searchBoxData['is-primary'] = $isPrimary;
|
|
|
|
$searchBoxData['form-id'] = $formId;
|
|
|
|
$searchBoxData['input-location'] = $this->getSearchBoxInputLocation();
|
|
|
|
|
|
|
|
// At lower resolutions the search input is hidden search and only the submit button is shown.
|
|
|
|
// It should behave like a form submit link (e.g. submit the form with no input value).
|
|
|
|
// We'll wire this up in a later task T284242.
|
|
|
|
$collapseIconAttrs = Linker::tooltipAndAccesskeyAttribs( 'search' );
|
2023-04-25 16:53:19 +00:00
|
|
|
$searchButton = new VectorComponentButton(
|
|
|
|
$this->localizer->msg( 'search' ),
|
|
|
|
'search',
|
|
|
|
'',
|
|
|
|
'search-toggle',
|
|
|
|
$collapseIconAttrs,
|
|
|
|
'quiet',
|
|
|
|
'default',
|
|
|
|
true,
|
|
|
|
Title::newFromText( $searchBoxData['page-title'] )->getLocalURL()
|
|
|
|
);
|
|
|
|
$searchBoxData['data-collapsed-search-button'] = $searchButton->getTemplateData();
|
2022-10-27 16:11:32 +00:00
|
|
|
|
|
|
|
return $searchBoxData;
|
|
|
|
}
|
|
|
|
}
|