ILIAS  trunk Revision v11.0_alpha-1715-g7fc467680fb
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
MainBar.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
32 use LogicException;
33 
37 class MainBar implements MainControls\MainBar
38 {
39  use ComponentHelper;
41 
42  public const ENTRY_ACTION_TRIGGER = 'trigger';
43  public const ENTRY_ACTION_REMOVE = 'remove';
44  public const ENTRY_ACTION_TRIGGER_MAPPED = 'trigger_mapped';
45  public const ENTRY_ACTION_TOGGLE_TOOLS = 'toggle_tools';
46  public const ENTRY_ACTION_DISENGAGE_ALL = 'disengage_all';
47  public const NONE_ACTIVE = '_none';
48 
52  protected array $entries = [];
53 
57  private array $initially_hidden_ids = [];
58 
62  private array $tool_signals = [];
63 
67  private array $close_buttons = [];
68 
72  private array $tool_entries = [];
73 
74  private ?Button\Bulky $tools_button = null;
75  private Button\Bulky $more_button;
76  private ?string $active = null;
77  private string $mainbar_tree_position;
84 
85  public function __construct(SignalGeneratorInterface $signal_generator)
86  {
87  $this->signal_generator = $signal_generator;
88  $this->initSignals();
89  }
90 
94  public function getEntries(): array
95  {
96  return $this->entries;
97  }
98 
102  public function withAdditionalEntry(string $id, $entry): MainControls\MainBar
103  {
104  $classes = [
105  Button\Bulky::class,
106  Link\Bulky::class,
107  MainControls\Slate\Slate::class
108  ];
109  $check = [$entry];
110  $this->checkArgListElements("Bulky or Slate", $check, $classes);
111 
112  if (array_key_exists($id, $this->entries)) {
113  throw new InvalidArgumentException("The id of this entry is already taken.", 1);
114  }
115 
116  $clone = clone $this;
117  $clone->entries[$id] = $entry;
118  return $clone;
119  }
120 
124  public function getToolEntries(): array
125  {
126  return $this->tool_entries;
127  }
128 
132  public function withAdditionalToolEntry(
133  string $id,
134  Slate $entry,
135  bool $initially_hidden = false,
136  ?Button\Close $close_button = null
137  ): MainControls\MainBar {
138  if (!$this->tools_button) {
139  throw new LogicException("There must be a tool-button configured to add tool-entries", 1);
140  }
141 
142  if (array_key_exists($id, $this->tool_entries)) {
143  throw new InvalidArgumentException("The id of this entry is already taken.", 1);
144  }
145 
146  $clone = clone $this;
147  $clone->tool_entries[$id] = $entry;
148  $signal = $this->signal_generator->create();
149  $signal->addOption('entry_id', $id);
150  $signal->addOption('action', self::ENTRY_ACTION_TRIGGER_MAPPED);
151  $clone->tool_signals[$id] = $signal;
152 
153  if ($initially_hidden) {
154  $clone->initially_hidden_ids[] = $id;
155  }
156 
157  if ($close_button) {
158  $clone->close_buttons[$id] = $close_button;
159  }
160  return $clone;
161  }
162 
166  public function withToolsButton(Button\Bulky $button): MainControls\MainBar
167  {
168  $clone = clone $this;
169  $clone->tools_button = $button;
170  return $clone;
171  }
172 
176  public function getToolsButton(): Button\Bulky
177  {
178  return $this->tools_button;
179  }
180 
184  public function getEntryClickSignal(): Signal
185  {
187  }
188 
192  public function getToolsClickSignal(): Signal
193  {
195  }
196 
200  public function getToolsRemovalSignal(): Signal
201  {
203  }
204 
208  public function getDisengageAllSignal(): Signal
209  {
211  }
212 
216  public function getToggleToolsSignal(): Signal
217  {
219  }
220 
224  protected function initSignals(): void
225  {
226  $this->entry_click_signal = $this->signal_generator->create();
227  $this->tools_click_signal = $this->signal_generator->create();
228  $this->tools_removal_signal = $this->signal_generator->create();
229  $this->disengage_all_signal = $this->signal_generator->create();
230  $this->disengage_all_signal->addOption('action', self::ENTRY_ACTION_DISENGAGE_ALL);
231  $this->toggle_tools_signal = $this->signal_generator->create();
232  $this->toggle_tools_signal->addOption('action', self::ENTRY_ACTION_TOGGLE_TOOLS);
233  }
234 
235  public function withResetSignals(): MainControls\MainBar
236  {
237  $clone = clone $this;
238  $clone->initSignals();
239  foreach (array_keys($this->tool_entries) as $tool_id) {
240  $this->tool_signals[$tool_id] = $this->signal_generator->create();
241  }
242  return $clone;
243  }
244 
248  public function getActive(): ?string
249  {
250  return $this->active;
251  }
252 
256  public function withActive(string $active): MainControls\MainBar
257  {
258  $valid_entries = array_merge(
259  array_keys($this->entries),
260  array_keys($this->tool_entries),
261  [self::NONE_ACTIVE]
262  );
263  if (!in_array($active, $valid_entries)) {
264  throw new InvalidArgumentException("Invalid entry to activate: $active", 1);
265  }
266 
267  $clone = clone $this;
268  $clone->active = $active;
269  return $clone;
270  }
271 
275  public function getInitiallyHiddenToolIds(): array
276  {
277  return array_unique($this->initially_hidden_ids);
278  }
279 
283  public function getEngageToolSignal(string $tool_id): Signal
284  {
285  return $this->tool_signals[$tool_id];
286  }
287 
291  public function getCloseButtons(): array
292  {
293  return $this->close_buttons;
294  }
295 
296 
297  public function withClearedEntries(): MainControls\MainBar
298  {
299  $clone = clone $this;
300  $clone->entries = [];
301  $clone->tool_entries = [];
302  return $clone;
303  }
304 
305  public function getTriggerSignal(
306  string $entry_id,
307  string $action
308  ): Signal {
309  if (!in_array($action, [self::ENTRY_ACTION_TRIGGER, self::ENTRY_ACTION_REMOVE])) {
310  throw new InvalidArgumentException("invalid action for mainbar entry: $action", 1);
311  }
312  $signal = $this->signal_generator->create();
313  $signal->addOption('entry_id', $entry_id);
314  $signal->addOption('action', $action);
315  return $signal;
316  }
317 
318  public function withMainBarTreePosition(string $tree_pos): MainBar
319  {
320  $clone = clone $this;
321  $clone->mainbar_tree_position = $tree_pos;
322  return $clone;
323  }
324 
325  public function withMappedSubNodes(callable $f): MainBar
326  {
327  $clone = clone $this;
328 
329  $counter = 0;
330  foreach ($clone->getEntries() as $k => $v) {
331  $clone->entries[$k] = $f($counter, $v, false);
332  $counter++;
333  }
334 
335  $counter = 0;
336  foreach ($clone->getToolEntries() as $k => $v) {
337  $clone->tool_entries[$k] = $f($counter, $v, true);
338  $counter++;
339  }
340 
341  return $clone;
342  }
343 }
This describes the MainBar.
Definition: MainBar.php:33
__construct(SignalGeneratorInterface $signal_generator)
Definition: MainBar.php:85
trait JavaScriptBindable
Trait for components implementing JavaScriptBindable providing standard implementation.
initSignals()
Set the signals for this component.
Definition: MainBar.php:224
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: Bulky.php:21
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
This describes a bulky button.
Definition: Bulky.php:28
getTriggerSignal(string $entry_id, string $action)
Definition: MainBar.php:305
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
$check
Definition: buildRTE.php:81
This describes a close button.
Definition: Close.php:33
withAdditionalToolEntry(string $id, Slate $entry, bool $initially_hidden=false, ?Button\Close $close_button=null)
Definition: MainBar.php:132