-
Notifications
You must be signed in to change notification settings - Fork 664
/
SplitButton.php
219 lines (207 loc) · 7.35 KB
/
SplitButton.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
<?php
/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
namespace TYPO3\CMS\Backend\Template\Components\Buttons;
/**
* SplitButton
*
* This button type renders a bootstrap split button.
* It takes multiple button objects as parameters
*
* EXAMPLE USAGE TO ADD A SPLIT BUTTON TO THE FIRST BUTTON GROUP IN THE LEFT BAR:
*
* $buttonBar = $this->moduleTemplate->getDocHeaderComponent()->getButtonBar();
*
* $saveButton = $buttonBar->makeInputButton()
* ->setName('save')
* ->setValue('1')
* ->setIcon($this->iconFactory->getIcon('actions-document-save', IconSize::SMALL))
* ->setTitle('Save');
*
* $saveAndCloseButton = $buttonBar->makeInputButton()
* ->setName('save_and_close')
* ->setValue('1')
* ->setTitle('Save and close')
* ->setIcon($this->iconFactory->getIcon('actions-document-save-close', IconSize::SMALL));
*
* $saveAndShowPageButton = $buttonBar->makeInputButton()
* ->setName('save_and_show')
* ->setValue('1')
* ->setTitle('Save and show')
* ->setIcon($this->iconFactory->getIcon('actions-document-save-view', IconSize::SMALL));
*
* $splitButtonElement = $buttonBar->makeSplitButton()
* ->addItem($saveButton, TRUE)
* ->addItem($saveAndCloseButton)
* ->addItem($saveAndShowPageButton);
*/
class SplitButton extends AbstractButton
{
/**
* Internal var that determines whether the split button has received any primary
* actions yet
*
* @var bool
*/
protected $containsPrimaryAction = false;
/**
* Internal array of items in the split button
*
* @var array
*/
protected $items = [];
/**
* Adds an instance of any button to the split button
*
* @param AbstractButton $item ButtonObject to add
* @param bool $primaryAction Is the button the primary action?
*
* @throws \InvalidArgumentException In case a button is not valid
*
* @return $this
*/
public function addItem(AbstractButton $item, $primaryAction = false)
{
if (!$item->isValid()) {
throw new \InvalidArgumentException(
'Only valid items may be assigned to a split Button. "' .
$item->getType() .
'" did not pass validation',
1441706330
);
}
if ($primaryAction && $this->containsPrimaryAction) {
throw new \InvalidArgumentException('A splitButton may only contain one primary action', 1441706340);
}
if ($primaryAction) {
$this->containsPrimaryAction = true;
$this->items['primary'] = clone $item;
} else {
$this->items['options'][] = clone $item;
}
return $this;
}
/**
* Returns the current button
*
* @return array
*/
public function getButton()
{
if (!isset($this->items['primary']) && isset($this->items['options'])) {
$primaryAction = array_shift($this->items['options']);
$this->items['primary'] = $primaryAction;
}
return $this->items;
}
/**
* Validates the current button
*
*
* @return bool
*/
public function isValid()
{
$subject = $this->getButton();
return isset($subject['primary'])
&& ($subject['primary'] instanceof AbstractButton)
&& isset($subject['options']);
}
/**
* Renders the HTML markup of the button
*
* @return string
*/
public function render()
{
$items = $this->getButton();
$attributes = [
'type' => 'submit',
'class' => 'btn btn-sm btn-default ' . $items['primary']->getClasses(),
];
if (method_exists($items['primary'], 'getName')) {
$attributes['name'] = $items['primary']->getName();
}
if (method_exists($items['primary'], 'getValue')) {
$attributes['value'] = $items['primary']->getValue();
}
if (method_exists($items['primary'], 'getForm') && !empty($items['primary']->getForm())) {
$attributes['form'] = $items['primary']->getForm();
}
$attributesString = '';
foreach ($attributes as $key => $value) {
$attributesString .= ' ' . htmlspecialchars($key) . '="' . htmlspecialchars($value) . '"';
}
$content = '
<div class="btn-group t3js-splitbutton">
<button' . $attributesString . '>
' . $items['primary']->getIcon()->render('inline') . '
' . htmlspecialchars($items['primary']->getTitle()) . '
</button>
<button type="button" class="btn btn-sm btn-default dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">
<span class="visually-hidden">Toggle Dropdown</span>
</button>
<ul class="dropdown-menu">';
/** @var AbstractButton $option */
foreach ($items['options'] as $option) {
if ($option instanceof InputButton) {
// if the option is an InputButton we have to create a custom rendering
$optionAttributes = [
'href' => '#',
'data-name' => $option->getName(),
'data-value' => $option->getValue(),
'data-form' => $option->getForm(),
];
if (!empty($option->getClasses())) {
$optionAttributes['class'] = $option->getClasses();
}
$optionAttributes['class'] = implode(' ', [$optionAttributes['class'] ?? '', 'dropdown-item']);
$optionAttributesString = '';
foreach ($optionAttributes as $key => $value) {
$optionAttributesString .= ' ' . htmlspecialchars($key) . '="' . htmlspecialchars($value) . '"';
}
$html = '' .
'<a' . $optionAttributesString . '>' .
'<span class="dropdown-item-columns">' .
'<span class="dropdown-item-column dropdown-item-column-icon" aria-hidden="true">' .
$option->getIcon()->render('inline') .
'</span>' .
'<span class="dropdown-item-column dropdown-item-column-title">' .
htmlspecialchars($option->getTitle()) .
'</span>' .
'</span>' .
'</a>';
} else {
// for any other kind of button we simply use what comes along (e.g. LinkButton)
$html = $option->render();
}
$content .= '
<li>
' . $html . '
</li>
';
}
$content .= '
</ul>
</div>
';
return $content;
}
/**
* Magic method so Fluid can access a button via {button}
*/
public function __toString(): string
{
return $this->render();
}
}