Browse Source

Refactored Modal.

tags/2.0.0-beta
Qiang Xue 12 years ago
parent
commit
cc7c6c7aac
  1. 95
      yii/bootstrap/Modal.php

95
yii/bootstrap/Modal.php

@ -102,34 +102,12 @@ class Modal extends Widget
{ {
parent::init(); parent::init();
$this->options = array_merge(array( $this->initOptions();
'class' => 'modal hide',
), $this->options);
$this->addCssClass($this->options, 'modal');
$this->pluginOptions = array_merge(array(
'show' => false,
), $this->pluginOptions);
if ($this->closeButton !== null) {
$this->closeButton = array_merge(array(
'data-dismiss' => 'modal',
'aria-hidden' => 'true',
'class' => 'close',
), $this->closeButton);
}
if ($this->toggleButton !== null) { echo $this->renderToggleButton() . "\n";
$this->toggleButton = array_merge(array( echo Html::beginTag('div', $this->options) . "\n";
'data-toggle' => 'modal', echo $this->renderHeader() . "\n";
), $this->toggleButton); echo $this->renderBodyBegin() . "\n";
if (!isset($this->toggleButton['data-target']) && !isset($this->toggleButton['href'])) {
$this->toggleButton['data-target'] = '#' . $this->options['id'];
}
}
ob_start();
ob_implicit_flush(false);
} }
/** /**
@ -137,14 +115,9 @@ class Modal extends Widget
*/ */
public function run() public function run()
{ {
$this->body = ob_get_clean() . $this->body; echo "\n" . $this->renderBodyEnd();
echo "\n" . $this->renderFooter();
echo $this->renderToggleButton(); echo "\n" . Html::endTag('div');
$html = $this->renderHeader() . "\n"
. $this->renderBody() . "\n"
. $this->renderFooter();
echo Html::tag('div', "\n" . $html . "\n", $this->options);
$this->registerPlugin('modal'); $this->registerPlugin('modal');
} }
@ -167,12 +140,21 @@ class Modal extends Widget
} }
/** /**
* Renders the HTML markup for the body of the modal * Renders the opening tag of the modal body.
* @return string the rendering result * @return string the rendering result
*/ */
protected function renderBody() protected function renderBodyBegin()
{ {
return Html::tag('div', $this->body, array('class' => 'modal-body')); return Html::beginTag('div', array('class' => 'modal-body'));
}
/**
* Renders the closing tag of the modal body.
* @return string the rendering result
*/
protected function renderBodyEnd()
{
return $this->body . "\n" . Html::endTag('div');
} }
/** /**
@ -182,7 +164,7 @@ class Modal extends Widget
protected function renderFooter() protected function renderFooter()
{ {
if ($this->footer !== null) { if ($this->footer !== null) {
return Html::tag('div', $this->footer, array('class' => 'modal-footer')); return Html::tag('div', "\n" . $this->footer . "\n", array('class' => 'modal-footer'));
} else { } else {
return null; return null;
} }
@ -200,7 +182,7 @@ class Modal extends Widget
if ($tag === 'button' && !isset($this->toggleButton['type'])) { if ($tag === 'button' && !isset($this->toggleButton['type'])) {
$this->toggleButton['type'] = 'button'; $this->toggleButton['type'] = 'button';
} }
return Html::tag($tag, $label, $this->toggleButton) . "\n"; return Html::tag($tag, $label, $this->toggleButton);
} else { } else {
return null; return null;
} }
@ -223,4 +205,37 @@ class Modal extends Widget
return null; return null;
} }
} }
/**
* Initializes the widget options.
* This method sets the default values for various options.
*/
protected function initOptions()
{
$this->options = array_merge(array(
'class' => 'modal hide',
), $this->options);
$this->addCssClass($this->options, 'modal');
$this->pluginOptions = array_merge(array(
'show' => false,
), $this->pluginOptions);
if ($this->closeButton !== null) {
$this->closeButton = array_merge(array(
'data-dismiss' => 'modal',
'aria-hidden' => 'true',
'class' => 'close',
), $this->closeButton);
}
if ($this->toggleButton !== null) {
$this->toggleButton = array_merge(array(
'data-toggle' => 'modal',
), $this->toggleButton);
if (!isset($this->toggleButton['data-target']) && !isset($this->toggleButton['href'])) {
$this->toggleButton['data-target'] = '#' . $this->options['id'];
}
}
}
} }

Loading…
Cancel
Save