1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ajax\semantic\html\collections\menus; |
4
|
|
|
|
5
|
|
|
use Ajax\bootstrap\html\HtmlLink; |
6
|
|
|
use Ajax\common\html\HtmlDoubleElement; |
7
|
|
|
use Ajax\semantic\html\base\constants\Direction; |
8
|
|
|
use Ajax\semantic\html\base\HtmlSemCollection; |
9
|
|
|
use Ajax\semantic\html\base\HtmlSemDoubleElement; |
10
|
|
|
use Ajax\semantic\html\base\constants\Wide; |
11
|
|
|
use Ajax\common\html\html5\HtmlImg; |
12
|
|
|
use Ajax\bootstrap\html\HtmlInput; |
13
|
|
|
use Ajax\semantic\html\modules\HtmlDropdown; |
14
|
|
|
use Ajax\common\html\BaseHtml; |
15
|
|
|
use Ajax\semantic\html\modules\HtmlPopup; |
16
|
|
|
use Ajax\semantic\html\elements\HtmlIcon; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Semantic Menu component |
20
|
|
|
* @see http://semantic-ui.com/collections/menu.html |
21
|
|
|
* @author jc |
22
|
|
|
* @version 1.001 |
23
|
|
|
*/ |
24
|
|
|
class HtmlMenu extends HtmlSemCollection { |
25
|
|
|
|
26
|
|
|
public function __construct($identifier, $items=array()) { |
27
|
|
|
parent::__construct($identifier, "div"); |
28
|
|
|
$this->setClass("ui menu"); |
29
|
|
|
$this->addItems($items); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Sets the menu type |
34
|
|
|
* @param string $type one of text,item |
35
|
|
|
* @return \Ajax\semantic\html\collections\HtmlMenu |
36
|
|
|
*/ |
37
|
|
|
public function setType($type=""){ |
38
|
|
|
return $this->addToPropertyCtrl("class", $type, array("","item","text")); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function setActiveItem($index){ |
42
|
|
|
$item=$this->getItem($index); |
43
|
|
|
if($item!==null){ |
44
|
|
|
$item->addToProperty("class", "active"); |
45
|
|
|
} |
46
|
|
|
return $this; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* {@inheritDoc} |
51
|
|
|
* @see \Ajax\common\html\html5\HtmlCollection::addItem() |
52
|
|
|
*/ |
53
|
|
|
public function addItem($item){ |
54
|
|
|
if($item instanceof HtmlInput || $item instanceof HtmlImg){ |
55
|
|
|
$itemO=new HtmlSemDoubleElement("item-".$this->identifier,"div",""); |
56
|
|
|
$itemO->setContent($item); |
57
|
|
|
$item=$itemO; |
58
|
|
|
} |
59
|
|
|
$item=parent::addItem($item); |
60
|
|
|
if(!$item instanceof HtmlMenu) |
61
|
|
|
$item->addToPropertyCtrl("class", "item",array("item")); |
62
|
|
|
else{ |
63
|
|
|
$this->setSecondary(); |
64
|
|
|
} |
65
|
|
|
return $item; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function generateMenuAsItem($menu,$header=null){ |
69
|
|
|
$count=$this->count(); |
70
|
|
|
$item=new HtmlSemDoubleElement("item-".$this->identifier."-".$count,"div"); |
71
|
|
View Code Duplication |
if(isset($header)){ |
|
|
|
|
72
|
|
|
$headerItem=new HtmlSemDoubleElement("item-header-".$this->identifier."-".$count,"div","header"); |
73
|
|
|
$headerItem->setContent($header); |
74
|
|
|
$item->addContent($headerItem); |
75
|
|
|
} |
76
|
|
|
$menu->setClass("menu"); |
77
|
|
|
$item->addContent($menu); |
78
|
|
|
return $item; |
79
|
|
|
} |
80
|
|
|
public function addMenuAsItem($menu,$header=null){ |
81
|
|
|
return $this->addItem($this->generateMenuAsItem($menu,$header)); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function addPopupAsItem($value,$identifier, $content=""){ |
85
|
|
|
$value=new HtmlSemDoubleElement($identifier,"a","browse item",$value); |
86
|
|
|
$value->addContent(new HtmlIcon("", "dropdown")); |
87
|
|
|
$value=$this->addItem($value); |
88
|
|
|
$popup=new HtmlPopup($value,"popup-".$this->identifier."-".$this->count(),$content); |
89
|
|
|
$popup->setFlowing()->setPosition("bottom left")->setOn("click"); |
90
|
|
|
$this->wrap("",$popup); |
91
|
|
|
return $popup; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function addDropdownAsItem($value,$items=NULL){ |
95
|
|
|
$dd=$value; |
96
|
|
|
if(\is_string($value)){ |
97
|
|
|
$dd=new HtmlDropdown("dropdown-".$this->identifier."-".$this->count(),$value,$items); |
98
|
|
|
} |
99
|
|
|
return $this->addItem($dd); |
100
|
|
|
} |
101
|
|
|
/** |
102
|
|
|
* {@inheritDoc} |
103
|
|
|
* @see \Ajax\common\html\html5\HtmlCollection::createItem() |
104
|
|
|
*/ |
105
|
|
|
protected function createItem($value) { |
106
|
|
|
$itemO=new HtmlLink("item-".\sizeof($this->content),"",$value); |
|
|
|
|
107
|
|
|
return $itemO->setClass("item"); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function setInverted(){ |
111
|
|
|
return $this->addToProperty("class", "inverted"); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function setSecondary(){ |
115
|
|
|
return $this->addToProperty("class", "secondary"); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function setVertical(){ |
119
|
|
|
return $this->addToPropertyCtrl("class", "vertical",array("vertical")); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function setPosition($value="right"){ |
123
|
|
|
return $this->addToPropertyCtrl("class", $value,array("right","left")); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function setPointing($value=Direction::NONE){ |
127
|
|
|
return $this->addToPropertyCtrl("class", $value." pointing",Direction::getConstantValues("pointing")); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
public function asTab($vertical=false){ |
131
|
|
|
$this->apply(function(HtmlDoubleElement &$item){$item->setTagName("a");}); |
132
|
|
|
if($vertical===true) |
133
|
|
|
$this->setVertical(); |
134
|
|
|
return $this->addToProperty("class", "tabular"); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public function asPagination(){ |
138
|
|
|
$this->apply(function(HtmlDoubleElement &$item){$item->setTagName("a");}); |
139
|
|
|
return $this->addToProperty("class", "pagination"); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
public function setFixed(){ |
143
|
|
|
return $this->addToProperty("class", "fixed"); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
public function setFluid(){ |
147
|
|
|
return $this->addToProperty("class", "fluid"); |
|
|
|
|
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
public function setCompact(){ |
151
|
|
|
return $this->addToProperty("class", "compact"); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/* (non-PHPdoc) |
155
|
|
|
* @see \Ajax\bootstrap\html\base\BaseHtml::fromDatabaseObject() |
156
|
|
|
*/ |
157
|
|
|
public function fromDatabaseObject($object, $function) { |
158
|
|
|
$return=$function($object); |
159
|
|
|
if(\is_array($return)) |
160
|
|
|
$this->addItems($return); |
161
|
|
|
else |
162
|
|
|
$this->addItem($return); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Defines the menu width |
167
|
|
|
* @param int $width |
168
|
|
|
* @return \Ajax\semantic\html\collections\menus\HtmlMenu |
169
|
|
|
*/ |
170
|
|
View Code Duplication |
public function setWidth($width){ |
|
|
|
|
171
|
|
|
if(\is_int($width)){ |
172
|
|
|
$width=Wide::getConstants()["W".$width]; |
173
|
|
|
} |
174
|
|
|
$this->addToPropertyCtrl("class", $width, Wide::getConstants()); |
175
|
|
|
return $this->addToPropertyCtrl("class", "item",array("item")); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
public function addImage($identifier, $src="", $alt=""){ |
179
|
|
|
return $this->addItem(new HtmlImg($identifier,$src,$alt)); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
public static function vertical($identifier,$items=array()){ |
183
|
|
|
return (new HtmlMenu($identifier,$items))->setVertical(); |
184
|
|
|
} |
185
|
|
|
} |
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.