Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 40
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
TagMultiselectWidget
0.00% covered (danger)
0.00%
0 / 40
0.00% covered (danger)
0.00%
0 / 4
156
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 1
2
 getConfig
0.00% covered (danger)
0.00%
0 / 18
0.00% covered (danger)
0.00%
0 / 1
90
 getNoJavaScriptFallback
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 getJavaScriptClassName
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace MediaWiki\Widget;
4
5use OOUI\MultilineTextInputWidget;
6use OOUI\Tag;
7use OOUI\Widget;
8
9/**
10 * Base class for widgets to select multiple users, titles,
11 * namespaces, etc.
12 *
13 * @copyright 2017 MediaWiki Widgets Team and others; see AUTHORS.txt
14 * @license MIT
15 */
16class TagMultiselectWidget extends Widget {
17    /** @var array */
18    protected $selectedArray;
19    /** @var string|null */
20    protected $inputName;
21    /** @var string|null */
22    protected $inputPlaceholder;
23    /** @var array */
24    protected $input;
25    /** @var int|null */
26    protected $tagLimit;
27    /** @var bool */
28    protected $allowArbitrary;
29    /** @var bool */
30    protected $allowReordering;
31    /** @var string[]|null */
32    protected $allowedValues;
33
34    /**
35     * @param array $config Configuration options
36     *   - array $config['default'] Array of items to use as preset data
37     *   - string $config['name'] Name attribute (used in forms)
38     *   - string $config['placeholder'] Placeholder message for input
39     *   - array $config['input'] Config options for the input widget
40     *   - int $config['tagLimit'] Maximum number of selected items
41     *   - bool $config['allowArbitrary'] Allow data items not present in the menu.
42     *   - bool $config['allowReordering'] Allow reordering of the items
43     *   - array $config['allowedValues'] Allowed items
44     */
45    public function __construct( array $config = [] ) {
46        parent::__construct( $config );
47
48        // Properties
49        $this->selectedArray = $config['default'] ?? [];
50        $this->inputName = $config['name'] ?? null;
51        $this->inputPlaceholder = $config['placeholder'] ?? null;
52        $this->input = $config['input'] ?? [];
53        $this->tagLimit = $config['tagLimit'] ?? null;
54        $this->allowArbitrary = $config['allowArbitrary'] ?? false;
55        $this->allowReordering = $config['allowReordering'] ?? true;
56        $this->allowedValues = $config['allowedValues'] ?? null;
57
58        $noJsFallback = ( new Tag( 'div' ) )
59            ->addClasses( [ 'mw-widgets-tagMultiselectWidget-nojs' ] )
60            ->appendContent( $this->getNoJavaScriptFallback() );
61
62        $pending = new PendingTextInputWidget();
63
64        $this->appendContent( $noJsFallback, $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->allowReordering !== null ) {
88            $config['allowReordering'] = $this->allowReordering;
89        }
90        if ( $this->allowedValues !== null ) {
91            $config['allowedValues'] = $this->allowedValues;
92        }
93
94        $config['$overlay'] = true;
95        return parent::getConfig( $config );
96    }
97
98    /**
99     * Provide the implementation for clients with JavaScript disabled.
100     *
101     * @stable to override
102     * @since 1.44
103     * @return Widget[]
104     */
105    protected function getNoJavaScriptFallback() {
106        $widget = new MultilineTextInputWidget( array_merge( [
107            'name' => $this->inputName,
108            'value' => implode( "\n", $this->selectedArray ),
109            'rows' => min( $this->tagLimit, 10 ) ?? 10,
110        ], $this->input ) );
111
112        return [ $widget ];
113    }
114
115    protected function getJavaScriptClassName() {
116        return 'mw.widgets.TagMultiselectWidget';
117    }
118}