Commit 04a825f4 by Qiang Xue

Finished bootstrap Widget and Modal.

parent 7f9cc397
...@@ -741,7 +741,7 @@ class View extends Component ...@@ -741,7 +741,7 @@ class View extends Component
$lines[] = Html::script(implode("\n", $this->js[self::POS_END]), array('type' => 'text/javascript')); $lines[] = Html::script(implode("\n", $this->js[self::POS_END]), array('type' => 'text/javascript'));
} }
if (!empty($this->js[self::POS_READY])) { 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')); $lines[] = Html::script($js, array('type' => 'text/javascript'));
} }
return empty($lines) ? '' : implode("\n", $lines) . "\n"; return empty($lines) ? '' : implode("\n", $lines) . "\n";
......
...@@ -9,19 +9,20 @@ namespace yii\bootstrap; ...@@ -9,19 +9,20 @@ namespace yii\bootstrap;
use Yii; use Yii;
use yii\base\View; 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 Antonio Ramirez <amigo.cobos@gmail.com>
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0 * @since 2.0
*/ */
class Widget extends \yii\base\Widget 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; public static $responsive = true;
...@@ -29,95 +30,75 @@ class Widget extends \yii\base\Widget ...@@ -29,95 +30,75 @@ class Widget extends \yii\base\Widget
* @var array the HTML attributes for the widget container tag. * @var array the HTML attributes for the widget container tag.
*/ */
public $options = array(); 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. * 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() public function init()
{ {
// ensure bundle parent::init();
$this->registerBundle(static::$responsive); if (!isset($this->options['id'])) {
$this->options['id'] = $this->getId();
}
} }
/** /**
* Registers plugin events with the API. * Registers a specific Bootstrap plugin and the related events
* @param string $selector the CSS selector. * @param string $name the name of the Bootstrap plugin
* @param string[] $events the JavaScript event configuration (name=>handler).
* @return boolean whether the events were registered.
* @todo To be discussed
*/ */
protected function registerEvents($selector, $events = array()) protected function registerPlugin($name)
{ {
if (empty($events)) $id = $this->options['id'];
return; $view = $this->getView();
$script = ''; $bundle = static::$responsive ? 'yii/bootstrap-responsive' : 'yii/bootstrap';
foreach ($events as $name => $handler) { $view->registerAssetBundle($bundle);
$handler = ($handler instanceof JsExpression)
? $handler
: new JsExpression($handler);
$script .= ";jQuery('{$selector}').on('{$name}', {$handler});"; if ($this->pluginOptions !== false) {
} $options = empty($this->pluginOptions) ? '' : Json::encode($this->pluginOptions);
if (!empty($script)) $js = "jQuery('#$id').$name($options);";
$this->view->registerJs($script); $view->registerJs($js);
} }
/** if (!empty($this->pluginEvents)) {
* Registers a specific Bootstrap plugin using the given selector and options. $js = array();
* foreach ($this->pluginEvents as $event => $handler) {
* @param string $name the name of the javascript widget to initialize $js[] = "jQuery('#$id').on('$event', $handler);";
* @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);
} }
$view->registerJs(implode("\n", $js));
/**
* Registers bootstrap bundle
* @param bool $responsive
*/
public function registerBundle($responsive = false)
{
$bundle = $responsive ? 'yii/bootstrap-responsive' : 'yii/bootstrap';
$this->view->registerAssetBundle($bundle);
} }
/**
* 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. * Adds a CSS class to the specified options.
* @param string $key the name of the item. * This method will ensure that the CSS class is unique and the "class" option is properly formatted.
* @param mixed $value the default value. * @param array $options the options to be modified.
* @return array * @param string $class the CSS class to be added
*/ */
protected function defaultOption($key, $value) protected function addCssClass(&$options, $class)
{ {
if (!isset($this->options[$key])) if (isset($options['class'])) {
$this->options[$key] = $value; $classes = preg_split('/\s+/', $options['class'] . ' ' . $class, -1, PREG_SPLIT_NO_EMPTY);
return $this->options; $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