Qiang Xue
12 years ago
23 changed files with 642 additions and 47 deletions
@ -0,0 +1,133 @@
|
||||
<?php |
||||
/** |
||||
* @link http://www.yiiframework.com/ |
||||
* @copyright Copyright (c) 2008 Yii Software LLC |
||||
* @license http://www.yiiframework.com/license/ |
||||
*/ |
||||
|
||||
namespace yii\bootstrap; |
||||
|
||||
use yii\base\InvalidConfigException; |
||||
use yii\helpers\base\ArrayHelper; |
||||
use yii\helpers\Html; |
||||
|
||||
/** |
||||
* Collapse renders an accordion bootstrap javascript component. |
||||
* |
||||
* For example: |
||||
* |
||||
* ```php |
||||
* echo Collapse::widget(array( |
||||
* 'items' => array( |
||||
* // equivalent to the above |
||||
* 'Collapsible Group Item #1' => array( |
||||
* 'content' => 'Anim pariatur cliche...', |
||||
* // open its content by default |
||||
* 'contentOptions' => array('class'=>'in') |
||||
* ), |
||||
* // another group item |
||||
* 'Collapsible Group Item #2' => array( |
||||
* 'content' => 'Anim pariatur cliche...', |
||||
* 'contentOptions' => array(...), |
||||
* 'options' => array(...), |
||||
* ), |
||||
* ) |
||||
* )); |
||||
* ``` |
||||
* |
||||
* @see http://twitter.github.io/bootstrap/javascript.html#collapse |
||||
* @author Antonio Ramirez <amigo.cobos@gmail.com> |
||||
* @since 2.0 |
||||
*/ |
||||
class Collapse 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 |
||||
* 'Collapsible Group Item #1' => array( |
||||
* // required, the content (HTML) of the group |
||||
* 'content' => 'Anim pariatur cliche...', |
||||
* // optional the HTML attributes of the content group |
||||
* 'contentOptions'=> array(), |
||||
* // optional the HTML attributes of the group |
||||
* 'options'=> array(), |
||||
* ) |
||||
* ``` |
||||
*/ |
||||
public $items = array(); |
||||
|
||||
|
||||
/** |
||||
* Initializes the widget. |
||||
*/ |
||||
public function init() |
||||
{ |
||||
parent::init(); |
||||
$this->addCssClass($this->options, 'accordion'); |
||||
} |
||||
|
||||
/** |
||||
* Renders the widget. |
||||
*/ |
||||
public function run() |
||||
{ |
||||
echo Html::beginTag('div', $this->options) . "\n"; |
||||
echo $this->renderItems() . "\n"; |
||||
echo Html::endTag('div') . "\n"; |
||||
$this->registerPlugin('collapse'); |
||||
} |
||||
|
||||
/** |
||||
* Renders collapsible items as specified on [[items]]. |
||||
* @return string the rendering result |
||||
*/ |
||||
public function renderItems() |
||||
{ |
||||
$items = array(); |
||||
$index = 0; |
||||
foreach ($this->items as $header => $item) { |
||||
$options = ArrayHelper::getValue($item, 'options', array()); |
||||
$this->addCssClass($options, 'accordion-group'); |
||||
$items[] = Html::tag('div', $this->renderItem($header, $item, ++$index), $options); |
||||
} |
||||
|
||||
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]] |
||||
* @param integer $index the item index as each item group content must have an id |
||||
* @return string the rendering result |
||||
* @throws InvalidConfigException |
||||
*/ |
||||
public function renderItem($header, $item, $index) |
||||
{ |
||||
if (isset($item['content'])) { |
||||
$id = $this->options['id'] . '-collapse' . $index; |
||||
$options = ArrayHelper::getValue($item, 'contentOptions', array()); |
||||
$options['id'] = $id; |
||||
$this->addCssClass($options, 'accordion-body collapse'); |
||||
|
||||
$header = Html::a($header, '#' . $id, array( |
||||
'class' => 'accordion-toggle', |
||||
'data-toggle' => 'collapse', |
||||
'data-parent' => '#' . $this->options['id'] |
||||
)) . "\n"; |
||||
|
||||
$content = Html::tag('div', $item['content'], array('class' => 'accordion-inner')) . "\n"; |
||||
} else { |
||||
throw new InvalidConfigException('The "content" option is required.'); |
||||
} |
||||
$group = array(); |
||||
|
||||
$group[] = Html::tag('div', $header, array('class' => 'accordion-heading')); |
||||
$group[] = Html::tag('div', $content, $options); |
||||
|
||||
return implode("\n", $group); |
||||
} |
||||
} |
@ -0,0 +1,96 @@
|
||||
<?php |
||||
/** |
||||
* @link http://www.yiiframework.com/ |
||||
* @copyright Copyright (c) 2008 Yii Software LLC |
||||
* @license http://www.yiiframework.com/license/ |
||||
*/ |
||||
|
||||
namespace yii\jui; |
||||
|
||||
use yii\base\InvalidConfigException; |
||||
use yii\helpers\base\ArrayHelper; |
||||
use yii\helpers\Html; |
||||
|
||||
/** |
||||
* Accordion renders an accordion jQuery UI widget. |
||||
* |
||||
* For example: |
||||
* |
||||
* ```php |
||||
* echo Accordion::widget(array( |
||||
* 'items' => array( |
||||
* array( |
||||
* 'header' => 'Section 1', |
||||
* 'content' => 'Mauris mauris ante, blandit et, ultrices a, suscipit eget...', |
||||
* ), |
||||
* array( |
||||
* 'header' => 'Section 2', |
||||
* 'headerOptions' => array(...), |
||||
* 'content' => 'Sed non urna. Phasellus eu ligula. Vestibulum sit amet purus...', |
||||
* 'options' => array(...), |
||||
* ), |
||||
* ), |
||||
* )); |
||||
* ``` |
||||
* |
||||
* @see http://api.jqueryui.com/accordion/ |
||||
* @author Alexander Kochetov <creocoder@gmail.com> |
||||
* @since 2.0 |
||||
*/ |
||||
class Accordion extends Widget |
||||
{ |
||||
/** |
||||
* @var array list of sections in the accordion widget. Each array element represents a single |
||||
* section with the following structure: |
||||
* |
||||
* ```php |
||||
* array( |
||||
* // required, the header (HTML) of the section |
||||
* 'header' => 'Section label', |
||||
* // required, the content (HTML) of the section |
||||
* 'content' => 'Mauris mauris ante, blandit et, ultrices a, suscipit eget...', |
||||
* // optional the HTML attributes of the section content container |
||||
* 'options'=> array(...), |
||||
* // optional the HTML attributes of the section header container |
||||
* 'headerOptions'=> array(...), |
||||
* ) |
||||
* ``` |
||||
*/ |
||||
public $items = array(); |
||||
|
||||
|
||||
/** |
||||
* Renders the widget. |
||||
*/ |
||||
public function run() |
||||
{ |
||||
echo Html::beginTag('div', $this->options) . "\n"; |
||||
echo $this->renderSections() . "\n"; |
||||
echo Html::endTag('div') . "\n"; |
||||
$this->registerWidget('accordion'); |
||||
} |
||||
|
||||
/** |
||||
* Renders collapsible sections as specified on [[items]]. |
||||
* @return string the rendering result. |
||||
* @throws InvalidConfigException. |
||||
*/ |
||||
protected function renderSections() |
||||
{ |
||||
$sections = array(); |
||||
foreach ($this->items as $item) { |
||||
if (!isset($item['header'])) { |
||||
throw new InvalidConfigException("The 'header' option is required."); |
||||
} |
||||
if (!isset($item['content'])) { |
||||
throw new InvalidConfigException("The 'content' option is required."); |
||||
} |
||||
$headerOptions = ArrayHelper::getValue($item, 'headerOptions', array()); |
||||
$sections[] = Html::tag('h3', $item['header'], $headerOptions); |
||||
$options = ArrayHelper::getValue($item, 'options', array()); |
||||
$sections[] = Html::tag('div', $item['content'], $options);; |
||||
} |
||||
|
||||
return implode("\n", $sections); |
||||
} |
||||
} |
@ -0,0 +1,86 @@
|
||||
<?php |
||||
/** |
||||
* @link http://www.yiiframework.com/ |
||||
* @copyright Copyright (c) 2008 Yii Software LLC |
||||
* @license http://www.yiiframework.com/license/ |
||||
*/ |
||||
|
||||
namespace yii\jui; |
||||
|
||||
use Yii; |
||||
use yii\base\View; |
||||
use yii\helpers\Json; |
||||
|
||||
|
||||
/** |
||||
* Menu renders a menu jQuery UI widget. |
||||
* |
||||
* @see http://api.jqueryui.com/menu/ |
||||
* @author Alexander Kochetov <creocoder@gmail.com> |
||||
* @since 2.0 |
||||
*/ |
||||
class Menu extends \yii\widgets\Menu |
||||
{ |
||||
/** |
||||
* @var array the options for the underlying jQuery UI widget. |
||||
* Please refer to the corresponding jQuery UI widget Web page for possible options. |
||||
* For example, [this page](http://api.jqueryui.com/accordion/) shows |
||||
* how to use the "Accordion" widget and the supported options (e.g. "header"). |
||||
*/ |
||||
public $clientOptions = array(); |
||||
/** |
||||
* @var array the event handlers for the underlying jQuery UI widget. |
||||
* Please refer to the corresponding jQuery UI widget Web page for possible events. |
||||
* For example, [this page](http://api.jqueryui.com/accordion/) shows |
||||
* how to use the "Accordion" widget and the supported events (e.g. "create"). |
||||
*/ |
||||
public $clientEvents = array(); |
||||
|
||||
|
||||
/** |
||||
* Initializes the widget. |
||||
* If you override this method, make sure you call the parent implementation first. |
||||
*/ |
||||
public function init() |
||||
{ |
||||
parent::init(); |
||||
if (!isset($this->options['id'])) { |
||||
$this->options['id'] = $this->getId(); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* Renders the widget. |
||||
*/ |
||||
public function run() |
||||
{ |
||||
parent::run(); |
||||
$this->registerWidget('menu'); |
||||
} |
||||
|
||||
/** |
||||
* Registers a specific jQuery UI widget and the related events |
||||
* @param string $name the name of the jQuery UI widget |
||||
*/ |
||||
protected function registerWidget($name) |
||||
{ |
||||
$id = $this->options['id']; |
||||
$view = $this->getView(); |
||||
$view->registerAssetBundle("yii/jui/$name"); |
||||
$view->registerAssetBundle(Widget::$theme . "/$name"); |
||||
|
||||
if ($this->clientOptions !== false) { |
||||
$options = empty($this->clientOptions) ? '' : Json::encode($this->clientOptions); |
||||
$js = "jQuery('#$id').$name($options);"; |
||||
$view->registerJs($js); |
||||
} |
||||
|
||||
if (!empty($this->clientEvents)) { |
||||
$js = array(); |
||||
foreach ($this->clientEvents as $event => $handler) { |
||||
$js[] = "jQuery('#$id').on('$name$event', $handler);"; |
||||
} |
||||
$view->registerJs(implode("\n", $js)); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,116 @@
|
||||
<?php |
||||
/** |
||||
* @link http://www.yiiframework.com/ |
||||
* @copyright Copyright (c) 2008 Yii Software LLC |
||||
* @license http://www.yiiframework.com/license/ |
||||
*/ |
||||
|
||||
namespace yii\jui; |
||||
|
||||
use yii\base\InvalidConfigException; |
||||
use yii\helpers\base\ArrayHelper; |
||||
use yii\helpers\Html; |
||||
|
||||
/** |
||||
* Tabs renders a tabs jQuery UI widget. |
||||
* |
||||
* For example: |
||||
* |
||||
* ```php |
||||
* echo Tabs::widget(array( |
||||
* 'items' => array( |
||||
* array( |
||||
* 'header' => 'One', |
||||
* 'content' => 'Mauris mauris ante, blandit et, ultrices a, suscipit eget...', |
||||
* ), |
||||
* array( |
||||
* 'header' => 'Two', |
||||
* 'headerOptions' => array(...), |
||||
* 'content' => 'Sed non urna. Phasellus eu ligula. Vestibulum sit amet purus...', |
||||
* 'options' => array(...), |
||||
* ), |
||||
* ), |
||||
* )); |
||||
* ``` |
||||
* |
||||
* @see http://api.jqueryui.com/tabs/ |
||||
* @author Alexander Kochetov <creocoder@gmail.com> |
||||
* @since 2.0 |
||||
*/ |
||||
class Tabs extends Widget |
||||
{ |
||||
/** |
||||
* @var array list of tabs in the tabs widget. Each array element represents a single |
||||
* tab with the following structure: |
||||
* |
||||
* ```php |
||||
* array( |
||||
* // required, the header (HTML) of the tab |
||||
* 'header' => 'Tab label', |
||||
* // required, the content (HTML) of the tab |
||||
* 'content' => 'Mauris mauris ante, blandit et, ultrices a, suscipit eget...', |
||||
* // optional the HTML attributes of the tab content container |
||||
* 'options'=> array(...), |
||||
* // optional the HTML attributes of the tab header container |
||||
* 'headerOptions'=> array(...), |
||||
* ) |
||||
* ``` |
||||
*/ |
||||
public $items = array(); |
||||
|
||||
|
||||
/** |
||||
* Renders the widget. |
||||
*/ |
||||
public function run() |
||||
{ |
||||
echo Html::beginTag('div', $this->options) . "\n"; |
||||
echo $this->renderHeaders() . "\n"; |
||||
echo $this->renderContents() . "\n"; |
||||
echo Html::endTag('div') . "\n"; |
||||
$this->registerWidget('tabs'); |
||||
} |
||||
|
||||
/** |
||||
* Renders tabs headers as specified on [[items]]. |
||||
* @return string the rendering result. |
||||
* @throws InvalidConfigException. |
||||
*/ |
||||
protected function renderHeaders() |
||||
{ |
||||
$headers = array(); |
||||
foreach ($this->items as $n => $item) { |
||||
if (!isset($item['header'])) { |
||||
throw new InvalidConfigException("The 'header' option is required."); |
||||
} |
||||
$options = ArrayHelper::getValue($item, 'options', array()); |
||||
$id = isset($options['id']) ? $options['id'] : $this->options['id'] . '-tab' . $n; |
||||
$headerOptions = ArrayHelper::getValue($item, 'headerOptions', array()); |
||||
$headers[] = Html::tag('li', Html::a($item['header'], "#$id"), $headerOptions); |
||||
} |
||||
|
||||
return Html::tag('ul', implode("\n", $headers)); |
||||
} |
||||
|
||||
/** |
||||
* Renders tabs contents as specified on [[items]]. |
||||
* @return string the rendering result. |
||||
* @throws InvalidConfigException. |
||||
*/ |
||||
protected function renderContents() |
||||
{ |
||||
$contents = array(); |
||||
foreach ($this->items as $n => $item) { |
||||
if (!isset($item['content'])) { |
||||
throw new InvalidConfigException("The 'content' option is required."); |
||||
} |
||||
$options = ArrayHelper::getValue($item, 'options', array()); |
||||
if (!isset($options['id'])) { |
||||
$options['id'] = $this->options['id'] . '-tab' . $n; |
||||
} |
||||
$contents[] = Html::tag('div', $item['content'], $options); |
||||
} |
||||
|
||||
return implode("\n", $contents); |
||||
} |
||||
} |
Loading…
Reference in new issue