Passed
Push — master ( 9a9b82...74dc3c )
by Jean-Christophe
02:28
created

FieldTrait::setFocus()   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\collections\form\traits;
4
5
use Ajax\semantic\html\modules\HtmlDropdown;
6
use Ajax\semantic\html\elements\HtmlButton;
7
use Ajax\semantic\html\base\constants\Direction;
8
use Ajax\semantic\html\base\constants\State;
9
use Ajax\semantic\html\modules\checkbox\HtmlCheckbox;
10
use Ajax\common\html\BaseHtml;
11
use Ajax\semantic\html\elements\HtmlLabel;
12
13
/**
14
 * @author jc
15
 * @property boolean $_hasIcon
16
 * @property string $identifier
17
 */
18
trait FieldTrait {
19
20
	abstract public function addToProperty($name, $value, $separator=" ");
21
	abstract public function addLabel($caption, $style="label-default", $leftSeparator="&nbsp;");
22
	abstract public function addContent($content,$before=false);
23
	abstract public function getField();
24
	abstract public function getDataField();
25
26
	public function setFocus() {
27
		$this->getField()->addToProperty("class", State::FOCUS);
28
	}
29
30
	public function addLoading() {
31
		if ($this->_hasIcon === false) {
32
			throw new \Exception("Input must have an icon for showing a loader, use addIcon before");
33
		}
34
		return $this->addToProperty("class", State::LOADING);
35
	}
36
37
	/**
38
	 * @param string|BaseHtml $label
39
	 * @param string $direction
40
	 * @param string $icon
41
	 * @return HtmlLabel
42
	 */
43
	public function labeled($label, $direction=Direction::LEFT, $icon=NULL) {
44
		$field=$this->getField();
45
		$labelO=$field->addLabel($label,$direction===Direction::LEFT,$icon);
46
		$field->addToProperty("class", $direction . " labeled");
47
		return $labelO;
48
	}
49
50
	/**
51
	 * @param string $direction
52
	 * @param string $caption
53
	 * @param string $value
54
	 * @param string $checkboxType
55
	 * @return HtmlLabel
56
	 */
57
	public function labeledCheckbox($direction=Direction::LEFT,$caption="",$value=NULL,$checkboxType=NULL){
58
		return $this->labeled(new HtmlCheckbox("lbl-ck-".$this->getField()->getIdentifier(),$caption,$value,$checkboxType),$direction);
59
	}
60
61
	/**
62
	 * @param string $icon
63
	 * @param string $direction
64
	 * @return HtmlLabel
65
	 */
66
	public function labeledToCorner($icon, $direction=Direction::LEFT) {
67
		return $this->labeled("", $direction . " corner", $icon)->toCorner($direction);
68
	}
69
70
	/**
71
	 * @param string $action
72
	 * @param string $direction
73
	 * @param string $icon
74
	 * @param boolean $labeled
75
	 * @return mixed|HtmlButton
76
	 */
77
	public function addAction($action, $direction=Direction::RIGHT, $icon=NULL, $labeled=false) {
78
		$field=$this->getField();
79
		$actionO=$action;
80
		if (\is_object($action) === false) {
0 ignored issues
show
introduced by
The condition is_object($action) === false is always true.
Loading history...
81
			$actionO=new HtmlButton("action-" . $this->identifier, $action);
0 ignored issues
show
Bug introduced by
$action of type object is incompatible with the type string expected by parameter $value of Ajax\semantic\html\eleme...mlButton::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

81
			$actionO=new HtmlButton("action-" . $this->identifier, /** @scrutinizer ignore-type */ $action);
Loading history...
82
			if (isset($icon))
83
				$actionO->addIcon($icon, true, $labeled);
84
		}
85
		$field->addToProperty("class", $direction . " action");
86
		$field->addContent($actionO, \strstr($direction, Direction::LEFT) !== false);
87
		return $actionO;
88
	}
89
90
	/**
91
	 * @param string $label
92
	 * @param array $items
93
	 * @param string $direction
94
	 * @return HtmlLabel
95
	 */
96
	public function addDropdown($label="", $items=array(),$direction=Direction::RIGHT){
97
		$labelO=new HtmlDropdown("dd-".$this->identifier,$label,$items);
98
		$labelO->asSelect("select-".$this->identifier,false,true);
99
		return $this->labeled($labelO,$direction);
100
	}
101
102
	public function setTransparent() {
103
		return $this->getField()->addToProperty("class", "transparent");
104
	}
105
106
	public function setReadonly(){
107
		$this->getDataField()->setProperty("readonly", "");
108
		return $this;
109
	}
110
111
	public function setName($name){
112
		$this->getDataField()->setProperty("name",$name);
113
		return $this;
114
	}
115
116
	public function setFluid(){
117
		$this->getField()->addToProperty("class","fluid");
118
		return $this;
119
	}
120
121
	public function setDisabled($disable=true) {
122
		$field=$this->getField();
123
		if($disable)
124
			$field->addToProperty("class", "disabled");
125
		return $this;
126
	}
127
	
128
	public function setJsContent($content){
129
		$id="";
130
		$field=$this->getDataField();
131
		if(isset($field)){
132
			$id=$field->getIdentifier();
133
		}
134
		if($id!==''){
135
			return '$("#'.$id.'").val('.$content.')';
136
		}
137
	}
138
	
139
	public function getJsContent(){
140
		return $this->setJsContent("");
141
	}
142
}
143