Widget.php 3 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 33

	/**
	 * @var array the HTML attributes for the widget container tag.
	 */
	public $options = array();
	/**
34 35 36 37
	 * @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").
38
	 */
39
	public $pluginOptions = array();
40
	/**
41 42 43 44
	 * @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").
45
	 */
46
	public $pluginEvents = array();
Antonio Ramirez committed
47

48 49

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

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

71 72
		$bundle = static::$responsive ? 'yii/bootstrap-responsive' : 'yii/bootstrap';
		$view->registerAssetBundle($bundle);
Antonio Ramirez committed
73

74 75 76 77 78 79 80 81 82 83 84 85 86
		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));
		}
87 88 89
	}

	/**
90 91 92 93
	 * 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
94
	 */
95
	protected function addCssClass(&$options, $class)
96
	{
97 98 99 100 101 102
		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;
		}
103
	}
104
}