1: <?php
2: /**
3: * TbCollapse class file.
4: * @author Christoffer Niska <[email protected]>
5: * @copyright Copyright © Christoffer Niska 2013-
6: * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
7: * @package bootstrap.widgets
8: */
9:
10: Yii::import('bootstrap.behaviors.TbWidget');
11: Yii::import('bootstrap.helpers.TbHtml');
12:
13: /**
14: * Bootstrap collapse widget.
15: * @see http://twitter.github.com/bootstrap/javascript.html#collapse
16: */
17: class TbCollapse extends CWidget
18: {
19: /**
20: * @var string the HTML tag for the container.
21: */
22: public $tagName = 'div';
23: /**
24: * @var string the content text.
25: */
26: public $content;
27: /**
28: * @var string the path to a partial view.
29: */
30: public $view;
31: /**
32: * @var string the CSS selector for the parent element.
33: */
34: public $parent;
35: /**
36: * @var boolean whether to be collapsed on invocation.
37: */
38: public $toggle;
39: /**
40: * @var string[] $events the JavaScript event configuration (name=>handler).
41: */
42: public $events = array();
43: /**
44: * @var array the HTML attributes for the container.
45: */
46: public $htmlOptions = array();
47: /**
48: * @var array additional data to be passed to the view.
49: */
50: public $viewData = array();
51:
52: /**
53: * Initializes the widget.
54: */
55: public function init()
56: {
57: $this->attachBehavior('TbWidget', new TbWidget);
58: $this->copyId();
59: TbHtml::addCssClass('collapse', $this->htmlOptions);
60: if (isset($this->parent)) {
61: TbArray::defaultValue('data-parent', $this->parent, $this->htmlOptions);
62: }
63: if (isset($this->toggle) && $this->toggle) {
64: TbHtml::addCssClass('in', $this->htmlOptions);
65: }
66: if (isset($this->view)) {
67: $controller = $this->getController();
68: if (isset($controller) && $controller->getViewFile($this->view) !== false) {
69: $this->content = $this->controller->renderPartial($this->view, $this->viewData, true);
70: }
71: }
72: echo TbHtml::openTag($this->tagName, $this->htmlOptions);
73: echo $this->content;
74: }
75:
76: /**
77: * Runs the widget.
78: */
79: public function run()
80: {
81: echo CHtml::closeTag($this->tagName);
82: $selector = '#' . $this->htmlOptions['id'];
83: $this->registerEvents($selector, $this->events);
84: }
85: }