Example #1
0
 /**
  * @inheritDoc
  */
 public function processSignal()
 {
     $signal = $this->getSignal();
     parent::processSignal();
     if ($signal && ($this->snippetMode = $this->isAjax())) {
         Nette\Bridges\ApplicationLatte\UIRuntime::renderSnippets($this);
         $this->sendPayload();
     }
 }
Example #2
0
 public function sendPayload()
 {
     $this->getHttpResponse()->setCode(Nette\Http\IResponse::S200_OK);
     $payload = $this->getPayload();
     $payload->redirect = $this->getHttpRequest()->getUrl()->getRelativeUrl();
     $lastPresenter = self::$lastPresenter;
     if ($lastPresenter instanceof Nette\Application\UI\Presenter) {
         try {
             Nette\Bridges\ApplicationLatte\UIRuntime::renderSnippets($lastPresenter);
         } catch (Throwable $e) {
         }
         $lastPayload = $lastPresenter->getPayload();
         if ($lastPayload && isset($lastPayload->snippets) && ($snippetId = $this->getSnippetId())) {
             $snippets = array_filter((array) $lastPayload->snippets, function (string $snippet) use($snippetId) {
                 return !Nette\Utils\Strings::startsWith($snippet, $this->getSnippetId());
             }, ARRAY_FILTER_USE_KEY);
             array_walk($snippets, function ($snippet, $id) use($payload) {
                 $payload->snippets[$id] = $snippet;
             });
         }
     }
     parent::sendPayload();
 }
Example #3
0
 /** @deprecated */
 public static function renderSnippets(Nette\Application\UI\Control $control, \stdClass $local, array $params)
 {
     UIRuntime::renderSnippets($control, $local, $params);
 }
Example #4
0
 public function __call($name, $arguments = [])
 {
     if (Nette\Utils\Strings::startsWith($name, self::RENDER_METHOD)) {
         $rendering = $this->isRendering();
         $arguments = current($arguments) ?: $arguments;
         $arguments = (array) $arguments + ['snippet' => !$rendering, 'echo' => TRUE];
         $name = lcfirst(Nette\Utils\Strings::substring($name, strlen(self::RENDER_METHOD))) ?: $this->view;
         $isAjax = $this->getPresenter()->isAjax();
         $payload = $this->getPresenter()->getPayload();
         $defaultView = $this->view;
         $defaultSnippetMode = $this->snippetMode;
         $views = $this->views;
         if ($rendering || !$isAjax) {
             $views = array_intersect_key($views, [$name => TRUE]);
         } elseif ($this->snippetMode) {
             $views = array_intersect_key($views, $this->invalidViews);
         }
         $this->isRendering(TRUE);
         foreach (array_diff_key($views, $this->rendered) as $view => $snippetMode) {
             $this->view = $view;
             $this->snippetMode = $isAjax && !$snippetMode;
             if ($this->cache && is_callable($snippetMode)) {
                 $dependencies = [Nette\Caching\Cache::TAGS => [], Nette\Caching\Cache::FILES => []];
                 $key = $this->getCacheKey();
                 $providers = [];
                 foreach ($snippetMode(...[&$dependencies]) as $dependency) {
                     if ($dependency instanceof Ytnuk\Cache\Provider) {
                         $providers[] = $dependency;
                         $key[] = $dependency->getCacheKey();
                     } else {
                         $key[] = $dependency;
                     }
                 }
                 list($output, $this->related) = $this->cache->load($key, function (&$dp) use(&$dependencies, $providers) {
                     foreach ($providers as $provider) {
                         if ($provider instanceof Ytnuk\Cache\Provider) {
                             $dependencies[Nette\Caching\Cache::TAGS] = array_merge($dependencies[Nette\Caching\Cache::TAGS], $provider->getCacheTags());
                         }
                     }
                     $dependencies[Nette\Caching\Cache::TAGS] = array_merge($dependencies[Nette\Caching\Cache::TAGS], $this->getCacheTags());
                     $dependencies[Nette\Caching\Cache::TAGS][] = static::class;
                     $output = $this->render();
                     $dependencies[Nette\Caching\Cache::FILES][] = $this->getTemplate()->getFile();
                     $dp = $dependencies;
                     return [$output, $this->related];
                 });
             } else {
                 $output = $this->render();
             }
             if ($snippetMode && $isAjax) {
                 $payload->snippets[$this->getSnippetId()] = $output;
             }
             if ($control = $this->lookupRendering()) {
                 $control->setRelated($this, $this->view);
             }
             $this->rendered[$view] = $output;
         }
         $this->view = $defaultView;
         foreach ($this->related[$this->view] as $subName => $subViews) {
             $related = $this[$subName] ?? NULL;
             if ($related instanceof Nette\Application\UI\IRenderable) {
                 $related->redrawControl();
             }
         }
         $output = NULL;
         if ($this->snippetMode = $defaultSnippetMode) {
             Nette\Bridges\ApplicationLatte\UIRuntime::renderSnippets($this);
         } elseif (array_key_exists($this->view = $name, $this->rendered)) {
             $output = $this->rendered[$this->view];
             if (!$isAjax) {
                 foreach ($this->related[$this->view] as $relatedName => $relatedViews) {
                     $related = $this->getComponent($relatedName);
                     if ($related instanceof self) {
                         foreach ($relatedViews as $relatedView => $relatedSnippetId) {
                             $relatedSnippet = Nette\Utils\Html::el(is_string($arguments['snippet']) ? $arguments['snippet'] : 'div', ['id' => $relatedSnippetId]);
                             if (strpos($output, (string) $relatedSnippet) !== FALSE) {
                                 $output = str_replace((string) $relatedSnippet, $relatedSnippet->setHtml($related->__call(self::RENDER_METHOD . ucfirst($relatedView), ['echo' => FALSE, 'snippet' => FALSE])), $output);
                             }
                         }
                     }
                 }
             }
             if ($arguments['snippet'] && $this->views[$this->view] && ($snippetId = $this->getSnippetId())) {
                 $snippet = Nette\Utils\Html::el(is_string($arguments['snippet']) ? $arguments['snippet'] : 'div', ['id' => $snippetId]);
                 if ($isAjax && $this->related[$this->view]) {
                     $snippetMode = $this->snippetMode;
                     Nette\Bridges\ApplicationLatte\UIRuntime::renderSnippets($this);
                     $this->snippetMode = $snippetMode;
                 }
                 if ($isAjax && !isset($payload->snippets[$snippetId]) || !$isAjax && !$this->lookupRendering()) {
                     $snippet->setHtml($output);
                 }
                 $output = $snippet->render();
             }
             if ($arguments['echo']) {
                 echo $output;
             }
             $this->view = $defaultView;
         }
         $this->isRendering(FALSE);
         return $output;
     }
     return parent::__call($name, $arguments);
 }
Example #5
0
 /** @deprecated */
 public static function renderSnippets(Nette\Application\UI\Control $control, \stdClass $local, array $params)
 {
     trigger_error(__METHOD__ . '() is deprecated.', E_USER_DEPRECATED);
     UIRuntime::renderSnippets($control, $local, $params);
 }