Trait kartik\base\WidgetTrait

Implemented bykartik\alert\Alert, kartik\alert\AlertBlock, kartik\base\Html5Input, kartik\base\InputWidget, kartik\base\Widget, kartik\builder\BaseForm, kartik\builder\Form, kartik\builder\FormGrid, kartik\builder\TabularForm, kartik\checkbox\CheckboxX, kartik\cmenu\ContextMenu, kartik\color\ColorInput, kartik\date\DatePicker, kartik\datecontrol\DateControl, kartik\daterange\DateRangePicker, kartik\datetime\DateTimePicker, kartik\depdrop\DepDrop, kartik\detail\DetailView, kartik\dialog\Dialog, kartik\dynagrid\DynaGrid, kartik\dynagrid\DynaGridDetail, kartik\editable\Editable, kartik\editors\Codemirror, kartik\editors\Summernote, kartik\field\FieldRange, kartik\file\FileInput, kartik\growl\Growl, kartik\ipinfo\IpInfo, kartik\label\LabelInPlace, kartik\markdown\MarkdownEditor, kartik\money\MaskMoney, kartik\nav\NavX, kartik\number\NumberControl, kartik\password\PasswordInput, kartik\popover\PopoverX, kartik\range\RangeInput, kartik\rating\StarRating, kartik\select2\Select2, kartik\slider\Slider, kartik\sortable\Sortable, kartik\sortinput\SortableInput, kartik\spinner\Spinner, kartik\switchinput\SwitchInput, kartik\tabs\TabsX, kartik\time\TimePicker, kartik\touchspin\TouchSpin, kartik\tree\TreeView, kartik\tree\TreeViewInput, kartik\typeahead\Typeahead, kartik\typeahead\TypeaheadBasic, kartik\widgets\Alert, kartik\widgets\AlertBlock, kartik\widgets\ColorInput, kartik\widgets\DatePicker, kartik\widgets\DateTimePicker, kartik\widgets\DepDrop, kartik\widgets\FileInput, kartik\widgets\Growl, kartik\widgets\InputWidget, kartik\widgets\RangeInput, kartik\widgets\Select2, kartik\widgets\Spinner, kartik\widgets\StarRating, kartik\widgets\SwitchInput, kartik\widgets\TimePicker, kartik\widgets\TouchSpin, kartik\widgets\Typeahead, kartik\widgets\TypeaheadBasic, kartik\widgets\Widget

WidgetTrait manages all methods used by Krajee widgets and input widgets.

Public Properties

Hide inherited properties

PropertyTypeDescriptionDefined 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

Hide inherited properties

PropertyTypeDescriptionDefined 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

Hide inherited methods

MethodDescriptionDefined 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

Hide inherited methods

MethodDescriptionDefined 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

$_dataVar protected property

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 string $_dataVar null
$_encOptions protected property

The JSON encoded plugin options.

protected string $_encOptions ''
$_hashVar protected property

The generated hashed variable name that will store the JSON encoded pluginOptions in yii\web\View::POS_HEAD.

protected string $_hashVar null
$baseSourcePath public property

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 property

Default HTML attributes or other settings for widgets.

public array $defaultOptions = []
$defaultPluginOptions public property

Default plugin options for the widget

$hashVarLoadPosition public property

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 integer $hashVarLoadPosition = \yii\web\View::POS_HEAD
$moduleId public property

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 string $moduleId null
$pjaxContainerId public property

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.

public string $pjaxContainerId null
$pjaxDuplicationFix public property

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 to true.
$pluginDestroyJs public property

The javascript that will be used to destroy the jQuery plugin

public string $pluginDestroyJs null
$pluginEvents public property

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 array $pluginEvents = []
$pluginName public property

The plugin name

public string $pluginName ''
$pluginOptions public property

Widget plugin options.

public array $pluginOptions = []
$sourcePath public property

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'
]);
public string $sourcePath null

Method Details

addAsset() protected method

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

fixPjaxDuplication() protected method

Fix for weird PJAX container duplication behavior on pressing browser back and forward buttons.

protected void fixPjaxDuplication ( $view )
$view yii\web\View
throws Exception
getBaseSourcePath() public method

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 ( )
getPluginScript() protected method

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
getView() public method

public yii\web\View getView ( )
return yii\web\View
hashPluginOptions() protected method

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

initDestroyJs() protected method

Generates the pluginDestroyJs script if it is not set.

protected void initDestroyJs ( )
mergeDefaultOptions() protected method

Merge default options

protected void mergeDefaultOptions ( )
registerPlugin() protected method

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
registerPluginOptions() protected method

Registers plugin options by storing within a uniquely generated javascript variable.

protected void registerPluginOptions ( $name )
$name string

The plugin name

throws Exception
registerWidgetJs() public method

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 $js as the key. If two JS code blocks are registered with the same key, the latter will overwrite the former.

throws Exception
setDataVar() protected method

Sets a HTML5 data variable.

protected void setDataVar ( $name )
$name string

The plugin name