summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/Utils/HtmlTabs.php
blob: 8cca3bf8e58a7200f76c4661c2081c689cb7e15d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<?php

namespace SMW\Utils;

use Html;

/**
 * @license GNU GPL v2+
 * @since 3.0
 *
 * @author mwjames
 */
class HtmlTabs {

	/**
	 * @var []
	 */
	private $tabs = [];

	/**
	 * @var []
	 */
	private $contents = [];

	/**
	 * @var []
	 */
	private $hidden = [];

	/**
	 * @var string|null
	 */
	private $activeTab = null;

	/**
	 * @var string
	 */
	private $group = 'tabs';

	/**
	 * @since 3.0
	 *
	 * @param string $activeTab
	 */
	public function setActiveTab( $activeTab ) {
		$this->activeTab = $activeTab;
	}

	/**
	 * @since 3.0
	 *
	 * @param string $group
	 */
	public function setGroup( $group ) {
		$this->group = $group;
	}

	/**
	 * @since 3.0
	 *
	 * @param array $attributes
	 *
	 * @return string
	 */
	public function buildHTML( array $attributes = [] ) {

		$tabs = $this->tabs;
		$contents = $this->contents;

		$this->tabs = [];
		$this->contents = [];

		$attributes = $this->mergeAttributes( 'smw-tabs', $attributes );

		return Html::rawElement(
			'div',
			$attributes,
			implode( '', $tabs ) . implode( '', $contents )
		);
	}

	/**
	 * @since 3.0
	 *
	 * @param string $id
	 * @param string $name
	 * @param array $params
	 *
	 * @return string
	 */
	public function html( $html, array $params = [] ) {

		if ( isset( $params['hide'] ) && $params['hide'] ) {
			return;
		}

		$this->tabs[] = $html;
	}

	/**
	 * @since 3.0
	 *
	 * @param string $id
	 * @param string $name
	 * @param array $params
	 *
	 * @return string
	 */
	public function tab( $id, $name = '', array $params = [] ) {

		if ( isset( $params['hide'] ) && $params['hide'] ) {
			return $this->hidden[$id] = true;
		}

		$isChecked = false;

		// No acive tab means, select the first tab being added
		if ( $this->activeTab === null ) {
			$this->activeTab = $id;
		}

		if ( $id === $this->activeTab ) {
			$isChecked = true;
		}

		$this->tabs[] = Html::rawElement(
			'input',
			[
				'id'    => "tab-$id",
				'class' => 'nav-tab',
				'type'  => 'radio',
				'name'  => $this->group
			] + ( $isChecked ? [ 'checked' => 'checked' ] : [] )
		) . Html::rawElement(
			'label',
			[
				'id'  => "tab-label-$id",
				'for' => "tab-$id"
			] + $this->mergeAttributes( 'nav-label', $params ),
			$name
		);
	}

	/**
	 * @since 3.0
	 *
	 * @param string $id
	 * @param string $content
	 */
	public function content( $id, $content ) {

		// Tab hidden?
		if ( isset( $this->hidden[$id] ) ) {
			return;
		}

		$this->contents[] = Html::rawElement(
			'section',
			[
				'id' => "tab-content-$id"
			],
			$content
		);
	}

	private function mergeAttributes( $class, $attr ) {

		$attributes = [];

		// A bit of attribute order
		if ( isset( $attr['id'] ) ) {
			$attributes['id'] = $attr['id'];
		}

		if ( isset( $attr['class'] ) ) {
			$attributes['class'] = $class . ' ' . $attr['class'];
		} else {
			$attributes['class'] = $class;
		}

		return $attributes += $attr;
	}

}