Skip to content

Commit

Permalink
implements Toast container and set dependency in standard toast (#4059)
Browse files Browse the repository at this point in the history
  • Loading branch information
iszmais authored Mar 15, 2022
1 parent 131b3f0 commit a284e26
Show file tree
Hide file tree
Showing 25 changed files with 482 additions and 108 deletions.
3 changes: 2 additions & 1 deletion src/UI/Component/Toast/Toast.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use ILIAS\UI\Component\Button\Shy;
use ILIAS\UI\Component\Component;
use ILIAS\UI\Component\JavaScriptBindable;
use ILIAS\UI\Component\Link\Link;
use ILIAS\UI\Component\Signal;
use ILIAS\UI\Component\Symbol\Icon\Icon;
Expand All @@ -13,7 +14,7 @@
* Interface Toast
* @package ILIAS\UI\Component\Toast
*/
interface Toast extends Component
interface Toast extends Component, JavaScriptBindable
{
/**
* @return string|Shy|Link
Expand Down
48 changes: 48 additions & 0 deletions src/UI/Implementation/Component/Toast/Container.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php declare(strict_types=1);

namespace ILIAS\UI\Implementation\Component\Toast;

use ILIAS\UI\Implementation\Component\ComponentHelper;
use ILIAS\UI\Component\Toast as ComponentInterface;

/******************************************************************************
*
* This file is part of ILIAS, a powerful learning management system.
*
* ILIAS is licensed with the GPL-3.0, you should have received a copy
* of said license along with the source code.
*
* If this is not the case or you just want to try ILIAS, you'll find
* us at:
* https://www.ilias.de
* https://github.com/ILIAS-eLearning
*
*****************************************************************************/
class Container implements ComponentInterface\Container
{
use ComponentHelper;

/**
* @var Toast[] $toasts
*/
protected array $toasts = [];

public function getToasts() : array
{
return $this->toasts;
}

public function withAdditionalToast(ComponentInterface\Toast $toast) : Container
{
$clone = clone $this;
$clone->toasts[] = $toast;
return $clone;
}

public function withoutToasts() : Container
{
$clone = clone $this;
$clone->toasts = [];
return $clone;
}
}
3 changes: 1 addition & 2 deletions src/UI/Implementation/Component/Toast/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace ILIAS\UI\Implementation\Component\Toast;

use ILIAS\UI\Component\Symbol\Icon\Icon;
use ILIAS\UI\Component\Toast\Container;
use ILIAS\UI\Implementation\Component\SignalGeneratorInterface;

class Factory implements \ILIAS\UI\Component\Toast\Factory
Expand All @@ -25,6 +24,6 @@ public function standard($title, Icon $icon) : Toast

public function container() : Container
{
throw new \ILIAS\UI\NotImplementedException();
return new Container();
}
}
31 changes: 29 additions & 2 deletions src/UI/Implementation/Component/Toast/Renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace ILIAS\UI\Implementation\Component\Toast;

use ilException;
use ILIAS\UI\Component\Button\Shy;
use ILIAS\UI\Component\Link\Link;
use ILIAS\UI\Implementation\Render\AbstractComponentRenderer;
Expand All @@ -23,6 +22,9 @@ public function render(Component\Component $component, RendererInterface $defaul
if ($component instanceof Component\Toast\Toast) {
return $this->renderToast($component, $default_renderer);
}
if ($component instanceof Component\Toast\Container) {
return $this->renderContainer($component, $default_renderer);
}

throw new LogicException("Cannot render: " . get_class($component));
}
Expand Down Expand Up @@ -62,16 +64,41 @@ protected function renderToast(Component\Toast\Toast $component, RendererInterfa
$tpl->setVariable("ICON", $default_renderer->render($component->getIcon()));
$tpl->setVariable("CLOSE", $default_renderer->render($this->getUIFactory()->button()->close()));

$component = $component->withAdditionalOnLoadCode(function ($id) {
return "
il.UI.toast.setToastSettings($id);
il.UI.toast.showToast($id);
";
});

$tpl->setCurrentBlock("id");
$tpl->setVariable('ID', $this->bindJavaScript($component));
$tpl->parseCurrentBlock();

return $tpl->get();
}

protected function renderContainer(Component\Toast\Container $component, RendererInterface $default_renderer) : string
{
$tpl = $this->getTemplate("tpl.container.html", true, true);
$tpl->setVariable("TOASTS", $default_renderer->render($component->getToasts()));
return $tpl->get();
}

public function registerResources(ResourceRegistry $registry) : void
{
parent::registerResources($registry);
$registry->register('./src/UI/templates/js/Toast/toast.js');
}

/**
* @inheritdoc
*/
protected function getComponentInterfaceName() : array
{
return [
Component\Toast\Toast::class
Component\Toast\Toast::class,
Component\Toast\Container::class
];
}
}
3 changes: 3 additions & 0 deletions src/UI/Implementation/Component/Toast/Toast.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@
use ILIAS\UI\Component\Symbol\Icon\Icon;
use ILIAS\UI\Implementation\Component\ComponentHelper;
use ILIAS\UI\Component\Toast as ComponentInterface;
use ILIAS\UI\Implementation\Component\JavaScriptBindable;
use ILIAS\UI\Implementation\Component\SignalGeneratorInterface;

class Toast implements ComponentInterface\Toast
{
use ComponentHelper;
use JavaScriptBindable;

public const DEFAULT_VANISH_TIME_IN_MS = 5000;
public const DEFAULT_DELAY_TIME_IN_MS = 500;

Expand Down
18 changes: 17 additions & 1 deletion src/UI/examples/Toast/Container/base.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,21 @@ function base() : string
{
global $DIC;
$tc = $DIC->ui()->factory()->toast()->container();
return $DIC->ui()->renderer()->render($tc);

$toasts = [];

$toasts = base64_encode($DIC->ui()->renderer()->renderAsync($toasts));
$button = $DIC->ui()->factory()->button()->standard($DIC->language()->txt('show'), '');
$button = $button->withAdditionalOnLoadCode(function ($id) use ($toasts) {
return "$id.addEventListener('click', () => {
$id.parentNode.querySelector('.il-toast-container').innerHTML = atob('$toasts');
$id.parentNode.querySelector('.il-toast-container').querySelectorAll('script').forEach(element => {
let newScript = document.createElement('script');
newScript.innerHTML = element.innerHTML;
element.parentNode.appendChild(newScript);
})
});";
});

return $DIC->ui()->renderer()->render([$button,$tc]);
}
48 changes: 30 additions & 18 deletions src/UI/examples/Toast/Container/with_multiple_toasts.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,35 @@
function with_multiple_toasts() : string
{
global $DIC;
$tc = $DIC->ui()->factory()->toast()->container()
->withAdditionalToast(
$DIC->ui()->factory()->toast()->standard(
'Example 1',
$DIC->ui()->factory()->symbol()->icon()->standard('info', 'Test')
)
)->withAdditionalToast(
$DIC->ui()->factory()->toast()->standard(
'Example 2',
$DIC->ui()->factory()->symbol()->icon()->standard('info', 'Test')
)
)->withAdditionalToast(
$DIC->ui()->factory()->toast()->standard(
'Example 3',
$DIC->ui()->factory()->symbol()->icon()->standard('info', 'Test')
)
);
$tc = $DIC->ui()->factory()->toast()->container();

return $DIC->ui()->renderer()->render($tc);
$toasts = [
$DIC->ui()->factory()->toast()->standard(
'Example 1',
$DIC->ui()->factory()->symbol()->icon()->standard('mail', 'Test')->withIsOutlined(true)
),
$DIC->ui()->factory()->toast()->standard(
'Example 2',
$DIC->ui()->factory()->symbol()->icon()->standard('mail', 'Test')->withIsOutlined(true)
),
$DIC->ui()->factory()->toast()->standard(
'Example 3',
$DIC->ui()->factory()->symbol()->icon()->standard('mail', 'Test')->withIsOutlined(true)
)
];

$toasts = base64_encode($DIC->ui()->renderer()->renderAsync($toasts));
$button = $DIC->ui()->factory()->button()->standard($DIC->language()->txt('show'), '');
$button = $button->withAdditionalOnLoadCode(function ($id) use ($toasts) {
return "$id.addEventListener('click', () => {
$id.parentNode.querySelector('.il-toast-container').innerHTML = atob('$toasts');
$id.parentNode.querySelector('.il-toast-container').querySelectorAll('script').forEach(element => {
let newScript = document.createElement('script');
newScript.innerHTML = element.innerHTML;
element.parentNode.appendChild(newScript);
})
});";
});

return $DIC->ui()->renderer()->render([$button,$tc]);
}
21 changes: 18 additions & 3 deletions src/UI/examples/Toast/Container/with_toast.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,27 @@
function with_toast() : string
{
global $DIC;
$tc = $DIC->ui()->factory()->toast()->container()->withAdditionalToast(
$tc = $DIC->ui()->factory()->toast()->container();

$toasts = [
$DIC->ui()->factory()->toast()->standard(
'Example',
$DIC->ui()->factory()->symbol()->icon()->standard('info', 'Test')
)
);
];

$toasts = base64_encode($DIC->ui()->renderer()->renderAsync($toasts));
$button = $DIC->ui()->factory()->button()->standard($DIC->language()->txt('show'), '');
$button = $button->withAdditionalOnLoadCode(function ($id) use ($toasts) {
return "$id.addEventListener('click', () => {
$id.parentNode.querySelector('.il-toast-container').innerHTML = atob('$toasts');
$id.parentNode.querySelector('.il-toast-container').querySelectorAll('script').forEach(element => {
let newScript = document.createElement('script');
newScript.innerHTML = element.innerHTML;
element.parentNode.appendChild(newScript);
})
});";
});

return $DIC->ui()->renderer()->render($tc);
return $DIC->ui()->renderer()->render([$button,$tc]);
}
21 changes: 18 additions & 3 deletions src/UI/examples/Toast/Standard/base.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,27 @@
function base() : string
{
global $DIC;
$tc = $DIC->ui()->factory()->toast()->container()->withAdditionalToast(
$tc = $DIC->ui()->factory()->toast()->container();

$toasts = [
$DIC->ui()->factory()->toast()->standard(
'Example',
$DIC->ui()->factory()->symbol()->icon()->standard('info', 'Test')
)
);
];

$toasts = base64_encode($DIC->ui()->renderer()->renderAsync($toasts));
$button = $DIC->ui()->factory()->button()->standard($DIC->language()->txt('show'), '');
$button = $button->withAdditionalOnLoadCode(function ($id) use ($toasts) {
return "$id.addEventListener('click', () => {
$id.parentNode.querySelector('.il-toast-container').innerHTML = atob('$toasts');
$id.parentNode.querySelector('.il-toast-container').querySelectorAll('script').forEach(element => {
let newScript = document.createElement('script');
newScript.innerHTML = element.innerHTML;
element.parentNode.appendChild(newScript);
})
});";
});

return $DIC->ui()->renderer()->render($tc);
return $DIC->ui()->renderer()->render([$button,$tc]);
}
22 changes: 19 additions & 3 deletions src/UI/examples/Toast/Standard/with_action.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,27 @@
function with_action() : string
{
global $DIC;
$tc = $DIC->ui()->factory()->toast()->container()->withAdditionalToast(
$tc = $DIC->ui()->factory()->toast()->container();

$toasts = [
$DIC->ui()->factory()->toast()->standard(
'Example',
$DIC->ui()->factory()->symbol()->icon()->standard('info', 'Test')
)->withAction('https://www.ilias.de')
);
return $DIC->ui()->renderer()->render($tc);
];

$toasts = base64_encode($DIC->ui()->renderer()->renderAsync($toasts));
$button = $DIC->ui()->factory()->button()->standard($DIC->language()->txt('show'), '');
$button = $button->withAdditionalOnLoadCode(function ($id) use ($toasts) {
return "$id.addEventListener('click', () => {
$id.parentNode.querySelector('.il-toast-container').innerHTML = atob('$toasts');
$id.parentNode.querySelector('.il-toast-container').querySelectorAll('script').forEach(element => {
let newScript = document.createElement('script');
newScript.innerHTML = element.innerHTML;
element.parentNode.appendChild(newScript);
})
});";
});

return $DIC->ui()->renderer()->render([$button,$tc]);
}
26 changes: 21 additions & 5 deletions src/UI/examples/Toast/Standard/with_additional_links.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,29 @@
function with_additional_links() : string
{
global $DIC;
$tc = $DIC->ui()->factory()->toast()->container()->withAdditionalToast(
$tc = $DIC->ui()->factory()->toast()->container();

$toasts = [
$DIC->ui()->factory()->toast()->standard(
'Example',
$DIC->ui()->factory()->symbol()->icon()->standard('info', 'Example')
)
->withAdditionalLink($DIC->ui()->factory()->link()->standard('ILIAS', 'https://www.ilias.de'))
->withAdditionalLink($DIC->ui()->factory()->link()->standard('GitHub', 'https://www.github.com'))
);
return $DIC->ui()->renderer()->render($tc);
->withAdditionalLink($DIC->ui()->factory()->link()->standard('ILIAS', 'https://www.ilias.de'))
->withAdditionalLink($DIC->ui()->factory()->link()->standard('GitHub', 'https://www.github.com'))
];

$toasts = base64_encode($DIC->ui()->renderer()->renderAsync($toasts));
$button = $DIC->ui()->factory()->button()->standard($DIC->language()->txt('show'), '');
$button = $button->withAdditionalOnLoadCode(function ($id) use ($toasts) {
return "$id.addEventListener('click', () => {
$id.parentNode.querySelector('.il-toast-container').innerHTML = atob('$toasts');
$id.parentNode.querySelector('.il-toast-container').querySelectorAll('script').forEach(element => {
let newScript = document.createElement('script');
newScript.innerHTML = element.innerHTML;
element.parentNode.appendChild(newScript);
})
});";
});

return $DIC->ui()->renderer()->render([$button,$tc]);
}
22 changes: 19 additions & 3 deletions src/UI/examples/Toast/Standard/with_description.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,27 @@
function with_description() : string
{
global $DIC;
$tc = $DIC->ui()->factory()->toast()->container()->withAdditionalToast(
$tc = $DIC->ui()->factory()->toast()->container();

$toasts = [
$DIC->ui()->factory()->toast()->standard(
'Example',
$DIC->ui()->factory()->symbol()->icon()->standard('info', 'Test')
)->withDescription('This is an example description.')
);
return $DIC->ui()->renderer()->render($tc);
];

$toasts = base64_encode($DIC->ui()->renderer()->renderAsync($toasts));
$button = $DIC->ui()->factory()->button()->standard($DIC->language()->txt('show'), '');
$button = $button->withAdditionalOnLoadCode(function ($id) use ($toasts) {
return "$id.addEventListener('click', () => {
$id.parentNode.querySelector('.il-toast-container').innerHTML = atob('$toasts');
$id.parentNode.querySelector('.il-toast-container').querySelectorAll('script').forEach(element => {
let newScript = document.createElement('script');
newScript.innerHTML = element.innerHTML;
element.parentNode.appendChild(newScript);
})
});";
});

return $DIC->ui()->renderer()->render([$button,$tc]);
}
Loading

0 comments on commit a284e26

Please sign in to comment.