phpMv -UI toolkit 2.4.12
jQuery, jQuery UI, Twitter Bootstrap and Semantic-UI library for php & php MVC Frameworks
Loading...
Searching...
No Matches
HtmlDropdown.php
Go to the documentation of this file.
1<?php
3
14
17 }
18
19 protected $mClass = "menu";
20
21 protected $mTagName = "div";
22
23 protected $items = array();
24
25 protected $_params = array(
26 "action" => "nothing",
27 "on" => "hover",
28 "showOnFocus" => true
29 );
30
31 protected $input;
32
33 protected $value;
34
35 protected $_associative;
36
37 protected $_multiple;
38
39 public function __construct($identifier, $value = "", $items = array(), $associative = true) {
40 parent::__construct($identifier, "div");
41 $this->_template = include dirname(__FILE__) . '/../templates/tplDropdown.php';
42 $this->setProperty("class", "ui dropdown");
43 $this->_multiple = false;
44 $content = [];
45 $text = '';
46 if ($value instanceof HtmlSemDoubleElement) {
47 $text = $value;
48 } elseif ($value !== null) {
49 $text = new HtmlSemDoubleElement("text-" . $this->identifier, "div");
50 $text->setClass("text");
51 $this->setValue($value);
52 }
53 if ($text != null) {
54 $content = [
55 "text" => $text
56 ];
57 }
58 $content["arrow"] = new HtmlIcon($identifier . "-icon", "dropdown");
59 $this->content = $content;
60 $this->tagName = "div";
61 $this->_associative = $associative;
62 $this->addItems($items);
63 }
64
65 public function getField() {
66 return $this->input;
67 }
68
69 public function getDataField() {
70 return $this->input;
71 }
72
73 public function addItem($item, $value = NULL, $image = NULL, $description = NULL) {
74 $itemO = $this->beforeAddItem($item, $value, $image, $description);
75 $this->items[] = $itemO;
76 return $itemO;
77 }
78
79 public function addIcon($icon, $before = true, $labeled = false) {
80 $this->removeArrow();
81 $this->addIconP($icon, $before, $labeled);
82 $elm = $this->getElementById("text-" . $this->identifier, $this->content);
83 if (isset($elm)) {
84 $elm->setWrapAfter("");
85 }
86 return $this;
87 }
88
89 public function addIcons($icons) {
90 $count = $this->count();
91 for ($i = 0; $i < \sizeof($icons) && $i < $count; $i ++) {
92 $this->getItem($i)->addIcon($icons[$i]);
93 }
94 }
95
103 public function insertItem($item, $position = 0) {
104 $itemO = $this->beforeAddItem($item);
105 $start = array_slice($this->items, 0, $position);
106 $end = array_slice($this->items, $position);
107 $start[] = $item;
108 $this->items = array_merge($start, $end);
109 return $itemO;
110 }
111
112 protected function removeArrow() {
113 if (\sizeof($this->content) > 1) {
114 unset($this->content["arrow"]);
115 $this->content = \array_values($this->content);
116 }
117 }
118
119 protected function beforeAddItem($item, $value = NULL, $image = NULL, $description = NULL) {
120 $itemO = $item;
121 if (\is_array($item)) {
122 $description = JArray::getValue($item, "description", 3);
123 $value = JArray::getValue($item, "value", 1);
124 $image = JArray::getValue($item, "image", 2);
125 $item = JArray::getValue($item, "item", 0);
126 }
127 if (! $item instanceof HtmlDropdownItem) {
128 $itemO = new HtmlDropdownItem("dd-item-" . $this->identifier . "-" . \sizeof($this->items), $item, $value, $image, $description);
129 } elseif ($itemO instanceof HtmlDropdownItem) {
130 $this->addToProperty("class", "vertical");
131 }
132 return $itemO;
133 }
134
135 /*
136 * (non-PHPdoc)
137 * @see \Ajax\bootstrap\html\base\BaseHtml::fromDatabaseObject()
138 */
139 public function fromDatabaseObject($object, $function) {
140 $this->addItem($function($object));
141 }
142
143 public function addInput($name) {
144 if (! isset($name))
145 $name = "input-" . $this->identifier;
146 $this->setAction("activate");
147 $this->input = new HtmlInput($name, "hidden");
148 $this->input->setIdentifier("input-" . $this->identifier);
149 return $this->input;
150 }
151
159 public function addSearchInputItem($placeHolder = NULL, $icon = NULL) {
160 return $this->addItem(HtmlDropdownItem::searchInput($placeHolder, $icon));
161 }
162
168 public function addDividerItem() {
169 return $this->addItem(HtmlDropdownItem::divider());
170 }
171
179 public function addHeaderItem($caption = NULL, $icon = NULL) {
180 return $this->addItem(HtmlDropdownItem::header($caption, $icon));
181 }
182
190 public function addCircularLabelItem($caption, $color) {
191 return $this->addItem(HtmlDropdownItem::circular($caption, $color));
192 }
193
200 public function addMiniAvatarImageItem($caption, $image) {
201 return $this->addItem(HtmlDropdownItem::avatar($caption, $image));
202 }
203
204 public function addItems($items) {
205 if (\is_array($items) && $this->_associative) {
206 foreach ($items as $k => $v) {
207 $this->addItem($v)->setData($k);
208 }
209 } else {
210 foreach ($items as $item) {
211 $this->addItem($item);
212 }
213 }
214 }
215
223 public function setPropertyValues($property, $values) {
224 $i = 0;
225 if (\is_array($values) === false) {
226 $values = \array_fill(0, $this->count(), $values);
227 }
228 foreach ($values as $value) {
229 $c = $this->items[$i ++];
230 if (isset($c)) {
231 $c->setProperty($property, $value);
232 } else {
233 return $this;
234 }
235 }
236 return $this;
237 }
238
239 public function each($callBack) {
240 foreach ($this->items as $index => $value) {
241 $callBack($index, $value);
242 }
243 return $this;
244 }
245
246 public function getItem($index) {
247 return $this->items[$index];
248 }
249
254 public function count() {
255 return \sizeof($this->items);
256 }
257
262 public function asDropdown($dropdown) {
263 if ($dropdown === false) {
264 $this->_template = include dirname(__FILE__) . '/../templates/tplDropdownMenu.php';
265 $dropdown = "menu";
266 } else {
267 $dropdown = "dropdown";
268 $this->mClass = "menu";
269 }
270 return $this->addToPropertyCtrl("class", $dropdown, array(
271 "menu",
272 "dropdown"
273 ));
274 }
275
276 public function setVertical() {
277 return $this->addToPropertyCtrl("class", "vertical", array(
278 "vertical"
279 ));
280 }
281
282 public function setInline() {
283 return $this->addToPropertyCtrl("class", "inline", [
284 "inline"
285 ]);
286 }
287
288 public function setSimple() {
289 return $this->addToPropertyCtrl("class", "simple", array(
290 "simple"
291 ));
292 }
293
294 public function asButton($floating = false) {
295 $this->removeArrow();
296 if ($floating)
297 $this->addToProperty("class", "floating");
298 $this->removePropertyValue("class", "selection");
299 return $this->addToProperty("class", "button");
300 }
301
302 public function asSelect($name = NULL, $multiple = false, $selection = true) {
303 $this->_multiple = $multiple;
304 if (isset($name))
305 $this->addInput($name);
306 if ($multiple) {
307 $this->addToProperty("class", "multiple");
308 }
309 if ($selection) {
310 if ($this->propertyContains("class", "button") === false)
311 $this->addToPropertyCtrl("class", "selection", array(
312 "selection"
313 ));
314 }
315 return $this;
316 }
317
318 public function asSearch($name = NULL, $multiple = false, $selection = true) {
319 $this->asSelect($name, $multiple, $selection);
320 return $this->addToProperty("class", "search");
321 }
322
323 public function setSelect($name = NULL, $multiple = false) {
324 $this->_template = '<%tagName% id="%identifier%" %properties%>%items%</%tagName%>';
325 if (! isset($name))
326 $name = "select-" . $this->identifier;
327 $this->input = null;
328 if ($multiple) {
329 $this->setProperty("multiple", true);
330 $this->addToProperty("class", "multiple");
331 }
332 $this->setAction("activate");
333 $this->tagName = "select";
334 $this->setProperty("name", $name);
335 $this->content = null;
336 foreach ($this->items as $item) {
337 $item->asOption();
338 }
339 return $this;
340 }
341
342 public function asSubmenu($pointing = NULL) {
343 $this->setClass("ui dropdown link item");
344 if (isset($pointing)) {
345 $this->setPointing($pointing);
346 }
347 return $this;
348 }
349
350 public function setPointing($value = Direction::NONE) {
351 return $this->addToPropertyCtrl("class", $value . " pointing", Direction::getConstantValues("pointing"));
352 }
353
354 public function setValue($value) {
355 $this->value = $value;
356 return $this;
357 }
358
359 public function setDefaultText($text) {
360 $this->content["text"] = new HtmlSemDoubleElement("", "div", "default text", $text);
361 return $this;
362 }
363
364 private function applyValue() {
366 if (isset($this->input) && isset($value)) {
367 $this->input->setProperty("value", $value);
368 } else {
369 $this->setProperty("value", $value);
370 }
371 $textElement = $this->getElementById("text-" . $this->identifier, $this->content);
372 if (isset($textElement) && ($textElement instanceof HtmlDoubleElement) && ! $this->_multiple)
373 $textElement->setContent($value);
374 return $this;
375 }
376
377 /*
378 * (non-PHPdoc)
379 * @see BaseHtml::run()
380 */
381 public function run(JsUtils $js) {
382 if ($this->propertyContains("class", "simple") === false) {
383 if (isset($this->_bsComponent) === false) {
384 $this->_bsComponent = $js->semantic()->dropdown("#" . $this->identifier, $this->_params);
385 $this->_bsComponent->setItemSelector(".item");
386 }
387 $this->addEventsOnRun($js);
388 return $this->_bsComponent;
389 }
390 }
391
392 public function setCompact() {
393 return $this->addToPropertyCtrl("class", "compact", array(
394 "compact"
395 ));
396 }
397
398 public function setAction($action) {
399 $this->_params["action"] = $action;
400 return $this;
401 }
402
403 public function setOn($on) {
404 $this->_params["on"] = $on;
405 return $this;
406 }
407
408 public function setShowOnFocus($value) {
409 $this->_params["showOnFocus"] = $value;
410 return $this;
411 }
412
413 public function setAllowAdditions($value) {
414 $this->_params["allowAdditions"] = $value;
415 return $this;
416 }
417
418 public function setFullTextSearch($value) {
419 $this->_params["fullTextSearch"] = $value;
420 return $this;
421 }
422
423 public function compile(JsUtils $js = NULL, &$view = NULL) {
424 $this->applyValue();
425 return parent::compile($js, $view);
426 }
427
428 public function getInput() {
429 return $this->input;
430 }
431
432 public function setIcon($icon = "dropdown") {
433 $this->content["arrow"] = new HtmlIcon($this->identifier . "-icon", $icon);
434 return $this;
435 }
436
437 public function jsAddItem($caption, $value = null) {
438 $value = $value ?? $caption;
439 $js = "var first=$('#{$this->identifier} .item').first();if(first!=undefined){var newItem =first.clone();first.parent().append(newItem);newItem.html({$caption});newItem.attr('data-value',{$value}).removeClass('active filtered');}";
440 return $js;
441 }
442
443 public function setClearable($value) {
444 $this->_params["clearable"] = $value;
445 return $this;
446 }
447
452 public function setOnAdd($jsCode) {
453 $this->_params["onAdd"] = $jsCode;
454 return $this;
455 }
456
461 public function setOnRemove($jsCode) {
462 $this->_params["onRemove"] = $jsCode;
463 return $this;
464 }
465}
JQuery PHP library.
Definition JsUtils.php:23
semantic(Semantic $semantic=NULL)
getter or setter of the Semantic-UI variable
Definition JsUtils.php:158
getElementById($identifier, $elements)
Definition BaseHtml.php:178
Base class for Semantic double elements.
addToPropertyCtrl($name, $value, $typeCtrl)
addToProperty($name, $value, $separator=" ")
Semantic Icon component.
Definition HtmlIcon.php:14
addSearchInputItem($placeHolder=NULL, $icon=NULL)
Adds a search input item.
__construct($identifier, $value="", $items=array(), $associative=true)
beforeAddItem($item, $value=NULL, $image=NULL, $description=NULL)
setSelect($name=NULL, $multiple=false)
run(JsUtils $js)
{{SimpleExtComponent\Ajax\common\html\BaseHtmlrun()\Ajax\common\html\BaseHtmlrun()}HtmlDoubleElement:...
addIcon($icon, $before=true, $labeled=false)
Adds an icon before or after.
addHeaderItem($caption=NULL, $icon=NULL)
Adds an header item.
asSearch($name=NULL, $multiple=false, $selection=true)
setOnAdd($jsCode)
Is called after a dropdown selection is added using a multiple select dropdown, only receives the add...
compile(JsUtils $js=NULL, &$view=NULL)
{{}BaseHtml::compile()}
setPropertyValues($property, $values)
Sets the values of a property for each item in the collection.
setOnRemove($jsCode)
Is called after a dropdown selection is removed using a multiple select dropdown, only receives the r...
addItem($item, $value=NULL, $image=NULL, $description=NULL)
asSelect($name=NULL, $multiple=false, $selection=true)
insertItem($item, $position=0)
Insert an item at a position.
addCircularLabelItem($caption, $color)
Adds an item with a circular label.