Widget.php 3.05 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

13 14

/**
15
 * Bootstrap is the base class for bootstrap widgets.
16 17 18 19
 *
 * @author Antonio Ramirez <amigo.cobos@gmail.com>
 * @since 2.0
 */
20
class Widget extends \yii\base\Widget
21 22 23 24 25
{

	/**
	 * @var bool whether to register the asset
	 */
Antonio Ramirez committed
26
	public static $responsive = true;
27 28 29 30 31 32 33 34 35 36 37

	/**
	 * @var array the HTML attributes for the widget container tag.
	 */
	public $options = array();

	/**
	 * Initializes the widget.
	 */
	public function init()
	{
38
		// ensure bundle
Antonio Ramirez committed
39
		$this->registerBundle(static::$responsive);
40 41 42 43 44 45 46
	}

	/**
	 * Registers plugin events with the API.
	 * @param string $selector the CSS selector.
	 * @param string[] $events  the JavaScript event configuration (name=>handler).
	 * @return boolean whether the events were registered.
Antonio Ramirez committed
47
	 * @todo To be discussed
48
	 */
49
	protected function registerEvents($selector, $events = array())
50
	{
Antonio Ramirez committed
51 52 53 54 55 56 57 58 59
		if (empty($events))
			return;

		$script = '';
		foreach ($events as $name => $handler) {
			$handler = ($handler instanceof JsExpression)
				? $handler
				: new JsExpression($handler);

60
			$script .= ";jQuery('{$selector}').on('{$name}', {$handler});";
Antonio Ramirez committed
61 62
		}
		if (!empty($script))
63
			$this->view->registerJs($script);
64 65 66 67
	}

	/**
	 * Registers a specific Bootstrap plugin using the given selector and options.
Antonio Ramirez committed
68
	 *
69
	 * @param string $name the name of the javascript widget to initialize
Antonio Ramirez committed
70
	 * @param array $options the Javascript options for the plugin
71
	 */
72
	public function registerPlugin($name, $options = array())
73
	{
74
		$selector = '#' . ArrayHelper::getValue($this->options, 'id');
Antonio Ramirez committed
75
		$options = !empty($options) ? Json::encode($options) : '';
76 77
		$script = ";jQuery('{$selector}').{$name}({$options});";
		$this->view->registerJs($script);
78 79 80
	}

	/**
Antonio Ramirez committed
81 82
	 * Registers bootstrap bundle
	 * @param bool $responsive
83
	 */
Antonio Ramirez committed
84
	public function registerBundle($responsive = false)
85
	{
86
		$bundle = $responsive ? 'yii/bootstrap-responsive' : 'yii/bootstrap';
Antonio Ramirez committed
87
		$this->view->registerAssetBundle($bundle);
88 89
	}

Antonio Ramirez committed
90

91
	/**
92 93
	 * Adds a new class to options. If the class key does not exists, it will create one, if it exists it will append
	 * the value and also makes sure the uniqueness of them.
94
	 *
95
	 * @param string $class
96 97
	 * @return array
	 */
98
	protected function addClassName($class)
99
	{
100 101 102 103 104 105
		if (isset($this->options['class'])) {
			if (!is_array($this->options['class']))
				$this->options['class'] = explode(' ', $this->options['class']);
			$this->options['class'][] = $class;
			$this->options['class'] = array_unique($this->options['class']);
			$this->options['class'] = implode(' ', $this->options['class']);
106
		} else
107
			$this->options['class'] = $class;
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
		return $this->options;
	}

	/**
	 * Sets the default value for an item if not set.
	 * @param string $key the name of the item.
	 * @param mixed $value the default value.
	 * @return array
	 */
	protected function defaultOption($key, $value)
	{
		if (!isset($this->options[$key]))
			$this->options[$key] = $value;
		return $this->options;
	}
}