Commit 04a825f4 by Qiang Xue

Finished bootstrap Widget and Modal.

parent 7f9cc397
......@@ -741,7 +741,7 @@ class View extends Component
$lines[] = Html::script(implode("\n", $this->js[self::POS_END]), array('type' => 'text/javascript'));
}
if (!empty($this->js[self::POS_READY])) {
$js = "jQuery(document).ready(function(){\n{" . implode("\n", $this->js[self::POS_READY]) . "}\n});";
$js = "jQuery(document).ready(function(){\n" . implode("\n", $this->js[self::POS_READY]) . "\n});";
$lines[] = Html::script($js, array('type' => 'text/javascript'));
}
return empty($lines) ? '' : implode("\n", $lines) . "\n";
......
......@@ -9,19 +9,20 @@ namespace yii\bootstrap;
use Yii;
use yii\base\View;
use yii\helpers\Json;
/**
* Bootstrap is the base class for bootstrap widgets.
* \yii\bootstrap\Widget is the base class for all bootstrap widgets.
*
* @author Antonio Ramirez <amigo.cobos@gmail.com>
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class Widget extends \yii\base\Widget
{
/**
* @var bool whether to register the asset
* @var boolean whether to use the responsive version of Bootstrap.
*/
public static $responsive = true;
......@@ -29,95 +30,75 @@ class Widget extends \yii\base\Widget
* @var array the HTML attributes for the widget container tag.
*/
public $options = array();
/**
* @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").
*/
public $pluginOptions = array();
/**
* @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").
*/
public $pluginEvents = array();
/**
* Initializes the widget.
* This method will register the bootstrap asset bundle. If you override this method,
* make sure you call the parent implementation first.
*/
public function init()
{
// ensure bundle
$this->registerBundle(static::$responsive);
parent::init();
if (!isset($this->options['id'])) {
$this->options['id'] = $this->getId();
}
}
/**
* 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.
* @todo To be discussed
* Registers a specific Bootstrap plugin and the related events
* @param string $name the name of the Bootstrap plugin
*/
protected function registerEvents($selector, $events = array())
protected function registerPlugin($name)
{
if (empty($events))
return;
$id = $this->options['id'];
$view = $this->getView();
$script = '';
foreach ($events as $name => $handler) {
$handler = ($handler instanceof JsExpression)
? $handler
: new JsExpression($handler);
$bundle = static::$responsive ? 'yii/bootstrap-responsive' : 'yii/bootstrap';
$view->registerAssetBundle($bundle);
$script .= ";jQuery('{$selector}').on('{$name}', {$handler});";
}
if (!empty($script))
$this->view->registerJs($script);
if ($this->pluginOptions !== false) {
$options = empty($this->pluginOptions) ? '' : Json::encode($this->pluginOptions);
$js = "jQuery('#$id').$name($options);";
$view->registerJs($js);
}
/**
* Registers a specific Bootstrap plugin using the given selector and options.
*
* @param string $name the name of the javascript widget to initialize
* @param array $options the Javascript options for the plugin
*/
public function registerPlugin($name, $options = array())
{
$selector = '#' . ArrayHelper::getValue($this->options, 'id');
$options = !empty($options) ? Json::encode($options) : '';
$script = ";jQuery('{$selector}').{$name}({$options});";
$this->view->registerJs($script);
if (!empty($this->pluginEvents)) {
$js = array();
foreach ($this->pluginEvents as $event => $handler) {
$js[] = "jQuery('#$id').on('$event', $handler);";
}
/**
* Registers bootstrap bundle
* @param bool $responsive
*/
public function registerBundle($responsive = false)
{
$bundle = $responsive ? 'yii/bootstrap-responsive' : 'yii/bootstrap';
$this->view->registerAssetBundle($bundle);
$view->registerJs(implode("\n", $js));
}
/**
* 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.
*
* @param string $class
* @return array
*/
protected function addClassName($class)
{
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']);
} else
$this->options['class'] = $class;
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
* 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
*/
protected function defaultOption($key, $value)
protected function addCssClass(&$options, $class)
{
if (!isset($this->options[$key]))
$this->options[$key] = $value;
return $this->options;
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;
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment