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    protected $config;
20    protected $namespace;
21    protected $associated = null;
22    protected $associatedLabel = null;
23    protected $invert = null;
24    protected $invertLabel = null;
25
26    /**
27     * @param array $config Configuration options
28     *   - array $config['namespace'] Configuration for the NamespaceInputWidget
29     *     dropdown with list of namespaces
30     *   - string $config['namespace']['includeAllValue'] If specified,
31     *     add an "all namespaces" option to the dropdown, and use this as the input value for it
32     *   - array|null $config['invert'] Configuration for the "invert selection"
33     *     CheckboxInputWidget. If null, the checkbox will not be generated.
34     *   - array|null $config['associated'] Configuration for the "include associated namespace"
35     *     CheckboxInputWidget. If null, the checkbox will not be generated.
36     *   - array $config['invertLabel'] Configuration for the FieldLayout with label
37     *     wrapping the "invert selection" checkbox
38     *   - string $config['invertLabel']['label'] Label text for the label
39     *   - array $config['associatedLabel'] Configuration for the FieldLayout with label
40     *     wrapping the "include associated namespace" checkbox
41     *   - string $config['associatedLabel']['label'] Label text for the label
42     *
43     * @throws Exception
44     */
45    public function __construct( array $config = [] ) {
46        // Configuration initialization
47        $config = array_merge(
48            [
49                // Config options for nested widgets
50                'namespace' => [],
51                'invert' => [],
52                'invertLabel' => [],
53                'associated' => [],
54                'associatedLabel' => [],
55            ],
56            $config
57        );
58
59        parent::__construct( $config );
60
61        // Properties
62        $this->config = $config;
63
64        $this->namespace = new NamespaceInputWidget( $config['namespace'] );
65        if ( $config['associated'] !== null ) {
66            $this->associated = new CheckboxInputWidget( array_merge(
67                [ 'value' => '1' ],
68                $config['associated']
69            ) );
70            // TODO Should use a LabelWidget? But they don't work like HTML <label>s yet
71            $this->associatedLabel = new FieldLayout(
72                $this->associated,
73                array_merge(
74                    [ 'align' => 'inline' ],
75                    $config['associatedLabel']
76                )
77            );
78        }
79        if ( $config['invert'] !== null ) {
80            $this->invert = new CheckboxInputWidget( array_merge(
81                [ 'value' => '1' ],
82                $config['invert']
83            ) );
84            // TODO Should use a LabelWidget? But they don't work like HTML <label>s yet
85            $this->invertLabel = new FieldLayout(
86                $this->invert,
87                array_merge(
88                    [ 'align' => 'inline' ],
89                    $config['invertLabel']
90                )
91            );
92        }
93
94        // Initialization
95        $this
96            ->addClasses( [ 'mw-widget-complexNamespaceInputWidget' ] )
97            ->appendContent( $this->namespace, $this->associatedLabel, $this->invertLabel );
98    }
99
100    protected function getJavaScriptClassName() {
101        return 'mw.widgets.ComplexNamespaceInputWidget';
102    }
103
104    public function getConfig( &$config ) {
105        $config = array_merge(
106            $config,
107            array_intersect_key(
108                $this->config,
109                array_fill_keys(
110                    [
111                        'namespace',
112                        'invert',
113                        'invertLabel',
114                        'associated',
115                        'associatedLabel'
116                    ],
117                    true
118                )
119            )
120        );
121        $config['namespace']['dropdown']['$overlay'] = true;
122        return parent::getConfig( $config );
123    }
124}