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.
132 lines
3.4 KiB
132 lines
3.4 KiB
12 years ago
|
<?php
|
||
|
/**
|
||
|
* @link http://www.yiiframework.com/
|
||
|
* @copyright Copyright (c) 2008 Yii Software LLC
|
||
|
* @license http://www.yiiframework.com/license/
|
||
|
*/
|
||
|
|
||
12 years ago
|
namespace yii\bootstrap;
|
||
12 years ago
|
|
||
|
use Yii;
|
||
|
use yii\base\View;
|
||
12 years ago
|
|
||
12 years ago
|
|
||
|
/**
|
||
12 years ago
|
* Bootstrap is the base class for bootstrap widgets.
|
||
12 years ago
|
*
|
||
|
* @author Antonio Ramirez <amigo.cobos@gmail.com>
|
||
|
* @since 2.0
|
||
|
*/
|
||
12 years ago
|
class Widget extends \yii\base\Widget
|
||
12 years ago
|
{
|
||
|
|
||
|
/**
|
||
|
* @var bool whether to register the asset
|
||
|
*/
|
||
12 years ago
|
public static $responsive = true;
|
||
12 years ago
|
|
||
|
/**
|
||
|
* @var array the HTML attributes for the widget container tag.
|
||
|
*/
|
||
|
public $options = array();
|
||
|
|
||
|
/**
|
||
12 years ago
|
* @var string the widget name (ie. modal, typeahead, tab)
|
||
12 years ago
|
*/
|
||
|
protected $name;
|
||
|
|
||
|
/**
|
||
|
* Initializes the widget.
|
||
|
*/
|
||
|
public function init()
|
||
|
{
|
||
12 years ago
|
// ensure bundle
|
||
12 years ago
|
$this->registerBundle(static::$responsive);
|
||
12 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* Registers plugin events with the API.
|
||
|
* @param string $selector the CSS selector.
|
||
|
* @param string[] $events the JavaScript event configuration (name=>handler).
|
||
|
* @param int $position the position of the JavaScript code.
|
||
|
* @return boolean whether the events were registered.
|
||
12 years ago
|
* @todo To be discussed
|
||
12 years ago
|
*/
|
||
|
protected function registerEvents($selector, $events = array(), $position = View::POS_END)
|
||
|
{
|
||
12 years ago
|
if (empty($events))
|
||
|
return;
|
||
|
|
||
|
$script = '';
|
||
|
foreach ($events as $name => $handler) {
|
||
|
$handler = ($handler instanceof JsExpression)
|
||
|
? $handler
|
||
|
: new JsExpression($handler);
|
||
|
|
||
|
$script .= ";jQuery(document).ready(function (){jQuery('{$selector}').on('{$name}', {$handler});});";
|
||
|
}
|
||
|
if (!empty($script))
|
||
|
$this->view->registerJs($script, array('position' => $position));
|
||
12 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* Registers a specific Bootstrap plugin using the given selector and options.
|
||
12 years ago
|
*
|
||
|
* @param string $selector the CSS selector
|
||
|
* @param array $options the Javascript options for the plugin
|
||
|
* @param int $position the position of the JavaScript code
|
||
12 years ago
|
*/
|
||
|
public function registerPlugin($selector, $options = array(), $position = View::POS_END)
|
||
|
{
|
||
12 years ago
|
$options = !empty($options) ? Json::encode($options) : '';
|
||
|
$script = ";jQuery(document).ready(function (){jQuery('{$selector}').{$this->name}({$options});});";
|
||
|
$this->view->registerJs($script, array('position' => $position));
|
||
12 years ago
|
}
|
||
|
|
||
|
/**
|
||
12 years ago
|
* Registers bootstrap bundle
|
||
|
* @param bool $responsive
|
||
12 years ago
|
*/
|
||
12 years ago
|
public function registerBundle($responsive = false)
|
||
12 years ago
|
{
|
||
12 years ago
|
$bundle = $responsive ? 'yii/bootstrap' : 'yii/bootstrap-responsive';
|
||
|
|
||
|
$this->view->registerAssetBundle($bundle);
|
||
12 years ago
|
}
|
||
|
|
||
12 years ago
|
|
||
12 years ago
|
/**
|
||
|
* Adds a new option. If the key does not exists, it will create one, if it exists it will append the value
|
||
|
* and also makes sure the uniqueness of them.
|
||
|
*
|
||
|
* @param string $key
|
||
|
* @param mixed $value
|
||
|
* @param string $glue
|
||
|
* @return array
|
||
|
*/
|
||
|
protected function addOption($key, $value, $glue = ' ')
|
||
|
{
|
||
|
if (isset($this->options[$key])) {
|
||
|
if (!is_array($this->options[$key]))
|
||
|
$this->options[$key] = explode($glue, $this->options[$key]);
|
||
|
$this->options[$key][] = $value;
|
||
|
$this->options[$key] = array_unique($this->options[$key]);
|
||
|
$this->options[$key] = implode($glue, $this->options[$key]);
|
||
|
} else
|
||
|
$this->options[$key] = $value;
|
||
|
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;
|
||
|
}
|
||
|
}
|