Skip to content

Commit

Permalink
init project
Browse files Browse the repository at this point in the history
  • Loading branch information
codenix-sv committed Nov 21, 2017
1 parent e944b31 commit 7bd7340
Show file tree
Hide file tree
Showing 14 changed files with 584 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
vendor
composer.lock
.idea
build
phpunit.xml
71 changes: 71 additions & 0 deletions README.md
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.


![flatpickr-theme](https://user-images.githubusercontent.com/17989224/33085187-a6a75f26-ceec-11e7-9c5f-56930360a488.png)
![flatpickr-theme-dark](https://user-images.githubusercontent.com/17989224/33085189-a6d0688a-ceec-11e7-8a38-be258ff692b2.png)

## 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.
46 changes: 46 additions & 0 deletions composer.json
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"
}
]
}
27 changes: 27 additions & 0 deletions phpunit.xml.dist
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>
110 changes: 110 additions & 0 deletions src/Flatpickr.php
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;
}
}
80 changes: 80 additions & 0 deletions src/assets/FlatpickrAsset.php
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';
}
}
}
Loading

0 comments on commit 7bd7340

Please sign in to comment.