Completed
Push — master ( 8e32f8...32ad89 )
by Jean-Christophe
03:26
created

HtmlDropdown::asSubmenu()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
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
use Ajax\semantic\html\base\constants\Direction;
12
use Ajax\semantic\html\base\traits\LabeledIconTrait;
13
14
class HtmlDropdown extends HtmlSemDoubleElement {
15
	use LabeledIconTrait {
16
		addIcon as addIconP;
17
	}
18
	protected $mClass="menu";
19
	protected $mTagName="div";
20
	protected $items=array ();
21
	protected $params=array("action"=>"nothing","on"=>"hover");
22
	protected $input;
23
24
	public function __construct($identifier, $value="", $items=array()) {
25
		parent::__construct($identifier, "div");
26
		$this->_template=include dirname(__FILE__).'/../templates/tplDropdown.php';
27
		$this->setProperty("class", "ui dropdown");
28
		$content=new HtmlSemDoubleElement("text-".$this->identifier,"div");
29
		$content->setClass("text");
30
		$content->setContent($value);
31
		$content->wrap("",new HtmlIcon("", "dropdown"));
32
		$this->content=array($content);
33
		$this->tagName="div";
34
		$this->addItems($items);
35
	}
36
37
	public function addItem($item,$value=NULL,$image=NULL){
38
		$itemO=$this->beforeAddItem($item,$value,$image);
39
		$this->items[]=$itemO;
40
		return $itemO;
41
	}
42
43
	public function addIcon($icon,$before=true,$labeled=false){
44
		$this->addIconP($icon,$before,$labeled);
45
		return $this->getElementById("text-".$this->identifier, $this->content)->setWrapAfter("");
46
	}
47
	/**
48
	 * Insert an item at a position
49
	 * @param mixed $item
50
	 * @param number $position
51
	 * @return \Ajax\semantic\html\content\HtmlDropdownItem|unknown
52
	 */
53
	public function insertItem($item,$position=0){
54
		$itemO=$this->beforeAddItem($item);
55
		 $start = array_slice($this->items, 0, $position);
56
		 $end = array_slice($this->items, $position);
57
		 $start[] = $item;
58
		 $this->items=array_merge($start, $end);
59
		 return $itemO;
60
	}
61
62
	protected function beforeAddItem($item,$value=NULL,$image=NULL){
63
		$itemO=$item;
64
		if(!$item instanceof HtmlDropdownItem){
65
			$itemO=new HtmlDropdownItem("dd-item-".$this->identifier."-".\sizeof($this->items),$item,$value,$image);
66
		}elseif($itemO instanceof HtmlDropdownItem){
67
			$this->addToProperty("class", "vertical");
68
		}
69
		return $itemO;
70
	}
71
72
	/* (non-PHPdoc)
73
	 * @see \Ajax\bootstrap\html\base\BaseHtml::fromDatabaseObject()
74
	 */
75
	public function fromDatabaseObject($object, $function) {
76
		$this->addItem($function($object));
77
	}
78
79
	public function addInput($name){
80
		if(!isset($name))
81
			$name="input-".$this->identifier;
82
		$this->setAction("activate");
83
		$this->input=new HtmlInput($name,"hidden");
84
	}
85
86 View Code Duplication
	public function addItems($items){
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
87
		if(JArray::isAssociative($items)){
88
			foreach ($items as $k=>$v){
89
				$this->addItem($v)->setData($k);
90
			}
91
		}else{
92
			foreach ($items as $item){
93
				$this->addItem($item);
94
			}
95
		}
96
	}
97
98
	public function count(){
99
		return \sizeof($this->items);
100
	}
101
	/**
102
	 * @param boolean $dropdown
103
	 */
104
	public function asDropdown($dropdown){
105
		if($dropdown===false){
106
			$this->_template=include dirname(__FILE__).'/../templates/tplDropdownMenu.php';
107
			$dropdown="menu";
108
		}else{
109
			$dropdown="dropdown";
110
			$this->mClass="menu";
111
		}
112
		return $this->addToPropertyCtrl("class", $dropdown,array("menu","dropdown"));
113
	}
114
115
	public function setVertical(){
116
		return $this->addToPropertyCtrl("class", "vertical",array("vertical"));
117
	}
118
119
	public function setSimple(){
120
		return $this->addToPropertyCtrl("class", "simple",array("simple"));
121
	}
122
123
	public function asButton($floating=false){
124
		if($floating)
125
			$this->addToProperty("class", "floating");
126
		return $this->addToProperty("class", "button");
127
	}
128
129
	public function asSelect($name=NULL,$multiple=false,$selection=true){
130
		if(isset($name))
131
			$this->addInput($name);
132
		if($multiple)
133
			$this->addToProperty("class", "multiple");
134
		if ($selection)
135
			$this->addToPropertyCtrl("class", "selection",array("selection"));
136
		return $this;
137
	}
138
139
	public function asSearch($name=NULL,$multiple=false,$selection=false){
140
		$this->asSelect($name,$multiple,$selection);
141
		return $this->addToProperty("class", "search");
142
	}
143
144
	public function setSelect($name=NULL,$multiple=false){
145
		if(!isset($name))
146
			$name="select-".$this->identifier;
147
		$this->input=null;
148
		if($multiple){
149
			$this->setProperty("multiple", true);
150
			$this->addToProperty("class", "multiple");
151
		}
152
		$this->setAction("activate");
153
		$this->tagName="select";
154
		$this->setProperty("name", $name);
155
		$this->content=null;
156
		foreach ($this->items as $item){
157
			$item->asOption();
158
		}
159
		return $this;
160
	}
161
162
	public function asSubmenu($pointing=NULL){
163
		$this->setClass("ui dropdown link item");
164
		if(isset($pointing)){
165
			$this->setPointing($pointing);
166
		}
167
		return $this;
168
	}
169
170
	public function setPointing($value=Direction::NONE){
171
		return $this->addToPropertyCtrl("class", $value." pointing",Direction::getConstantValues("pointing"));
172
	}
173
174
	public function setValue($value){
175
		if(isset($this->input)){
176
			$this->input->setProperty("value", $value);
177
		}else{
178
			$this->setProperty("value", $value);
179
		}
180
		$textElement=$this->getElementById("text-".$this->identifier, $this->content);
181
		if(isset($textElement))
182
			$textElement->setContent($value);
183
		return $this;
184
	}
185
186
	/*
187
	 * (non-PHPdoc)
188
	 * @see BaseHtml::run()
189
	 */
190
	public function run(JsUtils $js) {
191
		if($this->propertyContains("class", "simple")===false){
192
			if(isset($this->_bsComponent)===false)
193
				$this->_bsComponent=$js->semantic()->dropdown("#".$this->identifier,$this->params);
194
			$this->addEventsOnRun($js);
195
			return $this->_bsComponent;
196
		}
197
	}
198
199
	public function setAction($action){
200
		$this->params["action"]=$action;
201
	}
202
203
	public function setFullTextSearch($value){
204
		$this->params["fullTextSearch"]=$value;
205
	}
206
}