Completed
Push — master ( 4bf172...5f7aee )
by Jean-Christophe
03:35
created

HtmlMessage::setTimeout()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 3
nc 1
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\semantic\html\base\traits\HasTimeoutTrait;
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,HasTimeoutTrait;
20
	protected $icon;
21
	protected $close;
22
23
24
	public function __construct($identifier, $content="") {
25
		parent::__construct($identifier, "div");
26
		$this->_template="<%tagName% id='%identifier%' %properties%>%close%%icon%%wrapContentBefore%%content%%wrapContentAfter%</%tagName%>";
27
		$this->setClass("ui message");
28
		$this->setContent($content);
29
	}
30
31
	/**
32
	 * Adds an header to the message
33
	 * @param string|HtmlSemDoubleElement $header
34
	 * @return \Ajax\semantic\html\collections\HtmlMessage
35
	 */
36
	public function addHeader($header){
37
		$headerO=$header;
38
		if(\is_string($header)){
39
			$headerO=new HtmlSemDoubleElement("header-".$this->identifier,"div");
40
			$headerO->setClass("header");
41
			$headerO->setContent($header);
42
		}
43
		return $this->addContent($headerO,true);
44
	}
45
46
	public function setHeader($header){
47
		return $this->addHeader($header);
48
	}
49
50
	public function addList($elements,$ordered=false){
51
		$list=new HtmlList("list-".$this->identifier,$elements);
52
		$list->setOrdered($ordered);
53
		$list->setClass("ui list");
54
		$this->addContent($list);
55
	}
56
57
	public function setIcon($icon){
58
		$this->addToProperty("class", "icon");
59
		$this->wrapContent("<div class='content'>","</div>");
60
		$this->icon=new HtmlIcon("icon-".$this->identifier, $icon);
61
		return $this;
62
	}
63
64
	public function addLoader($loaderIcon="notched circle"){
65
		$this->setIcon($loaderIcon);
66
		$this->icon->addToIcon("loading");
67
		return $this;
68
	}
69
70
	public function setDismissable($dismiss=true){
71
		if($dismiss===true)
72
			$this->close=new HtmlIcon("close-".$this->identifier, "close");
73
		else
74
			$this->close=NULL;
75
		return $this;
76
	}
77
78
	/**
79
	 * {@inheritDoc}
80
	 * @see \Ajax\semantic\html\base\HtmlSemDoubleElement::run()
81
	 */
82
	public function run(JsUtils $js){
83
		if(!isset($this->_bsComponent)){
84
			if(isset($this->close)){
85
				$js->execOn("click", "#".$this->identifier." .close", "$(this).closest('.message').transition({$this->_closeTransition})");
86
			}
87
			if(isset($this->_timeout)){
88
				$js->exec("setTimeout(function() { $('#{$this->identifier}').transition({$this->_closeTransition}); }, {$this->_timeout});",true);
89
			}
90
		}
91
		return parent::run($js);
92
	}
93
94
	public function setState($visible=true){
95
		$visible=($visible===true)?"visible":"hidden";
96
		return $this->addToPropertyCtrl("class", $visible, array("visible","hidden"));
97
	}
98
99
	public function setVariation($value="floating"){
100
		return $this->addToPropertyCtrl("class", $value, array("floating","compact"));
101
	}
102
103
	public function setStyle($style){
104
		return $this->addToPropertyCtrl("class", $style, Style::getConstants());
105
	}
106
107
	public function setError(){
108
		return $this->setStyle("error");
109
	}
110
111
	public function setWarning(){
112
		return $this->setStyle("warning");
113
	}
114
115
	public function setMessage($message){
116
		if(\is_array($this->content)){
117
			$this->content[\sizeof($this->content)-1]=$message;
118
		}else
119
			$this->setContent($message);
120
	}
121
122
}