WidgetTrait
WidgetTrait manages all methods used by Krajee widgets and input widgets.
Tags
Table of Contents
Properties
- $baseSourcePath : string|false
- $defaultOptions : array<string|int, mixed>
- $defaultPluginOptions : array<string|int, mixed>
- $hashVarLoadPosition : int
- $moduleId : string
- $pjaxContainerId : string
- $pjaxDuplicationFix : bool
- $pluginDestroyJs : string
- $pluginEvents : array<string|int, mixed>
- $pluginName : string
- $pluginOptions : array<string|int, mixed>
- $sourcePath : string
- $_dataVar : string
- $_encOptions : string
- $_hashVar : string
Methods
- getBaseSourcePath() : 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.
- getView() : View
- registerWidgetJs() : mixed
- Registers a JS code block for the widget.
- addAsset() : mixed
- Adds an asset to the view.
- fixPjaxDuplication() : mixed
- Fix for weird PJAX container duplication behavior on pressing browser back and forward buttons.
- getPluginScript() : string
- Returns the plugin registration script.
- hashPluginOptions() : mixed
- Generates a hashed variable to store the pluginOptions.
- initDestroyJs() : mixed
- Generates the `pluginDestroyJs` script if it is not set.
- mergeDefaultOptions() : mixed
- Merge default options
- registerPlugin() : mixed
- Registers a specific plugin and the related events
- registerPluginOptions() : mixed
- Registers plugin options by storing within a uniquely generated javascript variable.
- setDataVar() : mixed
- Sets a HTML5 data variable.
Properties
$baseSourcePath
public
string|false
$baseSourcePath
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.
$defaultOptions
public
array<string|int, mixed>
$defaultOptions
= []
default HTML attributes or other settings for widgets.
$defaultPluginOptions
public
array<string|int, mixed>
$defaultPluginOptions
= []
default plugin options for the widget
$hashVarLoadPosition
public
int
$hashVarLoadPosition
= \yii\web\View::POS_HEAD
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
.
$moduleId
public
string
$moduleId
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
).
$pjaxContainerId
public
string
$pjaxContainerId
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.
$pjaxDuplicationFix
public
bool
$pjaxDuplicationFix
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
.
$pluginDestroyJs
public
string
$pluginDestroyJs
the javascript that will be used to destroy the jQuery plugin
$pluginEvents
public
array<string|int, mixed>
$pluginEvents
= []
widget JQuery events.
You must define events in event-name => event-function
format. For example:
pluginEvents = [
'change' => 'function() { log("change"); }',
'open' => 'function() { log("open"); }',
];
$pluginName
public
string
$pluginName
= ''
the plugin name
$pluginOptions
public
array<string|int, mixed>
$pluginOptions
= []
widget plugin options.
$sourcePath
public
string
$sourcePath
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'
]);
$_dataVar
protected
string
$_dataVar
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.
$_encOptions
protected
string
$_encOptions
= ''
the JSON encoded plugin options.
$_hashVar
protected
string
$_hashVar
the generated hashed variable name that will store the JSON encoded pluginOptions in [[View::POS_HEAD]].
Methods
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.
public
getBaseSourcePath() : string|false
Return values
string|falsegetView()
public
getView() : View
Return values
ViewregisterWidgetJs()
Registers a JS code block for the widget.
public
registerWidgetJs(string $js[, int $pos = View::POS_READY ][, string $key = null ]) : mixed
Parameters
- $js : string
-
the JS code block to be registered
- $pos : int = View::POS_READY
-
the position at which the JS script tag should be inserted in a page. The possible values are:
- [[View::POS_HEAD]]: in the head section
- [[View::POS_BEGIN]]: at the beginning of the body section
- [[View::POS_END]]: at the end of the body section
- [[View::POS_LOAD]]: enclosed within jQuery(window).load(). Note that by using this position, the method will automatically register the jQuery js file.
- [[View::POS_READY]]: enclosed within jQuery(document).ready(). This is the default value. Note that by using this position, the method will automatically register the jQuery js file.
- $key : string = null
-
the key that identifies the JS code block. If null, it will use
$js
as the key. If two JS code blocks are registered with the same key, the latter will overwrite the former.
Tags
addAsset()
Adds an asset to the view.
protected
addAsset(View $view, string $file, string $type, string $class) : mixed
Parameters
- $view : 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
fixPjaxDuplication()
Fix for weird PJAX container duplication behavior on pressing browser back and forward buttons.
protected
fixPjaxDuplication(View $view) : mixed
Parameters
- $view : View
Tags
getPluginScript()
Returns the plugin registration script.
protected
getPluginScript(string $name[, string $element = null ][, string $callback = null ][, string $callbackCon = null ]) : string
Parameters
- $name : string
-
the name of the plugin
- $element : string = null
-
the plugin target element
- $callback : string = null
-
the javascript callback function to be called after plugin loads
- $callbackCon : string = null
-
the javascript callback function to be passed to the plugin constructor
Tags
Return values
string —the generated plugin script
hashPluginOptions()
Generates a hashed variable to store the pluginOptions.
protected
hashPluginOptions(string $name) : mixed
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.).
Parameters
- $name : string
-
the name of the plugin
Tags
initDestroyJs()
Generates the `pluginDestroyJs` script if it is not set.
protected
initDestroyJs() : mixed
mergeDefaultOptions()
Merge default options
protected
mergeDefaultOptions() : mixed
registerPlugin()
Registers a specific plugin and the related events
protected
registerPlugin(string $name[, string $element = null ][, string $callback = null ][, string $callbackCon = null ]) : mixed
Parameters
- $name : string
-
the name of the plugin
- $element : string = null
-
the plugin target element
- $callback : string = null
-
the javascript callback function to be called after plugin loads
- $callbackCon : string = null
-
the javascript callback function to be passed to the plugin constructor
Tags
registerPluginOptions()
Registers plugin options by storing within a uniquely generated javascript variable.
protected
registerPluginOptions(string $name) : mixed
Parameters
- $name : string
-
the plugin name
Tags
setDataVar()
Sets a HTML5 data variable.
protected
setDataVar(string $name) : mixed
Parameters
- $name : string
-
the plugin name