ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
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  $center = $this->attachJSRerenderEvent($center);
70 
71  return $center;
72  }
73 
81  protected function attachJSShowEvent(Combined $center): \ILIAS\UI\Component\MainControls\Slate\Combined
82  {
83  $toggle_signal = $center->getToggleSignal();
84  $url = ClientNotifications::NOTIFY_ENDPOINT . "?" . $this->buildShowQuery();
85 
86  $center = $center->withAdditionalOnLoadCode(
87  function ($id) use ($toggle_signal, $url) {
88  return "
89  $(document).on('$toggle_signal', function(event, signalData) {
90  $.ajax({url: '$url'});
91  });";
92  }
93  );
94 
95  return $center;
96  }
97 
104  protected function attachJSRerenderEvent(Combined $center): \ILIAS\UI\Component\MainControls\Slate\Combined
105  {
106  $url = ClientNotifications::NOTIFY_ENDPOINT . "?" . $this->buildRerenderQuery();
107 
108  return $center->withAdditionalOnLoadCode(
109  function (string $id) use ($url): string {
110  return "document.addEventListener('rerenderNotificationCenter', () => {
111  let xhr = new XMLHttpRequest();
112  xhr.open('GET', '$url');
113  xhr.onload = () => {
114  if (xhr.status === 200) {
115  let response = JSON.parse(xhr.responseText);
116  $id.querySelector('.il-maincontrols-slate-content').innerHTML = response.html;
117  $id.querySelectorAll('.il-maincontrols-slate-content script').forEach( element => {
118  eval(element.innerHTML);
119  })
120  $id.parentNode.previousElementSibling.querySelector('.glyph').outerHTML = response.symbol;
121  } else {
122  console.error(xhr.status + ': ' + xhr.responseText);
123  }
124  };
125  xhr.send();
126  });";
127  }
128  );
129  }
130 
134  protected function buildShowQuery(): string
135  {
136  return http_build_query([
137  ClientNotifications::MODE => ClientNotifications::MODE_OPENED,
138  ClientNotifications::NOTIFICATION_IDENTIFIERS => $this->gs->collector()->notifications()->getNotificationsIdentifiersAsArray(true),
139  ]);
140  }
141 
142  protected function buildRerenderQuery(): string
143  {
144  return http_build_query([ClientNotifications::MODE => ClientNotifications::MODE_RERENDER]);
145  }
146 }
Interface Observer Contains several chained tasks and infos about them.
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...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$url
Definition: shib_logout.php:63
getToggleSignal()
Signal that toggles the slate when triggered.
attachJSShowEvent(Combined $center)
Attaches on load code for communicating back, that the notification center has been opened...
global $DIC
Definition: shib_login.php:25
attachJSRerenderEvent(Combined $center)
Attaches on load code for re-rendering the notification center.
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:24
__construct(Container $dic, ilPlugin $plugin)
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