Completed
Push — master ( 10d75a...30b23e )
by Jean-Christophe
03:40
created

HtmlDropdownItem::active()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
namespace Ajax\bootstrap\html\content;
4
5
use Ajax\JsUtils;
6
use Ajax\bootstrap\html\base\BaseHtml;
7
use Ajax\bootstrap\html\HtmlBadge;
8
use Ajax\bootstrap\html\base\HtmlDoubleElement;
9
use Ajax\bootstrap\html\HtmlGlyphicon;
10
use Ajax\service\PhalconUtils;
11
12
/**
13
 * Inner element for Twitter Bootstrap HTML Dropdown component
14
 * @author jc
15
 * @version 1.001
16
 */
17
class HtmlDropdownItem extends HtmlDoubleElement {
18
	protected $_htmlDropdown;
19
	protected $class;
20
	protected $itemClass;
21
	protected $href;
22
	protected $role;
23
	protected $itemRole;
24
	protected $target;
25
26
	/**
27
	 *
28
	 * @param string $identifier the id
29
	 */
30
	public function __construct($identifier) {
31
		parent::__construct($identifier);
32
		$this->class="";
33
		$this->itemClass="";
34
		$this->content="";
35
		$this->href="#";
36
		$this->role="menuitem";
37
		$this->target="_self";
38
		$this->_template='<li id="%identifier%" class="%class%" role="%role%"><a id="a-%identifier%" role="%itemRole%" class="%itemClass%" tabindex="-1" href="%href%" target="%target%">%content%</a></li>';
39
	}
40
41
	public function setItemClass($itemClass) {
42
		$this->itemClass=$itemClass;
43
		return $this;
44
	}
45
46
	public function setItemRole($itemRole) {
47
		$this->itemRole=$itemRole;
48
		return $this;
49
	}
50
51
	/**
52
	 * Set the item class
53
	 * @param string $value
54
	 * @return $this default : ''
55
	 */
56
	public function setClass($value) {
57
		$this->class=$value;
58
		return $this;
59
	}
60
61
	/**
62
	 * Set the item caption
63
	 * @param string $value
64
	 * @return $this
65
	 */
66
	public function setCaption($value) {
67
		if (PhalconUtils::startsWith($value, "-")) {
68
			$this->class="dropdown-header";
69
			$this->role="presentation";
70
			$this->_template='<li id="%identifier%" class="%class%" role="%role%">%content%</li>';
71
			if ($value==="-") {
72
				$this->class="divider";
73
			}
74
			$value=substr($value, 1);
75
		}
76
		$this->content=$value;
77
		return $this;
78
	}
79
80
	public function disable() {
81
		$this->role="presentation";
82
		$this->class="disabled";
83
	}
84
	
85
	public function active() {
86
		$this->role="menuitem";
87
		$this->class="active";
88
	}
89
90
	/**
91
	 * Set the item href
92
	 * @param string $value
93
	 * @return $this default : '#'
94
	 */
95
	public function setHref($value) {
96
		$this->href=$value;
97
		return $this;
98
	}
99
100
	/**
101
	 * Set the item role
102
	 * @param string $value
103
	 * @return $this default : ''
104
	 */
105
	public function setRole($value) {
106
		$this->role=$value;
107
		return $this;
108
	}
109
110
	public function isDivider() {
111
		return $this->class==="divider";
112
	}
113
114
	public function __toString() {
115
		return $this->compile();
116
	}
117
118
	/**
119
	 * /* Initialise l'objet à partir d'un tableau associatif
120
	 * array("identifier"=>"id","caption"=>"","class"=>"","href"=>"","role"=>"")
121
	 * @see BaseHtml::addProperties()
122
	 */
123
	public function fromArray($array) {
124
		return parent::fromArray($array);
125
	}
126
127
	public function run(JsUtils $js) {
128
		$this->_bsComponent=$js->bootstrap()->generic("#".$this->identifier);
129
		$this->addEventsOnRun($js);
130
		return $this->_bsComponent;
131
	}
132
133
	/**
134
	 * Js component creation when dropdownItem is in Navs/Pills
135
	 * @param JsUtils $js
136
	 */
137
	public function runNav(JsUtils $js) {
138
		$js->bootstrap()->tab("#".$this->identifier);
139
	}
140
141
	public function getHref() {
142
		return $this->href;
143
	}
144
145
	public function addBadge($caption, $leftSeparator="&nbsp;") {
146
		$badge=new HtmlBadge("badge-".$this->identifier);
147
		$badge->setContent($caption);
148
		$this->content.=$leftSeparator.$badge->compile();
149
		return $this;
150
	}
151
152
	public function addGlyph($glyphicon, $left=true, $separator="&nbsp;") {
153
		$glyph=new HtmlGlyphicon("glyph-".$this->identifier);
154
		$glyph->setGlyphicon($glyphicon);
155
		if ($left) {
156
			$this->content=$glyph->compile().$separator.$this->content;
157
		} else {
158
			$this->content.=$separator.$glyph->compile();
159
		}
160
		return $this;
161
	}
162
163
	public function getTarget() {
164
		return $this->target;
165
	}
166
167
	public function setTarget($target) {
168
		$this->target=$target;
169
		return $this;
170
	}
171
}