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
|
|
|
|
10
|
|
|
trait FieldTrait { |
11
|
|
|
|
12
|
|
|
abstract public function addToProperty($name, $value, $separator=" "); |
13
|
|
|
abstract public function addLabel($caption, $style="label-default", $leftSeparator=" "); |
14
|
|
|
abstract public function addContent($content,$before=false); |
15
|
|
|
abstract public function getField(); |
16
|
|
|
public function setFocus() { |
17
|
|
|
$this->getField()->addToProperty("class", State::FOCUS); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
public function addLoading() { |
21
|
|
|
if ($this->_hasIcon === false) { |
|
|
|
|
22
|
|
|
throw new \Exception("Input must have an icon for showing a loader, use addIcon before"); |
23
|
|
|
} |
24
|
|
|
return $this->addToProperty("class", State::LOADING); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function labeled($label, $direction=Direction::LEFT, $icon=NULL) { |
28
|
|
|
$field=$this->getField(); |
29
|
|
|
$labelO=$field->addLabel($label,$direction===Direction::LEFT,$icon); |
30
|
|
|
$field->addToProperty("class", $direction . " labeled"); |
31
|
|
|
return $labelO; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function labeledToCorner($icon, $direction=Direction::LEFT) { |
35
|
|
|
return $this->labeled("", $direction . " corner", $icon)->toCorner($direction); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function addAction($action, $direction=Direction::RIGHT, $icon=NULL, $labeled=false) { |
39
|
|
|
$field=$this->getField(); |
40
|
|
|
$actionO=$action; |
41
|
|
|
if (\is_object($action) === false) { |
42
|
|
|
$actionO=new HtmlButton("action-" . $this->identifier, $action); |
|
|
|
|
43
|
|
|
if (isset($icon)) |
44
|
|
|
$actionO->addIcon($icon, true, $labeled); |
45
|
|
|
} |
46
|
|
|
$field->addToProperty("class", $direction . " action"); |
47
|
|
|
$field->addContent($actionO, \strstr($direction, Direction::LEFT) !== false); |
48
|
|
|
return $actionO; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function addDropdown($label="", $items=array(),$direction=Direction::RIGHT){ |
52
|
|
|
$labelO=new HtmlDropdown("dd-".$this->identifier,$label,$items); |
53
|
|
|
$labelO->asSelect("select-".$this->identifier,false,true); |
54
|
|
|
return $this->labeled($labelO,$direction); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function setTransparent() { |
58
|
|
|
return $this->getField()->addToProperty("class", "transparent"); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function setReadonly(){ |
62
|
|
|
$this->getDataField()->setProperty("readonly", ""); |
|
|
|
|
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function setName($name){ |
66
|
|
|
$this->getDataField()->setProperty("name",$name); |
|
|
|
|
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
} |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: