Completed
Push — master ( 22ba15...552305 )
by Jean-Christophe
03:10
created

HtmlMessage::setMessage()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
3
namespace Ajax\semantic\html\collections;
4
5
use Ajax\semantic\html\base\HtmlSemDoubleElement;
6
use Ajax\common\html\html5\HtmlList;
7
use Ajax\semantic\html\elements\HtmlIcon;
8
use Ajax\JsUtils;
9
use Ajax\semantic\html\base\constants\Style;
10
use Ajax\semantic\html\base\traits\AttachedTrait;
11
use Ajax\common\html\HtmlDoubleElement;
12
/**
13
 * Semantic Message component
14
 * @see http://semantic-ui.com/collections/message.html
15
 * @author jc
16
 * @version 1.001
17
 */
18
class HtmlMessage extends HtmlSemDoubleElement {
19
	use AttachedTrait;
20
	protected $icon;
21
	protected $close;
22
	public function __construct($identifier, $content="") {
23
		parent::__construct($identifier, "div");
24
		$this->_template="<%tagName% id='%identifier%' %properties%>%close%%icon%%wrapContentBefore%%content%%wrapContentAfter%</%tagName%>";
25
		$this->setClass("ui message");
26
		$this->setContent($content);
27
	}
28
29
	/**
30
	 * Adds an header to the message
31
	 * @param string|HtmlSemDoubleElement $header
32
	 * @return \Ajax\semantic\html\collections\HtmlMessage
33
	 */
34
	public function addHeader($header){
35
		$headerO=$header;
36
		if(\is_string($header)){
37
			$headerO=new HtmlSemDoubleElement("header-".$this->identifier,"div");
38
			$headerO->setClass("header");
39
			$headerO->setContent($header);
40
		}
41
		return $this->addContent($headerO,true);
42
	}
43
44
	public function addList($elements,$ordered=false){
45
		$list=new HtmlList("list-".$this->identifier,$elements);
46
		$list->setOrdered($ordered);
47
		$list->setClass("ui list");
48
		$this->addContent($list);
49
	}
50
51
	public function setIcon($icon){
52
		$this->addToProperty("class", "icon");
53
		$this->wrapContent("<div class='content'>","</div>");
54
		$this->icon=new HtmlIcon("icon-".$this->identifier, $icon);
55
		return $this;
56
	}
57
58
	public function addLoader($loaderIcon="notched circle"){
59
		$this->setIcon($loaderIcon);
60
		$this->icon->addToIcon("loading");
61
		return $this;
62
	}
63
64
	public function setDismissable($dismiss=true){
65
		if($dismiss===true)
66
			$this->close=new HtmlIcon("close-".$this->identifier, "close");
67
		else
68
			$this->close=NULL;
69
		return $this;
70
	}
71
72
	/**
73
	 * {@inheritDoc}
74
	 * @see \Ajax\semantic\html\base\HtmlSemDoubleElement::run()
75
	 */
76
	public function run(JsUtils $js){
77
		parent::run($js);
78
		if(isset($this->close)){
79
			$js->execOn("click", "#".$this->identifier." .close", "$(this).closest('.message').transition('fade')");
80
		}
81
	}
82
83
	public function setState($visible=true){
84
		$visible=($visible===true)?"visible":"hidden";
85
		return $this->addToPropertyCtrl("class", $visible, array("visible","hidden"));
86
	}
87
88
	public function setVariation($value="floating"){
89
		return $this->addToPropertyCtrl("class", $value, array("floating","compact"));
90
	}
91
92
	public function setStyle($style){
93
		return $this->addToPropertyCtrl("class", $style, Style::getConstants());
94
	}
95
96
	public function setError(){
97
		return $this->setStyle("error");
98
	}
99
100
	public function setMessage($message){
101
		if(\is_array($this->content)){
102
			$this->content[\sizeof($this->content)-1]=$message;
103
		}else
104
			$this->setContent($message);
105
	}
106
}