phpMv  -UI toolkit 2.4.12
jQuery, jQuery UI, Twitter Bootstrap and Semantic-UI library for php & php MVC Frameworks
HtmlMenu.php
Go to the documentation of this file.
1 <?php
2 
4 
18 use Ajax\JsUtils;
22 
29 class HtmlMenu extends HtmlSemCollection {
30  use AttachedTrait;
31  private $_itemHeader;
32 
33  public function __construct($identifier, $items=array()) {
34  parent::__construct($identifier, "div", "ui menu");
35  $this->addItems($items);
36  }
37 
43  public function setType($type="") {
44  return $this->addToPropertyCtrl("class", $type, array ("","item","text" ));
45  }
46 
47  public function setActiveItem($index) {
48  $item=$this->getItem($index);
49  if ($item !== null) {
50  $item->addToProperty("class", "active");
51  }
52  return $this;
53  }
54 
55  private function getItemToInsert($item) {
56  if ($item instanceof HtmlInput || $item instanceof HtmlImg || $item instanceof HtmlIcon || $item instanceof HtmlButtonGroups || $item instanceof HtmlButton || $item instanceof HtmlLabel) {
57  $itemO=new HtmlMenuItem("item-" . $this->identifier . "-" . \sizeof($this->content) , $item);
58  $itemO->addClass("no-active");
59  $item=$itemO;
60  }
61  return $item;
62  }
63 
64  private function afterInsert($item) {
65  if (!$item instanceof HtmlMenu && $item->propertyContains("class", "header")===false)
66  $item->addToPropertyCtrl("class", "item", array ("item" ));
67  else {
68  $this->setSecondary();
69  }
70  return $item;
71  }
72 
79  public function addItem($item) {
80  $number=$item;
81  $item=parent::addItem($this->getItemToInsert($item));
82  if(\is_int($number))
83  $item->setProperty("data-page", $number);
84  return $this->afterInsert($item);
85  }
86 
93  public function insertItem($item, $position=0) {
94  $item=parent::insertItem($this->getItemToInsert($item), $position);
95  return $this->afterInsert($item);
96  }
97 
98  public function generateMenuAsItem($menu, $header=null) {
99  $count=$this->count();
100  $item=new HtmlSemDoubleElement("item-" . $this->identifier . "-" . $count, "div");
101  if (isset($header)) {
102  $headerItem=new HtmlSemDoubleElement("item-header-" . $this->identifier . "-" . $count, "div", "header");
103  $headerItem->setContent($header);
104  $item->addContent($headerItem);
105  $this->_itemHeader=$headerItem;
106  }
107  if(\is_array($menu)){
108  $menu=new HtmlMenu("menu-" . $this->identifier . "-" . $count,$menu);
109  }
110  $menu->setClass("menu");
111  $item->addContent($menu);
112  return $item;
113  }
114 
120  public function addHeader($caption){
121  if(!($caption instanceof HtmlDoubleElement)){
122  $header=new HtmlDoubleElement('','div');
123  $header->setContent($caption);
124  }else{
125  $header=$caption;
126  }
127  $header->addClass('item header');
128  $this->wrapContent($header);
129  return $header;
130  }
131 
132  public function addMenuAsItem($menu, $header=null) {
133  return $this->addItem($this->generateMenuAsItem($menu, $header));
134  }
135 
136  public function addPopupAsItem($value, $identifier, $content="") {
137  $value=new HtmlSemDoubleElement($identifier, "a", "browse item", $value);
138  $value->addContent(new HtmlIcon("", "dropdown"));
139  $value=$this->addItem($value);
140  $popup=new HtmlPopup($value, "popup-" . $this->identifier . "-" . $this->count(), $content);
141  $popup->setFlowing()->setPosition("bottom left")->setOn("click");
142  $this->wrap("", $popup);
143  return $popup;
144  }
145 
146  public function addDropdownAsItem($value, $items=NULL) {
147  $dd=$value;
148  if (\is_string($value)) {
149  $dd=new HtmlDropdown("dropdown-" . $this->identifier . "-" . $this->count(), $value, $items);
150  }
151  $this->addItem($dd);
152  return $dd;
153  }
154 
161  protected function createItem($value) {
162  $itemO=new HtmlMenuItem($this->identifier."-item-" . \sizeof($this->content),"");
163  $itemO->setTagName("a");
164  $itemO->setContent($value);
165  return $itemO;
166  }
167 
168  public function setSecondary($value=true) {
169  if($value)
170  $this->addToProperty("class", "secondary");
171  else
172  $this->removePropertyValue("class", "secondary");
173  return $this;
174  }
175 
176  public function setVertical() {
177  return $this->addToPropertyCtrl("class", "vertical", array ("vertical" ));
178  }
179 
180  public function setPosition($value="right") {
181  return $this->addToPropertyCtrl("class", $value, array ("right","left" ));
182  }
183 
184  public function setPointing($value=Direction::NONE) {
185  return $this->addToPropertyCtrl("class", $value . " pointing", Direction::getConstantValues("pointing"));
186  }
187 
188  public function asTab($vertical=false) {
189  $this->apply(function (HtmlDoubleElement &$item) {
190  $item->setTagName("a");
191  });
192  if ($vertical === true)
193  $this->setVertical();
194  return $this->addToProperty("class", "tabular");
195  }
196 
197  public function asPagination() {
198  $this->apply(function (HtmlDoubleElement &$item) {
199  $item->setTagName("a");
200  });
201  return $this->addToProperty("class", "pagination");
202  }
203 
204  public function setFixed() {
205  return $this->addToProperty("class", "fixed");
206  }
207 
208  public function setFluid() {
209  return $this->addToProperty("class", "fluid");
210  }
211 
212  public function setCompact() {
213  return $this->addToProperty("class", "compact");
214  }
215 
216  /*
217  * (non-PHPdoc)
218  * @see \Ajax\bootstrap\html\base\BaseHtml::fromDatabaseObject()
219  */
220  public function fromDatabaseObject($object, $function) {
221  $return=$function($object);
222  if (\is_array($return))
223  $this->addItems($return);
224  else
225  $this->addItem($return);
226  }
227 
233  public function setWidth($width) {
234  if (\is_int($width)) {
235  $width=Wide::getConstants()["W" . $width];
236  }
237  $this->addToPropertyCtrl("class", $width, Wide::getConstants());
238  return $this->addToPropertyCtrl("class", "item", array ("item" ));
239  }
240 
241  public function addImage($identifier, $src="", $alt="") {
242  return $this->addItem(new HtmlImg($identifier, $src, $alt));
243  }
244 
245  public static function vertical($identifier, $items=array()) {
246  return (new HtmlMenu($identifier, $items))->setVertical();
247  }
248 
249  public function getItemHeader() {
250  return $this->_itemHeader;
251  }
252 
253  public function setHasContainer(){
254  return $this->wrapContent("<div class='ui container'>","</div>");
255  }
256 
257  public function run(JsUtils $js){
258  if($this->identifier!=="" && !isset($this->_bsComponent))
259  $this->onClick('if(!$(this).hasClass("dropdown")&&!$(this).hasClass("no-active")){$(this).addClass("active").siblings().removeClass("active");}',false,false);
260  $result= parent::run($js);
261  return $result->setItemSelector(">.item:not(.header)");
262  }
263 }
static vertical($identifier, $items=array())
Definition: HtmlMenu.php:245
addToProperty($name, $value, $separator=" ")
addImage($identifier, $src="", $alt="")
Definition: HtmlMenu.php:241
Semantic Button component.
Definition: HtmlButton.php:18
static getConstants()
Definition: BaseEnum.php:17
setType($type="")
Sets the menu type.
Definition: HtmlMenu.php:43
Semantic Label component.
Definition: HtmlLabel.php:20
addHeader($caption)
Adds an header to the menu.
Definition: HtmlMenu.php:120
addPopupAsItem($value, $identifier, $content="")
Definition: HtmlMenu.php:136
wrap($before, $after="")
Definition: BaseHtml.php:171
onClick($jsCode, $stopPropagation=false, $preventDefault=true)
Semantic Icon component.
Definition: HtmlIcon.php:14
setWidth($width)
Defines the menu width.
Definition: HtmlMenu.php:233
static getConstantValues($postFix="", $prefixBefore=false)
Definition: BaseEnum.php:29
getItem($index)
Return the item at index.
Base class for Semantic double elements.
Base class for Semantic Html collections.
JQuery PHP library.
Definition: JsUtils.php:23
__construct($identifier, $items=array())
Definition: HtmlMenu.php:33
addToPropertyCtrl($name, $value, $typeCtrl)
Semantic UI Buttongroups component.