Skip to content

Commit

Permalink
Setup
Browse files Browse the repository at this point in the history
  • Loading branch information
Sandritsch91 committed Feb 10, 2022
1 parent d92f0b4 commit 26f8206
Show file tree
Hide file tree
Showing 5 changed files with 221 additions and 2 deletions.
29 changes: 29 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# yii2-flatpickr
flatpickr for yii2
A [flatpickr](https://github.com/flatpickr/flatpickr) widget for [Yii2](https://www.yiiframework.com/)
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
},
"autoload-dev": {
"psr-4": {
"yiiunit\\extensions\\kanban\\": "tests"
"yiiunit\\extensions\\flatpickr\\": "tests"
}
},
"repositories": [
Expand Down
156 changes: 156 additions & 0 deletions src/Flatpickr.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
<?php

namespace sandritsch91\yii2\flatpickr;

use ReflectionClass;
use ReflectionException;
use Yii;
use yii\base\InvalidConfigException;
use yii\bootstrap4\Html;
use yii\helpers\ArrayHelper;
use yii\helpers\FormatConverter;
use yii\helpers\Json;
use yii\helpers\StringHelper;
use yii\web\AssetBundle;
use yii\widgets\InputWidget;

class Flatpickr extends InputWidget
{
/**
* @var string
*/
public string $lang = 'de';
/**
* @var array the options for the underlying JS plugin.
*/
public array $clientOptions = [];
/**
* @var array the event handlers for the underlying JS plugin.
*/
public array $clientEvents = [];

/**
* {@inheritdoc}
* @throws InvalidConfigException|ReflectionException
*/
public function init()
{
parent::init();

if (!isset($this->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
]);
}
}
34 changes: 34 additions & 0 deletions src/FlatpickrAsset.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace sandritsch91\yii2\flatpickr;

use yii\web\AssetBundle;

class FlatpickrAsset extends AssetBundle
{
/**
* {@inheritDoc}
*/
public $css = [
'flatpickr.css'
];

/**
* {@inheritDoc}
*/
public $js = [
'flatpickr.js'
];

/**
* {@inheritDoc}
*/
public $depends = [
'npm-asset/flatpickr'
];

/**
* {@inheritdoc}
*/
public $sourcePath = '@vendor/npm-asset/flatpickr/dist';
}

0 comments on commit 26f8206

Please sign in to comment.