diff --git a/DateAsset.php b/DateAsset.php new file mode 100644 index 0000000..a4a4812 --- /dev/null +++ b/DateAsset.php @@ -0,0 +1,28 @@ + + */ +class DateAsset extends AssetBundle +{ + public $sourcePath = '@bower/persian-date/dist'; + public $js = [ + '0.1.8/an-date-0.1.8.js', + ]; + public $depends = [ + 'yii\web\JqueryAsset', + 'yii\bootstrap\BootstrapPluginAsset', + 'yii\bootstrap\BootstrapAsset', + ]; +} diff --git a/Datepicker.php b/Datepicker.php new file mode 100644 index 0000000..d918bc8 --- /dev/null +++ b/Datepicker.php @@ -0,0 +1,189 @@ + + * @see http://babakhani.github.io/PersianWebToolkit/doc/datepicker/ + */ +class Datepicker extends InputWidget +{ + /** + * @var array the options for the Persian Bootstrap DatePicker plugin. + * Please refer to the Persian Bootstrap DatePicker plugin Web page for possible options. + * @see http://babakhani.github.io/PersianWebToolkit/doc/datepicker/#/options/ + */ + public $clientOptions = []; + + /** + * @var array the event handlers for the underlying Persian Bootstrap DatePicker plugin. + * Please refer to the [DatePicker](http://babakhani.github.io/PersianWebToolkit/doc/datepicker/#/methods/) plugin + * Web page for possible events. + */ + public $clientEvents = []; + + /** + * @var array HTML attributes to render on the container + */ + public $containerOptions = []; + + /** + * @var string the Persian Bootstrap DatePicker plugin's theme. + */ + public $theme = 'default'; + + /** + * @var string the size of the input ('lg', 'md', 'sm', 'xs') + */ + public $size; + + /** + * @var string the addon markup if you wish to display the input as a component. If you don't wish to render as a + * component then set it to null or false. + */ + public $addon = ''; + + /** + * @var string the template to render the input. + */ + public $template = '{input}{addon}'; + + /** + * @var bool whether to render the input as an inline calendar + */ + public $inline = false; + + /** + * @inheritdoc + */ + public function init() + { + parent::init(); + + if ($this->inline) { + $this->options['readonly'] = 'readonly'; + Html::addCssClass($this->options, 'text-center'); + } + if ($this->size) { + Html::addCssClass($this->options, 'input-' . $this->size); + Html::addCssClass($this->containerOptions, 'input-group-' . $this->size); + } + Html::addCssClass($this->options, 'form-control'); + Html::addCssClass($this->containerOptions, 'input-group date'); + } + + /** + * @inheritdoc + */ + public function run() + { + + $input = $this->hasModel() + ? Html::activeTextInput($this->model, $this->attribute, $this->options) + : Html::textInput($this->name, $this->value, $this->options); + + if ($this->inline) { + $input .= '
'; + } + if ($this->addon && !$this->inline) { + $addon = Html::tag('span', $this->addon, ['class' => 'input-group-addon']); + $input = strtr($this->template, ['{input}' => $input, '{addon}' => $addon]); + $input = Html::tag('div', $input, $this->containerOptions); + } + if ($this->inline) { + $input = strtr($this->template, ['{input}' => $input, '{addon}' => '']); + } + echo $input; + + $this->registerClientScript(); + } + + /** + * Registers required script for the plugin to work as DatePicker + */ + public function registerClientScript() + { + $js = []; + $view = $this->getView(); + $this->clientOptions['theme'] = $this->theme; + + DateAsset::register($view); + + if ($this->theme === 'default') { + DatePickerAsset::register($view); + } else { + $this->{'registerDatepicker' . ucfirst($this->theme) . 'ThemeAsset'}($view); + } + + + $id = $this->options['id']; + $selector = ";jQuery('#$id')"; + + if ($this->addon || $this->inline) { + $selector .= ".parent()"; + } + + + $options = !empty($this->clientOptions) ? Json::encode($this->clientOptions) : ''; + + $js[] = "$selector.pDatepicker($options);"; + + if (!empty($this->clientEvents)) { + foreach ($this->clientEvents as $event => $handler) { + $js[] = "$selector.on('$event', $handler);"; + } + } + $view->registerJs(implode("\n", $js)); + } + + /** + * Register Persian Bootstrap Datepicker Blue theme asset into view. + * + * @param $view + */ + protected function registerDatepickerBlueThemeAsset($view) + { + DatepickerBlueThemeAsset::register($view); + } + + /** + * Register Persian Bootstrap Datepicker Cheerup theme asset into view. + * + * @param $view + */ + protected function registerDatepickerCheerupThemeAsset($view) + { + DatepickerCheerupThemeAsset::register($view); + } + + /** + * Register Persian Bootstrap Datepicker Dark theme asset into view. + * + * @param $view + */ + protected function registerDatepickerDarkThemeAsset($view) + { + DatepickerDarkThemeAsset::register($view); + } + + /** + * Register Persian Bootstrap Datepicker Redblack theme asset into view. + * + * @param $view + */ + protected function registerDatepickerRedblackThemeAsset($view) + { + DatepickerRedblackThemeAsset::register($view); + } +} diff --git a/DatepickerAsset.php b/DatepickerAsset.php new file mode 100644 index 0000000..bd60f39 --- /dev/null +++ b/DatepickerAsset.php @@ -0,0 +1,29 @@ + + */ +class DatepickerAsset extends AssetBundle +{ + public $sourcePath = '@bower/persian-datepicker/dist'; + public $css = [ + 'css/persian-datepicker-0.4.5.css', + ]; + public $js = [ + 'js/persian-datepicker-0.4.5.js', + ]; + public $depends = [ + 'mrlco\datepicker\DateAsset', + ]; +} diff --git a/DatepickerBlueThemeAsset.php b/DatepickerBlueThemeAsset.php new file mode 100644 index 0000000..880e799 --- /dev/null +++ b/DatepickerBlueThemeAsset.php @@ -0,0 +1,29 @@ + + */ +class DatepickerBlueThemeAsset extends AssetBundle +{ + public $sourcePath = '@bower/persian-datepicker/dist'; + public $css = [ + 'css/theme/persian-datepicker-blue.css', + ]; + public $js = [ + 'js/persian-datepicker-0.4.5.js', + ]; + public $depends = [ + 'mrlco\datepicker\DateAsset', + ]; +} diff --git a/DatepickerCheerupThemeAsset.php b/DatepickerCheerupThemeAsset.php new file mode 100644 index 0000000..0337019 --- /dev/null +++ b/DatepickerCheerupThemeAsset.php @@ -0,0 +1,29 @@ + + */ +class DatepickerCheerupThemeAsset extends AssetBundle +{ + public $sourcePath = '@bower/persian-date/dist'; + public $css = [ + 'css/theme/persian-datepicker-cheerup.css', + ]; + public $js = [ + 'js/persian-datepicker-0.4.5.js', + ]; + public $depends = [ + 'mrlco\datepicker\DateAsset', + ]; +} diff --git a/DatepickerDarkThemeAsset.php b/DatepickerDarkThemeAsset.php new file mode 100644 index 0000000..2f2a5e7 --- /dev/null +++ b/DatepickerDarkThemeAsset.php @@ -0,0 +1,29 @@ + + */ +class DatepickerDarkThemeAsset extends AssetBundle +{ + public $sourcePath = '@bower/persian-date/dist'; + public $css = [ + 'css/theme/persian-datepicker-dark.css', + ]; + public $js = [ + 'js/persian-datepicker-0.4.5.js', + ]; + public $depends = [ + 'mrlco\datepicker\DateAsset', + ]; +} diff --git a/DatepickerRedblackThemeAsset.php b/DatepickerRedblackThemeAsset.php new file mode 100644 index 0000000..7374d36 --- /dev/null +++ b/DatepickerRedblackThemeAsset.php @@ -0,0 +1,29 @@ + + */ +class DatepickerRedblackThemeAsset extends AssetBundle +{ + public $sourcePath = '@bower/persian-date/dist'; + public $css = [ + 'css/theme/persian-datepicker-redblack.css', + ]; + public $js = [ + 'js/persian-datepicker-0.4.5.js', + ]; + public $depends = [ + 'mrlco\datepicker\DateAsset', + ]; +} diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..ce5c8e2 --- /dev/null +++ b/composer.json @@ -0,0 +1,38 @@ +{ + "name": "mrlco/yii2-jalali-datepicker", + "description": "Jalali (persian) Bootstrap DatePicker Widget for Yii2", + "type": "yii2-extension", + "keywords": [ + "yii2", + "yii2-persian-datepicker", + "yii2-jalali-datepicker", + "yii2-hijri-datepicker", + "yii2 persian bootstrap datepicker", + "yii2 jalali bootstrap datepicker", + "yii2 hijri bootstrap datepicker" + ], + "homepage": "http://mrlco.ir", + "license": "BSD-3-Clause", + "authors": [ + { + "name": "Mehran Barzandeh", + "email": "admin@mrlco.ir", + "homepage": "http://mrlco.ir", + "role": "Creator" + } + ], + "require": { + "yiisoft/yii2": "~2.0.0", + "bower-asset/persian-datepicker": "~0.4.5" + }, + "autoload": { + "psr-4": { + "mrlco\\datepicker\\": "" + } + }, + "extra": { + "asset-installer-paths": { + "bower-asset-library": "vendor/bower" + } + } +}