Introduce TagMultiselectWidget.php
[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
16 protected $selectedArray = [];
17 protected $inputName = null;
18 protected $inputPlaceholder = null;
19 protected $tagLimit = null;
20
21 /**
22 * @param array $config Configuration options
23 * - array $config['default'] Array of items to use as preset data
24 * - array $config['name'] Name attribute (used in forms)
25 * - array $config['placeholder'] Placeholder message for input
26 * - array $config['input'] Config options for the input widget
27 * - number $config['tagLimit'] Maximum number of selected items
28 */
29 public function __construct( array $config = [] ) {
30 parent::__construct( $config );
31
32 // Properties
33 if ( isset( $config['default'] ) ) {
34 $this->selectedArray = $config['default'];
35 }
36 if ( isset( $config['name'] ) ) {
37 $this->inputName = $config['name'];
38 }
39 if ( isset( $config['placeholder'] ) ) {
40 $this->inputPlaceholder = $config['placeholder'];
41 }
42 if ( isset( $config['input'] ) ) {
43 $this->input = $config['input'];
44 } else {
45 $this->input = [];
46 }
47 if ( isset( $config['tagLimit'] ) ) {
48 $this->tagLimit = $config['tagLimit'];
49 }
50
51 $textarea = new MultilineTextInputWidget( array_merge( [
52 'name' => $this->inputName,
53 'value' => implode( "\n", $this->selectedArray ),
54 'rows' => 10,
55 'classes' => [
56 'mw-widgets-tagMultiselectWidget-multilineTextInputWidget'
57 ],
58 ], $this->input ) );
59
60 $pending = new PendingTextInputWidget();
61
62 $this->appendContent( $textarea, $pending );
63 $this->addClasses( [ 'mw-widgets-tagMultiselectWidget' ] );
64 }
65
66 public function getConfig( &$config ) {
67 if ( $this->selectedArray !== null ) {
68 $config['selected'] = $this->selectedArray;
69 }
70 if ( $this->inputName !== null ) {
71 $config['name'] = $this->inputName;
72 }
73 if ( $this->inputPlaceholder !== null ) {
74 $config['placeholder'] = $this->inputPlaceholder;
75 }
76 if ( $this->input !== null ) {
77 $config['input'] = $this->input;
78 }
79 if ( $this->tagLimit !== null ) {
80 $config['tagLimit'] = $this->tagLimit;
81 }
82
83 $config['$overlay'] = true;
84 return parent::getConfig( $config );
85 }
86
87 }