-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathEBootstrapCollapse.php
111 lines (96 loc) · 2.56 KB
/
EBootstrapCollapse.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?php
/**
* Creates an collapse sender
* This widget creates an element which can be used to toggle a collapse item
* http://twitter.github.com/bootstrap/javascript.html#collapse
*
* @author Tim Helfensdörfer <[email protected]>
* @version 0.3.0
* @package widgets.bootstrap
*/
class EBootstrapCollapse extends EBootstrapWidget {
/**
* Html element of the sender
*
* Examples: a, input, span, div, h3...
*
*/
public $sender = 'a';
/**
* Default value
*
* The value is rendered between the sender tags
* Example: <a>$value</a>
*/
public $value = 'Toggle';
/**
* Value if the sender is clicked
*
* The value is rendered between the sender tags. If it's set to false there will be no change.
* Example: <a>$value</a>
*/
public $valueToggle = false;
/**
* Selector of the target
*
* Examples: '#myElement', '.elements'
*/
public $target = '';
/**
* Selector of the parent element
*
* If it's unequal false, an accordion functionallity will be applied to the child elements
*/
public $parent = false;
/**
* JS file of the collapse plugin
*
* If its set to false, no file will be included
*/
public $jsFile = null;
/**
* Init the widget
*/
public function init() {
parent::init();
if (!isset($this->htmlOptions['id']))
$this->htmlOptions['id'] = $this->getId();
if (is_null($this->jsFile)) {
$jsFile = dirname(__FILE__).'/js/bootstrap.min.js';
$this->jsFile = Yii::app()->getAssetManager()->publish($jsFile);
Yii::app()->clientScript->registerScriptFile($this->jsFile);
}
elseif ($this->jsFile !== false) {
Yii::app()->clientScript->registerScriptFile($this->jsFile);
}
if ($this->valueToggle !== false) {
switch ($this->sender) {
case 'img':
case 'input':
$jsFunction = 'val';
break;
default:
$jsFunction = 'html';
}
Yii::app()->clientScript->registerScript('ebootstrap-collapse-'.$this->getId(), '
$("'.$this->target.'").on("show", function() {
$("#'.$this->htmlOptions['id'].'").'.$jsFunction.'("'.$this->valueToggle.'");
});
$("'.$this->target.'").on("hide", function() {
$("#'.$this->htmlOptions['id'].'").'.$jsFunction.'("'.$this->value.'");;
});
', CClientScript::POS_READY);
}
}
/**
* Execute the widget
*/
public function run() {
$this->htmlOptions['data-toggle'] = 'collapse';
$this->htmlOptions['data-target'] = $this->target;
if ($this->parent !== false)
$this->htmlOptions['data-parent'] = $this->parent;
echo EBootstrap::tag($this->sender, $this->htmlOptions, $this->value);
}
}
?>