Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 43
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
VectorComponentSearchBox
0.00% covered (danger)
0.00%
0 / 43
0.00% covered (danger)
0.00%
0 / 5
90
0.00% covered (danger)
0.00%
0 / 1
 getConfig
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 doesSearchHaveThumbnails
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 getSearchBoxInputLocation
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 __construct
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
2
 getTemplateData
0.00% covered (danger)
0.00%
0 / 31
0.00% covered (danger)
0.00%
0 / 1
30
1<?php
2namespace MediaWiki\Skins\Vector\Components;
3
4use MediaWiki\Config\Config;
5use MediaWiki\Linker\Linker;
6use MediaWiki\Title\Title;
7use MessageLocalizer;
8
9/**
10 * VectorSearchBox component
11 */
12class VectorComponentSearchBox implements VectorComponent {
13    /** @var MessageLocalizer */
14    private $localizer;
15    /** @var array */
16    private $searchBoxData;
17    /** @var bool */
18    private $isCollapsible;
19    /** @var bool */
20    private $isPrimary;
21    /** @var string */
22    private $formId;
23    /** @var bool */
24    private $autoExpandWidth;
25    /** @var string */
26    private $location;
27    /** @var Config */
28    private $config;
29    private const SEARCH_COLLAPSIBLE_CLASS = 'vector-search-box-collapses';
30    private const SEARCH_SHOW_THUMBNAIL_CLASS = 'vector-search-box-show-thumbnail';
31    private const SEARCH_AUTO_EXPAND_WIDTH_CLASS = 'vector-search-box-auto-expand-width';
32
33    private function getConfig(): Config {
34        return $this->config;
35    }
36
37    /**
38     * Returns `true` if Vue search is enabled to show thumbnails and `false` otherwise.
39     * Note this is only relevant for Vue search experience (not legacy search).
40     */
41    private function doesSearchHaveThumbnails(): bool {
42        $searchOptions = $this->getConfig()->get( 'VectorTypeahead' )['options'];
43        return $searchOptions['showThumbnail'];
44    }
45
46    /**
47     * Gets the value of the "input-location" parameter for the SearchBox Mustache template.
48     *
49     * @return string Either `Constants::SEARCH_BOX_INPUT_LOCATION_DEFAULT` or
50     *  `Constants::SEARCH_BOX_INPUT_LOCATION_MOVED`
51     */
52    private function getSearchBoxInputLocation(): string {
53        return $this->location;
54    }
55
56    /**
57     * @param array $searchBoxData
58     * @param bool $isCollapsible
59     * @param bool $isPrimary
60     * @param string $formId
61     * @param bool $autoExpandWidth
62     * @param Config $config
63     * @param string $location
64     * @param MessageLocalizer $localizer
65     */
66    public function __construct(
67        array $searchBoxData,
68        bool $isCollapsible,
69        bool $isPrimary,
70        string $formId,
71        bool $autoExpandWidth,
72        Config $config,
73        string $location,
74        MessageLocalizer $localizer
75    ) {
76        $this->searchBoxData = $searchBoxData;
77        $this->isCollapsible = $isCollapsible;
78        $this->isPrimary = $isPrimary;
79        $this->formId = $formId;
80        $this->autoExpandWidth = $autoExpandWidth;
81        $this->location = $location;
82        $this->config = $config;
83        $this->localizer = $localizer;
84    }
85
86    /**
87     * @inheritDoc
88     */
89    public function getTemplateData(): array {
90        $searchBoxData = $this->searchBoxData;
91        $isCollapsible = $this->isCollapsible;
92        $isThumbnail = $this->doesSearchHaveThumbnails();
93        $isAutoExpand = $isThumbnail && $this->autoExpandWidth;
94        $isPrimary = $this->isPrimary;
95        $formId = $this->formId;
96
97        $searchClass = 'vector-search-box-vue ';
98        $searchClass .= $isCollapsible ? ' ' . self::SEARCH_COLLAPSIBLE_CLASS : '';
99        $searchClass .= $isThumbnail ? ' ' . self::SEARCH_SHOW_THUMBNAIL_CLASS : '';
100        $searchClass .= $isAutoExpand ? ' ' . self::SEARCH_AUTO_EXPAND_WIDTH_CLASS : '';
101
102        // Annotate search box with a component class.
103        $searchBoxData['class'] = trim( $searchClass );
104        $searchBoxData['is-collapsible'] = $isCollapsible;
105        $searchBoxData['is-thumbnail'] = $isThumbnail;
106        $searchBoxData['is-auto-expand'] = $isAutoExpand;
107        $searchBoxData['is-primary'] = $isPrimary;
108        $searchBoxData['form-id'] = $formId;
109        $searchBoxData['input-location'] = $this->getSearchBoxInputLocation();
110
111        // At lower resolutions the search input is hidden search and only the submit button is shown.
112        // It should behave like a form submit link (e.g. submit the form with no input value).
113        // We'll wire this up in a later task T284242.
114        $collapseIconAttrs = Linker::tooltipAndAccesskeyAttribs( 'search' );
115        $searchButton = new VectorComponentButton(
116            $this->localizer->msg( 'search' ),
117            'search',
118            null,
119            'search-toggle',
120            $collapseIconAttrs,
121            'quiet',
122            'default',
123            true,
124            Title::newFromText( $searchBoxData['page-title'] )->getLocalURL()
125        );
126        $searchBoxData['data-collapsed-search-button'] = $searchButton->getTemplateData();
127
128        return $searchBoxData;
129    }
130}