Skip to content

Commit

Permalink
1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Sandritsch91 committed Feb 11, 2022
1 parent 26f8206 commit 3d32cb5
Show file tree
Hide file tree
Showing 9 changed files with 1,043 additions and 80 deletions.
103 changes: 57 additions & 46 deletions src/Flatpickr.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace sandritsch91\yii2\flatpickr;

use ReflectionClass;
use ReflectionException;
use Yii;
use yii\base\InvalidConfigException;
use yii\bootstrap4\Html;
Expand All @@ -17,9 +15,13 @@
class Flatpickr extends InputWidget
{
/**
* @var string
* @var string language, empty for en
*/
public string $lang = 'de';
public string $locale = 'de';
/**
* @var string the theme to use
*/
public string $theme = '';
/**
* @var array the options for the underlying JS plugin.
*/
Expand All @@ -28,89 +30,92 @@ class Flatpickr extends InputWidget
* @var array the event handlers for the underlying JS plugin.
*/
public array $clientEvents = [];
/**
* @var string|boolean|AssetBundle class of custom css AssetBundle
*/
public $customAssetBundle = '';


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

if (!isset($this->lang)) {
$this->lang = substr(Yii::$app->language, 0, 2);
if (!array_key_exists('autocomplete', $this->options)) {
$this->options['autocomplete'] = 'off';
}
if (false !== $this->customAssetBundle && $this->customAssetBundle === '') {
$this->customAssetBundle = str_replace('widgets\\', '', static::class . "CustomAsset");
}

$this->registerTranslations();
}

/**
* {@inheritDoc}
* @throws InvalidConfigException
*/
public function run(): string
{
$this->clientOptions = $this->getClientOptions();

$selector = null;
$this->registerPlugin('dateDropper', $selector);
$this->registerPlugin('flatpickr', $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.
* @throws InvalidConfigException
*/
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");

// register JS
if ($this->locale !== '') {
$langUrl = Yii::$app->assetManager->publish('@npm/flatpickr/dist/l10n/' . $this->locale . '.js');
$view->registerJsFile($langUrl[1], ['depends' => FlatpickrJsAsset::class]);
}
else {
FlatpickrJsAsset::register($view);
}

// register Css
if ($this->theme !== '') {
// flatpickr plugin theme
$langUrl = Yii::$app->assetManager->publish('@npm/flatpickr/dist/themes/' . $this->theme . '.css');
$view->registerCssFile($langUrl[1], ['depends' => FlatpickrJsAsset::class]);
}
elseif ($this->customAssetBundle) {
// own theme
$this->customAssetBundle::register($view);
}
else {
// flatpickr default theme
FlatpickrCssAsset::register($view);
}


if (empty($pluginName)) {
$pluginName = strtolower(StringHelper::basename($className));
$pluginName = strtolower(StringHelper::basename(static::class));
}
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);";
$js = "$pluginName('$selector', $options);";
$view->registerJs($js);
}

Expand Down Expand Up @@ -141,16 +146,22 @@ protected function registerClientEvents(string $selector = null)

/**
* Get client options
* Set some defaults, if not in options
*
* @return array
*/
protected function getClientOptions(): array
{
$format = ArrayHelper::remove($this->clientOptions, 'format', FormatConverter::convertDateIcuToPhp(Yii::$app->formatter->dateFormat));
$dateFormat = ArrayHelper::remove($this->clientOptions, 'dateFormat', FormatConverter::convertDateIcuToPhp(Yii::$app->formatter->dateFormat));
$allowInput = ArrayHelper::remove($this->clientOptions, 'allowInput', true);
$time_24hr = ArrayHelper::remove($this->clientOptions, 'time_24hr', true);

return ArrayHelper::merge($this->clientOptions, [
'format' => $format,
'lang' => $this->lang
'defaultDate' => $this->value,
'locale' => $this->locale,
'dateFormat' => $dateFormat,
'allowInput' => $allowInput,
'time_24hr' => $time_24hr,
]);
}
}
34 changes: 0 additions & 34 deletions src/FlatpickrAsset.php

This file was deleted.

18 changes: 18 additions & 0 deletions src/FlatpickrCssAsset.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace sandritsch91\yii2\flatpickr;

class FlatpickrCssAsset extends FlatpickrJsAsset
{
/**
* {@inheritdoc}
*/
public $js = [];

/**
* {@inheritdoc}
*/
public $css = [
'flatpickr.css'
];
}
25 changes: 25 additions & 0 deletions src/FlatpickrCustomAsset.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace sandritsch91\yii2\flatpickr;

class FlatpickrCustomAsset extends \yii\web\AssetBundle
{
/**
* {@inheritdoc}
*/
public $sourcePath = '@sandritsch91/yii2/flatpickr/assets';

/**
* {@inheritDoc}
*/
public $css = [
'css/custom.css'
];

/**
* {@inheritdoc}
*/
public $publishOptions = [
'forceCopy' => YII_DEBUG
];
}
43 changes: 43 additions & 0 deletions src/FlatpickrJsAsset.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace sandritsch91\yii2\flatpickr;

use yii\web\AssetBundle;

class FlatpickrJsAsset extends AssetBundle
{
/**
* {@inheritdoc}
*/
public $sourcePath = '@npm/flatpickr/dist';

/**
* {@inheritDoc}
*/
public $js = [
'flatpickr.js',
'plugins/confirmDate/confirmDate.js',
'plugins/labelPlugin/labelPlugin.js',
'plugins/monthSelect/index.js',
'plugins/weekSelect/weekSelect.js',
'plugins/minMaxTimePlugin.js',
'plugins/momentPlugin.js',
'plugins/rangePlugin.js',
'plugins/scrollPlugin.js',
];

/**
* {@inheritDoc}
*/
public $css = [
'plugins/confirmDate/confirmDate.css',
'plugins/monthSelect/style.css',
];

/**
* {@inheritdoc}
*/
public $publishOptions = [
'forceCopy' => YII_DEBUG
];
}
Loading

0 comments on commit 3d32cb5

Please sign in to comment.