-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Carousel.php
163 lines (143 loc) · 4.41 KB
/
Carousel.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
<?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;
/**
* Carousel for Twitter Bootstrap.
*/
class Carousel extends \Ease\Html\DivTag
{
/**
* Carousel name.
*/
public string $name = '';
/**
* Carousel's inner div.
*/
public \Ease\Html\DivTag $inner;
/**
* Carousel's inner div.
*/
public \Ease\Html\OlTag $indicators;
/**
* Which slide is active ?
*/
public int $active = 0;
/**
* Twitter bootstrap Carousel.
*
* @url http://getbootstrap.com/javascript/#carousel
*
* @param string $name
* @param array $properties ['data-ride'=>'carousel'] means autorun
*/
public function __construct($name = null, $properties = [])
{
parent::__construct(null, $properties);
$this->name = $this->setTagID($name);
$this->setTagClass('carousel slide');
$this->indicators = $this->addItem(new \Ease\Html\OlTag(
null,
['class' => 'carousel-indicators'],
));
$this->inner = $this->addItem(new \Ease\Html\DivTag(
null,
['class' => 'carousel-inner', 'role' => 'listbox'],
));
}
/**
* Carousel Slide.
*
* @param ImgTag|mixed $slide body Image or something else
* @param string $capHeading
* @param string $caption
* @param bool $default show slide by default
*/
public function addSlide(
$slide,
$capHeading = '',
$caption = '',
$default = false
): void {
$item = new \Ease\Html\DivTag($slide, ['class' => 'item']);
if ($default) {
$item->addTagClass('active');
}
if ($capHeading || $caption) {
$cpt = $item->addItem(new \Ease\Html\DivTag(
null,
['class' => 'carousel-caption'],
));
if ($capHeading) {
$cpt->addItem(new \Ease\Html\H4Tag($capHeading));
}
if ($caption) {
$cpt->addItem(new \Ease\Html\PTag($caption));
}
}
$to = $this->indicators->getItemsCount();
$indicator = new \Ease\Html\LiTag(
null,
['data-target' => '#'.$this->name, 'data-slide-to' => $to],
);
if ($default) {
$indicator->addTagClass('active');
$this->active = $to;
}
$this->indicators->addItem($indicator);
$this->inner->addItem($item);
}
/**
* Add Navigation buttons.
*/
public function finalize(): void
{
Part::twBootstrapize();
if (null === $this->active && $this->getItemsCount()) { // We need one slide active
$this->indicators->getFirstPart()->setTagClass('active');
$this->inner->getFirstPart()->addTagClass('active');
}
$this->inner->addItem(
new \Ease\Html\ATag(
'#'.$this->getTagID(),
[
new \Ease\Html\Span(
null,
['class' => 'glyphicon glyphicon-chevron-left', 'aria-hidden' => 'true'],
),
new \Ease\Html\Span(_('Previous'), ['class' => 'sr-only']),
],
['class' => 'left carousel-control', 'data-slide' => 'prev', 'role' => 'button'],
),
);
$this->inner->addItem(
new \Ease\Html\ATag(
'#'.$this->getTagID(),
[
new \Ease\Html\Span(
null,
['class' => 'glyphicon glyphicon-chevron-right', 'aria-hidden' => 'true'],
),
new \Ease\Html\Span(_('Next'), ['class' => 'sr-only']),
],
['class' => 'right carousel-control', 'data-slide' => 'next', 'role' => 'button'],
),
);
if ($this->getTagProperty('data-ride') !== 'carousel') {
$this->addJavaScript(
'$(\'#'.$this->name.'\').carousel();',
null,
true,
);
}
}
}