|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* File containing the Component class |
|
4
|
|
|
* |
|
5
|
|
|
* This file is part of the MediaWiki skin Chameleon. |
|
6
|
|
|
* |
|
7
|
|
|
* @copyright 2013 - 2014, Stephan Gambke |
|
8
|
|
|
* @license GNU General Public License, version 3 (or any later version) |
|
9
|
|
|
* |
|
10
|
|
|
* The Chameleon skin is free software: you can redistribute it and/or modify |
|
11
|
|
|
* it under the terms of the GNU General Public License as published by the Free |
|
12
|
|
|
* Software Foundation, either version 3 of the License, or (at your option) any |
|
13
|
|
|
* later version. |
|
14
|
|
|
* |
|
15
|
|
|
* The Chameleon skin is distributed in the hope that it will be useful, but |
|
16
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
17
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
|
18
|
|
|
* details. |
|
19
|
|
|
* |
|
20
|
|
|
* You should have received a copy of the GNU General Public License along |
|
21
|
|
|
* with this program. If not, see <http://www.gnu.org/licenses/>. |
|
22
|
|
|
* |
|
23
|
|
|
* @file |
|
24
|
|
|
* @ingroup Skins |
|
25
|
|
|
*/ |
|
26
|
|
|
|
|
27
|
|
|
namespace Skins\Chameleon\Components; |
|
28
|
|
|
|
|
29
|
|
|
use SkinChameleon; |
|
30
|
|
|
use Skins\Chameleon\ChameleonTemplate; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Component class |
|
34
|
|
|
* |
|
35
|
|
|
* This is the base class of all components. |
|
36
|
|
|
* |
|
37
|
|
|
* @author Stephan Gambke |
|
38
|
|
|
* @since 1.0 |
|
39
|
|
|
* @ingroup Skins |
|
40
|
|
|
*/ |
|
41
|
|
|
abstract class Component { |
|
42
|
|
|
|
|
43
|
|
|
private $mSkinTemplate; |
|
44
|
|
|
private $mIndent = 0; |
|
45
|
|
|
private $mClasses = array(); |
|
46
|
|
|
private $mDomElement = null; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @param ChameleonTemplate $template |
|
50
|
|
|
* @param \DOMElement|null $domElement |
|
51
|
|
|
* @param int $indent |
|
52
|
|
|
*/ |
|
53
|
18 |
|
public function __construct( ChameleonTemplate $template, \DOMElement $domElement = null, $indent = 0 ) { |
|
54
|
|
|
|
|
55
|
18 |
|
$this->mSkinTemplate = $template; |
|
56
|
18 |
|
$this->mIndent = (int) $indent; |
|
57
|
18 |
|
$this->mDomElement = $domElement; |
|
58
|
|
|
|
|
59
|
18 |
|
if ( $domElement !== null ) { |
|
60
|
1 |
|
$this->addClasses( $domElement->getAttribute( 'class' ) ); |
|
61
|
1 |
|
} |
|
62
|
18 |
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Sets the class string that should be assigned to the top-level html element of this component |
|
66
|
|
|
* |
|
67
|
|
|
* @param string | array | null $classes |
|
68
|
|
|
* |
|
69
|
|
|
*/ |
|
70
|
6 |
|
public function setClasses( $classes ) { |
|
71
|
|
|
|
|
72
|
6 |
|
$this->mClasses = array(); |
|
73
|
6 |
|
$this->addClasses( $classes ); |
|
74
|
|
|
|
|
75
|
5 |
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Adds the given class to the class string that should be assigned to the top-level html element of this component |
|
79
|
|
|
* |
|
80
|
|
|
* @param string | array | null $classes |
|
81
|
|
|
* |
|
82
|
|
|
* @return string | array |
|
83
|
|
|
*/ |
|
84
|
7 |
|
public function addClasses( $classes ) { |
|
85
|
|
|
|
|
86
|
7 |
|
$classesArray = $this->transformClassesToArray( $classes ); |
|
87
|
|
|
|
|
88
|
7 |
|
if ( !empty( $classesArray ) ) { |
|
89
|
7 |
|
$classesArray = array_combine( $classesArray, $classesArray ); |
|
90
|
7 |
|
$this->mClasses = array_merge( $this->mClasses, $classesArray ); |
|
91
|
7 |
|
} |
|
92
|
7 |
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @param string | array | null $classes |
|
96
|
|
|
* |
|
97
|
|
|
* @return array |
|
98
|
|
|
* @throws \MWException |
|
99
|
|
|
*/ |
|
100
|
201 |
|
protected function transformClassesToArray ( $classes ) { |
|
101
|
|
|
|
|
102
|
201 |
|
if ( empty( $classes ) ) { |
|
103
|
174 |
|
return array(); |
|
104
|
128 |
|
} elseif ( is_array( $classes )) { |
|
105
|
5 |
|
return $classes; |
|
106
|
127 |
|
} elseif ( is_string( $classes ) ) { |
|
107
|
126 |
|
return explode( ' ', $classes ); |
|
108
|
|
|
} else { |
|
109
|
1 |
|
throw new \MWException( __METHOD__ . ': Expected String or Array; ' . getType( $classes ) . ' given.' ); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @return ChameleonTemplate |
|
116
|
|
|
*/ |
|
117
|
1 |
|
public function getSkinTemplate() { |
|
118
|
|
|
|
|
119
|
1 |
|
return $this->mSkinTemplate; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* @since 1.1 |
|
124
|
|
|
* @return SkinChameleon |
|
125
|
|
|
*/ |
|
126
|
|
|
public function getSkin() { |
|
127
|
|
|
|
|
128
|
|
|
return $this->mSkinTemplate->getSkin(); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Returns the current indentation level |
|
133
|
|
|
* |
|
134
|
|
|
* @return int |
|
135
|
|
|
*/ |
|
136
|
1 |
|
public function getIndent() { |
|
137
|
|
|
|
|
138
|
1 |
|
return $this->mIndent; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Returns the class string that should be assigned to the top-level html element of this component |
|
143
|
|
|
* |
|
144
|
|
|
* @return string |
|
145
|
|
|
*/ |
|
146
|
21 |
|
public function getClassString() { |
|
147
|
|
|
|
|
148
|
21 |
|
return implode( ' ', $this->mClasses ); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* Removes the given class from the class string that should be assigned to the top-level html element of this component |
|
153
|
|
|
* |
|
154
|
|
|
* @param string | array | null $classes |
|
155
|
|
|
* |
|
156
|
|
|
* @return string |
|
157
|
|
|
*/ |
|
158
|
7 |
|
public function removeClasses( $classes ) { |
|
159
|
|
|
|
|
160
|
7 |
|
$classesArray = $this->transformClassesToArray( $classes ); |
|
161
|
|
|
|
|
162
|
7 |
|
$this->mClasses = array_diff( $this->mClasses, $classesArray ); |
|
163
|
7 |
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* Returns the DOMElement from the description XML file associated with this element. |
|
167
|
|
|
* |
|
168
|
|
|
* @return \DOMElement |
|
169
|
|
|
*/ |
|
170
|
1 |
|
public function getDomElement() { |
|
171
|
1 |
|
return $this->mDomElement; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* Builds the HTML code for this component |
|
176
|
|
|
* |
|
177
|
|
|
* @return String the HTML code |
|
178
|
|
|
*/ |
|
179
|
|
|
abstract public function getHtml(); |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* @return string[] the resource loader modules needed by this component |
|
183
|
|
|
*/ |
|
184
|
|
|
public function getResourceLoaderModules() { |
|
185
|
|
|
return array(); |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* Adds $indent to (or subtracts from if negative) the current indentation level. |
|
190
|
|
|
* Inserts a new line and a number of tabs according to the new indentation level. |
|
191
|
|
|
* |
|
192
|
|
|
* @param int $indent |
|
193
|
|
|
* @return string |
|
194
|
|
|
* @throws \MWException |
|
195
|
|
|
*/ |
|
196
|
146 |
|
protected function indent( $indent = 0 ) { |
|
197
|
|
|
|
|
198
|
146 |
|
$this->mIndent += (int) $indent; |
|
199
|
|
|
|
|
200
|
146 |
|
if ( $this->mIndent < 0 ) { |
|
201
|
|
|
throw new \MWException('Attempted HTML indentation of ' .$this->mIndent ); |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
146 |
|
return "\n" . str_repeat( "\t", $this->mIndent ); |
|
205
|
|
|
} |
|
206
|
|
|
} |
|
207
|
|
|
|