ILIAS  release_7 Revision v7.30-3-g800a261c036
All Data Structures Namespaces Files Functions Variables Modules Pages
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  public const ENTRY_ACTION_TRIGGER = 'trigger';
25  public const ENTRY_ACTION_REMOVE = 'remove';
26  public const ENTRY_ACTION_TRIGGER_MAPPED = 'trigger_mapped';
27  public const ENTRY_ACTION_TOGGLE_TOOLS = 'toggle_tools';
28  public const ENTRY_ACTION_DISENGAGE_ALL = 'disengage_all';
29  public 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 
105 
106  public function __construct(SignalGeneratorInterface $signal_generator)
107  {
108  $this->signal_generator = $signal_generator;
109  $this->initSignals();
110  }
111 
115  public function getEntries() : array
116  {
117  return $this->entries;
118  }
119 
123  public function withAdditionalEntry(string $id, $entry) : MainControls\MainBar
124  {
125  $classes = [
126  Button\Bulky::class,
127  Link\Bulky::class,
128  MainControls\Slate\Slate::class
129  ];
130  $check = [$entry];
131  $this->checkArgListElements("Bulky or Slate", $check, $classes);
132 
133  if (array_key_exists($id, $this->entries)) {
134  throw new \InvalidArgumentException("The id of this entry is already taken.", 1);
135  }
136 
137  $clone = clone $this;
138  $clone->entries[$id] = $entry;
139  return $clone;
140  }
141 
145  public function getToolEntries() : array
146  {
147  return $this->tool_entries;
148  }
149 
153  public function withAdditionalToolEntry(
154  string $id,
155  Slate $entry,
156  bool $initially_hidden = false,
157  Button\Close $close_button = null
158  ) : MainControls\MainBar {
159  if (!$this->tools_button) {
160  throw new \LogicException("There must be a tool-button configured to add tool-entries", 1);
161  }
162 
163  if (array_key_exists($id, $this->tool_entries)) {
164  throw new \InvalidArgumentException("The id of this entry is already taken.", 1);
165  }
166 
167  $clone = clone $this;
168  $clone->tool_entries[$id] = $entry;
169  $signal = $this->signal_generator->create();
170  $signal->addOption('entry_id', $id);
171  $signal->addOption('action', self::ENTRY_ACTION_TRIGGER_MAPPED);
172  $clone->tool_signals[$id] = $signal;
173 
174  if ($initially_hidden) {
175  $clone->initially_hidden_ids[] = $id;
176  }
177 
178  if ($close_button) {
179  $clone->close_buttons[$id] = $close_button;
180  }
181  return $clone;
182  }
183 
187  public function withToolsButton(Button\Bulky $button) : MainControls\MainBar
188  {
189  $clone = clone $this;
190  $clone->tools_button = $button;
191  return $clone;
192  }
193 
197  public function getToolsButton() : Button\Bulky
198  {
199  return $this->tools_button;
200  }
201 
205  public function getEntryClickSignal() : Signal
206  {
207  return $this->entry_click_signal;
208  }
209 
213  public function getToolsClickSignal() : Signal
214  {
215  return $this->tools_click_signal;
216  }
217 
221  public function getToolsRemovalSignal() : Signal
222  {
223  return $this->tools_removal_signal;
224  }
225 
229  public function getDisengageAllSignal() : Signal
230  {
231  return $this->disengage_all_signal;
232  }
233 
237  public function getToggleToolsSignal() : Signal
238  {
239  return $this->toggle_tools_signal;
240  }
241 
245  protected function initSignals()
246  {
247  $this->entry_click_signal = $this->signal_generator->create();
248  $this->tools_click_signal = $this->signal_generator->create();
249  $this->tools_removal_signal = $this->signal_generator->create();
250  $this->disengage_all_signal = $this->signal_generator->create();
251  $this->disengage_all_signal->addOption('action', self::ENTRY_ACTION_DISENGAGE_ALL);
252  $this->toggle_tools_signal = $this->signal_generator->create();
253  $this->toggle_tools_signal->addOption('action', self::ENTRY_ACTION_TOGGLE_TOOLS);
254  }
255 
256  public function withResetSignals() : MainControls\MainBar
257  {
258  $clone = clone $this;
259  $clone->initSignals();
260  foreach (array_keys($this->tool_entries) as $tool_id) {
261  $this->tool_signals[$tool_id] = $this->signal_generator->create();
262  }
263  return $clone;
264  }
265 
269  public function getActive()
270  {
271  return $this->active;
272  }
273 
277  public function withActive(string $active) : MainControls\MainBar
278  {
279  $valid_entries = array_merge(
280  array_keys($this->entries),
281  array_keys($this->tool_entries),
282  [self::NONE_ACTIVE]
283  );
284  if (!in_array($active, $valid_entries)) {
285  throw new \InvalidArgumentException("Invalid entry to activate: $active", 1);
286  }
287 
288  $clone = clone $this;
289  $clone->active = $active;
290  return $clone;
291  }
292 
296  public function getInitiallyHiddenToolIds() : array
297  {
298  return array_unique($this->initially_hidden_ids);
299  }
300 
304  public function getEngageToolSignal(string $tool_id) : Signal
305  {
306  return $this->tool_signals[$tool_id];
307  }
308 
312  public function getCloseButtons() : array
313  {
314  return $this->close_buttons;
315  }
316 
317 
318  public function withClearedEntries() : MainControls\MainBar
319  {
320  $clone = clone $this;
321  $clone->entries = [];
322  $clone->tool_entries = [];
323  return $clone;
324  }
325 
326  public function getTriggerSignal(
327  string $entry_id,
328  string $action
329  ) : Signal {
330  if (!in_array($action, [self::ENTRY_ACTION_TRIGGER, self::ENTRY_ACTION_REMOVE])) {
331  throw new \InvalidArgumentException("invalid action for mainbar entry: $action", 1);
332  }
333  $signal = $this->signal_generator->create();
334  $signal->addOption('entry_id', $entry_id);
335  $signal->addOption('action', $action);
336  return $signal;
337  }
338 
339  public function withMainBarTreePosition(string $tree_pos) : MainBar
340  {
341  $clone = clone $this;
342  $clone->mainbar_tree_position = $tree_pos;
343  return $clone;
344  }
345 
346  public function withMappedSubNodes(callable $f) : MainBar
347  {
348  $clone = clone $this;
349 
350  $counter = 0;
351  foreach ($clone->getEntries() as $k => $v) {
352  $clone->entries[$k] = $f($counter, $v, false);
353  $counter++;
354  }
355 
356  $counter = 0;
357  foreach ($clone->getToolEntries() as $k => $v) {
358  $clone->tool_entries[$k] = $f($counter, $v, true);
359  $counter++;
360  }
361 
362  return $clone;
363  }
364 }
This describes the MainBar.
Definition: MainBar.php:16
__construct(SignalGeneratorInterface $signal_generator)
Definition: MainBar.php:106
withAdditionalToolEntry(string $id, Slate $entry, bool $initially_hidden=false, Button\Close $close_button=null)
Definition: MainBar.php:153
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:245
This describes a bulky button.
Definition: Bulky.php:9
getTriggerSignal(string $entry_id, string $action)
Definition: MainBar.php:326
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. ...