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();
/** /**
* Initializes the widget. * @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 function init() public $pluginOptions = array();
{
// ensure bundle
$this->registerBundle(static::$responsive);
}
/** /**
* Registers plugin events with the API. * @var array the event handlers for the underlying Bootstrap JS plugin.
* @param string $selector the CSS selector. * Please refer to the corresponding Bootstrap plugin Web page for possible events.
* @param string[] $events the JavaScript event configuration (name=>handler). * For example, [this page](http://twitter.github.io/bootstrap/javascript.html#modals) shows
* @return boolean whether the events were registered. * how to use the "Modal" plugin and the supported events (e.g. "shown").
* @todo To be discussed
*/ */
protected function registerEvents($selector, $events = array()) public $pluginEvents = array();
{
if (empty($events))
return;
$script = '';
foreach ($events as $name => $handler) {
$handler = ($handler instanceof JsExpression)
? $handler
: new JsExpression($handler);
$script .= ";jQuery('{$selector}').on('{$name}', {$handler});";
}
if (!empty($script))
$this->view->registerJs($script);
}
/** /**
* Registers a specific Bootstrap plugin using the given selector and options. * Initializes the widget.
* * This method will register the bootstrap asset bundle. If you override this method,
* @param string $name the name of the javascript widget to initialize * make sure you call the parent implementation first.
* @param array $options the Javascript options for the plugin
*/ */
public function registerPlugin($name, $options = array()) public function init()
{ {
$selector = '#' . ArrayHelper::getValue($this->options, 'id'); parent::init();
$options = !empty($options) ? Json::encode($options) : ''; if (!isset($this->options['id'])) {
$script = ";jQuery('{$selector}').{$name}({$options});"; $this->options['id'] = $this->getId();
$this->view->registerJs($script); }
} }
/** /**
* Registers bootstrap bundle * Registers a specific Bootstrap plugin and the related events
* @param bool $responsive * @param string $name the name of the Bootstrap plugin
*/ */
public function registerBundle($responsive = false) protected function registerPlugin($name)
{ {
$bundle = $responsive ? 'yii/bootstrap-responsive' : 'yii/bootstrap'; $id = $this->options['id'];
$this->view->registerAssetBundle($bundle); $view = $this->getView();
}
$bundle = static::$responsive ? 'yii/bootstrap-responsive' : 'yii/bootstrap';
$view->registerAssetBundle($bundle);
/** if ($this->pluginOptions !== false) {
* Adds a new class to options. If the class key does not exists, it will create one, if it exists it will append $options = empty($this->pluginOptions) ? '' : Json::encode($this->pluginOptions);
* the value and also makes sure the uniqueness of them. $js = "jQuery('#$id').$name($options);";
* $view->registerJs($js);
* @param string $class }
* @return array
*/ if (!empty($this->pluginEvents)) {
protected function addClassName($class) $js = array();
{ foreach ($this->pluginEvents as $event => $handler) {
if (isset($this->options['class'])) { $js[] = "jQuery('#$id').on('$event', $handler);";
if (!is_array($this->options['class'])) }
$this->options['class'] = explode(' ', $this->options['class']); $view->registerJs(implode("\n", $js));
$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;
}
} }
} }
\ No newline at end of file
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