summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/Utils/HtmlVTabs.php
blob: 25ced9bc1ddabf8e1be4365b250911b492a827d0 (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
185
186
187
188
189
190
191
192
193
194
195
<?php

namespace SMW\Utils;

use Html;

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

	/**
	 * Identifies which link/content to be active
	 */
	const IS_ACTIVE = 'active';

	/**
	 * Match an active status against a id
	 */
	const FIND_ACTIVE_LINK = 'find';

	/**
	 * Hide content
	 */
	const IS_HIDDEN = 'hidden';

	/**
	 * @var string
	 */
	private static $active = '';

	/**
	 * @var string
	 */
	private static $direction = 'right';

	/**
	 * @since 3.0
	 */
	public static function init() {
		self::$active = '';
		self::$direction = 'right';
	}

	/**
	 * @since 3.0
	 *
	 * @return array
	 */
	public static function getModules() {
		return [ 'ext.smw.vtabs' ];
	}

	/**
	 * @since 3.0
	 *
	 * @return array
	 */
	public static function getModuleStyles() {
		return [ 'ext.smw.vtabs.styles' ];
	}

	/**
	 * @since 3.0
	 *
	 * @param string $direction
	 */
	public static function setDirection( $direction ) {
		self::$direction = $direction;
	}

	/**
	 * Encapsulate generate tab links into a navigation container.
	 *
	 * @since 3.0
	 *
	 * @param string $html
	 * @param array $attributes
	 *
	 * @return string
	 */
	public static function nav( $html = '', array $attributes = [] ) {

		$direction = self::$direction === 'right' ? 'nav-right' : 'nav-left';

		$attributes = self::mergeAttributes( "smw-vtab-nav", $attributes );
		$attributes['class'] .= " $direction";

		return Html::rawElement(
			'div',
			$attributes,
			$html
		);
	}

	/**
	 * Generate an individual tab link.
	 *
	 * @since 3.0
	 *
	 * @param string $id
	 * @param string $label
	 * @param string|array $flag
	 * @param array $attributes
	 *
	 * @return string
	 */
	public static function navLink( $id, $label = '', $flag = false, array $attributes = [] ) {

		if ( $flag === self::IS_HIDDEN ) {
			return '';
		}

		// Match an active status against an id
		if ( is_array( $flag ) && isset( $flag[self::FIND_ACTIVE_LINK] ) && $flag[self::FIND_ACTIVE_LINK] === $id ) {
			$flag = self::IS_ACTIVE;
		}

		$id = 'tab-' . $id;
		$direction = self::$direction === 'right' ? 'nav-right' : 'nav-left';

		$attributes['data-id'] = $id;
		$attributes['id'] = 'vtab-item-' . $id;

		$attributes = self::mergeAttributes( "smw-vtab-link", $attributes );
		$attributes['class'] .= " $direction";

		if ( $flag === self::IS_ACTIVE && self::$active == '' ) {
			$attributes['class'] .= ' active';
			self::$active = $id;
		}

		return Html::rawElement(
			'button',
			$attributes,
			Html::rawElement( 'a', [ 'href' => '#' . $id ], $label )
		);
	}

	/**
	 * Encapsulate the content that relates to a tab link using the ID as identifier
	 * to distinguish content sections.
	 *
	 * @since 3.0
	 *
	 * @param string $id
	 * @param string $html
	 * @param array $attributes
	 *
	 * @return string
	 */
	public static function content( $id, $html = '', array $attributes = [] ) {

		$id = 'tab-' . $id;
		$attributes['id'] = $id;

		if ( self::$active !== $id ) {
			if ( !isset( $attributes['style'] ) ) {
				$attributes['style'] = 'display:none;';
			} else {
				$attributes['style'] .= ' display:none;';
			}
		}

		$attributes = self::mergeAttributes( 'smw-vtab-content', $attributes );

		return Html::rawElement(
			'div',
			$attributes,
			$html
		);
	}

	private static 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;
	}

}