Debian Codegraph @

includes/widget/TagMultiselectWidget.php

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