Passed
Push — master ( fa0f64...7ebbb8 )
by Jean-Christophe
01:58
created

FieldTrait::setDisabled()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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

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