Completed
Push — master ( 409900...f20dfb )
by Jean-Christophe
03:24
created

HtmlDropdown::addItem()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 6
nc 2
nop 3
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\JsUtils;
8
use Ajax\semantic\html\elements\HtmlIcon;
9
use Ajax\common\html\html5\HtmlInput;
10
use Ajax\service\JArray;
11
12
class HtmlDropdown extends HtmlSemDoubleElement {
13
	protected $mClass="menu";
14
	protected $mTagName="div";
15
	protected $items=array ();
16
	protected $params=array("action"=>"select");
17
	protected $input;
18
19
	public function __construct($identifier, $value="", $items=array()) {
20
		parent::__construct($identifier, "div");
21
		$this->_template=include dirname(__FILE__).'/../templates/tplDropdown.php';
22
		$this->setProperty("class", "ui dropdown");
23
		$content=new HtmlSemDoubleElement("","div");
24
		$content->setClass("text");
25
		$content->setContent($value);
26
		$content->wrap("",new HtmlIcon("", "dropdown"));
27
		$this->content=array($content);
28
		$this->tagName="div";
29
		$this->addItems($items);
30
	}
31
32
	public function addItem($item,$value=NULL,$image=NULL){
33
		$itemO=$item;
34
		if(\is_object($item)===false){
35
			$itemO=new HtmlDropdownItem("dd-item-".$this->identifier,$item,$value,$image);
36
		}
37
		$this->items[]=$itemO;
38
		return $itemO;
39
	}
40
41
	public function addInput($name){
42
		if(!isset($name))
43
			$name="input-".$this->identifier;
44
		$this->setAction("activate");
45
		$this->input=new HtmlInput($name,"hidden");
46
	}
47
48
	public function addItems($items){
49
		if(JArray::isAssociative($items)){
50
			foreach ($items as $k=>$v){
51
				$this->addItem($v)->setData($k);
52
			}
53
		}else{
54
			foreach ($items as $item){
55
				$this->addItem($item);
56
			}
57
		}
58
	}
59
60
	public function asButton(){
61
		return $this->addToProperty("class", "button");
62
	}
63
64
	public function asSelect($name=NULL,$multiple=false,$selection=true){
65
		if(isset($name))
66
			$this->addInput($name);
67
		if($multiple)
68
			$this->addToProperty("class", "multiple");
69
		if ($selection)
70
			$this->addToPropertyCtrl("class", "selection",array("selection"));
71
		return $this;
72
	}
73
74
	public function asSearch($name=NULL,$multiple=false,$selection=false){
75
		$this->asSelect($name,$multiple,$selection);
76
		return $this->addToProperty("class", "search");
77
	}
78
79
	public function setSelect($name=NULL,$multiple=false){
80
		if(!isset($name))
81
			$name="select-".$this->identifier;
82
		$this->input=null;
83
		if($multiple){
84
			$this->setProperty("multiple", true);
85
			$this->addToProperty("class", "multiple");
86
		}
87
		$this->setAction("activate");
88
		$this->tagName="select";
89
		$this->setProperty("name", $name);
90
		$this->content=null;
91
		foreach ($this->items as $item){
92
			$item->asOption();
93
		}
94
		return $this;
95
	}
96
97
	public function setValue($value){
98
		if(isset($this->input)){
99
			$this->input->setProperty("value", $value);
100
		}else{
101
			$this->setProperty("value", $value);
102
		}
103
		return $this;
104
	}
105
106
	/*
107
	 * (non-PHPdoc)
108
	 * @see BaseHtml::run()
109
	 */
110
	public function run(JsUtils $js) {
111
		$this->_bsComponent=$js->semantic()->dropdown("#".$this->identifier,$this->params);
112
		$this->addEventsOnRun($js);
113
		return $this->_bsComponent;
114
	}
115
116
	public function setAction($action){
117
		$this->params["action"]=$action;
118
	}
119
}