Skip to content

Commit

Permalink
UI/DataTable: Actions may open Prompts
Browse files Browse the repository at this point in the history
  • Loading branch information
nhaagen committed Oct 24, 2024
1 parent 4160451 commit acec51b
Show file tree
Hide file tree
Showing 14 changed files with 194 additions and 42 deletions.
2 changes: 1 addition & 1 deletion components/ILIAS/UI/resources/js/Table/dist/table.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 10 additions & 2 deletions components/ILIAS/UI/resources/js/Table/src/datatable.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ export default class DataTable {
*/
#responseContent;

/**
* @type {HTMLDivElement}
*/
#prompt;

/**
* @type {HTMLTableElement}
*/
Expand Down Expand Up @@ -93,6 +98,7 @@ export default class DataTable {
this.#modalResponseArea = this.#component.getElementsByClassName('c-table-data__async_modal_container').item(0);
this.#responseContainer = this.#component.getElementsByClassName('c-table-data__async_message').item(0);
this.#responseContent = this.#responseContainer.getElementsByClassName('c-table-data__async_messageresponse').item(0);
this.#prompt = this.#component.querySelector('.c-table-data__dialog_container .il-prompt');

this.#jquery = jquery;
this.#signalConstants = {
Expand All @@ -106,7 +112,7 @@ export default class DataTable {

/**
* @param {string} actionId
* @param {bool} async
* @param {string} async
* @param {URLBuilder} urlBuilder
* @param {Map} urlTokens
* @return {void}
Expand Down Expand Up @@ -210,8 +216,10 @@ export default class DataTable {
action.urlBuilder.writeParameter(token, rowIds);
const target = decodeURI(action.urlBuilder.getUrl().toString());

if (!action.async) {
if (action.async === 'none') {
window.location.href = target;
} else if (action.async === 'prompt') {
il.UI.prompt.get(this.#prompt.id).show(target);
} else {
this.asyncAction(target);
}
Expand Down
2 changes: 1 addition & 1 deletion components/ILIAS/UI/src/Component/Prompt/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ interface Factory
* ---
* @return \ILIAS\UI\Component\Prompt\Prompt
*/
public function standard(URI $async_url): Prompt;
public function standard(URI $async_url = null): Prompt;

/**
* ---
Expand Down
8 changes: 8 additions & 0 deletions components/ILIAS/UI/src/Component/Table/Action/Action.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,13 @@

interface Action extends \ILIAS\UI\Component\Component
{
/**
* call the Action asynchronously
*/
public function withAsync(bool $async = true): self;

/**
* open a Prompt with an async call to the Actions's target
*/
public function withPrompt(bool $prompt = true): self;
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function __construct(
) {
}

public function standard(URI $async_url): I\Prompt
public function standard(URI $async_url = null): I\Prompt
{
return new Standard($this->signal_generator, $async_url);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
use ILIAS\UI\Implementation\Component\ComponentHelper;
use ILIAS\UI\Implementation\Component\JavaScriptBindable;
use ILIAS\UI\Implementation\Component\SignalGenerator;

use ILIAS\Data\URI;

/**
Expand All @@ -41,13 +40,13 @@ abstract class Prompt implements I\Prompt

public function __construct(
SignalGenerator $signal_generator,
protected URI $async_url
protected ?URI $async_url
) {
$this->show_signal = $signal_generator->create();
$this->close_signal = $signal_generator->create();
}

public function getAsyncUrl(): URI
public function getAsyncUrl(): ?URI
{
return $this->async_url;
}
Expand All @@ -56,7 +55,7 @@ public function getShowSignal(?URI $uri = null): Signal
{
$target = $uri ?? $this->async_url;
$signal = clone $this->show_signal;
$signal->addOption('url', $target->__toString());
$signal->addOption('url', $target ? $target->__toString() : '#');
return $signal;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ protected function renderPrompt(Component\Prompt\Prompt $component, RendererInte
$tpl = $this->getTemplate('tpl.prompt.html', true, true);
$show_signal = $component->getShowSignal();
$close_signal = $component->getCloseSignal();
$url = $component->getAsyncUrl()->__toString();
$url = $component->getAsyncUrl()?->__toString();
$component = $component->withAdditionalOnLoadCode(
fn($id) => "
il.UI.prompt.init('$id');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ abstract class Action implements I\Action

protected Signal|URI $target;
protected bool $async = false;
protected bool $prompt = false;

public function __construct(
protected string $label,
Expand Down Expand Up @@ -68,6 +69,9 @@ public function withSignalTarget(Signal $target): self

public function withAsync(bool $async = true): self
{
if ($this->isPrompt()) {
throw new \LogicException("Action is configured to use Prompt already. Use either Async or Prompt", 1);
}
$clone = clone $this;
$clone->async = $async;
return $clone;
Expand All @@ -78,6 +82,21 @@ public function isAsync(): bool
return $this->async;
}

public function withPrompt(bool $prompt = true): self
{
if ($this->isAsync()) {
throw new \LogicException("Action is async already. Use either Async or Prompt", 1);
}
$clone = clone $this;
$clone->prompt = $prompt;
return $clone;
}

public function isPrompt(): bool
{
return $this->prompt;
}

public function withRowId(string $row_id): self
{
$clone = clone $this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,10 @@ public function renderDataTable(Component\Table\Data $component, RendererInterfa
$tpl->setVariable('COL_COUNT', (string) $component->getColumnCount());
$tpl->setVariable('VIEW_CONTROLS', $default_renderer->render($view_controls));

$tpl->setVariable('DIALOG', $default_renderer->render(
$this->getUIFactory()->prompt()->standard()
));

$sortation_signal = null;
// if the generator is empty, and thus invalid, we render an empty row.
if (!$rows->valid()) {
Expand Down Expand Up @@ -369,7 +373,7 @@ protected function registerActions(Component\Table\Table $component): Component\
$actions = [];
foreach ($component->getAllActions() as $action_id => $action) {
$component = $component->withAdditionalOnLoadCode($this->getActionRegistration((string) $action_id, $action));
if ($action->isAsync()) {
if ($action->isAsync() || $action->isPrompt()) {
$signal = clone $component->getAsyncActionSignal();
$signal->addOption(Action::OPT_ACTIONID, $action_id);
$action = $action->withSignalTarget($signal);
Expand Down Expand Up @@ -503,13 +507,19 @@ protected function getActionRegistration(
string $action_id,
Action $action
): \Closure {
$async = $action->isAsync() ? 'true' : 'false';
$async = 'none';
if ($action->isAsync()) {
$async = 'async';
}
if ($action->isPrompt()) {
$async = 'prompt';
}
$url_builder_js = $action->getURLBuilderJS();
$tokens_js = $action->getURLBuilderTokensJS();

return static function ($id) use ($action_id, $async, $url_builder_js, $tokens_js): string {
return "
il.UI.table.data.get('{$id}').registerAction('{$action_id}', {$async}, {$url_builder_js}, {$tokens_js});
il.UI.table.data.get('{$id}').registerAction('{$action_id}', '{$async}', {$url_builder_js}, {$tokens_js});
";
};
}
Expand Down
Loading

0 comments on commit acec51b

Please sign in to comment.