Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
toolbar.php
1<?
2
3namespace Bitrix\UI\Toolbar;
4
8
9class Toolbar
10{
11 private $id;
12 private $filter;
13 private $beforeTitleHtml;
14 private $afterTitleHtml;
15 private $underTitleHtml;
16 private string $rightCustomHtml;
17 private $titleMinWidth;
18 private $titleMaxWidth;
19 private $favoriteStar = true;
20
24 private $afterTitleButtons = [];
28 private $buttons = [];
29 private $filterButtons = [];
30 private $options;
31
38 public function __construct($id, $options)
39 {
40 $this->id = $id;
41 $this->options = $options;
42
43 if (isset($this->options['filter']))
44 {
45 $this->addFilter($this->options['filter']);
46 }
47 }
48
52 public function getId()
53 {
54 return $this->id;
55 }
56
64 public function addButton($button, $location = ButtonLocation::RIGHT)
65 {
66 if (is_array($button))
67 {
68 $button = new Button($button);
69 }
70
71 if (!($button instanceof Button))
72 {
73 throw new ArgumentTypeException("button", Button::class);
74 }
75
76 if ($location === ButtonLocation::AFTER_FILTER)
77 {
78 $this->filterButtons[] = $button;
79 }
80 elseif($location === ButtonLocation::AFTER_TITLE)
81 {
82 $this->afterTitleButtons[] = $button;
83 }
84 else
85 {
86 $this->buttons[] = $button;
87 }
88 }
89
90 public function deleteButtons(\Closure $closure)
91 {
92 foreach ($this->buttons as $i => $button)
93 {
94 if ($closure($button, ButtonLocation::RIGHT) === true)
95 {
96 unset($this->buttons[$i]);
97 }
98 }
99
100 foreach ($this->filterButtons as $i => $button)
101 {
102 if ($closure($button, ButtonLocation::AFTER_FILTER) === true)
103 {
104 unset($this->filterButtons[$i]);
105 }
106 }
107 }
108
109 public function shuffleButtons(\Closure $closure, $buttonLocation)
110 {
111 $buttonList = null;
112 switch ($buttonLocation)
113 {
115 $buttonList = $this->buttons;
116 break;
118 $buttonList = $this->filterButtons;
119 break;
120 }
121
122 if ($buttonList)
123 {
124 $buttonList = $closure($buttonList);
125 if (!is_array($buttonList))
126 {
127 throw new ArgumentTypeException('buttonList', 'array');
128 }
129
130 switch ($buttonLocation)
131 {
133 $this->buttons = $buttonList;
134 break;
136 $this->filterButtons = $buttonList;
137 break;
138 }
139 }
140 }
141
142 public function hasFavoriteStar()
143 {
144 return (bool)$this->favoriteStar;
145 }
146
147 public function addFavoriteStar()
148 {
149 $this->favoriteStar = true;
150
151 return $this;
152 }
153
154 public function deleteFavoriteStar()
155 {
156 $this->favoriteStar = false;
157
158 return $this;
159 }
160
161 public function addFilter(array $filterOptions = [])
162 {
163 ob_start();
164 $GLOBALS['APPLICATION']->includeComponent('bitrix:main.ui.filter', '', $filterOptions);
165 $this->filter = ob_get_clean();
166 }
167
168 public function setFilter(string $filter)
169 {
170 $this->filter = $filter;
171 }
172
173 public function getFilter()
174 {
175 return $this->filter;
176 }
177
178 public function addBeforeTitleHtml(string $html)
179 {
180 $this->beforeTitleHtml = $html;
181 }
182
183 public function getBeforeTitleHtml(): ?string
184 {
185 return $this->beforeTitleHtml;
186 }
187
188 public function addAfterTitleHtml(string $html)
189 {
190 $this->afterTitleHtml = $html;
191 }
192
193 public function getAfterTitleHtml(): ?string
194 {
195 return $this->afterTitleHtml;
196 }
197
198 public function addUnderTitleHtml(string $html)
199 {
200 $this->underTitleHtml = $html;
201 }
202
203 public function getUnderTitleHtml(): ?string
204 {
205 return $this->underTitleHtml;
206 }
207
208 public function addRightCustomHtml(string $html): void
209 {
210 $this->rightCustomHtml = $html;
211 }
212
213 public function getRightCustomHtml(): string
214 {
215 return $this->rightCustomHtml ?? '';
216 }
217
221 public function getButtons()
222 {
223 return array_merge($this->afterTitleButtons, $this->filterButtons, $this->buttons);
224 }
225
226 public function renderAfterTitleButtons()
227 {
228 return implode(array_map(function(Button $button) {
229 return self::processButtonRender($button);
230 }, $this->afterTitleButtons));
231 }
232
233 public function renderRightButtons()
234 {
235 return implode(array_map(function(Button $button) {
236 return self::processButtonRender($button);
237 }, $this->buttons));
238 }
239
240 public function renderAfterFilterButtons()
241 {
242 return implode(array_map(function(Button $button) {
243 return self::processButtonRender($button);
244 }, $this->filterButtons));
245 }
246
251 public function renderFilterRightButtons()
252 {
253 return $this->renderAfterFilterButtons();
254 }
255
256 protected function processButtonRender(Button $button)
257 {
258 $shouldAddThemeModifier = (bool)array_intersect($button->getClassList(), [
259 'ui-btn-light-border',
260 'ui-btn-light',
261 'ui-btn-link',
262 ]);
263
264 if ($shouldAddThemeModifier)
265 {
266 $button->addClass('ui-btn-themes');
267 }
268
269 return $button->render(false);
270 }
271
272 public function setTitleMinWidth($width)
273 {
274 if (is_int($width) && $width > 0)
275 {
276 $this->titleMinWidth = $width;
277 }
278 }
279
280 public function getTitleMinWidth()
281 {
282 return $this->titleMinWidth;
283 }
284
285 public function setTitleMaxWidth($width)
286 {
287 if (is_int($width) && $width > 0)
288 {
289 $this->titleMaxWidth = $width;
290 }
291 }
292
293 public function getTitleMaxWidth()
294 {
295 return $this->titleMaxWidth;
296 }
297}
__construct($id, $options)
Definition toolbar.php:38
addButton($button, $location=ButtonLocation::RIGHT)
Definition toolbar.php:64
addFilter(array $filterOptions=[])
Definition toolbar.php:161
addAfterTitleHtml(string $html)
Definition toolbar.php:188
setFilter(string $filter)
Definition toolbar.php:168
addBeforeTitleHtml(string $html)
Definition toolbar.php:178
addRightCustomHtml(string $html)
Definition toolbar.php:208
shuffleButtons(\Closure $closure, $buttonLocation)
Definition toolbar.php:109
deleteButtons(\Closure $closure)
Definition toolbar.php:90
addUnderTitleHtml(string $html)
Definition toolbar.php:198
processButtonRender(Button $button)
Definition toolbar.php:256