<commit_before><?php
declare(strict_types=1);

namespace Brosland\Modals\UI;

use Nette\Application\UI\ITemplate;
use Nette\Application\UI\Presenter;
use Nette\Bridges\ApplicationLatte\Template;
use Nette\InvalidArgumentException;
use Nette\Utils\Random;
use RuntimeException;

trait ModalManagerTrait
{
    /**
     * @persistent
     * @var string|null
     */
    public $modal = null;
    /**
     * @var bool
     */
    private $initialized = false;
    /**
     * @var Modal|null
     */
    private $activeModal;
    /**
     * @var string
     */
    private static $COOKIE_PREFIX = 'brosland_modals__';

    public abstract function getPresenter(): ?Presenter;

    public abstract function getTemplate(): ITemplate;

    public function getActiveModal(): ?Modal
    {
        if (!$this->initialized) {
            $this->init();
        }

        return $this->activeModal;
    }

    public function setActiveModal(Modal $modal = null): void
    {
        if (!$this->initialized) {
            $this->init();
        }

        $httpResponse = $this->requirePresenter()->getHttpResponse();

        if ($this->modal !== null) {
            $httpResponse->deleteCookie(self::$COOKIE_PREFIX . $this->modal);

            $this->modal = null;
            $this->activeModal = null;
        }

        if ($modal !== null) {
            $this->modal = Random::generate(6);
            $this->activeModal = $modal;

            $httpResponse->setCookie(
                self::$COOKIE_PREFIX . $this->modal,
                $modal->getUniqueId(),
                '1 day'
            );
        }
    }

    public function updateModal(): void
    {
        if (!$this->initialized) {
            $this->init();
        }

        /** @var Template $template */
        $template = $this->getTemplate();
        $template->add('modal', $this->activeModal);

        $presenter = $this->requirePresenter();
        $session = $presenter->getSession(Modal::class);

        $closeModal = isset($session['close']);
        unset($session['close']);

        if ($presenter->isAjax()) {
            $presenter->redrawControl(
                'modal',
                $this->activeModal !== null && $this->activeModal->isOpenRequired()
            );

            if ($closeModal) {
                $presenter->getPayload()->brosland_modals__closeModal = true;
            }
        }
    }

    private function init(): void
    {
        $this->initialized = true;

        if ($this->modal === null) {
            return;
        }

        $httpRequest = $this->requirePresenter()->getHttpRequest();
        $activeModalId = (string)$httpRequest->getCookie(self::$COOKIE_PREFIX . $this->modal);

        try {
            $control = $this->requirePresenter()->getComponent($activeModalId);

            if (!$control instanceof Modal) {
                throw new InvalidArgumentException();
            }

            $this->activeModal = $control;
        } catch (InvalidArgumentException $e) {
            $this->setActiveModal(null);
        }
    }

    private function requirePresenter(): Presenter
    {
        $presenter = $this->getPresenter();

        if ($presenter === null) {
            throw new RuntimeException('The component has not been attached to the presenter.');
        }

        return $presenter;
    }
}<commit_msg>Removed method ModalManagerTrait::requirePresenter().<commit_after><?php
declare(strict_types=1);

namespace Brosland\Modals\UI;

use Nette\Application\UI\ITemplate;
use Nette\Application\UI\Presenter;
use Nette\Bridges\ApplicationLatte\Template;
use Nette\InvalidArgumentException;
use Nette\Utils\Random;
use RuntimeException;

trait ModalManagerTrait
{
    /**
     * @persistent
     * @var string|null
     */
    public $modal = null;
    /**
     * @var bool
     */
    private $initialized = false;
    /**
     * @var Modal|null
     */
    private $activeModal;
    /**
     * @var string
     */
    private static $COOKIE_PREFIX = 'brosland_modals__';

    public abstract function getPresenter(): ?Presenter;

    public abstract function getTemplate(): ITemplate;

    public function getActiveModal(): ?Modal
    {
        if (!$this->initialized) {
            $this->init();
        }

        return $this->activeModal;
    }

    public function setActiveModal(Modal $modal = null): void
    {
        if (!$this->initialized) {
            $this->init();
        }

        $presenter = $this->getPresenter();

        if ($presenter === null) {
            throw new RuntimeException('The component is not attached to the presenter.');
        }

        $httpResponse = $presenter->getHttpResponse();

        if ($this->modal !== null) {
            $httpResponse->deleteCookie(self::$COOKIE_PREFIX . $this->modal);

            $this->modal = null;
            $this->activeModal = null;
        }

        if ($modal !== null) {
            $this->modal = Random::generate(6);
            $this->activeModal = $modal;

            $httpResponse->setCookie(
                self::$COOKIE_PREFIX . $this->modal,
                $modal->getUniqueId(),
                '1 day'
            );
        }
    }

    public function updateModal(): void
    {
        if (!$this->initialized) {
            $this->init();
        }

        /** @var Template $template */
        $template = $this->getTemplate();
        $template->add('modal', $this->activeModal);

        $presenter = $this->getPresenter();

        if ($presenter === null) {
            throw new RuntimeException('The component is not attached to the presenter.');
        }

        $session = $presenter->getSession(Modal::class);

        $closeModal = isset($session['close']);
        unset($session['close']);

        if ($presenter->isAjax()) {
            $presenter->redrawControl(
                'modal',
                $this->activeModal !== null && $this->activeModal->isOpenRequired()
            );

            if ($closeModal) {
                $presenter->getPayload()->brosland_modals__closeModal = true;
            }
        }
    }

    private function init(): void
    {
        $this->initialized = true;

        if ($this->modal === null) {
            return;
        }

        $presenter = $this->getPresenter();

        if ($presenter === null) {
            throw new RuntimeException('The component is not attached to the presenter.');
        }

        $httpRequest = $presenter->getHttpRequest();
        $activeModalId = (string)$httpRequest->getCookie(self::$COOKIE_PREFIX . $this->modal);

        try {
            $control = $presenter->getComponent($activeModalId);

            if (!$control instanceof Modal) {
                throw new InvalidArgumentException();
            }

            $this->activeModal = $control;
        } catch (InvalidArgumentException $e) {
            $this->setActiveModal(null);
        }
    }
}
{'fraction_non_alphanumeric': 0.13901734104046243, 'fraction_numerical': 0.0008670520231213873, 'mean_word_length': 1.9665666523789114, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 85, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3510909', 'n_tokens_mistral': 1920, 'n_tokens_neox': 1883, 'n_words': 375}