ILIAS  release_7 Revision v7.30-3-g800a261c036
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 
41  private $gs;
42 
46  private $lng;
47 
51  public function __construct()
52  {
53  global $DIC;
54  $this->gs = $DIC->globalScreen();
55  $this->lng = $DIC->language();
57  }
58 
63  protected function getSpecificComponentForItem(isItem $item) : Component
64  {
65  $f = $this->ui->factory();
66 
67  $center = $f->mainControls()->slate()->combined($this->lng->txt("noc"), $this->buildIcon($item))
68  ->withEngaged(false);
69 
70  foreach ($this->gs->collector()->notifications()->getNotifications() as $notification) {
71  $center = $center->withAdditionalEntry($notification->getRenderer($this->ui->factory())->getNotificationComponentForItem($notification));
72  }
73 
74  $center = $this->attachJSShowEvent($center);
75 
76  return $this->attachJSRerenderEvent($center);
77  }
78 
86  protected function attachJSShowEvent(Combined $center) : \ILIAS\UI\Component\MainControls\Slate\Combined
87  {
88  $toggle_signal = $center->getToggleSignal();
89  $url = ClientNotifications::NOTIFY_ENDPOINT . "?" . $this->buildShowQuery();
90 
91  return $center->withAdditionalOnLoadCode(
92  function ($id) use ($toggle_signal, $url) {
93  return "
94  $(document).on('$toggle_signal', function(event, signalData) {
95  $.ajax({url: '$url'});
96  });";
97  }
98  );
99  }
100 
107  protected function attachJSRerenderEvent(Combined $center) : \ILIAS\UI\Component\MainControls\Slate\Combined
108  {
109  $url = ClientNotifications::NOTIFY_ENDPOINT . "?" . $this->buildRerenderQuery();
110 
111  return $center->withAdditionalOnLoadCode(
112  function (string $id) use ($url) : string {
113  return "document.addEventListener('rerenderNotificationCenter', () => {
114  let xhr = new XMLHttpRequest();
115  xhr.open('GET', '$url');
116  xhr.onload = () => {
117  if (xhr.status === 200) {
118  let response = JSON.parse(xhr.responseText);
119  $id.querySelector('.il-maincontrols-slate-content').innerHTML = response.html;
120  $id.querySelectorAll('.il-maincontrols-slate-content script').forEach( element => {
121  eval(element.innerHTML);
122  })
123  $id.parentNode.previousElementSibling.querySelector('.glyph').outerHTML = response.symbol;
124  } else {
125  console.error(xhr.status + ': ' + xhr.responseText);
126  }
127  };
128  xhr.send();
129  });";
130  }
131  );
132  }
133 
137  protected function buildShowQuery() : string
138  {
139  return http_build_query([
140  ClientNotifications::MODE => ClientNotifications::MODE_OPENED,
141  ClientNotifications::NOTIFICATION_IDENTIFIERS => $this->gs->collector()->notifications()->getNotificationsIdentifiersAsArray(true),
142  ]);
143  }
144 
145  protected function buildRerenderQuery() : string
146  {
147  return http_build_query([ClientNotifications::MODE => ClientNotifications::MODE_RERENDER]);
148  }
149 }
Class Factory.
Class ChatMainBarProvider .
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: goto.php:24
ui()
Definition: ui.php:5
attachJSRerenderEvent(Combined $center)
Attaches on load code for re-rendering the notification center.
withAdditionalOnLoadCode(\Closure $binder)
Add some onload-code to the component instead of replacing the existing one.
__construct(Container $dic, ilPlugin $plugin)
$url
This describes the Combined Slate.
Definition: Combined.php:12