...
|
...
|
@@ -8,6 +8,69 @@ |
|
|
namespace backend\widgets;
|
|
|
|
|
|
use Yii;
|
|
|
use yii\web\View;
|
|
|
use yii\bootstrap\Html;
|
|
|
use yii\helpers\ArrayHelper;
|
|
|
|
|
|
class _Widget extends \yii\base\Widget
|
|
|
{
|
|
|
|
|
|
/**
|
|
|
* @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 = [];
|
|
|
/**
|
|
|
* @var array the HTML attributes for the widget container tag.
|
|
|
* @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
|
|
|
*/
|
|
|
public $options = [];
|
|
|
|
|
|
/**
|
|
|
* 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();
|
|
|
|
|
|
$id = $this->options['id'];
|
|
|
|
|
|
if ($this->clientOptions !== false) {
|
|
|
$options = empty($this->clientOptions) ? '' : Json::htmlEncode($this->clientOptions);
|
|
|
$js = "jQuery('#$id').$name($options);";
|
|
|
$view->registerJs($js, View::POS_END);
|
|
|
}
|
|
|
|
|
|
$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), View::POS_END);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* Alert widget renders a message from session flash. All flash messages are displayed
|
...
|
...
|
@@ -28,7 +91,7 @@ use Yii; |
|
|
* @author Kartik Visweswaran <kartikv2@gmail.com>
|
|
|
* @author Alexander Makarov <sam@rmcreative.ru>
|
|
|
*/
|
|
|
class Alert extends \yii\bootstrap\Widget
|
|
|
class Alert extends _Widget
|
|
|
{
|
|
|
/**
|
|
|
* @var array the alert types configuration for the flash messages.
|
...
|
...
|
@@ -48,7 +111,6 @@ class Alert extends \yii\bootstrap\Widget |
|
|
*/
|
|
|
public $closeButton = [];
|
|
|
|
|
|
|
|
|
public function init()
|
|
|
{
|
|
|
parent::init();
|
...
|
...
|
@@ -67,7 +129,7 @@ class Alert extends \yii\bootstrap\Widget |
|
|
/* assign unique id to each alert box */
|
|
|
$this->options['id'] = $this->getId() . '-' . $type . '-' . $i;
|
|
|
|
|
|
echo \yii\bootstrap\Alert::widget([
|
|
|
echo _Alert::widget([
|
|
|
'body' => $message,
|
|
|
'closeButton' => $this->closeButton,
|
|
|
'options' => $this->options,
|
...
|
...
|
@@ -79,3 +141,107 @@ class Alert extends \yii\bootstrap\Widget |
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
class _Alert extends _Widget
|
|
|
{
|
|
|
/**
|
|
|
* @var string the body content in the alert component. Note that anything between
|
|
|
* the [[begin()]] and [[end()]] calls of the Alert widget will also be treated
|
|
|
* as the body content, and will be rendered before this.
|
|
|
*/
|
|
|
public $body;
|
|
|
/**
|
|
|
* @var array the options for rendering the close button tag.
|
|
|
* The close button is displayed in the header of the modal window. Clicking
|
|
|
* on the button will hide the modal window. If this is false, no close button will be rendered.
|
|
|
*
|
|
|
* The following special options are supported:
|
|
|
*
|
|
|
* - tag: string, the tag name of the button. Defaults to 'button'.
|
|
|
* - label: string, the label of the button. Defaults to '×'.
|
|
|
*
|
|
|
* The rest of the options will be rendered as the HTML attributes of the button tag.
|
|
|
* Please refer to the [Alert documentation](http://getbootstrap.com/components/#alerts)
|
|
|
* for the supported HTML attributes.
|
|
|
*/
|
|
|
public $closeButton = [];
|
|
|
|
|
|
|
|
|
/**
|
|
|
* Initializes the widget.
|
|
|
*/
|
|
|
public function init()
|
|
|
{
|
|
|
parent::init();
|
|
|
|
|
|
$this->initOptions();
|
|
|
|
|
|
echo Html::beginTag('div', $this->options) . "\n";
|
|
|
echo $this->renderBodyBegin() . "\n";
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* Renders the widget.
|
|
|
*/
|
|
|
public function run()
|
|
|
{
|
|
|
echo "\n" . $this->renderBodyEnd();
|
|
|
echo "\n" . Html::endTag('div');
|
|
|
|
|
|
$this->registerPlugin('alert');
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* Renders the close button if any before rendering the content.
|
|
|
* @return string the rendering result
|
|
|
*/
|
|
|
protected function renderBodyBegin()
|
|
|
{
|
|
|
return $this->renderCloseButton();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* Renders the alert body (if any).
|
|
|
* @return string the rendering result
|
|
|
*/
|
|
|
protected function renderBodyEnd()
|
|
|
{
|
|
|
return $this->body . "\n";
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* Renders the close button.
|
|
|
* @return string the rendering result
|
|
|
*/
|
|
|
protected function renderCloseButton()
|
|
|
{
|
|
|
if (($closeButton = $this->closeButton) !== false) {
|
|
|
$tag = ArrayHelper::remove($closeButton, 'tag', 'button');
|
|
|
$label = ArrayHelper::remove($closeButton, 'label', '×');
|
|
|
if ($tag === 'button' && !isset($closeButton['type'])) {
|
|
|
$closeButton['type'] = 'button';
|
|
|
}
|
|
|
|
|
|
return Html::tag($tag, $label, $closeButton);
|
|
|
} else {
|
|
|
return null;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* Initializes the widget options.
|
|
|
* This method sets the default values for various options.
|
|
|
*/
|
|
|
protected function initOptions()
|
|
|
{
|
|
|
Html::addCssClass($this->options, ['alert', 'fade', 'in']);
|
|
|
|
|
|
if ($this->closeButton !== false) {
|
|
|
$this->closeButton = array_merge([
|
|
|
'data-dismiss' => 'alert',
|
|
|
'aria-hidden' => 'true',
|
|
|
'class' => 'close',
|
|
|
], $this->closeButton);
|
|
|
}
|
|
|
}
|
|
|
} |
...
|
...
|
|