ILIAS  trunk Revision v11.0_alpha-1851-ga8564da6fed
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
Renderer.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
27 
35 {
39  public function render(Component\Component $component, RendererInterface $default_renderer): string
40  {
41  if (!$component instanceof Component\Popover\Popover) {
42  $this->cannotHandleComponent($component);
43  }
44 
45  $tpl = $this->getTemplate('tpl.popover.html', true, true);
46  $tpl->setVariable('FORCE_RENDERING', '');
47 
48  $replacement = array(
49  '"' => '\"',
50  "\n" => "",
51  "\t" => "",
52  "\r" => "",
53  );
54 
55  $options = array(
56  'title' => $this->escape($component->getTitle()),
57  'placement' => $component->getPosition(),
58  'multi' => true,
59  'template' => str_replace(array_keys($replacement), array_values($replacement), $tpl->get()),
60  );
61 
62  if ($component->isFixedPosition()) {
63  $options['style'] = "fixed";
64  }
65 
66  $is_async = $component->getAsyncContentUrl();
67  if ($is_async) {
68  $options['type'] = 'async';
69  $options['url'] = $component->getAsyncContentUrl();
70  }
71 
72  $show = $component->getShowSignal();
73  $replace = $component->getReplaceContentSignal();
74 
75  $component = $component->withAdditionalOnLoadCode(function ($id) use ($options, $show, $replace, $is_async) {
76  if (!$is_async) {
77  $options["url"] = "#$id";
78  }
79  $options = json_encode($options);
80 
81  return
82  "$(document).on('$show', function(event, signalData) {
83  il.UI.popover.showFromSignal(signalData, JSON.parse('$options'));
84  });" .
85  "$(document).on('$replace', function(event, signalData) {
86  il.UI.popover.replaceContentFromSignal('$show', signalData);
87  });";
88  });
89 
90  $id = $this->bindJavaScript($component);
91 
92  if ($component->getAsyncContentUrl()) {
93  return '';
94  }
95 
96  if ($component instanceof Component\Popover\Standard) {
97  return $this->renderStandardPopover($component, $default_renderer, $id);
98  } elseif ($component instanceof Component\Popover\Listing) {
99  return $this->renderListingPopover($component, $default_renderer, $id);
100  }
101 
102  $this->cannotHandleComponent($component);
103  }
104 
108  public function registerResources(ResourceRegistry $registry): void
109  {
110  parent::registerResources($registry);
111  $registry->register('assets/js/jquery.webui-popover.min.js');
112  $registry->register('assets/js/popover.js');
113  }
114 
115  protected function renderStandardPopover(
116  Component\Popover\Standard $popover,
117  RendererInterface $default_renderer,
118  string $id
119  ): string {
120  $tpl = $this->getTemplate('tpl.standard-popover-content.html', true, true);
121  $tpl->setVariable('ID', $id);
122  $tpl->setVariable('CONTENT', $default_renderer->render($popover->getContent()));
123 
124  return $tpl->get();
125  }
126 
127  protected function renderListingPopover(
128  Component\Popover\Listing $popover,
129  RendererInterface $default_renderer,
130  string $id
131  ): string {
132  $tpl = $this->getTemplate('tpl.listing-popover-content.html', true, true);
133  $tpl->setVariable('ID', $id);
134  foreach ($popover->getItems() as $item) {
135  $tpl->setCurrentBlock('item');
136  $tpl->setVariable('ITEM', $default_renderer->render($item));
137  $tpl->parseCurrentBlock();
138  }
139 
140  return $tpl->get();
141  }
142 
143  protected function escape(string $str): string
144  {
145  return strip_tags(htmlentities($str, ENT_QUOTES, 'UTF-8'));
146  }
147 }
Registry for resources required by rendered output like Javascript or CSS.
render(Component\Component $component, RendererInterface $default_renderer)
Definition: Renderer.php:39
getTemplate(string $name, bool $purge_unfilled_vars, bool $purge_unused_blocks)
Get template of component this renderer is made for.
renderListingPopover(Component\Popover\Listing $popover, RendererInterface $default_renderer, string $id)
Definition: Renderer.php:127
cannotHandleComponent(Component $component)
This method MUST be called by derived component renderers, if.
renderStandardPopover(Component\Popover\Standard $popover, RendererInterface $default_renderer, string $id)
Definition: Renderer.php:115
register(string $name)
Add a dependency.
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
registerResources(ResourceRegistry $registry)
Announce resources this renderer requires.
Definition: Renderer.php:108
bindJavaScript(JavaScriptBindable $component)
Bind the component to JavaScript.