Merge "output: Narrow Title type hint to LinkTarget"
[lhc/web/wiklou.git] / includes / widget / TagMultiselectWidget.php
1 <?php
2
3 namespace MediaWiki\Widget;
4
5 use OOUI\MultilineTextInputWidget;
6
7 /**
8 * Abstract base class for widgets to select multiple users, titles,
9 * namespaces, etc.
10 *
11 * @copyright 2017 MediaWiki Widgets Team and others; see AUTHORS.txt
12 * @license MIT
13 */
14 abstract class TagMultiselectWidget extends \OOUI\Widget {
15 /** @var array */
16 protected $selectedArray;
17 /** @var string|null */
18 protected $inputName;
19 /** @var string|null */
20 protected $inputPlaceholder;
21 /** @var array */
22 protected $input;
23 /** @var int|null */
24 protected $tagLimit;
25
26 /**
27 * @param array $config Configuration options
28 * - array $config['default'] Array of items to use as preset data
29 * - string $config['name'] Name attribute (used in forms)
30 * - string $config['placeholder'] Placeholder message for input
31 * - array $config['input'] Config options for the input widget
32 * - int $config['tagLimit'] Maximum number of selected items
33 */
34 public function __construct( array $config = [] ) {
35 parent::__construct( $config );
36
37 // Properties
38 $this->selectedArray = $config['default'] ?? [];
39 $this->inputName = $config['name'] ?? null;
40 $this->inputPlaceholder = $config['placeholder'] ?? null;
41 $this->input = $config['input'] ?? [];
42 $this->tagLimit = $config['tagLimit'] ?? null;
43
44 $textarea = new MultilineTextInputWidget( array_merge( [
45 'name' => $this->inputName,
46 'value' => implode( "\n", $this->selectedArray ),
47 'rows' => 10,
48 'classes' => [
49 'mw-widgets-tagMultiselectWidget-multilineTextInputWidget'
50 ],
51 ], $this->input ) );
52
53 $pending = new PendingTextInputWidget();
54
55 $this->appendContent( $textarea, $pending );
56 $this->addClasses( [ 'mw-widgets-tagMultiselectWidget' ] );
57 }
58
59 public function getConfig( &$config ) {
60 if ( $this->selectedArray !== null ) {
61 $config['selected'] = $this->selectedArray;
62 }
63 if ( $this->inputName !== null ) {
64 $config['name'] = $this->inputName;
65 }
66 if ( $this->inputPlaceholder !== null ) {
67 $config['placeholder'] = $this->inputPlaceholder;
68 }
69 if ( $this->input !== null ) {
70 $config['input'] = $this->input;
71 }
72 if ( $this->tagLimit !== null ) {
73 $config['tagLimit'] = $this->tagLimit;
74 }
75
76 $config['$overlay'] = true;
77 return parent::getConfig( $config );
78 }
79
80 }