-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e944b31
commit 7bd7340
Showing
14 changed files
with
584 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
vendor | ||
composer.lock | ||
.idea | ||
build | ||
phpunit.xml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
# Flatpickr widget for Yii2 | ||
|
||
This is Yii2 widget wrapper for [Flatpickr datetime picker](https://github.com/chmln/flatpickr). | ||
Flatpickr is a lightweight and powerful datetime picker written in vanilla javascript. | ||
|
||
|
||
 | ||
 | ||
|
||
## Latest release | ||
The latest stable version of the extension is v1.0.0 | ||
|
||
## Installation | ||
|
||
The preferred way to install this extension is through [composer](http://getcomposer.org/download/). | ||
|
||
Either run | ||
|
||
```bash | ||
$ composer require codenix-sv/yii2-flatpickr:~1.0 | ||
``` | ||
or add | ||
|
||
```json | ||
"codenix-sv/yii2-flatpickr" : "~1.0" | ||
``` | ||
|
||
to the require section of your application's `composer.json` file. | ||
|
||
## Basic usage | ||
|
||
### Example of use with an `ActiveForm`: | ||
|
||
```php | ||
<?php | ||
|
||
use codenixsv\flatpickr\Flatpickr; | ||
|
||
?> | ||
... | ||
<?= $form->field($model, 'date')->widget(Flatpickr::className()) ?> | ||
``` | ||
### Example of use as a widget: | ||
```php | ||
<?= Flatpickr::widget(['model' => $model, 'attribute' => 'email']) ?> | ||
``` | ||
|
||
## Usage with options | ||
```php | ||
<?php | ||
|
||
use codenixsv\flatpickr\Flatpickr; | ||
|
||
?> | ||
... | ||
<?= $form->field($model, 'email')->widget(Flatpickr::className(), [ | ||
'theme' =>'dark', | ||
'clientOptions' => [ | ||
'locale' => 'ru', | ||
'enableTime' => true | ||
] | ||
]) ?> | ||
``` | ||
|
||
## Further Information | ||
Please, check the [Flatpickr site](https://chmln.github.io/flatpickr/options/) documentation for further | ||
information about configuration options. | ||
|
||
## License | ||
|
||
**yii2-flatpickr** is released under the MIT License. See the bundled [LICENSE](./LICENSE) for details. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
{ | ||
"name": "codenix-sv/yii2-flatpickr", | ||
"description": "Lightweight and powerful datetimepicker for Yii2 Framework", | ||
"keywords": [ | ||
"yii2", | ||
"yii2 flatpickr", | ||
"yii2 datepicker", | ||
"flatpickr" | ||
], | ||
"type": "yii2-extension", | ||
"license": "MIT", | ||
"support": { | ||
"issues": "https://github.com/codenix-sv/yii2-flatpickr/issues?state=open", | ||
"source": "https://github.com/codenix-sv/yii2-flatpickr" | ||
}, | ||
"authors": [ | ||
{ | ||
"name": "Vladymyr Svyryd", | ||
"email": "[email protected]", | ||
"role": "Developer" | ||
} | ||
], | ||
"require": { | ||
"yiisoft/yii2": "~2.0.0", | ||
"npm-asset/flatpickr": "~4.0" | ||
}, | ||
"require-dev": { | ||
"phpunit/phpunit": "~5.0" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"codenixsv\\flatpickr\\": "src/" | ||
} | ||
}, | ||
"autoload-dev": { | ||
"psr-4": { | ||
"codenixsv\\flatpickr\\": "tests/" | ||
} | ||
}, | ||
"repositories": [ | ||
{ | ||
"type": "composer", | ||
"url": "https://asset-packagist.org" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<phpunit bootstrap="./tests/functional/bootstrap.php" | ||
backupGlobals="false" | ||
backupStaticAttributes="false" | ||
colors="true" | ||
verbose="true" | ||
convertErrorsToExceptions="true" | ||
convertNoticesToExceptions="true" | ||
convertWarningsToExceptions="true" | ||
processIsolation="false" | ||
stopOnFailure="false"> | ||
<testsuites> | ||
<testsuite name="Yii2 Flatpickr Test Suite"> | ||
<directory>./tests/functional</directory> | ||
</testsuite> | ||
</testsuites> | ||
<filter> | ||
<whitelist> | ||
<directory suffix=".php">./src/</directory> | ||
</whitelist> | ||
</filter> | ||
<logging> | ||
<log type="coverage-html" target="build/coverage" charset="UTF-8" yui="true" highlight="true"/> | ||
<log type="coverage-text" target="build/coverage.txt"/> | ||
<log type="coverage-clover" target="build/logs/clover.xml"/> | ||
</logging> | ||
</phpunit> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
<?php | ||
/** | ||
* @link https://github.com/codenix-sv/yii2-flatpickr | ||
* @copyright Copyright (c) 2017 codenix-sv | ||
* @license https://github.com/codenix-sv/yii2-flatpickr/blob/master/LICENSE.md | ||
*/ | ||
|
||
namespace codenixsv\flatpickr; | ||
|
||
use yii\helpers\ArrayHelper; | ||
use yii\helpers\Json; | ||
use yii\helpers\Html; | ||
use yii\widgets\InputWidget; | ||
use codenixsv\flatpickr\assets\FlatpickrAsset; | ||
|
||
/** | ||
* Class Flatpickr | ||
* @package codenixsv\flatpickr | ||
*/ | ||
class Flatpickr extends InputWidget | ||
{ | ||
/** | ||
* @link https://chmln.github.io/flatpickr/options/ Flatpickr options | ||
* @var array | ||
*/ | ||
public $clientOptions = []; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
public $defaultOptions = [ | ||
'allowInput' => true | ||
]; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $_locale; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
public $theme; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $_id; | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function init() | ||
{ | ||
parent::init(); | ||
|
||
if (isset($this->clientOptions['locale'])) { | ||
$this->_locale = $this->clientOptions['locale']; | ||
} else { | ||
$language = explode('-', \Yii::$app->language); | ||
$this->_locale = $language[0]; | ||
} | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
* @return string | ||
*/ | ||
public function run() | ||
{ | ||
parent::run(); | ||
|
||
$this->registerClientScript(); | ||
|
||
$content = $this->renderContent(); | ||
|
||
return $content; | ||
} | ||
|
||
/** | ||
* Registers client scripts. | ||
*/ | ||
private function registerClientScript() | ||
{ | ||
$this->_id = $this->options['id']; | ||
$view = $this->getView(); | ||
|
||
$config = Json::encode(ArrayHelper::merge($this->defaultOptions, $this->clientOptions)); | ||
|
||
$asset = FlatpickrAsset::register($view); | ||
$asset->locale = $this->_locale; | ||
$asset->theme = $this->theme; | ||
|
||
$view->registerJs("flatpickr('#{$this->_id}', {$config});"); | ||
} | ||
|
||
/** | ||
* Renders widget | ||
* @return string | ||
*/ | ||
private function renderContent() | ||
{ | ||
$options = ArrayHelper::merge(['class' => 'form-control', 'data-input' => true], $this->options); | ||
$content = $this->hasModel() | ||
? Html::activeTextInput($this->model, $this->attribute, $options) | ||
: Html::textInput($this->name, $this->value, $options); | ||
|
||
return $content; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
<?php | ||
/** | ||
* @link https://github.com/codenix-sv/yii2-flatpickr | ||
* @copyright Copyright (c) 2017 codenix-sv | ||
* @license https://github.com/codenix-sv/yii2-flatpickr/blob/master/LICENSE.md | ||
*/ | ||
|
||
namespace codenixsv\flatpickr\assets; | ||
|
||
use yii\web\AssetBundle; | ||
|
||
/** | ||
* Class FlatpickrAsset | ||
* @package codenixsv\flatpickr\assets | ||
*/ | ||
class FlatpickrAsset extends AssetBundle | ||
{ | ||
/** | ||
* @inheritdoc | ||
* @var string | ||
*/ | ||
public $sourcePath = '@npm/flatpickr/dist'; | ||
|
||
/** | ||
* @inheritdoc | ||
* @var array | ||
*/ | ||
public $js = [ | ||
'flatpickr.min.js', | ||
]; | ||
|
||
/** | ||
* @inheritdoc | ||
* @var array | ||
*/ | ||
public $css = [ | ||
'flatpickr.min.css', | ||
]; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
public $theme; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
public $locale; | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function registerAssetFiles($view) | ||
{ | ||
$this->registerLanguageFile(); | ||
$this->registerThemeFile(); | ||
|
||
parent::registerAssetFiles($view); | ||
} | ||
|
||
/** | ||
* Registers language file | ||
*/ | ||
private function registerLanguageFile() | ||
{ | ||
if (!empty($this->locale) && ($this->locale !== 'en')) { | ||
$this->js[] = 'l10n/' . $this->locale . '.js'; | ||
} | ||
} | ||
|
||
/** | ||
* Registers theme | ||
*/ | ||
private function registerThemeFile() | ||
{ | ||
if (!empty($this->theme)) { | ||
$this->css[] = 'themes/' . $this->theme . '.css'; | ||
} | ||
} | ||
} |
Oops, something went wrong.