From 4b9edc22c7d5adea15461f644a0a3c7808c6b923 Mon Sep 17 00:00:00 2001 From: Kartik Visweswaran Date: Tue, 4 Sep 2018 19:55:40 +0530 Subject: [PATCH] Fix #94: Refactor code and consolidate / optimize properties within traits --- CHANGE.md | 1 + src/AssetBundle.php | 31 -------- src/BootstrapTrait.php | 37 ++++++++-- src/InputWidget.php | 148 ++++++--------------------------------- src/Module.php | 41 ----------- src/TranslationTrait.php | 16 ++++- src/Widget.php | 132 +--------------------------------- src/WidgetTrait.php | 98 ++++++++++++++++++++++++-- 8 files changed, 160 insertions(+), 344 deletions(-) diff --git a/CHANGE.md b/CHANGE.md index 9fe276d..72815dd 100644 --- a/CHANGE.md +++ b/CHANGE.md @@ -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 diff --git a/src/AssetBundle.php b/src/AssetBundle.php index 4657822..e048871 100644 --- a/src/AssetBundle.php +++ b/src/AssetBundle.php @@ -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 */ @@ -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 diff --git a/src/BootstrapTrait.php b/src/BootstrapTrait.php index f45a85e..33243b9 100644 --- a/src/BootstrapTrait.php +++ b/src/BootstrapTrait.php @@ -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 */ 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]] @@ -107,4 +134,4 @@ protected static function isSameVersion($ver1, $ver2) } return static::parseVer($ver1) === static::parseVer($ver2); } -} +} \ No newline at end of file diff --git a/src/InputWidget.php b/src/InputWidget.php index 64e928e..0d9e107 100644 --- a/src/InputWidget.php +++ b/src/InputWidget.php @@ -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; /** @@ -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 * @since 1.0 */ @@ -44,28 +59,9 @@ class InputWidget extends YiiInputWidget const LOAD_PROGRESS = '
 
'; /** - * @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 @@ -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 */ @@ -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. @@ -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. */ @@ -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() { diff --git a/src/Module.php b/src/Module.php index 6055be8..e61b3e0 100644 --- a/src/Module.php +++ b/src/Module.php @@ -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 diff --git a/src/TranslationTrait.php b/src/TranslationTrait.php index 7859491..dbd92db 100644 --- a/src/TranslationTrait.php +++ b/src/TranslationTrait.php @@ -16,13 +16,25 @@ /** * TranslationTrait manages methods for all translations used in Krajee extensions * - * @property array $i18n - * * @author Kartik Visweswaran * @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 * diff --git a/src/Widget.php b/src/Widget.php index ea1ff9d..35cde27 100644 --- a/src/Widget.php +++ b/src/Widget.php @@ -11,8 +11,6 @@ use yii\base\InvalidConfigException; use yii\base\Widget as YiiWidget; -use yii\helpers\ArrayHelper; -use yii\web\View; /** * Base class for widgets extending [[YiiWidget]] used in Krajee extensions. @@ -25,138 +23,11 @@ class Widget extends YiiWidget use TranslationTrait; use WidgetTrait; - /** - * @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`). - */ - public $moduleId; - - /** - * @var array default HTML attributes or other settings for widgets. - */ - public $defaultOptions = []; - /** * @var array HTML attributes or other settings for widgets. */ public $options = []; - /** - * @var string the javascript that will be used to destroy the jQuery plugin - */ - public $pluginDestroyJs; - - /** - * @var string the name of the jQuery plugin. - */ - public $pluginName = ''; - - /** - * @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 integer the position where the client JS hash variables for the 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 translation message file category name for i18n. - * - * @see [[\yii\i18n\I18N]] - */ - protected $_msgCat = ''; - - /** - * @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 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 @@ -165,8 +36,7 @@ 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(); if (empty($this->options['id'])) { $this->options['id'] = $this->getId(); } diff --git a/src/WidgetTrait.php b/src/WidgetTrait.php index 88c0dcf..cf62a6f 100644 --- a/src/WidgetTrait.php +++ b/src/WidgetTrait.php @@ -9,6 +9,7 @@ namespace kartik\base; +use yii\helpers\ArrayHelper; use yii\helpers\Json; use yii\web\JsExpression; use yii\web\View; @@ -16,14 +17,7 @@ /** * WidgetTrait manages all methods used by Krajee widgets and input widgets. * - * @property bool $enablePopStateFix - * @property string $pluginName - * @property string $pluginDestroyJs * @property array $options - * @property array $pluginOptions - * @property array $_encOptions - * @property string $_hashVar - * @property string $_dataVar * * @method View getView() * @@ -34,6 +28,88 @@ trait WidgetTrait { use BootstrapTrait; + /** + * @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`). + */ + public $moduleId; + + /** + * @var boolean enable pop state fix for pjax container on press of browser back & forward buttons. + */ + public $enablePopStateFix = false; + + /** + * @var string the plugin name + */ + public $pluginName; + + /** + * @var string the javascript that will be used to destroy the jQuery plugin + */ + public $pluginDestroyJs; + + /** + * @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 array widget plugin options. + */ + public $pluginOptions = []; + + /** + * @var array widget plugin options. + */ + public $defaultPluginOptions = []; + + /** + * @var array default HTML attributes or other settings for widgets. + */ + public $defaultOptions = []; + + + /** + * @var string the identifier for the PJAX widget container if the editable widget is to be rendered inside a PJAX + * container. This will ensure the PopoverX plugin is initialized correctly after a PJAX request is completed. + * If this is not set, no re-initialization will be done for pjax. + */ + public $pjaxContainerId; + + /** + * @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 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 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; + /** * Sets a HTML5 data variable. * @@ -45,6 +121,14 @@ protected function setDataVar($name) $this->_dataVar = "data-krajee-{$name}"; } + /** + * Merge default options + */ + protected function mergeDefaultOptions() + { + $this->pluginOptions = ArrayHelper::merge($this->defaultPluginOptions, $this->pluginOptions); + $this->options = ArrayHelper::merge($this->defaultOptions, $this->options); + } /** * Generates the `pluginDestroyJs` script if it is not set.