ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
MainBar.php
Go to the documentation of this file.
1 <?php
2 
3 /* Copyright (c) 2018 Nils Haagen <nils.haagen@concepts-and-training.de> Extended GPL, see docs/LICENSE */
4 
6 
15 
19 class MainBar implements MainControls\MainBar
20 {
21  use ComponentHelper;
23 
24  const ENTRY_ACTION_TRIGGER = 'trigger';
25  const ENTRY_ACTION_REMOVE = 'remove';
26  const ENTRY_ACTION_TRIGGER_MAPPED = 'trigger_mapped';
27  const ENTRY_ACTION_TOGGLE_TOOLS = 'toggle_tools';
28  const ENTRY_ACTION_DISENGAGE_ALL = 'disengage_all';
29  const NONE_ACTIVE = '_none';
30 
35 
40 
45 
50 
55 
59  protected $entries = [];
60 
64  private $tool_entries = [];
65 
69  private $tools_button;
70 
74  private $more_button;
75 
79  private $active;
80 
84  private $initially_hidden_ids = [];
85 
89  private $tool_signals = [];
90 
94  private $close_buttons = [];
95 
100 
101  public function __construct(SignalGeneratorInterface $signal_generator)
102  {
103  $this->signal_generator = $signal_generator;
104  $this->initSignals();
105  }
106 
110  public function getEntries() : array
111  {
112  return $this->entries;
113  }
114 
118  public function withAdditionalEntry(string $id, $entry) : MainControls\MainBar
119  {
120  $classes = [
121  Button\Bulky::class,
122  Link\Bulky::class,
123  MainControls\Slate\Slate::class
124  ];
125  $check = [$entry];
126  $this->checkArgListElements("Bulky or Slate", $check, $classes);
127 
128  if (array_key_exists($id, $this->entries)) {
129  throw new \InvalidArgumentException("The id of this entry is already taken.", 1);
130  }
131 
132  $clone = clone $this;
133  $clone->entries[$id] = $entry;
134  return $clone;
135  }
136 
140  public function getToolEntries() : array
141  {
142  return $this->tool_entries;
143  }
144 
148  public function withAdditionalToolEntry(
149  string $id,
150  Slate $entry,
151  bool $initially_hidden = false,
152  Button\Close $close_button = null
153  ) : MainControls\MainBar {
154  if (!$this->tools_button) {
155  throw new \LogicException("There must be a tool-button configured to add tool-entries", 1);
156  }
157 
158  if (array_key_exists($id, $this->tool_entries)) {
159  throw new \InvalidArgumentException("The id of this entry is already taken.", 1);
160  }
161 
162  $clone = clone $this;
163  $clone->tool_entries[$id] = $entry;
164  $signal = $this->signal_generator->create();
165  $signal->addOption('entry_id', $id);
166  $signal->addOption('action', self::ENTRY_ACTION_TRIGGER_MAPPED);
167  $clone->tool_signals[$id] = $signal;
168 
169  if ($initially_hidden) {
170  $clone->initially_hidden_ids[] = $id;
171  }
172 
173  if ($close_button) {
174  $clone->close_buttons[$id] = $close_button;
175  }
176  return $clone;
177  }
178 
182  public function withToolsButton(Button\Bulky $button) : MainControls\MainBar
183  {
184  $clone = clone $this;
185  $clone->tools_button = $button;
186  return $clone;
187  }
188 
192  public function getToolsButton() : Button\Bulky
193  {
194  return $this->tools_button;
195  }
196 
200  public function withMoreButton(Button\Bulky $button) : MainControls\MainBar
201  {
202  $clone = clone $this;
203  $clone->more_button = $button;
204  return $clone;
205  }
206 
210  public function getMoreButton() : Button\Bulky
211  {
212  return $this->more_button;
213  }
214 
218  public function getEntryClickSignal() : Signal
219  {
220  return $this->entry_click_signal;
221  }
222 
226  public function getToolsClickSignal() : Signal
227  {
228  return $this->tools_click_signal;
229  }
230 
234  public function getToolsRemovalSignal() : Signal
235  {
236  return $this->tools_removal_signal;
237  }
238 
242  public function getDisengageAllSignal() : Signal
243  {
244  return $this->disengage_all_signal;
245  }
246 
250  public function getToggleToolsSignal() : Signal
251  {
252  return $this->toggle_tools_signal;
253  }
254 
258  protected function initSignals()
259  {
260  $this->entry_click_signal = $this->signal_generator->create();
261  $this->tools_click_signal = $this->signal_generator->create();
262  $this->tools_removal_signal = $this->signal_generator->create();
263  $this->disengage_all_signal = $this->signal_generator->create();
264  $this->disengage_all_signal->addOption('action', self::ENTRY_ACTION_DISENGAGE_ALL);
265  $this->toggle_tools_signal = $this->signal_generator->create();
266  $this->toggle_tools_signal->addOption('action', self::ENTRY_ACTION_TOGGLE_TOOLS);
267  }
268 
269  public function withResetSignals() : MainControls\MainBar
270  {
271  $clone = clone $this;
272  $clone->initSignals();
273  foreach (array_keys($this->tool_entries) as $tool_id) {
274  $this->tool_signals[$tool_id] = $this->signal_generator->create();
275  }
276  return $clone;
277  }
278 
282  public function getActive()
283  {
284  return $this->active;
285  }
286 
290  public function withActive(string $active) : MainControls\MainBar
291  {
292  $valid_entries = array_merge(
293  array_keys($this->entries),
294  array_keys($this->tool_entries),
295  [self::NONE_ACTIVE]
296  );
297  if (!in_array($active, $valid_entries)) {
298  throw new \InvalidArgumentException("Invalid entry to activate: $active", 1);
299  }
300 
301  $clone = clone $this;
302  $clone->active = $active;
303  return $clone;
304  }
305 
309  public function getInitiallyHiddenToolIds() : array
310  {
311  return array_unique($this->initially_hidden_ids);
312  }
313 
317  public function getEngageToolSignal(string $tool_id) : Signal
318  {
319  return $this->tool_signals[$tool_id];
320  }
321 
325  public function getCloseButtons() : array
326  {
327  return $this->close_buttons;
328  }
329 
330 
331  public function withClearedEntries() : MainControls\MainBar
332  {
333  $clone = clone $this;
334  $clone->entries = [];
335  $clone->tool_entries = [];
336  return $clone;
337  }
338 
339  public function getTriggerSignal(
340  string $entry_id,
341  string $action
342  ) : Signal {
343  if (!in_array($action, [self::ENTRY_ACTION_TRIGGER, self::ENTRY_ACTION_REMOVE])) {
344  throw new InvalidArgumentException("invalid action for mainbar entry: $action", 1);
345  }
346  $signal = $this->signal_generator->create();
347  $signal->addOption('entry_id', $entry_id);
348  $signal->addOption('action', $action);
349  return $signal;
350  }
351 
352  public function withMainBarTreePosition(string $tree_pos) : MainBar
353  {
354  $clone = clone $this;
355  $clone->mainbar_tree_position = $tree_pos;
356  return $clone;
357  }
358 
359  public function withMappedSubNodes(callable $f) : MainBar
360  {
361  $clone = clone $this;
362 
363  $counter = 0;
364  foreach ($clone->getEntries() as $k => $v) {
365  $clone->entries[$k] = $f($counter, $v, false);
366  $counter++;
367  }
368 
369  $counter = 0;
370  foreach ($clone->getToolEntries() as $k => $v) {
371  $clone->tool_entries[$k] = $f($counter, $v, true);
372  $counter++;
373  }
374 
375  return $clone;
376  }
377 }
This describes the MainBar.
Definition: MainBar.php:15
__construct(SignalGeneratorInterface $signal_generator)
Definition: MainBar.php:101
withAdditionalToolEntry(string $id, Slate $entry, bool $initially_hidden=false, Button\Close $close_button=null)
Definition: MainBar.php:148
trait JavaScriptBindable
Trait for components implementing JavaScriptBindable providing standard implementation.
trait ComponentHelper
Provides common functionality for component implementations.
initSignals()
Set the signals for this component.
Definition: MainBar.php:258
This describes a bulky button.
Definition: Bulky.php:9
getTriggerSignal(string $entry_id, string $action)
Definition: MainBar.php:339
This describes a close button.
Definition: Close.php:17
checkArgListElements($which, array &$values, $classes)
Check every element of the list if it is an instance of one of the given classes. ...