You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
107 lines
3.2 KiB
107 lines
3.2 KiB
<?php |
|
/** |
|
* @link http://www.yiiframework.com/ |
|
* @copyright Copyright (c) 2008 Yii Software LLC |
|
* @license http://www.yiiframework.com/license/ |
|
*/ |
|
|
|
namespace yii\bootstrap; |
|
|
|
use Yii; |
|
use yii\helpers\Json; |
|
|
|
/** |
|
* BootstrapWidgetTrait is the trait, which provides basic for all bootstrap widgets features. |
|
* |
|
* Note: class, which uses this trait must declare public field named `options` with the array default value: |
|
* |
|
* ```php |
|
* class MyWidget extends \yii\base\Widget |
|
* { |
|
* use BootstrapWidgetTrait; |
|
* |
|
* public $options = []; |
|
* } |
|
* ``` |
|
* |
|
* This field is not present in the trait in order to avoid possible PHP Fatal error on definition conflict. |
|
* |
|
* @author Antonio Ramirez <amigo.cobos@gmail.com> |
|
* @author Qiang Xue <qiang.xue@gmail.com> |
|
* @author Paul Klimov <klimov.paul@gmail.com> |
|
* @since 2.0.5 |
|
*/ |
|
trait BootstrapWidgetTrait |
|
{ |
|
/** |
|
* @var array the options for the underlying Bootstrap JS plugin. |
|
* Please refer to the corresponding Bootstrap plugin Web page for possible options. |
|
* For example, [this page](http://getbootstrap.com/javascript/#modals) shows |
|
* how to use the "Modal" plugin and the supported options (e.g. "remote"). |
|
*/ |
|
public $clientOptions = []; |
|
/** |
|
* @var array the event handlers for the underlying Bootstrap JS plugin. |
|
* Please refer to the corresponding Bootstrap plugin Web page for possible events. |
|
* For example, [this page](http://getbootstrap.com/javascript/#modals) shows |
|
* how to use the "Modal" plugin and the supported events (e.g. "shown"). |
|
*/ |
|
public $clientEvents = []; |
|
|
|
|
|
/** |
|
* Initializes the widget. |
|
* This method will register the bootstrap asset bundle. If you override this method, |
|
* make sure you call the parent implementation first. |
|
*/ |
|
public function init() |
|
{ |
|
parent::init(); |
|
if (!isset($this->options['id'])) { |
|
$this->options['id'] = $this->getId(); |
|
} |
|
} |
|
|
|
/** |
|
* Registers a specific Bootstrap plugin and the related events |
|
* @param string $name the name of the Bootstrap plugin |
|
*/ |
|
protected function registerPlugin($name) |
|
{ |
|
$view = $this->getView(); |
|
|
|
BootstrapPluginAsset::register($view); |
|
|
|
$id = $this->options['id']; |
|
|
|
if ($this->clientOptions !== false) { |
|
$options = empty($this->clientOptions) ? '' : Json::htmlEncode($this->clientOptions); |
|
$js = "jQuery('#$id').$name($options);"; |
|
$view->registerJs($js); |
|
} |
|
|
|
$this->registerClientEvents(); |
|
} |
|
|
|
/** |
|
* Registers JS event handlers that are listed in [[clientEvents]]. |
|
* @since 2.0.2 |
|
*/ |
|
protected function registerClientEvents() |
|
{ |
|
if (!empty($this->clientEvents)) { |
|
$id = $this->options['id']; |
|
$js = []; |
|
foreach ($this->clientEvents as $event => $handler) { |
|
$js[] = "jQuery('#$id').on('$event', $handler);"; |
|
} |
|
$this->getView()->registerJs(implode("\n", $js)); |
|
} |
|
} |
|
|
|
/** |
|
* @return \yii\web\View the view object that can be used to render views or view files. |
|
* @see yii\base\Widget::getView() |
|
*/ |
|
abstract function getView(); |
|
}
|
|
|