Completed
Push — master ( 0ba4b1...2affe6 )
by Jean-Christophe
03:09
created

HtmlInput::setFocus()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Ajax\semantic\html\elements;
4
5
use Ajax\semantic\html\base\HtmlSemDoubleElement;
6
use Ajax\semantic\html\base\constants\State;
7
use Ajax\semantic\html\base\constants\Direction;
8
9
class HtmlInput extends HtmlSemDoubleElement{
10
	private $_hasIcon=false;
11
12
	public function __construct($identifier,$type="text",$value="",$placeholder=""){
13
		parent::__construct("div-".$identifier,"div","ui input");
14
		$this->content=new \Ajax\common\html\html5\HtmlInput($identifier,$type,$value,$placeholder);
15
		$this->_states=[State::DISABLED,State::FOCUS,State::ERROR,State::LOADING];
16
		$this->_variations=[];
17
	}
18
19
	public function setFocus(){
20
		$this->addToProperty("class", State::FOCUS);
21
	}
22
23
	public function addIcon($icon,$direction=Direction::LEFT){
24
		if($this->_hasIcon===false){
25
			$iconO=$icon;
26
			if(\is_string($icon)){
27
				$iconO=new HtmlIcon("icon-".$this->identifier, $icon);
28
			}
29
			$this->addToPropertyCtrl("class", $direction." icon", Direction::getConstantValues("icon"));
30
			$this->addContent($iconO,false);
31
			$this->_hasIcon=true;
32
		}else{
33
			$iconO=$this->getIcon();
34
			$iconO->setIcon($icon);
35
			$this->addToPropertyCtrl("class", $direction." icon", Direction::getConstantValues("icon"));
36
		}
37
		return $iconO;
38
	}
39
40
	public function getIcon(){
41
		if(\is_array($this->content)){
42
			foreach ($this->content as $item){
43
				if($item instanceof HtmlIcon)
44
					return $item;
45
			}
46
		}
47
	}
48
49
	public function addLoading(){
50
		if($this->_hasIcon===false){
51
			throw new \Exception("Input must have an icon for showing a loader, use addIcon before");
52
		}
53
		return $this->addToProperty("class", State::LOADING);
54
	}
55
56
	public function addLabel($label,$direction=Direction::LEFT,$icon=NULL){
57
		$labelO=$label;
58
		if(\is_object($label)===false){
59
			$labelO=new HtmlLabel("label-".$this->identifier,$label);
60
			if(isset($icon))
61
				$labelO->addIcon($icon);
62
		}else{
63
			$labelO->addToPropertyCtrl("class", "label", array("label"));
64
		}
65
		$this->addToProperty("class", $direction." labeled");
66
		$this->addContent($labelO,\strstr($direction,Direction::LEFT)!==false);
67
		return $labelO;
68
	}
69
70
	public function addLabelToCorner($label,$direction=Direction::LEFT,$icon=NULL){
71
		return $this->addLabel($label,$direction." corner",$icon)->toCorner($direction);
72
	}
73
74
	public function addAction($action,$direction=Direction::LEFT,$icon=NULL,$labeled=false){
75
		$actionO=$action;
76
		if(\is_object($action)===false){
77
			$actionO=new HtmlButton("action-".$this->identifier,$action);
78
			if(isset($icon))
79
				$actionO->addIcon($icon,true,$labeled);
80
		}
81
		$this->addToProperty("class", $direction." action");
82
		$this->addContent($actionO,\strstr($direction,Direction::LEFT)!==false);
83
		return $actionO;
84
	}
85
86
}