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