From 26f82068dd6693cf0b2c046fe564ab0b31476639 Mon Sep 17 00:00:00 2001 From: Sandritsch91 Date: Thu, 10 Feb 2022 16:08:59 +0100 Subject: [PATCH] Setup --- .gitignore | 29 ++++++++ README.md | 2 +- composer.json | 2 +- src/Flatpickr.php | 156 +++++++++++++++++++++++++++++++++++++++++ src/FlatpickrAsset.php | 34 +++++++++ 5 files changed, 221 insertions(+), 2 deletions(-) create mode 100644 .gitignore create mode 100644 src/Flatpickr.php create mode 100644 src/FlatpickrAsset.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..32a8c94 --- /dev/null +++ b/.gitignore @@ -0,0 +1,29 @@ +# phpstorm project files +.idea +*.iml + +# netbeans project files +nbproject + +# zend studio for eclipse project files +.buildpath +.project +.settings + +# windows thumbnail cache +Thumbs.db + +# composer vendor dir +/vendor + +# composer itself is not needed and related files +composer.phar +composer.lock + +# Mac DS_Store Files +.DS_Store + +# phpunit itself is not needed +phpunit.phar +# local phpunit config +/phpunit.xml diff --git a/README.md b/README.md index 5278a11..0473896 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,2 @@ # yii2-flatpickr -flatpickr for yii2 +A [flatpickr](https://github.com/flatpickr/flatpickr) widget for [Yii2](https://www.yiiframework.com/) diff --git a/composer.json b/composer.json index 1eadcd4..14e294c 100644 --- a/composer.json +++ b/composer.json @@ -46,7 +46,7 @@ }, "autoload-dev": { "psr-4": { - "yiiunit\\extensions\\kanban\\": "tests" + "yiiunit\\extensions\\flatpickr\\": "tests" } }, "repositories": [ diff --git a/src/Flatpickr.php b/src/Flatpickr.php new file mode 100644 index 0000000..8617580 --- /dev/null +++ b/src/Flatpickr.php @@ -0,0 +1,156 @@ +lang)) { + $this->lang = substr(Yii::$app->language, 0, 2); + } + + $this->registerTranslations(); + } + + /** + * {@inheritDoc} + */ + public function run(): string + { + $this->clientOptions = $this->getClientOptions(); + + $selector = null; + $this->registerPlugin('dateDropper', $selector); + + return ($this->hasModel()) + ? Html::activeInput('text', $this->model, $this->attribute, $this->options) + : Html::input('text', $this->name, $this->value, $this->options); + } + + /** + * Init translations + */ + public function registerTranslations() + { + $reflector = new ReflectionClass(static::class); + $dir = rtrim(dirname($reflector->getFileName()), '\\/'); + $dir = rtrim(preg_replace('#widgets$#', '', $dir), '\\/') . DIRECTORY_SEPARATOR . 'messages'; + $category = str_replace(StringHelper::basename(static::class), '', static::class); + $category = rtrim(str_replace(['\\', 'yii2/', 'widgets', 'models'], ['/', ''], $category), '/') . '*'; + + if (!is_dir($dir)) { + return; + } + + Yii::$app->i18n->translations[$category] = [ + 'class' => 'yii\i18n\GettextMessageSource', + 'sourceLanguage' => 'en-US', + 'basePath' => $dir, + 'forceTranslation' => true + ]; + } + + /** + * Registers a specific plugin and the related events + * + * @param string|null $pluginName optional plugin name + * @param string|null $selector optional javascript selector for the plugin initialization. Defaults to widget id. + */ + protected function registerPlugin(string $pluginName = null, string $selector = null) + { + $view = $this->view; + $id = $this->options['id']; + + $className = static::class; + $assetClassName = str_replace('widgets\\', '', $className . "Asset"); + if (empty($pluginName)) { + $pluginName = strtolower(StringHelper::basename($className)); + } + if (empty($selector)) { + $selector = "#$id"; + } + if (class_exists($assetClassName)) { + /** + * @var AssetBundle $assetClassName + */ + $assetClassName::register($view); + } + + if ($this->clientOptions !== false) { + $options = empty($this->clientOptions) ? '' : Json::htmlEncode($this->clientOptions); + $js = "jQuery('$selector').$pluginName($options);"; + $view->registerJs($js); + } + + $this->registerClientEvents($selector); + } + + /** + * Registers JS event handlers that are listed in [[clientEvents]]. + * + * @param string|null $selector optional javascript selector for the plugin initialization. Defaults to widget id. + */ + protected function registerClientEvents(string $selector = null) + { + if (!empty($this->clientEvents)) { + $id = $this->options['id']; + + if (empty($selector)) { + $selector = "#$id"; + } + + $js = []; + foreach ($this->clientEvents as $event => $handler) { + $js[] = "jQuery('$selector').on('$event', $handler);"; + } + $this->view->registerJs(implode("\n", $js)); + } + } + + /** + * Get client options + * + * @return array + */ + protected function getClientOptions(): array + { + $format = ArrayHelper::remove($this->clientOptions, 'format', FormatConverter::convertDateIcuToPhp(Yii::$app->formatter->dateFormat)); + + return ArrayHelper::merge($this->clientOptions, [ + 'format' => $format, + 'lang' => $this->lang + ]); + } +} diff --git a/src/FlatpickrAsset.php b/src/FlatpickrAsset.php new file mode 100644 index 0000000..b31c581 --- /dev/null +++ b/src/FlatpickrAsset.php @@ -0,0 +1,34 @@ +