ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
NotificationCenterRenderer.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
23 use ILIAS\GlobalScreen\Client\Notifications as ClientNotifications;
29 
35 {
36  use isSupportedTrait;
37 
38  private \ILIAS\GlobalScreen\Services $gs;
39 
40  private \ilLanguage $lng;
41 
45  public function __construct()
46  {
47  global $DIC;
48  $this->gs = $DIC->globalScreen();
49  $this->lng = $DIC->language();
51  }
52 
57  protected function getSpecificComponentForItem(isItem $item): Component
58  {
59  $f = $this->ui->factory();
60 
61  $center = $f->mainControls()->slate()->combined($this->lng->txt("noc"), $this->buildIcon($item))
62  ->withEngaged(false);
63 
64  foreach ($this->gs->collector()->notifications()->getNotifications() as $notification) {
65  $center = $center->withAdditionalEntry($notification->getRenderer($this->ui->factory())->getNotificationComponentForItem($notification));
66  }
67 
68  $center = $this->attachJSShowEvent($center);
69 
70  return $this->attachJSRerenderEvent($center);
71  }
72 
80  protected function attachJSShowEvent(Combined $center): \ILIAS\UI\Component\MainControls\Slate\Combined
81  {
82  $toggle_signal = $center->getToggleSignal();
83  $url = ClientNotifications::NOTIFY_ENDPOINT . "?" . $this->buildShowQuery();
84 
85  return $center->withAdditionalOnLoadCode(
86  function ($id) use ($toggle_signal, $url) {
87  return "
88  $(document).on('$toggle_signal', function(event, signalData) {
89  $.ajax({url: '$url'});
90  });";
91  }
92  );
93  }
94 
101  protected function attachJSRerenderEvent(Combined $center): \ILIAS\UI\Component\MainControls\Slate\Combined
102  {
103  $url = ClientNotifications::NOTIFY_ENDPOINT . "?" . $this->buildRerenderQuery();
104 
105  return $center->withAdditionalOnLoadCode(
106  function (string $id) use ($url): string {
107  return "document.addEventListener('rerenderNotificationCenter', () => {
108  let xhr = new XMLHttpRequest();
109  xhr.open('GET', '$url');
110  xhr.onload = () => {
111  if (xhr.status === 200) {
112  let response = JSON.parse(xhr.responseText);
113  $id.querySelector('.il-maincontrols-slate-content').innerHTML = response.html;
114  $id.querySelectorAll('.il-maincontrols-slate-content script').forEach( element => {
115  eval(element.innerHTML);
116  })
117  $id.parentNode.previousElementSibling.querySelector('.glyph').outerHTML = response.symbol;
118  } else {
119  console.error(xhr.status + ': ' + xhr.responseText);
120  }
121  };
122  xhr.send();
123  });";
124  }
125  );
126  }
127 
131  protected function buildShowQuery(): string
132  {
133  return http_build_query([
134  ClientNotifications::MODE => ClientNotifications::MODE_OPENED,
135  ClientNotifications::NOTIFICATION_IDENTIFIERS => $this->gs->collector()->notifications()->getNotificationsIdentifiersAsArray(true),
136  ]);
137  }
138 
139  protected function buildRerenderQuery(): string
140  {
141  return http_build_query([ClientNotifications::MODE => ClientNotifications::MODE_RERENDER]);
142  }
143 }
Class Factory.
Class ChatMainBarProvider .
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
getToggleSignal()
Signal that toggles the slate when triggered.
global $DIC
Definition: feed.php:28
attachJSShowEvent(Combined $center)
Attaches on load code for communicating back, that the notification center has been opened...
attachJSRerenderEvent(Combined $center)
Attaches on load code for re-rendering the notification center.
__construct(Container $dic, ilPlugin $plugin)
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
$url
withAdditionalOnLoadCode(Closure $binder)
Add some onload-code to the component instead of replacing the existing one.
This describes the Combined Slate.
Definition: Combined.php:29