ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
NotificationCenterRenderer.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
25use ILIAS\GlobalScreen\Client\Notifications as ClientNotifications;
26use ILIAS\GlobalScreen\Collector\Renderer\isSupportedTrait;
31
37{
38 use isSupportedTrait;
39
40 private Services $gs;
41
42 private \ilLanguage $lng;
43
47 public function __construct()
48 {
49 global $DIC;
50 $this->gs = $DIC->globalScreen();
51 $this->lng = $DIC->language();
53 }
54
59 protected function getSpecificComponentForItem(isItem $item): Component
60 {
61 $f = $this->ui->factory();
62
63 $center = $f->mainControls()->slate()->combined($this->lng->txt("noc"), $this->buildIcon($item))
64 ->withEngaged(false);
65
66 foreach ($this->gs->collector()->notifications()->getNotifications() as $notification) {
67 $center = $center->withAdditionalEntry($notification->getRenderer($this->ui->factory())->getNotificationComponentForItem($notification));
68 }
69
70 $center = $this->attachJSShowEvent($center);
71 $center = $this->attachJSRerenderEvent($center);
72
73 return $center;
74 }
75
83 protected function attachJSShowEvent(Combined $center): Combined
84 {
85 $toggle_signal = $center->getToggleSignal();
86 $url = ClientNotifications::NOTIFY_ENDPOINT . "?" . $this->buildShowQuery();
87
88 $center = $center->withAdditionalOnLoadCode(
89 fn($id): string => "
90 $(document).on('$toggle_signal', function(event, signalData) {
91 $.ajax({url: '$url'});
92 });"
93 );
94
95 return $center;
96 }
97
104 protected function attachJSRerenderEvent(Combined $center): Combined
105 {
106 $url = ClientNotifications::NOTIFY_ENDPOINT . "?" . $this->buildRerenderQuery();
107
108 return $center->withAdditionalOnLoadCode(
109 fn(string $id): string => "document.addEventListener('rerenderNotificationCenter', () => {
110 let xhr = new XMLHttpRequest();
111 xhr.open('GET', '$url');
112 xhr.onload = () => {
113 if (xhr.status === 200) {
114 let response = JSON.parse(xhr.responseText);
115 $id.querySelector('.il-maincontrols-slate-content').innerHTML = response.html;
116 $id.querySelectorAll('.il-maincontrols-slate-content script').forEach( element => {
117 eval(element.innerHTML);
118 })
119 $id.parentNode.previousElementSibling.querySelector('.glyph').outerHTML = response.symbol;
120 } else {
121 console.error(xhr.status + ': ' + xhr.responseText);
122 }
123 };
124 xhr.send();
125 });"
126 );
127 }
128
132 protected function buildShowQuery(): string
133 {
134 return http_build_query([
135 ClientNotifications::MODE => ClientNotifications::MODE_OPENED,
136 ClientNotifications::NOTIFICATION_IDENTIFIERS => $this->gs->collector()->notifications()->getNotificationsIdentifiersAsArray(true),
137 ]);
138 }
139
140 protected function buildRerenderQuery(): string
141 {
142 return http_build_query([ClientNotifications::MODE => ClientNotifications::MODE_RERENDER]);
143 }
144}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
attachJSRerenderEvent(Combined $center)
Attaches on load code for re-rendering the notification center.
attachJSShowEvent(Combined $center)
Attaches on load code for communicating back, that the notification center has been opened.
A component is the most general form of an entity in the UI.
Definition: Component.php:28
withAdditionalOnLoadCode(Closure $binder)
Add some onload-code to the component instead of replacing the existing one.
This describes the Combined Slate.
Definition: Combined.php:30
getToggleSignal()
Signal that toggles the slate when triggered.
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
trait JavaScriptBindable
Trait for components implementing JavaScriptBindable providing standard implementation.
global $DIC
Definition: shib_login.php:26
$url
Definition: shib_logout.php:68