From aee4b5e70802c59e133156eacdd30025d5c09e3b Mon Sep 17 00:00:00 2001 From: Jan Skrasek Date: Sun, 10 Feb 2019 17:55:15 +0100 Subject: [PATCH] added AutocompleteControl --- examples/.gitignore | 2 + .../autocomplete/AutocompletePresenter.latte | 24 +++++++ .../autocomplete/AutocompletePresenter.php | 61 +++++++++++++++++ examples/autocomplete/config.neon | 7 ++ examples/autocomplete/index.php | 19 ++++++ src/Controls/AutocompleteControl.php | 67 +++++++++++++++++++ 6 files changed, 180 insertions(+) create mode 100644 examples/.gitignore create mode 100644 examples/autocomplete/AutocompletePresenter.latte create mode 100644 examples/autocomplete/AutocompletePresenter.php create mode 100644 examples/autocomplete/config.neon create mode 100644 examples/autocomplete/index.php create mode 100644 src/Controls/AutocompleteControl.php diff --git a/examples/.gitignore b/examples/.gitignore new file mode 100644 index 0000000..c1e0f82 --- /dev/null +++ b/examples/.gitignore @@ -0,0 +1,2 @@ +log +temp diff --git a/examples/autocomplete/AutocompletePresenter.latte b/examples/autocomplete/AutocompletePresenter.latte new file mode 100644 index 0000000..9c52d06 --- /dev/null +++ b/examples/autocomplete/AutocompletePresenter.latte @@ -0,0 +1,24 @@ + + + + + + + Autocomplete demo + + +{control form} + + + diff --git a/examples/autocomplete/AutocompletePresenter.php b/examples/autocomplete/AutocompletePresenter.php new file mode 100644 index 0000000..7d20512 --- /dev/null +++ b/examples/autocomplete/AutocompletePresenter.php @@ -0,0 +1,61 @@ +enableDebugger(__DIR__ . '/log'); +$configurator->setTempDirectory(__DIR__ . '/temp'); +$configurator->createRobotLoader()->addDirectory(__DIR__)->register(); +$configurator->addConfig(__DIR__ . '/config.neon'); + +$container = $configurator->createContainer(); +$app = $container->getByType(Application::class); +$app->run(); diff --git a/src/Controls/AutocompleteControl.php b/src/Controls/AutocompleteControl.php new file mode 100644 index 0000000..ceedf29 --- /dev/null +++ b/src/Controls/AutocompleteControl.php @@ -0,0 +1,67 @@ +setCallback($callback); + } + $this->monitor( + IPresenter::class, + function () { + $this->control->{'data-autocomplete-url'} = $this->link('autocomplete!', ['q' => '__QUERY_PLACEHOLDER__']); + } + ); + } + + + public function getControl(): Html + { + $control = parent::getControl(); + $control->addClass('autocomplete'); + return $control; + } + + + public function setCallback(callable $callback) + { + $this->callback = $callback; + } + + + public function handleAutocomplete(string $q) + { + if (!$this->callback) { + throw new InvalidStateException('Undefined autocomplete callback.'); + } + + $out = call_user_func($this->callback, $q); + $presenter = $this->getPresenter(); + assert($presenter !== null); + $presenter->sendJson($out); + } +}