ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ButtonAdapterGUI.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
25
30{
31 protected const TYPE_STD = 0;
32 protected const TYPE_SUBMIT = 1;
33 protected const TYPE_STD_PRIMARY = 2;
37 protected bool $is_primary = false;
38 protected string $on_click = "";
39 protected int $type;
40
41 protected Button $button;
42 protected \ilToolbarGUI $toolbar;
43 protected \ILIAS\DI\UIServices $ui;
44 protected string $caption = "";
45 protected string $cmd = "";
46 protected bool $disabled = false;
47
48 public function __construct(
49 string $caption,
50 string $cmd
51 ) {
52 global $DIC;
53
54 $this->caption = $caption;
55 $this->cmd = $cmd;
56 $this->ui = $DIC->ui();
57 $this->toolbar = $DIC->toolbar();
58 $this->type = self::TYPE_STD;
59 $this->on_click = "";
60 }
61
62 public function submit(): self
63 {
64 $this->type = self::TYPE_SUBMIT;
65 return $this;
66 }
67
68 public function primary(): self
69 {
70 $this->type = self::TYPE_STD_PRIMARY;
71 $this->is_primary = true;
72 return $this;
73 }
74
75 public function onClick(string $on_click): self
76 {
77 $this->on_click = $on_click;
78 return $this;
79 }
80
81 public function disabled(bool $disabled): self
82 {
83 $this->disabled = $disabled;
84 return $this;
85 }
86
88 {
90 if ($this->is_primary) {
91 $b = $this->ui->factory()->button()->primary(
92 $this->caption,
93 ""
94 );
95 } else {
96 $b = $this->ui->factory()->button()->standard(
97 $this->caption,
98 ""
99 );
100 }
101 $b = $b->withOnLoadCode(function ($id) use ($cmd) {
102 $code = <<<EOT
103(function() {
104 const el = document.getElementById('$id');
105 el.name = "cmd[$cmd]";
106 el.type = "submit";
107}());
108EOT;
109 return $code;
110 });
111 return $b;
112 }
113
115 {
116 return $this->ui->factory()->button()->standard(
117 $this->caption,
118 $this->cmd
119 );
120 }
121
123 {
124 return $this->ui->factory()->button()->primary(
125 $this->caption,
126 $this->cmd
127 );
128 }
129
130 protected function getButton(): \ILIAS\UI\Component\Button\Button
131 {
132 switch ($this->type) {
134 $button = $this->getSubmitButton();
135 break;
136
138 $button = $this->getStdPrimaryButton();
139 break;
140
141 default:
142 $button = $this->getStandardButton();
143 break;
144 }
145 if ($this->on_click !== "") {
146 $click = $this->on_click;
147 $button = $button->withOnLoadCode(function ($id) use ($click) {
148 $code = <<<EOT
149(function() {
150 const el = document.getElementById('$id').addEventListener('click', () => { $click });
151}());
152EOT;
153 return $code;
154 });
155 }
156
157 if ($this->disabled) {
158 $button = $button->withUnavailableAction();
159 }
160
161 return $button;
162 }
163
164 public function toToolbar(bool $sticky = false, ?\ilToolbarGUI $toolbar = null): void
165 {
166 $button = $this->getButton();
167 if (is_null($toolbar)) {
169 }
170 if ($sticky) {
172 } else {
174 }
175 }
176
177 public function render(): string
178 {
179 return $this->ui->renderer()->render($this->getButton());
180 }
181}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
toToolbar(bool $sticky=false, ?\ilToolbarGUI $toolbar=null)
Definition: UI.php:24
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
addComponent(\ILIAS\UI\Component\Component $a_comp)
addStickyItem( $a_item, bool $a_output_label=false)
Add a sticky item.
This describes commonalities between standard and primary buttons.
Definition: Button.php:34
withUnavailableAction(bool $flag=true)
Get a button like this, but action should be unavailable atm.
This describes a standard button.
Definition: Standard.php:27
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: Bulky.php:21
Interface Observer \BackgroundTasks Contains several chained tasks and infos about them.
global $DIC
Definition: shib_login.php:26