phpMv  -UI toolkit 2.4.12
jQuery, jQuery UI, Twitter Bootstrap and Semantic-UI library for php & php MVC Frameworks
HtmlDropdown.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Ajax\bootstrap\html;
4 
5 use Ajax\JsUtils;
10 
16 class HtmlDropdown extends HtmlButton {
17  protected $btnCaption="Dropdown button";
18  protected $class="dropdown-toggle";
19  protected $mClass="dropdown";
20  protected $mTagName="div";
21  protected $items=array ();
22 
27  public function __construct($identifier, $value="", $items=array(), $cssStyle=null, $onClick=null) {
28  parent::__construct($identifier, "", $cssStyle, $onClick);
29  $this->_template=include 'templates/tplDropdown.php';
30  $this->btnCaption=$value;
31  $this->tagName="a";
32  $this->fromArray($items);
33  if ($cssStyle!==NULL) {
34  $this->asButton($cssStyle);
35  }
36  }
37 
42  public function setMTagName($value) {
43  $this->mTagName=$value;
44  }
45 
52  public function setStyle($cssStyle) {
53  if (is_int($cssStyle)) {
54  return $this->addToMember($this->class, CssRef::buttonStyles()[$cssStyle]);
55  }
56  if (JString::startsWith($cssStyle, "btn-")===false) {
57  $cssStyle="btn".$cssStyle;
58  }
59  return $this->addToMemberCtrl($this->class, $cssStyle, CssRef::buttonStyles());
60  }
61 
62  /*
63  * (non-PHPdoc)
64  * @see \Ajax\bootstrap\html\HtmlButton::setValue()
65  */
66  public function setValue($value) {
67  $this->btnCaption=$value;
68  }
69 
77  public function setSize($size) {
78  if (is_int($size)) {
79  return $this->addToProperty("class", CssRef::sizes("btn-group")[$size]);
80  }
81  return $this->addToPropertyCtrl("class", $size, CssRef::sizes("btn-group"));
82  }
83 
90  public function addItem($caption, $href="#") {
91  if($caption instanceof HtmlDropdownItem){
92  $item=$caption;
93  }else{
94  $iid=$this->getItemsCount()+1;
95  $item=new HtmlDropdownItem($this->identifier."-dropdown-item-".$iid);
96  $item->setCaption($caption)->setHref($href);
97  }
98  $this->items []=$item;
99  return $item;
100  }
101 
102  public function addDivider() {
103  return $this->addItem("-");
104  }
105 
106  public function addHeader($caption) {
107  return $this->addItem("-".$caption);
108  }
109 
110  public function addItems($items) {
111  $iid=$this->getItemsCount()+1;
112  if (\is_array($items)) {
113  foreach ( $items as $item ) {
114  if (is_string($item)) {
115  $this->addItem($item);
116  } else if (\is_array($item)) {
117  $dropDownItem=new HtmlDropdownItem($this->identifier."-dropdown-item-".$iid);
118  $dropDownItem->fromArray($item);
119  $this->items []=$dropDownItem;
120  } else if ($item instanceof HtmlDropdownItem) {
121  $this->items []=$item;
122  }
123  }
124  }
125  return $this;
126  }
127 
128  /*
129  * (non-PHPdoc)
130  * @see BaseHtml::fromArray()
131  */
132  public function fromArray($array) {
133  if (array_keys($array)!==range(0, count($array)-1))
134  return parent::fromArray($array);
135  else
136  return $this->addItems($array);
137  }
138 
139  public function setItems($items) {
140  $this->items=array ();
141  $this->addItems($items);
142  }
143 
149  public function getItem($index) {
150  return $this->items [$index];
151  }
152 
153  public function setBtnClass($value) {
154  $this->class=$value;
155  }
156 
157  public function setMClass($value) {
158  $this->mClass=$value;
159  }
160 
161  public function addBtnClass($value) {
162  $this->addToMember($this->class, $value);
163  }
164 
165  public function addmClass($value) {
166  $this->addToMember($this->mClass, $value);
167  }
168 
169  /*
170  * (non-PHPdoc)
171  * @see BaseHtml::run()
172  */
173  public function run(JsUtils $js) {
174  if ($this->getProperty("role")==="nav") {
175  foreach ( $this->items as $dropdownItem ) {
176  $dropdownItem->runNav($js);
177  }
178  }
179  $this->_bsComponent=$js->bootstrap()->dropdown("#".$this->identifier);
180  $this->addEventsOnRun($js);
181  return $this->_bsComponent;
182  }
183 
188  public function setTagName($tagName) {
189  if ($tagName=="button")
190  $this->class="btn dropdown-toggle";
191  return parent::setTagName($tagName);
192  }
193 
194  public function __toString() {
195  return $this->compile();
196  }
197 
198  public function setBtnCaption($btnCaption) {
199  $this->btnCaption=$btnCaption;
200  return $this;
201  }
202 
203  public function getItemsCount() {
204  return sizeof($this->items);
205  }
206 
207  public function setAlignment($alignment) {
208  if (is_int($alignment))
209  $alignment="dropdown-menu-".CssRef::alignment()[$alignment];
210  return $this->addToMemberCtrl($this->class, $alignment, CssRef::alignment());
211  }
212 
213  public function dropup() {
214  $this->addToMember($this->mClass, "dropup");
215  }
216 
217  public function getItems() {
218  return $this->items;
219  }
220 
221  public function asButton($cssStyle="btn-primary") {
222  $this->setTagName("button");
223  $this->setBtnClass("btn dropdown-toggle");
224  $this->setStyle($cssStyle);
225  }
226 
232  public function onShow($jsCode) {
233  return $this->addEvent("show.bs.dropdown", $jsCode);
234  }
235 
241  public function onShown($jsCode) {
242  return $this->addEvent("shown.bs.dropdown", $jsCode);
243  }
244 
250  public function onHide($jsCode) {
251  return $this->addEvent("hide.bs.dropdown", $jsCode);
252  }
253 
259  public function onHidden($jsCode) {
260  return $this->addEvent("hidden.bs.dropdown", $jsCode);
261  }
262 
263 
264  /* (non-PHPdoc)
265  * @see \Ajax\bootstrap\html\base\BaseHtml::on()
266  */
267  public function on($event, $jsCode, $stopPropagation = false, $preventDefault = false) {
268  foreach ($this->items as $item){
269  $item->on($event, $jsCode,$stopPropagation,$preventDefault);
270  }
271  }
272 
273  /* (non-PHPdoc)
274  * @see \Ajax\bootstrap\html\base\BaseHtml::fromDatabaseObject()
275  */
276  public function fromDatabaseObject($object, $function) {
277  $this->addItem($function($object));
278  }
279 
280 }
addToProperty($name, $value, $separator=" ")
onShown($jsCode)
This event is fired when a dropdown element has been made visible to the user (will wait for CSS tran...
static alignment($prefix="")
Definition: CssRef.php:57
onShow($jsCode)
This event fires immediately when the show instance method is called.
addEventsOnRun(JsUtils $js=NULL)
setStyle($cssStyle)
define the button style avaible values : "btn-default","btn-primary","btn-success","btn-info","btn-warning","btn-danger"
onHide($jsCode)
This event is fired immediately when the hide method has been called.
setTagName($tagName)
Sets the tagName&#39;s dropdown.
compile(JsUtils $js=NULL, &$view=NULL)
Definition: BaseHtml.php:208
Inner element for Twitter Bootstrap HTML Dropdown component.
Twitter Bootstrap Button component.
Definition: HtmlButton.php:15
getItem($index)
Return the item at $index.
bootstrap(Bootstrap $bootstrap=NULL)
getter or setter of the Twitter Bootstrap variable
Definition: JsUtils.php:141
__construct($identifier, $value="", $items=array(), $cssStyle=null, $onClick=null)
addToMember(&$name, $value, $separator=' ')
Definition: BaseHtml.php:99
JQuery PHP library.
Definition: JsUtils.php:23
addItem($caption, $href="#")
add an HtmlDropdownItem
addEvent($event, $jsCode, $stopPropagation=false, $preventDefault=false)
asButton($cssStyle="btn-primary")
Twitter Bootstrap HTML Dropdown component.
setSize($size)
define the buttons size available values : "btn-group-lg","","btn-group-sm","btn-group-xs" ...
setMTagName($value)
Define the tagName of the main element.
on($event, $jsCode, $stopPropagation=false, $preventDefault=false)
static sizes($prefix="btn")
Definition: CssRef.php:38
fromDatabaseObject($object, $function)
addToPropertyCtrl($name, $value, $typeCtrl)
onHidden($jsCode)
This event is fired when a dropdown element has been hidden from the user (will wait for CSS transiti...
addToMemberCtrl(&$name, $value, $typeCtrl, $separator=" ")
Definition: BaseHtml.php:90