Passed
Push — master ( 421c61...09a178 )
by Jean-Christophe
02:45
created

HtmlDropdown::addItem()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 4
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Ajax\semantic\html\modules;
4
5
use Ajax\semantic\html\base\HtmlSemDoubleElement;
6
use Ajax\semantic\html\content\HtmlDropdownItem;
7
use Ajax\semantic\html\elements\HtmlIcon;
8
use Ajax\common\html\html5\HtmlInput;
9
use Ajax\service\JArray;
10
use Ajax\semantic\html\base\constants\Direction;
11
use Ajax\semantic\html\base\traits\LabeledIconTrait;
12
use Ajax\JsUtils;
13
use Ajax\semantic\html\collections\form\traits\FieldTrait;
14
use Ajax\common\html\HtmlCollection;
15
use Ajax\common\html\HtmlDoubleElement;
16
17
class HtmlDropdown extends HtmlSemDoubleElement {
18
	use FieldTrait,LabeledIconTrait {
19
		addIcon as addIconP;
20
	}
21
	protected $mClass="menu";
22
	protected $mTagName="div";
23
	protected $items=array ();
24
	protected $_params=array("action"=>"nothing","on"=>"hover","showOnFocus"=>false);
25
	protected $input;
26
	protected $value;
27
	protected $_associative;
28
	protected $_multiple;
29
30
	public function __construct($identifier, $value="", $items=array(),$associative=true) {
31
		parent::__construct($identifier, "div");
32
		$this->_template=include dirname(__FILE__).'/../templates/tplDropdown.php';
33
		$this->setProperty("class", "ui dropdown");
34
		$this->_multiple=false;
35
		$content=[];
36
		if(isset($value)){
37
			if($value instanceof HtmlSemDoubleElement){
38
				$text=$value;
39
			}else{
40
				$text=new HtmlSemDoubleElement("text-".$this->identifier,"div");
41
				$text->setClass("text");
42
				$this->setValue($value);
43
			}
44
			$content=["text"=>$text];
45
		}
46
		$content["arrow"]=new HtmlIcon($identifier."-icon", "dropdown");
47
		$this->content=$content;
48
		$this->tagName="div";
49
		$this->_associative=$associative;
50
		$this->addItems($items);
51
	}
52
53
	public function getField(){
54
		return $this->input;
55
	}
56
57
	public function getDataField(){
58
		return $this->input;
59
	}
60
61
	public function addItem($item,$value=NULL,$image=NULL,$description=NULL){
62
		$itemO=$this->beforeAddItem($item,$value,$image,$description);
63
		$this->items[]=$itemO;
64
		return $itemO;
65
	}
66
67
	public function addIcon($icon,$before=true,$labeled=false){
68
		$this->removeArrow();
69
		$this->addIconP($icon,$before,$labeled);
70
		return $this->getElementById("text-".$this->identifier, $this->content)->setWrapAfter("");
71
	}
72
73
	public function addIcons($icons){
74
		$count=$this->count();
75
		for ($i=0;$i<\sizeof($icons) && $i<$count;$i++){
76
			$this->getItem($i)->addIcon($icons[$i]);
77
		}
78
	}
79
80
	/**
81
	 * Insert an item at a position
82
	 * @param mixed $item
83
	 * @param int $position
84
	 * @return HtmlDropdownItem
85
	 */
86
	public function insertItem($item,$position=0){
87
		$itemO=$this->beforeAddItem($item);
88
		 $start = array_slice($this->items, 0, $position);
89
		 $end = array_slice($this->items, $position);
90
		 $start[] = $item;
91
		 $this->items=array_merge($start, $end);
92
		 return $itemO;
93
	}
94
95
	protected function removeArrow(){
96
		if(\sizeof($this->content)>1){
97
			unset($this->content["arrow"]);
98
			$this->content=\array_values($this->content);
99
		}
100
	}
101
102
	protected function beforeAddItem($item,$value=NULL,$image=NULL,$description=NULL){
103
		$itemO=$item;
104
		if(\is_array($item)){
105
			$description=JArray::getValue($item, "description", 3);
106
			$value=JArray::getValue($item, "value", 1);
107
			$image=JArray::getValue($item, "image", 2);
108
			$item=JArray::getValue($item, "item", 0);
109
		}
110
		if(!$item instanceof HtmlDropdownItem){
111
			$itemO=new HtmlDropdownItem("dd-item-".$this->identifier."-".\sizeof($this->items),$item,$value,$image,$description);
112
		}elseif($itemO instanceof HtmlDropdownItem){
113
			$this->addToProperty("class", "vertical");
114
		}
115
		return $itemO;
116
	}
117
118
	/* (non-PHPdoc)
119
	 * @see \Ajax\bootstrap\html\base\BaseHtml::fromDatabaseObject()
120
	 */
121
	public function fromDatabaseObject($object, $function) {
122
		$this->addItem($function($object));
123
	}
124
125
	public function addInput($name){
126
		if(!isset($name))
127
			$name="input-".$this->identifier;
128
		$this->setAction("activate");
129
		$this->input=new HtmlInput($name,"hidden");
130
	}
131
132
	/**
133
	 * Adds a search input item
134
	 * @param string $placeHolder
135
	 * @param string $icon
136
	 * @return HtmlDropdownItem
137
	 */
138
	public function addSearchInputItem($placeHolder=NULL,$icon=NULL){
139
		return $this->addItem(HtmlDropdownItem::searchInput($placeHolder,$icon));
140
	}
141
142
	/**
143
	 * Adds a divider item
144
	 * @return HtmlDropdownItem
145
	 */
146
	public function addDividerItem(){
147
		return $this->addItem(HtmlDropdownItem::divider());
148
	}
149
150
	/**
151
	 * Adds an header item
152
	 * @param string $caption
153
	 * @param string $icon
154
	 * @return HtmlDropdownItem
155
	 */
156
	public function addHeaderItem($caption=NULL,$icon=NULL){
157
		return $this->addItem(HtmlDropdownItem::header($caption,$icon));
158
	}
159
160
	/**
161
	 * Adds an item with a circular label
162
	 * @param string $caption
163
	 * @param string $color
164
	 * @return HtmlDropdownItem
165
	 */
166
	public function addCircularLabelItem($caption,$color){
167
		return $this->addItem(HtmlDropdownItem::circular($caption, $color));
168
	}
169
170
	/**
171
	 * @param string $caption
172
	 * @param string $image
173
	 * @return \Ajax\semantic\html\content\HtmlDropdownItem
174
	 */
175
	public function addMiniAvatarImageItem($caption,$image){
176
		return $this->addItem(HtmlDropdownItem::avatar($caption, $image));
177
	}
178
179
	public function addItems($items){
180
		if(\is_array($items) && $this->_associative){
181
			foreach ($items as $k=>$v){
182
				$this->addItem($v)->setData($k);
183
			}
184
		}else{
185
			foreach ($items as $item){
186
				$this->addItem($item);
187
			}
188
		}
189
	}
190
191
	/**
192
	 * Sets the values of a property for each item in the collection
193
	 * @param string $property
194
	 * @param array $values
195
	 * @return HtmlCollection
196
	 */
197
	public function setPropertyValues($property,$values){
198
		$i=0;
199
		if(\is_array($values)===false){
0 ignored issues
show
introduced by
The condition is_array($values) === false can never be true.
Loading history...
200
			$values=\array_fill(0, $this->count(),$values);
201
		}
202
		foreach ($values as $value){
203
			$c=$this->items[$i++];
204
			if(isset($c)){
205
				$c->setProperty($property,$value);
206
			}
207
			else{
208
				return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Ajax\semantic\html\modules\HtmlDropdown which is incompatible with the documented return type Ajax\common\html\HtmlCollection.
Loading history...
209
			}
210
		}
211
		return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Ajax\semantic\html\modules\HtmlDropdown which is incompatible with the documented return type Ajax\common\html\HtmlCollection.
Loading history...
212
	}
213
214
	public function each($callBack){
215
		foreach ($this->items as $index=>$value){
216
			$callBack($index,$value);
217
		}
218
		return $this;
219
	}
220
221
	public function getItem($index){
222
		return $this->items[$index];
223
	}
224
225
	/**
226
	 * @return int
227
	 */
228
	public function count(){
229
		return \sizeof($this->items);
230
	}
231
	/**
232
	 * @param boolean $dropdown
233
	 */
234
	public function asDropdown($dropdown){
235
		if($dropdown===false){
236
			$this->_template=include dirname(__FILE__).'/../templates/tplDropdownMenu.php';
237
			$dropdown="menu";
238
		}else{
239
			$dropdown="dropdown";
240
			$this->mClass="menu";
241
		}
242
		return $this->addToPropertyCtrl("class", $dropdown,array("menu","dropdown"));
243
	}
244
245
	public function setVertical(){
246
		return $this->addToPropertyCtrl("class", "vertical",array("vertical"));
247
	}
248
249
	public function setInline(){
250
		return $this->addToPropertyCtrl("class", "inline",["inline"]);
251
	}
252
253
	public function setSimple(){
254
		return $this->addToPropertyCtrl("class", "simple",array("simple"));
255
	}
256
257
	public function asButton($floating=false){
258
		$this->removeArrow();
259
		if($floating)
260
			$this->addToProperty("class", "floating");
261
		$this->removePropertyValue("class", "selection");
262
		return $this->addToProperty("class", "button");
263
	}
264
265
	public function asSelect($name=NULL,$multiple=false,$selection=true){
266
		$this->_multiple=$multiple;
267
		if(isset($name))
268
			$this->addInput($name);
269
		if($multiple){
270
			$this->addToProperty("class", "multiple");
271
		}
272
		if ($selection){
273
			if($this->propertyContains("class", "button")===false)
274
				$this->addToPropertyCtrl("class", "selection",array("selection"));
275
		}
276
		return $this;
277
	}
278
279
	public function asSearch($name=NULL,$multiple=false,$selection=true){
280
		$this->asSelect($name,$multiple,$selection);
281
		return $this->addToProperty("class", "search");
282
	}
283
284
	public function setSelect($name=NULL,$multiple=false){
285
		if(!isset($name))
286
			$name="select-".$this->identifier;
287
		$this->input=null;
288
		if($multiple){
289
			$this->setProperty("multiple", true);
290
			$this->addToProperty("class", "multiple");
291
		}
292
		$this->setAction("activate");
293
		$this->tagName="select";
294
		$this->setProperty("name", $name);
295
		$this->content=null;
296
		foreach ($this->items as $item){
297
			$item->asOption();
298
		}
299
		return $this;
300
	}
301
302
	public function asSubmenu($pointing=NULL){
303
		$this->setClass("ui dropdown link item");
304
		if(isset($pointing)){
305
			$this->setPointing($pointing);
306
		}
307
		return $this;
308
	}
309
310
	public function setPointing($value=Direction::NONE){
311
		return $this->addToPropertyCtrl("class", $value." pointing",Direction::getConstantValues("pointing"));
312
	}
313
314
	public function setValue($value){
315
		$this->value=$value;
316
		return $this;
317
	}
318
	private function applyValue(){
319
		$value=$this->value;
320
		if(isset($this->input) && isset($value)){
321
			$this->input->setProperty("value", $value);
322
		}else{
323
			$this->setProperty("value", $value);
324
		}
325
			$textElement=$this->getElementById("text-".$this->identifier, $this->content);
326
			if(isset($textElement) && ($textElement instanceof HtmlDoubleElement) && !$this->_multiple)
327
				$textElement->setContent($value);
328
		return $this;
329
	}
330
331
	/*
332
	 * (non-PHPdoc)
333
	 * @see BaseHtml::run()
334
	 */
335
	public function run(JsUtils $js) {
336
		if($this->propertyContains("class", "simple")===false){
337
			if(isset($this->_bsComponent)===false){
338
				$this->_bsComponent=$js->semantic()->dropdown("#".$this->identifier,$this->_params);
339
				$this->_bsComponent->setItemSelector(".item");
340
			}
341
			$this->addEventsOnRun($js);
342
			return $this->_bsComponent;
343
		}
344
	}
345
346
	public function setCompact(){
347
		return $this->addToPropertyCtrl("class", "compact", array("compact"));
348
	}
349
350
	public function setAction($action){
351
		$this->_params["action"]=$action;
352
	}
353
354
	public function setOn($on){
355
		$this->_params["on"]=$on;
356
	}
357
358
	public function setShowOnFocus($value){
359
		$this->_params["showOnFocus"]=$value;
360
	}
361
362
	public function setFullTextSearch($value){
363
		$this->_params["fullTextSearch"]=$value;
364
	}
365
366
	public function compile(JsUtils $js=NULL, &$view=NULL) {
367
		$this->applyValue();
368
		return parent::compile($js,$view);
369
	}
370
371
	public function getInput() {
372
		return $this->input;
373
	}
374
375
	public function setIcon($icon="dropdown"){
376
		$this->content["arrow"]=new HtmlIcon($this->identifier."-icon", $icon);
377
		return $this;
378
	}
379
380
	public function jsAddItem($caption){
381
		$js="var first=$('#{$this->identifier} .item').first();if(first!=undefined){var new =first.clone();first.parent().append(new);first.html('{$caption}};')";
382
		return $js;
383
	}
384
}
385