Skip to content

Commit

Permalink
Fix #163: Enhancements to support Bootstrap v5.x
Browse files Browse the repository at this point in the history
  • Loading branch information
kartik-v committed Sep 1, 2021
1 parent 66f9abb commit 7d80d6e
Show file tree
Hide file tree
Showing 12 changed files with 62 additions and 45 deletions.
7 changes: 6 additions & 1 deletion CHANGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@ Change Log: `yii2-date-range`

## Version 1.7.3

**Date:** _under development_
**Date:** 01-Sep-2021

- (enh #163): Enhancements to support Bootstrap v5.x.
- (enh #162): Update Uzbek Translations.
- (enh #156): Update Greek Translations.
- (enh #155): Update Indonesian Translations.
- (enh #154): Update Kazakh Translations.
- (enh #150): Add following properties to DateRangePicker (applicable only when `presetDropdown` is `true`).
- `includeDaysFilter` : _bool_ defaults to `true`
- `presetFilterDays` : _array_ defaults to `[7, 30]`
Expand Down
2 changes: 1 addition & 1 deletion LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2015 - 2020, Kartik Visweswaran
Copyright (c) 2015 - 2021, Kartik Visweswaran
Krajee.com
All rights reserved.

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
}
],
"require": {
"kartik-v/yii2-krajee-base": ">=2.0.0"
"kartik-v/yii2-krajee-base": ">=3.0.0"
},
"autoload": {
"psr-4": {
Expand Down
2 changes: 1 addition & 1 deletion src/DateRangeBehavior.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

/**
* @copyright Copyright &copy; Kartik Visweswaran, Krajee.com, 2015 - 2020
* @copyright Copyright &copy; Kartik Visweswaran, Krajee.com, 2015 - 2021
* @package yii2-date-range
* @version 1.7.3
*/
Expand Down
80 changes: 46 additions & 34 deletions src/DateRangePicker.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
<?php

/**
* @copyright Copyright &copy; Kartik Visweswaran, Krajee.com, 2015 - 2020
* @copyright Copyright &copy; Kartik Visweswaran, Krajee.com, 2015 - 2021
* @package yii2-date-range
* @version 1.7.3
*/

namespace kartik\daterange;

use Exception;
use kartik\base\InputWidget;
use ReflectionException;
use Yii;
use yii\base\InvalidConfigException;
use yii\helpers\ArrayHelper;
Expand Down Expand Up @@ -209,7 +211,7 @@ class DateRangePicker extends InputWidget
* @see http://php.net/manual/en/function.date.php
* @see http://momentjs.com/docs/#/parsing/string-format/
*
* @param string $format the PHP date format string
* @param string $format the PHP date format string
*
* @return string
*/
Expand Down Expand Up @@ -259,13 +261,14 @@ protected static function convertDateFormat($format)
// unix timestamp
'U' => 'X',
];

return strtr($format, $conversions);
}

/**
* Parses and returns a JsExpression
*
* @param string|JsExpression $value
* @param string|JsExpression $value
*
* @return JsExpression
*/
Expand All @@ -277,7 +280,7 @@ protected static function parseJsExpr($value)
/**
* @inheritdoc
* @throws InvalidConfigException
* @throws \ReflectionException
* @throws ReflectionException
*/
public function run()
{
Expand All @@ -292,10 +295,10 @@ public function registerAssets()
{
$view = $this->getView();
MomentAsset::register($view);
$input = 'jQuery("#' . $this->options['id'] . '")';
$input = 'jQuery("#'.$this->options['id'].'")';
$id = $input;
if ($this->hideInput) {
$id = 'jQuery("#' . $this->containerOptions['id'] . '")';
$id = 'jQuery("#'.$this->containerOptions['id'].'")';
}
if (!empty($this->_langFile)) {
LanguageAsset::register($view)->js[] = $this->_langFile;
Expand All @@ -307,7 +310,7 @@ public function registerAssets()
if (ArrayHelper::getValue($this->pluginOptions, 'singleDatePicker', false)) {
$val = "start.format('{$this->_format}')";
}
$rangeJs = $this->getRangeJs('start') . $this->getRangeJs('end');
$rangeJs = $this->getRangeJs('start').$this->getRangeJs('end');
$change = "{$input}.val(val).trigger('change');{$rangeJs}";
if ($this->presetDropdown) {
$id = "{$id}.find('.kv-drp-dropdown')";
Expand All @@ -321,6 +324,7 @@ public function registerAssets()
$script = "var val={$val};{$change}";
} else {
$this->registerPlugin($this->pluginName, $id);

return;
}
$this->callback = "function(start,end,label){{$script}}";
Expand Down Expand Up @@ -368,18 +372,18 @@ public function registerAssets()
* Initializes widget settings
*
* @throws InvalidConfigException
* @throws \ReflectionException
* @throws ReflectionException
*/
protected function initSettings()
{
$isBs4 = $this->isBs4();
$notBs3 = !$this->isBs(3);
$this->_msgCat = 'kvdrp';
if (!isset($this->pickerIcon)) {
$iconCss = $isBs4 ? 'fas fa-calendar-alt' : 'glyphicon glyphicon-calendar';
$iconCss = $notBs3 ? 'fas fa-calendar-alt' : 'glyphicon glyphicon-calendar';
$this->pickerIcon = Html::tag('i', '', ['class' => $iconCss]);
}
if (!isset($this->pluginOptions['cancelButtonClasses'])) {
$this->pluginOptions['cancelButtonClasses'] = $isBs4 ? 'btn-secondary' : 'btn-default';
$this->pluginOptions['cancelButtonClasses'] = $notBs3 ? 'btn-secondary' : 'btn-default';
}
$this->initI18N(__DIR__);
$this->initLocale();
Expand All @@ -405,7 +409,7 @@ protected function initSettings()
if ($this->startAttribute && $this->endAttribute) {
$start = $this->getRangeValue('start');
$end = $this->getRangeValue('end');
$this->value = $start . $this->_separator . $end;
$this->value = $start.$this->_separator.$end;
if ($this->hasModel()) {
$attr = Html::getAttributeName($this->attribute);
$this->model->$attr = $this->value;
Expand All @@ -415,7 +419,7 @@ protected function initSettings()
}
}
$this->containerTemplate = strtr($this->containerTemplate, [
'{value}' => isset($this->value) ? $this->value : '',
'{value}' => $this->value ?? '',
'{pickerIcon}' => $this->pickerIcon,
]);
// Set `autoUpdateInput` to false for certain settings
Expand All @@ -425,19 +429,19 @@ protected function initSettings()
$this->_startInput = $this->getRangeInput('start');
$this->_endInput = $this->getRangeInput('end');
if (empty($this->containerOptions['id'])) {
$this->containerOptions['id'] = $this->options['id'] . '-container';
$this->containerOptions['id'] = $this->options['id'].'-container';
}
if (empty($this->containerOptions['class'])) {
$css = $this->useWithAddon && !$this->presetDropdown && !$this->hideInput ? ' input-group' : '';
$this->containerOptions['class'] = 'kv-drp-container' . $css;
$this->containerOptions['class'] = 'kv-drp-container'.$css;
}
$this->initRange();
$this->registerAssets();
}

/**
* Initialize locale settings
* @throws \ReflectionException
* @throws ReflectionException|Exception
*/
protected function initLocale()
{
Expand Down Expand Up @@ -534,37 +538,39 @@ protected function initRange()
*/
protected function renderInput()
{
$append = $this->_startInput . $this->_endInput;
$append = $this->_startInput.$this->_endInput;
if (!$this->hideInput) {
return $this->getInput('textInput') . $append;
return $this->getInput('textInput').$append;
}
$content = str_replace('{input}', $this->getInput('hiddenInput') . $append, $this->containerTemplate);
$content = str_replace('{input}', $this->getInput('hiddenInput').$append, $this->containerTemplate);
$tag = ArrayHelper::remove($this->containerOptions, 'tag', 'div');

return Html::tag($tag, $content, $this->containerOptions);
}

/**
* Gets input options based on type
*
* @param string $type whether `start` or `end`
* @param string $type whether `start` or `end`
*
* @return array|mixed
*/
protected function getInputOpts($type = '')
{
$opts = $type . 'InputOptions';
$opts = $type.'InputOptions';

return isset($this->$opts) && is_array($this->$opts) ? $this->$opts : [];
}

/**
* Sets input options for a specific type
*
* @param string $type whether `start` or `end`
* @param array $options the options to set
* @param string $type whether `start` or `end`
* @param array $options the options to set
*/
protected function setInputOpts($type = '', $options = [])
{
$opts = $type . 'InputOptions';
$opts = $type.'InputOptions';
if (property_exists($this, $opts)) {
$this->$opts = $options;
}
Expand All @@ -573,20 +579,21 @@ protected function setInputOpts($type = '', $options = [])
/**
* Gets the range attribute value based on type
*
* @param string $type whether `start` or `end`
* @param string $type whether `start` or `end`
*
* @return mixed|string
*/
protected function getRangeAttr($type = '')
{
$attr = $type . 'Attribute';
$attr = $type.'Attribute';

return $type && isset($this->$attr) ? $this->$attr : '';
}

/**
* Generates and returns the client script on date range change, when the start and end attributes are set
*
* @param string $type whether `start` or `end`
* @param string $type whether `start` or `end`
*
* @return string
*/
Expand All @@ -597,15 +604,16 @@ protected function getRangeJs($type = '')
return '';
}
$options = $this->getInputOpts($type);
$input = "jQuery('#" . $this->options['id'] . "')";
return "var v={$input}.val() ? {$type}.format('{$this->_format}') : '';jQuery('#" . $options['id'] .
$input = "jQuery('#".$this->options['id']."')";

return "var v={$input}.val() ? {$type}.format('{$this->_format}') : '';jQuery('#".$options['id'].
"').val(v).trigger('change');";
}

/**
* Generates and returns the hidden input markup when one of start or end attributes are set.
*
* @param string $type whether `start` or `end`
* @param string $type whether `start` or `end`
*
* @return string
*/
Expand All @@ -617,23 +625,25 @@ protected function getRangeInput($type = '')
}
$options = $this->getInputOpts($type);
if (empty($options['id'])) {
$options['id'] = $this->options['id'] . '-' . $type;
$options['id'] = $this->options['id'].'-'.$type;
}
if ($this->hasModel()) {
$this->setInputOpts($type, $options);

return Html::activeHiddenInput($this->model, $attr, $options);
}
$options['type'] = 'hidden';
$options['name'] = $attr;
$this->setInputOpts($type, $options);

return Html::tag('input', '', $options);
}

/**
* Initializes the range values when one of start or end attributes are set.
*
* @param string $type whether `start` or `end`
* @param string $value the value to set
* @param string $type whether `start` or `end`
* @param string $value the value to set
*/
protected function initRangeValue($type = '', $value = '')
{
Expand All @@ -653,9 +663,10 @@ protected function initRangeValue($type = '', $value = '')
/**
* Generates and returns the hidden input markup when one of start or end attributes are set.
*
* @param string $type whether `start` or `end`
* @param string $type whether `start` or `end`
*
* @return string
* @throws Exception
*/
protected function getRangeValue($type = '')
{
Expand All @@ -664,6 +675,7 @@ protected function getRangeValue($type = '')
return '';
}
$options = $this->getInputOpts($type);

return $this->hasModel() ? Html::getAttributeValue($this->model, $attr) :
ArrayHelper::getValue($options, 'value', '');
}
Expand Down
2 changes: 1 addition & 1 deletion src/DateRangePickerAsset.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

/**
* @copyright Copyright &copy; Kartik Visweswaran, Krajee.com, 2015 - 2020
* @copyright Copyright &copy; Kartik Visweswaran, Krajee.com, 2015 - 2021
* @package yii2-date-range
* @version 1.7.3
*/
Expand Down
2 changes: 1 addition & 1 deletion src/LanguageAsset.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

/**
* @copyright Copyright &copy; Kartik Visweswaran, Krajee.com, 2015 - 2020
* @copyright Copyright &copy; Kartik Visweswaran, Krajee.com, 2015 - 2021
* @package yii2-date-range
* @version 1.7.3
*/
Expand Down
2 changes: 1 addition & 1 deletion src/MomentAsset.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

/**
* @copyright Copyright &copy; Kartik Visweswaran, Krajee.com, 2015 - 2020
* @copyright Copyright &copy; Kartik Visweswaran, Krajee.com, 2015 - 2021
* @package yii2-date-range
* @version 1.7.3
*/
Expand Down
2 changes: 1 addition & 1 deletion src/assets/css/daterangepicker-kv.css
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*!
* @copyright Copyright &copy; Kartik Visweswaran, Krajee.com, 2015 - 2020
* @copyright Copyright &copy; Kartik Visweswaran, Krajee.com, 2015 - 2021
* @version 1.7.3
*
* Custom styling for DateRangePicker
Expand Down
2 changes: 1 addition & 1 deletion src/assets/css/daterangepicker-kv.min.css
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*!
* @copyright Copyright &copy; Kartik Visweswaran, Krajee.com, 2015 - 2020
* @copyright Copyright &copy; Kartik Visweswaran, Krajee.com, 2015 - 2021
* @version 1.7.3
*
* Custom styling for DateRangePicker
Expand Down
2 changes: 1 addition & 1 deletion src/assets/css/daterangepicker.css
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*!
* @copyright Copyright &copy; Kartik Visweswaran, Krajee.com, 2015 - 2020
* @copyright Copyright &copy; Kartik Visweswaran, Krajee.com, 2015 - 2021
* @version 1.7.3
*
* Modified Bootstrap 3.x & 4.x styling for DateRangePicker
Expand Down
2 changes: 1 addition & 1 deletion src/assets/css/daterangepicker.min.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 7d80d6e

Please sign in to comment.