From 792d548cd07f43d060f8a7e474264367f56169b8 Mon Sep 17 00:00:00 2001 From: Alexander Kochetov Date: Thu, 23 May 2013 19:28:34 +0400 Subject: [PATCH] jQuery Accordeon widget --- framework/yii/jui/Accordion.php | 107 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100644 framework/yii/jui/Accordion.php diff --git a/framework/yii/jui/Accordion.php b/framework/yii/jui/Accordion.php new file mode 100644 index 0000000..0deab15 --- /dev/null +++ b/framework/yii/jui/Accordion.php @@ -0,0 +1,107 @@ + array( + * 'Section 1' => array( + * 'content' => 'Mauris mauris ante, blandit et, ultrices a, suscipit eget...', + * 'contentOptions' => array(...), + * ), + * 'Section 2' => array( + * 'content' => 'Sed non urna. Phasellus eu ligula. Vestibulum sit amet purus...', + * 'headerOptions' => array(...), + * ), + * ), + * )); + * ``` + * + * @see http://api.jqueryui.com/accordion/ + * @author Alexander Kochetov + * @since 2.0 + */ +class Accordion extends Widget +{ + /** + * @var array list of groups in the collapse widget. Each array element represents a single + * group with the following structure: + * + * ```php + * // item key is the actual group header + * 'Section' => array( + * // required, the content (HTML) of the group + * 'content' => 'Mauris mauris ante, blandit et, ultrices a, suscipit eget...', + * // optional the HTML attributes of the content group + * 'contentOptions'=> array(...), + * // optional the HTML attributes of the header group + * 'headerOptions'=> array(...), + * ) + * ``` + */ + public $items = array(); + + + /** + * Renders the widget. + */ + public function run() + { + echo Html::beginTag('div', $this->options) . "\n"; + echo $this->renderItems() . "\n"; + echo Html::endTag('div') . "\n"; + $this->registerWidget('accordion'); + } + + /** + * Renders collapsible items as specified on [[items]]. + * @return string the rendering result. + */ + public function renderItems() + { + $items = array(); + foreach ($this->items as $header => $item) { + $items[] = $this->renderItem($header, $item); + } + + return implode("\n", $items); + } + + /** + * Renders a single collapsible item group. + * @param string $header a label of the item group [[items]]. + * @param array $item a single item from [[items]]. + * @return string the rendering result. + * @throws InvalidConfigException. + */ + public function renderItem($header, $item) + { + if (isset($item['content'])) { + $contentOptions = ArrayHelper::getValue($item, 'contentOptions', array()); + $content = Html::tag('div', $item['content']) . "\n"; + } else { + throw new InvalidConfigException("The 'content' option is required."); + } + + $group = array(); + $headerOptions = ArrayHelper::getValue($item, 'headerOptions', array()); + $group[] = Html::tag('h3', $header, $headerOptions); + $group[] = Html::tag('div', $content, $contentOptions); + + return implode("\n", $group); + } +}