From cc7c6c7aacd62594c3994a15fd50577119570a48 Mon Sep 17 00:00:00 2001 From: Qiang Xue Date: Mon, 20 May 2013 21:18:19 -0400 Subject: [PATCH] Refactored Modal. --- yii/bootstrap/Modal.php | 95 ++++++++++++++++++++++++++++--------------------- 1 file changed, 55 insertions(+), 40 deletions(-) diff --git a/yii/bootstrap/Modal.php b/yii/bootstrap/Modal.php index b3b13b3..1a70209 100644 --- a/yii/bootstrap/Modal.php +++ b/yii/bootstrap/Modal.php @@ -102,34 +102,12 @@ class Modal extends Widget { parent::init(); - $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); - } + $this->initOptions(); - 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']; - } - } - - ob_start(); - ob_implicit_flush(false); + echo $this->renderToggleButton() . "\n"; + echo Html::beginTag('div', $this->options) . "\n"; + echo $this->renderHeader() . "\n"; + echo $this->renderBodyBegin() . "\n"; } /** @@ -137,14 +115,9 @@ class Modal extends Widget */ public function run() { - $this->body = ob_get_clean() . $this->body; - - echo $this->renderToggleButton(); - - $html = $this->renderHeader() . "\n" - . $this->renderBody() . "\n" - . $this->renderFooter(); - echo Html::tag('div', "\n" . $html . "\n", $this->options); + echo "\n" . $this->renderBodyEnd(); + echo "\n" . $this->renderFooter(); + echo "\n" . Html::endTag('div'); $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 */ - 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() { 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 { return null; } @@ -200,7 +182,7 @@ class Modal extends Widget if ($tag === 'button' && !isset($this->toggleButton['type'])) { $this->toggleButton['type'] = 'button'; } - return Html::tag($tag, $label, $this->toggleButton) . "\n"; + return Html::tag($tag, $label, $this->toggleButton); } else { return null; } @@ -223,4 +205,37 @@ class Modal extends Widget 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']; + } + } + } }