-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTabs.php
239 lines (199 loc) · 5.9 KB
/
Tabs.php
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
<?php
declare(strict_types=1);
/**
* This file is part of the EaseTWBootstrap3 package
*
* https://github.com/VitexSoftware/php-ease-twbootstrap
*
* (c) Vítězslav Dvořák <http://vitexsoftware.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Ease\TWB;
/**
* Create TWBootstrap tabs.
*
* @see http://getbootstrap.com/2.3.2/components.html#navs
*
* @author Vítězslav Dvořák <vitex@hippy.cz>
*/
class Tabs extends \Ease\Container
{
/**
* Název.
*/
public string $partName = 'TWBTabs';
/**
* Array of tab names=>contents.
*/
public array $tabs = [];
/**
* Jméno aktivního tabu.
*/
private string $activeTab = '';
/**
* Create TWBootstrap tabs.
*
* @param string $partName - DIV id
* @param array $tabsList
* @param array $tagProperties
*/
public function __construct(
$partName,
$tabsList = null,
$tagProperties = null
) {
$this->partName = $partName;
parent::__construct();
if (\is_array($tabsList)) {
$this->tabs = array_merge($this->tabs, $tabsList);
}
if (null !== $tagProperties) {
$this->setPartProperties($tagProperties);
}
}
/**
* Vytvoří nový tab a vloží do něj obsah.
*
* @param string $tabName jméno a titulek tabu
* @param mixed $tabContent
* @param bool $active Má být tento tab aktivní ?
*
* @return \Ease\Html\DivTag odkaz na vložený obsah
*/
public function &addTab($tabName, $tabContent = null, $active = false)
{
if (null === $tabContent) {
$tabContent = new \Ease\Html\DivTag();
}
$this->tabs[$tabName] = ['static' => $tabContent];
if ($active) {
$this->activeTab = $tabName;
}
return $this->tabs[$tabName]['static'];
}
/**
* Create new Dynamic Tab.
*
* @param string $tabName jméno a titulek tabu
* @param string $tabUrl where to obtain tab content
* @param bool $active Má být tento tab aktivní ?
*
* @return \Ease\Html\DivTag odkaz na vložený obsah
*/
public function &addAjaxTab($tabName, $tabUrl, $active = false)
{
$this->tabs[$tabName] = ['ajax' => $tabUrl];
if ($active) {
$this->activeTab = $tabName;
}
\Ease\WebPage::singleton()->addJavaScript(<<<'EOD'
$('#
EOD.$this->getTagID().<<<'EOD'
a').click(function (e) {
e.preventDefault();
var url = $(this).attr("data-url");
var href = this.hash;
var pane = $(this);
// ajax load from data-url
$(href).load(url,function(result){
pane.tab('show');
});
});
EOD);
return $this->tabs[$tabName];
}
/**
* Vrací ID tagu.
*
* @return string
*/
public function getTagID()
{
return $this->partName;
}
/**
* Vložení skriptu a divů do stránky.
*/
public function finalize(): void
{
if (null === $this->activeTab) {
$this->activeTab = current(array_keys($this->tabs));
}
$tabsUl = $this->addItem(new \Ease\Html\UlTag(
null,
['class' => 'nav nav-tabs', 'id' => $this->partName],
));
foreach ($this->tabs as $tabName => $tab) {
$tabProperties = ['data-toggle' => 'tab'];
if (key($tab) === 'ajax') {
$tabProperties['data-url'] = current($tab);
}
$anchor = '#'.\Ease\Functions::lettersOnly(str_replace(['(', ')'], '', $this->partName.$tabName));
if ($tabName === $this->activeTab) {
$tabsUl->addItem(new \Ease\Html\LiTag(new \Ease\Html\ATag(
$anchor,
$tabName,
$tabProperties,
), ['class' => 'active']));
} else {
$tabsUl->addItem(new \Ease\Html\LiTag(new \Ease\Html\ATag(
$anchor,
$tabName,
$tabProperties,
)));
}
}
$tabDiv = $this->addItem(new \Ease\Html\DivTag(
null,
['id' => $this->partName.'body', 'class' => 'tab-content'],
));
foreach ($this->tabs as $tabName => $tab) {
switch (key($tab)) {
case 'static':
$tabContent = current($tab);
break;
case 'ajax':
$tabContent = '';
break;
}
if ($tabName === $this->activeTab) {
$tabDiv->addItem(new \Ease\Html\DivTag(
$tabContent,
['id' => $this->partName.\Ease\Functions::lettersOnly($tabName),
'class' => 'tab-pane active',
],
));
} else {
$tabDiv->addItem(new \Ease\Html\DivTag(
$tabContent,
['id' => $this->partName.\Ease\Functions::lettersOnly($tabName),
'class' => 'tab-pane',
],
));
}
}
Part::twBootstrapize();
if ($this->activeTab && is_array($this->tabs[$this->activeTab]) && key($this->tabs[$this->activeTab]) === 'ajax') {
\Ease\WebPage::singleton()->addJavaScript(<<<'EOD'
// load first tab content
$('#
EOD.$this->partName.$this->activeTab.<<<'EOD'
').load($('.active a').attr("data-url"),function(result){
$('.active a').tab('show');
});
EOD);
} else {
\Ease\WebPage::singleton()->addJavaScript(
<<<'EOD'
$('#
EOD.$this->partName.' a[href="#'.\Ease\Functions::lettersOnly($this->activeTab).<<<'EOD'
"]').tab('show');
EOD,
null,
true,
);
}
}
}