Passed
Push — master ( be8276...dfdbfa )
by Jean-Christophe
02:11
created

HtmlDropdown::addDividerItem()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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