HtmlMenu::afterInsert()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 5
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 7
rs 10
1
<?php
2
3
namespace Ajax\semantic\html\collections\menus;
4
5
use Ajax\common\html\HtmlDoubleElement;
6
use Ajax\semantic\html\base\constants\Direction;
7
use Ajax\semantic\html\base\HtmlSemCollection;
8
use Ajax\semantic\html\base\HtmlSemDoubleElement;
9
use Ajax\semantic\html\base\constants\Wide;
10
use Ajax\common\html\html5\HtmlImg;
11
use Ajax\semantic\html\modules\HtmlDropdown;
12
use Ajax\semantic\html\modules\HtmlPopup;
13
use Ajax\semantic\html\elements\HtmlIcon;
14
use Ajax\semantic\html\elements\HtmlInput;
15
use Ajax\semantic\html\elements\HtmlButton;
16
use Ajax\semantic\html\base\traits\AttachedTrait;
17
use Ajax\semantic\html\content\HtmlMenuItem;
18
use Ajax\JsUtils;
19
use Ajax\semantic\html\elements\HtmlButtonGroups;
20
use Ajax\semantic\html\elements\HtmlLabel;
21
use Ajax\common\html\HtmlCollection;
22
23
/**
24
 * Semantic Menu component
25
 * @see http://semantic-ui.com/collections/menu.html
26
 * @author jc
27
 * @version 1.001
28
 */
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
38
	/**
39
	 * Sets the menu type
40
	 * @param string $type one of text,item
41
	 * @return HtmlMenu
42
	 */
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
73
	/**
74
	 *
75
	 * {@inheritDoc}
76
	 *
77
	 * @see HtmlCollection::addItem()
78
	 */
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
87
	/**
88
	 *
89
	 * {@inheritDoc}
90
	 *
91
	 * @see \Ajax\common\html\HtmlCollection::insertItem()
92
	 */
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
	
115
	/**
116
	 * Adds an header to the menu
117
	 * @param String|HtmlDoubleElement $caption
118
	 * @return \Ajax\common\html\HtmlDoubleElement
119
	 */
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
155
	/**
156
	 *
157
	 * {@inheritDoc}
158
	 *
159
	 * @see HtmlCollection::createItem()
160
	 */
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
228
	/**
229
	 * Defines the menu width
230
	 * @param int $width
231
	 * @return \Ajax\semantic\html\collections\menus\HtmlMenu
232
	 */
233
	public function setWidth($width) {
234
		if (\is_int($width)) {
0 ignored issues
show
introduced by
The condition is_int($width) is always true.
Loading history...
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
}
264