|
|
@ -19,13 +19,15 @@ use yii\helpers\Html; |
|
|
|
* ```php |
|
|
|
* ```php |
|
|
|
* echo Accordion::widget(array( |
|
|
|
* echo Accordion::widget(array( |
|
|
|
* 'items' => array( |
|
|
|
* 'items' => array( |
|
|
|
* 'Section 1' => array( |
|
|
|
* array( |
|
|
|
|
|
|
|
* 'header' => 'Section 1', |
|
|
|
* 'content' => 'Mauris mauris ante, blandit et, ultrices a, suscipit eget...', |
|
|
|
* 'content' => 'Mauris mauris ante, blandit et, ultrices a, suscipit eget...', |
|
|
|
* 'contentOptions' => array(...), |
|
|
|
|
|
|
|
* ), |
|
|
|
* ), |
|
|
|
* 'Section 2' => array( |
|
|
|
* array( |
|
|
|
* 'content' => 'Sed non urna. Phasellus eu ligula. Vestibulum sit amet purus...', |
|
|
|
* 'header' => 'Section 2', |
|
|
|
* 'headerOptions' => array(...), |
|
|
|
* 'headerOptions' => array(...), |
|
|
|
|
|
|
|
* 'content' => 'Sed non urna. Phasellus eu ligula. Vestibulum sit amet purus...', |
|
|
|
|
|
|
|
* 'options' => array(...), |
|
|
|
* ), |
|
|
|
* ), |
|
|
|
* ), |
|
|
|
* ), |
|
|
|
* )); |
|
|
|
* )); |
|
|
@ -42,13 +44,14 @@ class Accordion extends Widget |
|
|
|
* section with the following structure: |
|
|
|
* section with the following structure: |
|
|
|
* |
|
|
|
* |
|
|
|
* ```php |
|
|
|
* ```php |
|
|
|
* // item key is the actual section header |
|
|
|
* array( |
|
|
|
* 'Section' => array( |
|
|
|
* // required, the header (HTML) of the section |
|
|
|
|
|
|
|
* 'header' => 'Section label', |
|
|
|
* // required, the content (HTML) of the section |
|
|
|
* // required, the content (HTML) of the section |
|
|
|
* 'content' => 'Mauris mauris ante, blandit et, ultrices a, suscipit eget...', |
|
|
|
* 'content' => 'Mauris mauris ante, blandit et, ultrices a, suscipit eget...', |
|
|
|
* // optional the HTML attributes of the content section |
|
|
|
* // optional the HTML attributes of the section content container |
|
|
|
* 'contentOptions'=> array(...), |
|
|
|
* 'options'=> array(...), |
|
|
|
* // optional the HTML attributes of the header section |
|
|
|
* // optional the HTML attributes of the section header container |
|
|
|
* 'headerOptions'=> array(...), |
|
|
|
* 'headerOptions'=> array(...), |
|
|
|
* ) |
|
|
|
* ) |
|
|
|
* ``` |
|
|
|
* ``` |
|
|
@ -62,46 +65,32 @@ class Accordion extends Widget |
|
|
|
public function run() |
|
|
|
public function run() |
|
|
|
{ |
|
|
|
{ |
|
|
|
echo Html::beginTag('div', $this->options) . "\n"; |
|
|
|
echo Html::beginTag('div', $this->options) . "\n"; |
|
|
|
echo $this->renderItems() . "\n"; |
|
|
|
echo $this->renderSections() . "\n"; |
|
|
|
echo Html::endTag('div') . "\n"; |
|
|
|
echo Html::endTag('div') . "\n"; |
|
|
|
$this->registerWidget('accordion'); |
|
|
|
$this->registerWidget('accordion'); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Renders collapsible items as specified on [[items]]. |
|
|
|
* Renders collapsible sections 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 section. |
|
|
|
|
|
|
|
* @param string $header a label of the item section [[items]]. |
|
|
|
|
|
|
|
* @param array $item a single item from [[items]]. |
|
|
|
|
|
|
|
* @return string the rendering result. |
|
|
|
* @return string the rendering result. |
|
|
|
* @throws InvalidConfigException. |
|
|
|
* @throws InvalidConfigException. |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public function renderItem($header, $item) |
|
|
|
protected function renderSections() |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (isset($item['content'])) { |
|
|
|
$sections = array(); |
|
|
|
$contentOptions = ArrayHelper::getValue($item, 'contentOptions', array()); |
|
|
|
foreach ($this->items as $item) { |
|
|
|
$content = Html::tag('div', $item['content']) . "\n"; |
|
|
|
if (!isset($item['header'])) { |
|
|
|
} else { |
|
|
|
throw new InvalidConfigException("The 'header' option is required."); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (!isset($item['content'])) { |
|
|
|
throw new InvalidConfigException("The 'content' option is required."); |
|
|
|
throw new InvalidConfigException("The 'content' option is required."); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
$group = array(); |
|
|
|
|
|
|
|
$headerOptions = ArrayHelper::getValue($item, 'headerOptions', array()); |
|
|
|
$headerOptions = ArrayHelper::getValue($item, 'headerOptions', array()); |
|
|
|
$group[] = Html::tag('h3', $header, $headerOptions); |
|
|
|
$sections[] = Html::tag('h3', $item['header'], $headerOptions); |
|
|
|
$group[] = Html::tag('div', $content, $contentOptions); |
|
|
|
$options = ArrayHelper::getValue($item, 'options', array()); |
|
|
|
|
|
|
|
$sections[] = Html::tag('div', $item['content'], $options);; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return implode("\n", $group); |
|
|
|
return implode("\n", $sections); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|