Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
67.74% |
21 / 31 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
| VectorComponentLanguageDropdown | |
67.74% |
21 / 31 |
|
50.00% |
1 / 2 |
10.15 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
2 | |||
| getTemplateData | |
100.00% |
21 / 21 |
|
100.00% |
1 / 1 |
7 | |||
| 1 | <?php |
| 2 | namespace MediaWiki\Skins\Vector\Components; |
| 3 | |
| 4 | use MediaWiki\Title\Title; |
| 5 | |
| 6 | /** |
| 7 | * VectorComponentLanguageButton component |
| 8 | */ |
| 9 | class VectorComponentLanguageDropdown implements VectorComponent { |
| 10 | /** @var string */ |
| 11 | private $label; |
| 12 | /** @var string */ |
| 13 | private $ariaLabel; |
| 14 | /** @var string */ |
| 15 | private $class; |
| 16 | /** @var int */ |
| 17 | private $numLanguages; |
| 18 | /** @var array */ |
| 19 | private $menuContentsData; |
| 20 | /** @var Title|null */ |
| 21 | private $title; |
| 22 | |
| 23 | /** |
| 24 | * @param string $label human readable |
| 25 | * @param string $ariaLabel label for accessibility |
| 26 | * @param string $class of the dropdown component |
| 27 | * @param int $numLanguages |
| 28 | * @param string $itemHTML the HTML of the list e.g. `<li>...</li>` |
| 29 | * @param string $beforePortlet no known usages. Perhaps can be removed in future |
| 30 | * @param string $afterPortlet used by Extension:ULS |
| 31 | * @param Title|null $title |
| 32 | */ |
| 33 | public function __construct( |
| 34 | string $label, string $ariaLabel, string $class, int $numLanguages, |
| 35 | // @todo: replace with >MenuContents class. |
| 36 | string $itemHTML, string $beforePortlet = '', string $afterPortlet = '', $title = null |
| 37 | ) { |
| 38 | $this->label = $label; |
| 39 | $this->ariaLabel = $ariaLabel; |
| 40 | $this->class = $class; |
| 41 | $this->numLanguages = $numLanguages; |
| 42 | $this->menuContentsData = [ |
| 43 | 'html-items' => $itemHTML, |
| 44 | 'html-before-portal' => $beforePortlet, |
| 45 | 'html-after-portal' => $afterPortlet, |
| 46 | ]; |
| 47 | $this->title = $title; |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * @inheritDoc |
| 52 | */ |
| 53 | public function getTemplateData(): array { |
| 54 | $title = $this->title; |
| 55 | $isSubjectPage = ( $title && $title->exists() && !$title->isTalkPage() ) || |
| 56 | ( $title && $title->isSpecialPage() && $this->numLanguages ); |
| 57 | // If page doesn't exist or if it's in a talk namespace, we should |
| 58 | // display a less prominent "language" button, without a label, and |
| 59 | // quiet instead of progressive. For this reason some default values |
| 60 | // should be updated for this case. (T316559) |
| 61 | // |
| 62 | // However, if it is a special page and has interlanguage links, those |
| 63 | // should be displayed. (T389192) |
| 64 | $buttonClasses = 'cdx-button cdx-button--fake-button cdx-button--fake-button--enabled cdx-button--weight-quiet'; |
| 65 | if ( !$isSubjectPage ) { |
| 66 | $icon = 'language'; |
| 67 | $this->class .= ' mw-portlet-lang-icon-only'; |
| 68 | $labelClass = $buttonClasses . ' cdx-button--icon-only mw-portlet-lang-heading-empty'; |
| 69 | $checkboxClass = 'mw-interlanguage-selector-empty'; |
| 70 | } else { |
| 71 | $icon = 'language-progressive'; |
| 72 | $labelClass = $buttonClasses . ' cdx-button--action-progressive' |
| 73 | . ' mw-portlet-lang-heading-' . strval( $this->numLanguages ); |
| 74 | $checkboxClass = 'mw-interlanguage-selector'; |
| 75 | } |
| 76 | $dropdown = new VectorComponentDropdown( 'p-lang-btn', $this->label, $this->class ); |
| 77 | $dropdownData = $dropdown->getTemplateData(); |
| 78 | // override default heading class. |
| 79 | $dropdownData['label-class'] = $labelClass; |
| 80 | // ext.uls.interface attaches click handler to this selector. |
| 81 | $dropdownData['checkbox-class'] = $checkboxClass; |
| 82 | $dropdownData['icon'] = $icon; |
| 83 | $dropdownData['aria-label'] = $this->ariaLabel; |
| 84 | $dropdownData['is-language-selector-empty'] = !$isSubjectPage; |
| 85 | |
| 86 | return $dropdownData + $this->menuContentsData; |
| 87 | } |
| 88 | } |