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