1 <?php
2 3 4 5 6 7 8 9
10
11 12 13 14
15 class TbCollapse extends CWidget
16 {
17 18 19
20 public $tagName = 'div';
21 22 23
24 public $parent = false;
25 26 27
28 public $toggle = true;
29 30 31
32 public $options = array();
33 34 35
36 public $events = array();
37 38 39
40 public $htmlOptions = array();
41
42 private static $_containerId = 0;
43
44 45 46
47 public function init()
48 {
49 if (!isset($this->htmlOptions['id']))
50 $this->htmlOptions['id'] = $this->getId();
51
52 if (isset($this->parent) && !isset($this->options['parent']))
53 $this->options['parent'] = $this->parent;
54
55 if (isset($this->toggle) && !isset($this->options['toggle']))
56 $this->options['toggle'] = $this->toggle;
57
58 echo CHtml::tag($this->tagName, $this->htmlOptions);
59 }
60
61 62 63
64 public function run()
65 {
66 $id = $this->htmlOptions['id'];
67
68 echo CHtml::closeTag($this->tagName);
69
70
71 $cs = Yii::app()->getClientScript();
72 $options = !empty($this->options) ? CJavaScript::encode($this->options) : '';
73 $cs->registerScript(__CLASS__.'#'.$id, "jQuery('#{$id}').collapse({$options});");
74
75 foreach ($this->events as $name => $handler)
76 {
77 $handler = CJavaScript::encode($handler);
78 $cs->registerScript(__CLASS__.'#'.$id.'_'.$name, "jQuery('#{$id}').on('{$name}', {$handler});");
79 }
80 }
81
82 83 84 85 86
87 public static function getNextContainerId()
88 {
89 return 'collapse_'.self::$_containerId++;
90 }
91 }
92
93