Widget.php 2.97 KB
Newer Older
1 2 3 4 5 6 7
<?php
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

Antonio Ramirez committed
8
namespace yii\bootstrap;
9 10 11

use Yii;
use yii\base\View;
12
use yii\helpers\Json;
13

14 15

/**
16
 * \yii\bootstrap\Widget is the base class for all bootstrap widgets.
17 18
 *
 * @author Antonio Ramirez <amigo.cobos@gmail.com>
19
 * @author Qiang Xue <qiang.xue@gmail.com>
20 21
 * @since 2.0
 */
22
class Widget extends \yii\base\Widget
23 24
{
	/**
25
	 * @var boolean whether to use the responsive version of Bootstrap.
26
	 */
Antonio Ramirez committed
27
	public static $responsive = true;
28 29 30 31 32
	/**
	 * @var array the HTML attributes for the widget container tag.
	 */
	public $options = array();
	/**
33 34 35 36
	 * @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://twitter.github.io/bootstrap/javascript.html#modals) shows
	 * how to use the "Modal" plugin and the supported options (e.g. "remote").
37
	 */
38
	public $pluginOptions = array();
39
	/**
40 41 42 43
	 * @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://twitter.github.io/bootstrap/javascript.html#modals) shows
	 * how to use the "Modal" plugin and the supported events (e.g. "shown").
44
	 */
45
	public $pluginEvents = array();
Antonio Ramirez committed
46

47 48

	/**
49 50 51
	 * Initializes the widget.
	 * This method will register the bootstrap asset bundle. If you override this method,
	 * make sure you call the parent implementation first.
52
	 */
53
	public function init()
54
	{
55 56 57 58
		parent::init();
		if (!isset($this->options['id'])) {
			$this->options['id'] = $this->getId();
		}
59 60 61
	}

	/**
62 63
	 * Registers a specific Bootstrap plugin and the related events
	 * @param string $name the name of the Bootstrap plugin
64
	 */
65
	protected function registerPlugin($name)
66
	{
67 68
		$id = $this->options['id'];
		$view = $this->getView();
69
		$view->registerAssetBundle(static::$responsive ? 'yii/bootstrap/responsive' : 'yii/bootstrap');
70

71 72 73 74 75 76 77 78 79 80 81 82 83
		if ($this->pluginOptions !== false) {
			$options = empty($this->pluginOptions) ? '' : Json::encode($this->pluginOptions);
			$js = "jQuery('#$id').$name($options);";
			$view->registerJs($js);
		}

		if (!empty($this->pluginEvents)) {
			$js = array();
			foreach ($this->pluginEvents as $event => $handler) {
				$js[] = "jQuery('#$id').on('$event', $handler);";
			}
			$view->registerJs(implode("\n", $js));
		}
84 85 86
	}

	/**
87 88 89 90
	 * Adds a CSS class to the specified options.
	 * This method will ensure that the CSS class is unique and the "class" option is properly formatted.
	 * @param array $options the options to be modified.
	 * @param string $class the CSS class to be added
91
	 */
92
	protected function addCssClass(&$options, $class)
93
	{
94 95 96 97 98 99
		if (isset($options['class'])) {
			$classes = preg_split('/\s+/', $options['class'] . ' ' . $class, -1, PREG_SPLIT_NO_EMPTY);
			$options['class'] = implode(' ', array_unique($classes));
		} else {
			$options['class'] = $class;
		}
100
	}
101
}