ILIAS  release_7 Revision v7.30-3-g800a261c036
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
12
16abstract class Button implements C\Button\Button
17{
20 use Triggerer;
21 use Engageable;
22
26 protected $label;
27
31 protected $action;
32
36 protected $active = true;
37
41 protected $aria_label;
42
46 protected $aria_checked = false;
47
48
49 public function __construct($label, $action)
50 {
51 $this->checkStringArg("label", $label);
52 $this->checkStringOrSignalArg("action", $action);
53 $this->label = $label;
54 if (is_string($action)) {
55 $this->action = $action;
56 } else {
57 $this->action = null;
58 $this->setTriggeredSignal($action, "click");
59 }
60 }
61
65 public function getLabel()
66 {
67 return $this->label;
68 }
69
73 public function withLabel($label)
74 {
75 $this->checkStringArg("label", $label);
76 $clone = clone $this;
77 $clone->label = $label;
78 return $clone;
79 }
80
84 public function getAction()
85 {
86 if ($this->action !== null) {
87 return $this->action;
88 }
89 return $this->getTriggeredSignalsFor("click");
90 }
91
95 public function isActive()
96 {
97 return $this->active;
98 }
99
103 public function withUnavailableAction()
104 {
105 $clone = clone $this;
106 $clone->active = false;
107 return $clone;
108 }
109
113 public function withOnClick(Signal $signal)
114 {
115 $this->action = null;
116 return $this->withTriggeredSignal($signal, 'click');
117 }
118
122 public function appendOnClick(Signal $signal)
123 {
124 return $this->appendTriggeredSignal($signal, 'click');
125 }
126
130 public function withOnHover(Signal $signal)
131 {
132 // Note: The event 'hover' maps to 'mouseenter' in javascript. Although 'hover' is available in JQuery,
133 // it encodes the 'mouseenter' and 'mouseleave' events and thus expects two event handlers.
134 // In the context of this framework, the signal MUST only be triggered on the 'mouseenter' event.
135 // See also: https://api.jquery.com/hover/
136 return $this->withTriggeredSignal($signal, 'mouseenter');
137 }
138
142 public function appendOnHover(Signal $signal)
143 {
144 return $this->appendTriggeredSignal($signal, 'mouseenter');
145 }
146
150 public function withAriaLabel($aria_label)
151 {
152 $this->checkStringArg("label", $aria_label);
153 $clone = clone $this;
154 $clone->aria_label = $aria_label;
155 return $clone;
156 }
157
161 public function getAriaLabel()
162 {
163 return $this->aria_label;
164 }
165}
An exception for terminatinating execution or to throw for unit testing.
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
checkStringArg($which, $value)
Throw an InvalidArgumentException if $value is no string.
appendTriggeredSignal(Component\Signal $signal, $event)
Append a triggered signal to other signals of the same event.
Definition: Triggerer.php:31
setTriggeredSignal(Component\Signal $signal, $event)
Add a triggered signal, replacing any othe signals registered on the same event.
Definition: Triggerer.php:65
getTriggeredSignalsFor($event)
Get signals that are triggered for a certain event.
Definition: Triggerer.php:85
trait JavaScriptBindable
Trait for components implementing JavaScriptBindable providing standard implementation.
trait ComponentHelper
Provides common functionality for component implementations.