Skip to content

Commit

Permalink
Fix #94: Refactor code and consolidate / optimize properties within t…
Browse files Browse the repository at this point in the history
…raits
  • Loading branch information
kartik-v committed Sep 4, 2018
1 parent df57418 commit 538c886
Show file tree
Hide file tree
Showing 8 changed files with 156 additions and 344 deletions.
1 change: 1 addition & 0 deletions CHANGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Change Log: `yii2-krajee-base`

**Date:** _under development_

- (enh #94): Refactor code and consolidate / optimize properties within traits.
- Add Bootstrap button default CSS and icon prefix parsing.

## Version 1.9.3
Expand Down
31 changes: 0 additions & 31 deletions src/AssetBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,6 @@ class AssetBundle extends BaseAssetBundle
{
use BootstrapTrait;

/**
* @var int|string the bootstrap library version.
*
* To use with bootstrap 3 - you can set this to any string starting with 3 (e.g. `3` or `3.3.7` or `3.x`)
* To use with bootstrap 4 - you can set this to any string starting with 4 (e.g. `4` or `4.1.1` or `4.x`)
*
* This property can be set up globally in Yii application params in your Yii2 application config file.
*
* For example:
* `Yii::$app->params['bsVersion'] = '4.x'` to use with Bootstrap 4.x globally
*
* If this property is set, this setting will override the `Yii::$app->params['bsVersion']`. If this is not set, and
* `Yii::$app->params['bsVersion']` is also not set, this will default to `3.x` (Bootstrap 3.x version).
*/
public $bsVersion;

/**
* @var bool whether the bootstrap JS plugins are to be loaded and enabled
*/
Expand All @@ -52,21 +36,6 @@ class AssetBundle extends BaseAssetBundle
'yii\web\YiiAsset',
];

/**
* @var string default icon CSS prefix
*/
protected $_defaultIconPrefix;

/**
* @var string default bootstrap button CSS
*/
protected $_defaultBtnCss;

/**
* @var bool flag to detect whether bootstrap 4.x version is set
*/
protected $_isBs4;

/**
* @inheritdoc
* @throws InvalidConfigException
Expand Down
37 changes: 32 additions & 5 deletions src/BootstrapTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,42 @@
/**
* BootstrapTrait includes bootstrap library init and parsing methods
*
* @property string $bsVersion
* @property string $_defaultIconPrefix
* @property string $_defaultBtnCss
* @property bool $_isBs4
*
* @author Kartik Visweswaran <kartikv2@gmail.com>
*/
trait BootstrapTrait
{
/**
* @var int|string the bootstrap library version.
*
* To use with bootstrap 3 - you can set this to any string starting with 3 (e.g. `3` or `3.3.7` or `3.x`)
* To use with bootstrap 4 - you can set this to any string starting with 4 (e.g. `4` or `4.1.1` or `4.x`)
*
* This property can be set up globally in Yii application params in your Yii2 application config file.
*
* For example:
* `Yii::$app->params['bsVersion'] = '4.x'` to use with Bootstrap 4.x globally
*
* If this property is set, this setting will override the `Yii::$app->params['bsVersion']`. If this is not set, and
* `Yii::$app->params['bsVersion']` is also not set, this will default to `3.x` (Bootstrap 3.x version).
*/
public $bsVersion;

/**
* @var string default icon CSS prefix
*/
protected $_defaultIconPrefix;

/**
* @var string default bootstrap button CSS
*/
protected $_defaultBtnCss;

/**
* @var bool flag to detect whether bootstrap 4.x version is set
*/
protected $_isBs4;

/**
* Initializes bootstrap versions for the widgets and asset bundles.
* Sets the [[_isBs4]] flag by parsing [[bsVersion]]
Expand Down Expand Up @@ -107,4 +134,4 @@ protected static function isSameVersion($ver1, $ver2)
}
return static::parseVer($ver1) === static::parseVer($ver2);
}
}
}
148 changes: 21 additions & 127 deletions src/InputWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use yii\helpers\FormatConverter;
use yii\helpers\Html;
use yii\helpers\ArrayHelper;
use yii\web\View;
use yii\widgets\InputWidget as YiiInputWidget;

/**
Expand All @@ -33,6 +32,22 @@
* ]) ?>
* ```
*
* @property string|int $bsVersion
* @property bool $enablePopStateFix
* @property string $pluginDestroyJs
* @property array $pluginEvents
* @property array $pluginOptions
* @property array $defaultPluginOptions
* @property array $defaultOptions
* @property array $i18n
* @property string $_defaultIconPrefix
* @property string $_defaultBtnCss
* @property integer $hashVarLoadPosition
* @property string $_hashVar
* @property string $_dataVar
* @property array $_encOptions
* @property bool $_isBs4
*
* @author Kartik Visweswaran <kartikv2@gmail.com>
* @since 1.0
*/
Expand All @@ -44,28 +59,9 @@ class InputWidget extends YiiInputWidget
const LOAD_PROGRESS = '<div class="kv-plugin-loading">&nbsp;</div>';

/**
* @var int|string the bootstrap library version.
*
* To use with bootstrap 3 - you can set this to any string starting with 3 (e.g. `3` or `3.3.7` or `3.x`)
* To use with bootstrap 4 - you can set this to any string starting with 4 (e.g. `4` or `4.1.1` or `4.x`)
*
* This property can be set up globally in Yii application params in your Yii2 application config file.
*
* For example:
* `Yii::$app->params['bsVersion'] = '4.x'` to use with Bootstrap 4.x globally
*
* If this property is set, this setting will override the `Yii::$app->params['bsVersion']`. If this is not set, and
* `Yii::$app->params['bsVersion']` is also not set, this will default to `3.x` (Bootstrap 3.x version).
*/
public $bsVersion;

/**
* @var string the module identifier if this widget is part of a module. If not set, the module identifier will
* be auto derived based on the \yii\base\Module::getInstance method. This can be useful, if you are setting
* multiple module identifiers for the same module in your Yii configuration file. To specify children or grand
* children modules you can specify the module identifiers relative to the parent module (e.g. `admin/content`).
* @var string the name of the jQuery plugin.
*/
public $moduleId;
public $pluginName = '';

/**
* @var string the language configuration (e.g. 'fr-FR', 'zh-CN'). The format for the language/locale is
Expand All @@ -85,11 +81,6 @@ class InputWidget extends YiiInputWidget
*/
public $readonly = false;

/**
* @var string the javascript that will be used to destroy the jQuery plugin
*/
public $pluginDestroyJs;

/**
* @var boolean show loading indicator while plugin loads
*/
Expand All @@ -100,52 +91,6 @@ class InputWidget extends YiiInputWidget
*/
public $data = [];

/**
* @var string the name of the jQuery plugin.
*/
public $pluginName = '';

/**
* @var array the default HTML attributes for the input tag.
* @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
*/
public $defaultOptions = [];

/**
* @var array widget plugin options.
*/
public $defaultPluginOptions = [];

/**
* @var array widget plugin options.
*/
public $pluginOptions = [];

/**
* @var array widget JQuery events. You must define events in `event-name => event-function` format. For example:
*
* ~~~
* pluginEvents = [
* 'change' => 'function() { log("change"); }',
* 'open' => 'function() { log("open"); }',
* ];
* ~~~
*/
public $pluginEvents = [];

/**
* @var string a pjax container identifier if applicable inside which the widget will be rendered. If this is set,
* the widget will automatically reinitialize on pjax render completion.
*
* Note: You can set this to the HTML id attribute of any PJAX parent container element that encloses the widget.
*/
public $pjaxContainerId;

/**
* @var boolean enable pop state fix for pjax container on press of browser back & forward buttons.
*/
public $enablePopStateFix = false;

/**
* @var boolean whether the widget should automatically format the date from the PHP DateTime format to the
* javascript/jquery plugin format. This is more applicable for widgets that manage date / time inputs.
Expand All @@ -154,37 +99,6 @@ class InputWidget extends YiiInputWidget
*/
public $convertFormat = false;

/**
* @var integer the position where the client JS hash variables for the input widget will be loaded.
* Defaults to `View::POS_HEAD`. This can be set to `View::POS_READY` for specific scenarios like when
* rendering the widget via `renderAjax`.
*/
public $hashVarLoadPosition = View::POS_HEAD;

/**
* @var array the the internalization configuration for this widget.
*
* @see [[\yii\i18n\I18N]] component for understanding the configuration details.
*/
public $i18n = [];

/**
* @var string the HTML5 data variable name that will be used to store the Json encoded pluginOptions within the
* element on which the jQuery plugin will be initialized.
*/
protected $_dataVar;

/**
* @var string the generated hashed variable name that will store the JSON encoded pluginOptions in
* [[View::POS_HEAD]].
*/
protected $_hashVar;

/**
* @var string the JSON encoded plugin options.
*/
protected $_encOptions = '';

/**
* @var string the indicator to be displayed while plugin is loading.
*/
Expand All @@ -200,43 +114,23 @@ class InputWidget extends YiiInputWidget
*/
protected $_langFile = '';

/**
* @var string translation message file category name for i18n.
*
* @see [[\yii\i18n\I18N]]
*/
protected $_msgCat = '';

/**
* @var string default icon CSS prefix
*/
protected $_defaultIconPrefix;

/**
* @var string default bootstrap button CSS
*/
protected $_defaultBtnCss;

/**
* @var bool flag to detect whether bootstrap 4.x version is set
*/
protected $_isBs4;
/**
* @inheritdoc
* @throws InvalidConfigException
* @throws \ReflectionException
*/
public function init()
{
$this->initBsVersion();
parent::init();
$this->pluginOptions = ArrayHelper::merge($this->defaultPluginOptions, $this->pluginOptions);
$this->options = ArrayHelper::merge($this->defaultOptions, $this->options);
$this->mergeDefaultOptions();
$this->initDestroyJs();
$this->initInputWidget();
}

/**
* Initializes the input widget.
* @throws \ReflectionException
*/
protected function initInputWidget()
{
Expand Down
41 changes: 0 additions & 41 deletions src/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,47 +24,6 @@ class Module extends YiiModule
use TranslationTrait;
use BootstrapTrait;

/**
* @var int|string the bootstrap library version.
*
* To use with bootstrap 3 - you can set this to any string starting with 3 (e.g. `3` or `3.3.7` or `3.x`)
* To use with bootstrap 4 - you can set this to any string starting with 4 (e.g. `4` or `4.1.1` or `4.x`)
*
* This property can be set up globally in Yii application params in your Yii2 application config file.
*
* For example:
* `Yii::$app->params['bsVersion'] = '4.x'` to use with Bootstrap 4.x globally
*
* If this property is set, this setting will override the `Yii::$app->params['bsVersion']`. If this is not set, and
* `Yii::$app->params['bsVersion']` is also not set, this will default to `3.x` (Bootstrap 3.x version).
*/
public $bsVersion;

/**
* @var array the the internalization configuration for this widget
*/
public $i18n = [];

/**
* @var string translation message file category name for i18n
*/
protected $_msgCat = '';

/**
* @var string default icon CSS prefix
*/
protected $_defaultIconPrefix;

/**
* @var string default bootstrap button CSS
*/
protected $_defaultBtnCss;

/**
* @var bool flag to detect whether bootstrap 4.x version is set
*/
protected $_isBs4;

/**
* @inheritdoc
* @throws InvalidConfigException
Expand Down
16 changes: 14 additions & 2 deletions src/TranslationTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,25 @@
/**
* TranslationTrait manages methods for all translations used in Krajee extensions
*
* @property array $i18n
*
* @author Kartik Visweswaran <kartikv2@gmail.com>
* @since 1.9.3
*/
trait TranslationTrait
{
/**
* @var array the the internalization configuration for this widget.
*
* @see [[\yii\i18n\I18N]] component for understanding the configuration details.
*/
public $i18n = [];

/**
* @var string translation message file category name for i18n.
*
* @see [[\yii\i18n\I18N]]
*/
protected $_msgCat = '';

/**
* Yii i18n messages configuration for generating translations
*
Expand Down
Loading

0 comments on commit 538c886

Please sign in to comment.