Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 60
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
ComplexNamespaceInputWidget
0.00% covered (danger)
0.00%
0 / 60
0.00% covered (danger)
0.00%
0 / 3
30
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 41
0.00% covered (danger)
0.00%
0 / 1
12
 getJavaScriptClassName
0.00% covered (danger)
0.00%
0 / 1
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
2
1<?php
2
3namespace MediaWiki\Widget;
4
5use OOUI\CheckboxInputWidget;
6use OOUI\Exception;
7use OOUI\FieldLayout;
8use OOUI\Widget;
9
10/**
11 * Namespace input widget. Displays a dropdown box with the choice of available namespaces, plus two
12 * checkboxes to include associated namespace or to invert selection.
13 *
14 * @copyright 2011-2015 MediaWiki Widgets Team and others; see AUTHORS.txt
15 * @license MIT
16 */
17class ComplexNamespaceInputWidget extends Widget {
18
19    /** @var array */
20    protected $config;
21    /** @var NamespaceInputWidget */
22    protected $namespace;
23    /** @var CheckboxInputWidget|null */
24    protected $associated = null;
25    /** @var FieldLayout|null */
26    protected $associatedLabel = null;
27    /** @var CheckboxInputWidget|null */
28    protected $invert = null;
29    /** @var FieldLayout|null */
30    protected $invertLabel = null;
31
32    /**
33     * @param array $config Configuration options
34     *   - array $config['namespace'] Configuration for the NamespaceInputWidget
35     *     dropdown with list of namespaces
36     *   - string $config['namespace']['includeAllValue'] If specified,
37     *     add an "all namespaces" option to the dropdown, and use this as the input value for it
38     *   - array|null $config['invert'] Configuration for the "invert selection"
39     *     CheckboxInputWidget. If null, the checkbox will not be generated.
40     *   - array|null $config['associated'] Configuration for the "include associated namespace"
41     *     CheckboxInputWidget. If null, the checkbox will not be generated.
42     *   - array $config['invertLabel'] Configuration for the FieldLayout with label
43     *     wrapping the "invert selection" checkbox
44     *   - string $config['invertLabel']['label'] Label text for the label
45     *   - array $config['associatedLabel'] Configuration for the FieldLayout with label
46     *     wrapping the "include associated namespace" checkbox
47     *   - string $config['associatedLabel']['label'] Label text for the label
48     *
49     * @throws Exception
50     */
51    public function __construct( array $config = [] ) {
52        // Configuration initialization
53        $config = array_merge(
54            [
55                // Config options for nested widgets
56                'namespace' => [],
57                'invert' => [],
58                'invertLabel' => [],
59                'associated' => [],
60                'associatedLabel' => [],
61            ],
62            $config
63        );
64
65        parent::__construct( $config );
66
67        // Properties
68        $this->config = $config;
69
70        $this->namespace = new NamespaceInputWidget( $config['namespace'] );
71        if ( $config['associated'] !== null ) {
72            $this->associated = new CheckboxInputWidget( array_merge(
73                [ 'value' => '1' ],
74                $config['associated']
75            ) );
76            // TODO Should use a LabelWidget? But they don't work like HTML <label>s yet
77            $this->associatedLabel = new FieldLayout(
78                $this->associated,
79                array_merge(
80                    [ 'align' => 'inline' ],
81                    $config['associatedLabel']
82                )
83            );
84        }
85        if ( $config['invert'] !== null ) {
86            $this->invert = new CheckboxInputWidget( array_merge(
87                [ 'value' => '1' ],
88                $config['invert']
89            ) );
90            // TODO Should use a LabelWidget? But they don't work like HTML <label>s yet
91            $this->invertLabel = new FieldLayout(
92                $this->invert,
93                array_merge(
94                    [ 'align' => 'inline' ],
95                    $config['invertLabel']
96                )
97            );
98        }
99
100        // Initialization
101        $this
102            ->addClasses( [ 'mw-widget-complexNamespaceInputWidget' ] )
103            ->appendContent( $this->namespace, $this->associatedLabel, $this->invertLabel );
104    }
105
106    protected function getJavaScriptClassName() {
107        return 'mw.widgets.ComplexNamespaceInputWidget';
108    }
109
110    public function getConfig( &$config ) {
111        $config = array_merge(
112            $config,
113            array_intersect_key(
114                $this->config,
115                array_fill_keys(
116                    [
117                        'namespace',
118                        'invert',
119                        'invertLabel',
120                        'associated',
121                        'associatedLabel'
122                    ],
123                    true
124                )
125            )
126        );
127        $config['namespace']['dropdown']['$overlay'] = true;
128        return parent::getConfig( $config );
129    }
130}