Trait kartik\base\WidgetTrait
WidgetTrait manages all methods used by Krajee widgets and input widgets.
Public Properties
Property | Type | Description | Defined By |
---|---|---|---|
$baseSourcePath | string|false | Get parsed base source path based on $sourcePath setting. If $sourcePath is not set, it will return the current working directory of this widget class. | kartik\base\WidgetTrait |
$defaultOptions | array | Default HTML attributes or other settings for widgets. | kartik\base\WidgetTrait |
$defaultPluginOptions | array | Default plugin options for the widget | kartik\base\WidgetTrait |
$hashVarLoadPosition | integer | The position where the client JS hash variables for the input widget will be loaded. | kartik\base\WidgetTrait |
$moduleId | string | The module identifier if this widget is part of a module. | kartik\base\WidgetTrait |
$pjaxContainerId | string | The identifier for the PJAX widget container if the widget is to be rendered inside a PJAX container. | kartik\base\WidgetTrait |
$pjaxDuplicationFix | boolean | Prevent duplication of pjax containers when browser back & forward buttons are pressed. | kartik\base\WidgetTrait |
$pluginDestroyJs | string | The javascript that will be used to destroy the jQuery plugin | kartik\base\WidgetTrait |
$pluginEvents | array | Widget JQuery events. | kartik\base\WidgetTrait |
$pluginName | string | The plugin name | kartik\base\WidgetTrait |
$pluginOptions | array | Widget plugin options. | kartik\base\WidgetTrait |
$sourcePath | string | Directory path to the original widget source. | kartik\base\WidgetTrait |
Protected Properties
Property | Type | Description | Defined By |
---|---|---|---|
$_dataVar | 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. | kartik\base\WidgetTrait |
$_encOptions | string | The JSON encoded plugin options. | kartik\base\WidgetTrait |
$_hashVar | string | The generated hashed variable name that will store the JSON encoded pluginOptions in yii\web\View::POS_HEAD. | kartik\base\WidgetTrait |
Public Methods
Method | Description | Defined By |
---|---|---|
getBaseSourcePath() | Get parsed base source path based on $sourcePath setting. If $sourcePath is not set, it will return the current working directory of this widget class. | kartik\base\WidgetTrait |
getView() | kartik\base\WidgetTrait | |
registerWidgetJs() | Registers a JS code block for the widget. | kartik\base\WidgetTrait |
Protected Methods
Method | Description | Defined By |
---|---|---|
addAsset() | Adds an asset to the view. | kartik\base\WidgetTrait |
fixPjaxDuplication() | Fix for weird PJAX container duplication behavior on pressing browser back and forward buttons. | kartik\base\WidgetTrait |
getPluginScript() | Returns the plugin registration script. | kartik\base\WidgetTrait |
hashPluginOptions() | Generates a hashed variable to store the pluginOptions. | kartik\base\WidgetTrait |
initDestroyJs() | Generates the pluginDestroyJs script if it is not set. |
kartik\base\WidgetTrait |
mergeDefaultOptions() | Merge default options | kartik\base\WidgetTrait |
registerPlugin() | Registers a specific plugin and the related events | kartik\base\WidgetTrait |
registerPluginOptions() | Registers plugin options by storing within a uniquely generated javascript variable. | kartik\base\WidgetTrait |
setDataVar() | Sets a HTML5 data variable. | kartik\base\WidgetTrait |
Property Details
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.
The JSON encoded plugin options.
The generated hashed variable name that will store the JSON encoded pluginOptions in yii\web\View::POS_HEAD.
Get parsed base source path based on $sourcePath setting. If $sourcePath is not set, it will return the current working directory of this widget class.
Default HTML attributes or other settings for widgets.
Default plugin options for the widget
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
.
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
).
The identifier for the PJAX widget container if the widget is to be rendered inside a PJAX container.
This will ensure the any jQuery plugin using the widget is initialized correctly after a PJAX request is completed. If this is not set, no re-initialization will be done for pjax.
Prevent duplication of pjax containers when browser back & forward buttons are pressed.
- If this property is not set, it will be defaulted from Yii::$app->params['pjaxDuplicationFix'].
- If
Yii::$app->params['pjaxDuplicationFix']
is not set, then this property will default totrue
.
The javascript that will be used to destroy the jQuery plugin
Widget JQuery events.
You must define events in event-name => event-function
format. For example:
pluginEvents = [
'change' => 'function() { log("change"); }',
'open' => 'function() { log("open"); }',
];
The plugin name
Widget plugin options.
Directory path to the original widget source. If not set, will default to the working directory for
the current widget's class. Setting this property can be useful in specific cases, like when you are extending
the Krajee widget with your own custom namespaced class. In that case, set this property to the original Krajee
Widget Base path. Yii path alias parsing is supported (using @
symbols). For example:
// your custom extended widget
namespace myapp\widgets;
class MyDateRangePicker extends kartik\daterange\DateRangePicker {
// directly set the property to the original Krajee base widget directory
// you can use Yii path aliases
public $sourcePath = '@vendor/kartik-v/yii2-date-range/src';
}
// Alternatively you can also override this property while rendering the widget
// view.php: where widget is rendered
use myapp\widgets\MyDateRangePicker;
echo MyDateRangePicker::widget([
'name' => 'custom',
'sourcePath' => '@vendor/kartik-v/yii2-date-range/src'
]);
Method Details
Adds an asset to the view.
protected void addAsset ( $view, $file, $type, $class ) | ||
$view | yii\web\View | The View object |
$file | string | The asset file name |
$type | string | The asset file type (css or js) |
$class | string | The class name of the AssetBundle |
Fix for weird PJAX container duplication behavior on pressing browser back and forward buttons.
protected void fixPjaxDuplication ( $view ) | ||
$view | yii\web\View | |
throws | Exception |
---|
Get parsed base source path based on $sourcePath setting. If $sourcePath is not set, it will return the current working directory of this widget class.
public string|false getBaseSourcePath ( ) |
Returns the plugin registration script.
protected string getPluginScript ( $name, $element = null, $callback = null, $callbackCon = null ) | ||
$name | string | The name of the plugin |
$element | string | The plugin target element |
$callback | string | The javascript callback function to be called after plugin loads |
$callbackCon | string | The javascript callback function to be passed to the plugin constructor |
return | string | The generated plugin script |
---|---|---|
throws | Exception |
public yii\web\View getView ( ) | ||
return | yii\web\View |
---|
Generates a hashed variable to store the pluginOptions.
The following special data attributes will also be setup for the input widget, that can be accessed through javascript :
data-krajee-{name}
will store the hashed variable storing the plugin options. The{name}
token will be replaced with the plugin name (e.g.select2
,typeahead
etc.).
See also https://github.com/kartik-v/yii2-krajee-base/issues/6.
protected void hashPluginOptions ( $name ) | ||
$name | string | The name of the plugin |
Generates the pluginDestroyJs
script if it is not set.
protected void initDestroyJs ( ) |
Merge default options
protected void mergeDefaultOptions ( ) |
Registers a specific plugin and the related events
protected void registerPlugin ( $name, $element = null, $callback = null, $callbackCon = null ) | ||
$name | string | The name of the plugin |
$element | string | The plugin target element |
$callback | string | The javascript callback function to be called after plugin loads |
$callbackCon | string | The javascript callback function to be passed to the plugin constructor |
throws | Exception |
---|
Registers plugin options by storing within a uniquely generated javascript variable.
protected void registerPluginOptions ( $name ) | ||
$name | string | The plugin name |
throws | Exception |
---|
Registers a JS code block for the widget.
public void registerWidgetJs ( $js, $pos = \yii\web\View::POS_READY, $key = null ) | ||
$js | string | The JS code block to be registered |
$pos | integer | The position at which the JS script tag should be inserted in a page. The possible values are:
|
$key | string | The key that identifies the JS code block. If null, it will use |
throws | Exception |
---|
Sets a HTML5 data variable.
protected void setDataVar ( $name ) | ||
$name | string | The plugin name |