1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ajax\bootstrap\html; |
4
|
|
|
|
5
|
|
|
use Ajax\bootstrap\html\base\CssRef; |
6
|
|
|
use Ajax\bootstrap\html\base\HtmlBsDoubleElement; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Twitter Bootstrap Buttongroups component |
10
|
|
|
* @see http://getbootstrap.com/components/#btn-groups |
11
|
|
|
* @author jc |
12
|
|
|
* @version 1.001 |
13
|
|
|
*/ |
14
|
|
|
class HtmlButtongroups extends HtmlBsDoubleElement { |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var array[HtmlButton] |
18
|
|
|
*/ |
19
|
|
|
protected $elements; |
20
|
|
|
|
21
|
|
View Code Duplication |
public function __construct($identifier, $elements=array(), $cssStyle=NULL, $size=NULL, $tagName="div") { |
|
|
|
|
22
|
|
|
parent::__construct($identifier, $tagName); |
23
|
|
|
$this->_template="<%tagName% id='%identifier%' %properties%>%elements%</%tagName%>"; |
24
|
|
|
$this->setProperty("class", "btn-group"); |
25
|
|
|
$this->setRole("group"); |
26
|
|
|
$this->addElements($elements); |
27
|
|
|
if (isset($cssStyle)) { |
28
|
|
|
$this->setStyle($cssStyle); |
29
|
|
|
} |
30
|
|
|
if (isset($size)) { |
31
|
|
|
$this->setSize($size); |
32
|
|
|
} |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* define the buttons size |
37
|
|
|
* available values : "btn-group-lg","","btn-group-sm","btn-group-xs" |
38
|
|
|
* @param string|int $size |
39
|
|
|
* @return HtmlButtongroups default : "" |
40
|
|
|
*/ |
41
|
|
|
public function setSize($size) { |
42
|
|
|
foreach ( $this->elements as $element ) { |
43
|
|
|
$element->setSize($size); |
44
|
|
|
} |
45
|
|
View Code Duplication |
if (is_int($size)) { |
|
|
|
|
46
|
|
|
return $this->addToPropertyUnique("class", CssRef::sizes("btn-group")[$size], CssRef::sizes("btn-group")); |
47
|
|
|
} |
48
|
|
|
return $this->addToPropertyCtrl("class", $size, CssRef::sizes("btn-group")); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function setStyle($value) { |
52
|
|
|
foreach ( $this->elements as $element ) |
53
|
|
|
$element->setStyle($value); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
private function dropdownAsButton($bt) { |
57
|
|
|
$this->addExistingDropDown($bt); |
58
|
|
|
$bt->setTagName("button"); |
59
|
|
|
$bt->addBtnClass("dropdown-toogle"); |
60
|
|
|
$bt->addBtnClass("btn-default"); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
private function addExistingDropDown($bt) { |
64
|
|
|
$bt->setMTagName("div"); |
65
|
|
|
$bt->setRole("group"); |
66
|
|
|
$bt->setMClass("btn-group"); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function addElement($element) { |
70
|
|
|
$result=$element; |
71
|
|
|
$iid=sizeof($this->elements)+1; |
72
|
|
|
if (($element instanceof HtmlDropdown)||($element instanceof HtmlSplitbutton)) { |
73
|
|
|
$this->addExistingDropDown($element); |
74
|
|
|
} elseif (\is_array($element)) { |
75
|
|
|
$result=$this->_addArrayElement($element,$iid); |
76
|
|
|
} elseif (is_string($element)) { |
77
|
|
|
$result=new HtmlButton($this->identifier."-button-".$iid,$element); |
78
|
|
|
} |
79
|
|
|
if($result instanceof HtmlButton) |
80
|
|
|
$this->elements[]=$result; |
81
|
|
|
return $result; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
private function _addArrayElement(array $element,int $iid){ |
85
|
|
|
if (array_key_exists("glyph", $element)) |
86
|
|
|
$bt=new HtmlGlyphButton($this->identifier."-button-".$iid); |
87
|
|
|
elseif (array_key_exists("btnCaption", $element)) { |
88
|
|
|
if (array_key_exists("split", $element)) |
89
|
|
|
$bt=new HtmlSplitbutton($this->identifier."-dropdown-".$iid); |
90
|
|
|
else{ |
91
|
|
|
$bt=new HtmlDropdown($this->identifier."-dropdown-".$iid); |
92
|
|
|
$this->dropdownAsButton($bt); |
93
|
|
|
} |
94
|
|
|
} else |
95
|
|
|
$bt=new HtmlButton($this->identifier."-button-".$iid); |
96
|
|
|
$bt->fromArray($element); |
97
|
|
|
return $bt; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function addElements($elements) { |
101
|
|
|
foreach ( $elements as $element ) { |
102
|
|
|
$this->addElement($element); |
103
|
|
|
} |
104
|
|
|
return $this; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/* |
108
|
|
|
* (non-PHPdoc) |
109
|
|
|
* @see \Ajax\bootstrap\html\HtmlSingleElement::fromArray() |
110
|
|
|
*/ |
111
|
|
|
public function fromArray($array) { |
112
|
|
|
$this->addElements($array); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function setAlignment($value) { |
116
|
|
|
if (is_int($value)) { |
117
|
|
|
$value=CssRef::alignment("btn-group")[$value]; |
118
|
|
|
} else |
119
|
|
|
$value="btn-group-".$value; |
120
|
|
|
if (strstr($value, "justified")) { |
121
|
|
|
foreach ( $this->elements as $element ) { |
122
|
|
|
$element->wrap('<div class="btn-group" role="group">', '</div>'); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
$this->addToPropertyCtrl("class", $value, CssRef::alignment("btn-group-")); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Return the element at index |
130
|
|
|
* @param int $index |
131
|
|
|
* @return HtmlButton |
132
|
|
|
*/ |
133
|
|
|
public function getElement($index) { |
134
|
|
|
if (is_int($index)) |
135
|
|
|
return $this->elements[$index]; |
|
|
|
|
136
|
|
|
else { |
137
|
|
|
$elm=$this->getElementById($index, $this->elements); |
138
|
|
|
return $elm; |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
public function setElement($index, $button) { |
143
|
|
|
$this->elements[$index]=$button; |
144
|
|
|
return $this; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/* |
148
|
|
|
* (non-PHPdoc) |
149
|
|
|
* @see \Ajax\bootstrap\html\base\BaseHtml::on() |
150
|
|
|
*/ |
151
|
|
|
public function on($event, $jsCode, $stopPropagation=false, $preventDefault=false) { |
152
|
|
|
foreach ( $this->elements as $element ) { |
153
|
|
|
$element->on($event, $jsCode, $stopPropagation, $preventDefault); |
154
|
|
|
} |
155
|
|
|
return $this; |
|
|
|
|
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
public function getElements() { |
159
|
|
|
return $this->elements; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/* (non-PHPdoc) |
163
|
|
|
* @see \Ajax\bootstrap\html\base\BaseHtml::fromDatabaseObject() |
164
|
|
|
*/ |
165
|
|
|
public function fromDatabaseObject($object, $function) { |
166
|
|
|
$this->addElement($function($object)); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.