ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
Button.php
Go to the documentation of this file.
1 <?php
2 
3 /* Copyright (c) 2016 Richard Klees <richard.klees@concepts-and-training.de> Extended GPL, see docs/LICENSE */
4 
6 
7 use ILIAS\UI\Component as C;
12 
16 abstract class Button implements C\Button\Button
17 {
18  use ComponentHelper;
20  use Triggerer;
21 
25  protected $label;
26 
30  protected $action;
31 
35  protected $active = true;
36 
40  protected $aria_label;
41 
45  protected $aria_checked = false;
46 
47 
48  public function __construct($label, $action)
49  {
50  $this->checkStringArg("label", $label);
51  $this->checkStringOrSignalArg("action", $action);
52  $this->label = $label;
53  if (is_string($action)) {
54  $this->action = $action;
55  } else {
56  $this->action = null;
57  $this->setTriggeredSignal($action, "click");
58  }
59  }
60 
64  public function getLabel()
65  {
66  return $this->label;
67  }
68 
72  public function withLabel($label)
73  {
74  $this->checkStringArg("label", $label);
75  $clone = clone $this;
76  $clone->label = $label;
77  return $clone;
78  }
79 
83  public function getAction()
84  {
85  if ($this->action !== null) {
86  return $this->action;
87  }
88  return $this->getTriggeredSignalsFor("click");
89  }
90 
94  public function isActive()
95  {
96  return $this->active;
97  }
98 
102  public function withUnavailableAction()
103  {
104  $clone = clone $this;
105  $clone->active = false;
106  return $clone;
107  }
108 
112  public function withOnClick(Signal $signal)
113  {
114  $this->action = null;
115  return $this->withTriggeredSignal($signal, 'click');
116  }
117 
121  public function appendOnClick(Signal $signal)
122  {
123  return $this->appendTriggeredSignal($signal, 'click');
124  }
125 
129  public function withOnHover(Signal $signal)
130  {
131  // Note: The event 'hover' maps to 'mouseenter' in javascript. Although 'hover' is available in JQuery,
132  // it encodes the 'mouseenter' and 'mouseleave' events and thus expects two event handlers.
133  // In the context of this framework, the signal MUST only be triggered on the 'mouseenter' event.
134  // See also: https://api.jquery.com/hover/
135  return $this->withTriggeredSignal($signal, 'mouseenter');
136  }
137 
141  public function appendOnHover(Signal $signal)
142  {
143  return $this->appendTriggeredSignal($signal, 'mouseenter');
144  }
145 
149  public function withAriaLabel($aria_label)
150  {
151  $this->checkStringArg("label", $aria_label);
152  $clone = clone $this;
153  $clone->aria_label = $aria_label;
154  return $clone;
155  }
156 
160  public function getAriaLabel()
161  {
162  return $this->aria_label;
163  }
164 
168  public function withAriaChecked()
169  {
170  $clone = clone $this;
171  $clone->aria_checked = true;
172  return $clone;
173  }
174 
178  public function isAriaChecked()
179  {
180  return $this->aria_checked;
181  }
182 }
trait JavaScriptBindable
Trait for components implementing JavaScriptBindable providing standard implementation.
trait ComponentHelper
Provides common functionality for component implementations.
checkStringArg($which, $value)
Throw an InvalidArgumentException if $value is no string.
getTriggeredSignalsFor($event)
Get signals that are triggered for a certain event.
Definition: Triggerer.php:85
setTriggeredSignal(Component\Signal $signal, $event)
Add a triggered signal, replacing any othe signals registered on the same event.
Definition: Triggerer.php:65
appendTriggeredSignal(Component\Signal $signal, $event)
Append a triggered signal to other signals of the same event.
Definition: Triggerer.php:31
checkStringOrSignalArg($which, $value)
Throw an InvalidArgumentException if $value is no string or Signal.
withTriggeredSignal(Component\Signal $signal, $event)
Add a triggered signal, replacing any other signals registered on the same event. ...
Definition: Triggerer.php:48