Implement NamespaceInputWidget
[lhc/web/wiklou.git] / includes / widget / NamespaceInputWidget.php
1 <?php
2 /**
3 * MediaWiki Widgets – NamespaceInputWidget class.
4 *
5 * @copyright 2011-2015 MediaWiki Widgets Team and others; see AUTHORS.txt
6 * @license The MIT License (MIT); see LICENSE.txt
7 */
8 namespace MediaWiki\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 class NamespaceInputWidget extends \OOUI\Widget {
15
16 protected $namespace = null;
17 protected $associated = null;
18 protected $invert = null;
19 protected $allValue = null;
20
21 /**
22 * @param array $config Configuration options
23 * @param string $config['nameNamespace'] HTML input name for the namespace dropdown box (default:
24 * 'namespace')
25 * @param string $config['nameInvert'] HTML input name for the "invert selection" checkbox. If
26 * null, the checkbox will not be generated. (default: 'invert')
27 * @param string $config['nameAssociated'] HTML input name for the "include associated namespace"
28 * checkbox. If null, the checkbox will not be generated. (default: 'associated')
29 * @param string $config['includeAllValue'] If specified, add a "all namespaces" option to the
30 * namespace dropdown, and use this as the input value for it
31 * @param int|string $config['valueNamespace'] Input value of the namespace dropdown box. May be a
32 * string only if 'includeAllValue' is set.
33 * @param boolean $config['valueInvert'] Input value of the "invert selection" checkbox (default:
34 * false)
35 * @param boolean $config['valueAssociated'] Input value of the "include associated namespace"
36 * checkbox (default: false)
37 * @param string $config['labelInvert'] Text of label to use for "invert selection" checkbox
38 * @param string $config['labelAssociated'] Text of label to use for "include associated
39 * namespace" checkbox
40 */
41 public function __construct( array $config = array() ) {
42 // Configuration initialization
43
44 $config = array_merge(
45 array(
46 'nameNamespace' => 'namespace',
47 'nameInvert' => 'invert',
48 'nameAssociated' => 'associated',
49 // Choose first available: either main namespace or the "all namespaces" option
50 'valueNamespace' => null,
51 'valueInvert' => false,
52 'valueAssociated' => false,
53 ),
54 $config
55 );
56
57 // Parent constructor
58 parent::__construct( $config );
59
60 // Properties
61 $this->allValue = isset( $config['includeAllValue'] ) ? $config['includeAllValue'] : null;
62 $this->namespace = new \OOUI\DropdownInputWidget( array(
63 'name' => $config['nameNamespace'],
64 'value' => $config['valueNamespace'],
65 'options' => $this->getNamespaceDropdownOptions( $config ),
66 ) );
67 if ( $config['nameAssociated'] !== null ) {
68 // FIXME Should use a LabelWidget? But they don't work like HTML <label>s yet
69 $this->associated = new \OOUI\FieldLayout(
70 new \OOUI\CheckboxInputWidget( array(
71 'name' => $config['nameAssociated'],
72 'selected' => $config['valueAssociated'],
73 'value' => '1',
74 ) ),
75 array(
76 'align' => 'inline',
77 'label' => $config['labelAssociated'],
78 )
79 );
80 }
81 if ( $config['nameInvert'] !== null ) {
82 $this->invert = new \OOUI\FieldLayout(
83 new \OOUI\CheckboxInputWidget( array(
84 'name' => $config['nameInvert'],
85 'selected' => $config['valueInvert'],
86 'value' => '1',
87 ) ),
88 array(
89 'align' => 'inline',
90 'label' => $config['labelInvert'],
91 )
92 );
93 }
94
95 // Initialization
96 $this
97 ->addClasses( array( 'mw-widget-namespaceInputWidget' ) )
98 ->appendContent( $this->namespace, $this->associated, $this->invert );
99 }
100
101 protected function getNamespaceDropdownOptions( array $config ) {
102 $namespaceOptionsParams = isset( $config['includeAllValue'] ) ?
103 array( 'all' => $config['includeAllValue'] ) : array();
104 $namespaceOptions = \Html::namespaceSelectorOptions( $namespaceOptionsParams );
105
106 $options = array();
107 foreach( $namespaceOptions as $id => $name ) {
108 $options[] = array(
109 'data' => (string)$id,
110 'label' => $name,
111 );
112 }
113
114 return $options;
115 }
116
117 protected function getJavaScriptClassName() {
118 return 'mw.widgets.NamespaceInputWidget';
119 }
120
121 public function getConfig( &$config ) {
122 $config['namespace'] = $this->namespace;
123 $config['associated'] = $this->associated;
124 $config['invert'] = $this->invert;
125 $config['allValue'] = $this->allValue;
126 return parent::getConfig( $config );
127 }
128 }