Completed
Push — master ( 7ebf5f...c286d3 )
by Jean-Christophe
02:51
created

HtmlButton::setToggle()   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 0
1
<?php
2
3
namespace Ajax\semantic\html\elements;
4
5
use Ajax\semantic\html\base\HtmlSemDoubleElement;
6
use Ajax\semantic\html\base\traits\LabeledIconTrait;
7
use Ajax\semantic\html\base\constants\Emphasis;
8
use Ajax\semantic\html\base\constants\Social;
9
10
/**
11
 * Semantic Button component
12
 * @see http://phpmv-ui.kobject.net/index/direct/main/31
13
 * @see http://semantic-ui.com/elements/button.html
14
 * @author jc
15
 * @version 1.001
16
 */
17
class HtmlButton extends HtmlSemDoubleElement {
18
	use LabeledIconTrait;
19
20
	/**
21
	 * Constructs an HTML Semantic button
22
	 * @param string $identifier HTML id
23
	 * @param string $value value of the Button
24
	 * @param string $cssStyle btn-default, btn-primary...
25
	 * @param string $onClick JS Code for click event
26
	 */
27
	public function __construct($identifier, $value=null, $cssStyle=null, $onClick=null) {
28
		parent::__construct($identifier, "button", "ui button");
29
		$this->content=$value;
30
		if (isset($cssStyle)) {
31
			$this->setStyle($cssStyle);
32
		}
33
		if (isset($onClick)) {
34
			$this->onClick($onClick);
35
		}
36
	}
37
38
	/**
39
	 * Set the button value
40
	 * @param string $value
41
	 * @return HtmlButton
42
	 */
43
	public function setValue($value) {
44
		$this->content=$value;
45
		return $this;
46
	}
47
48
	/**
49
	 * define the button style
50
	 * @param string|int $cssStyle
51
	 * @return HtmlButton default : ""
52
	 */
53
	public function setStyle($cssStyle) {
54
		return $this->addToProperty("class", $cssStyle);
55
	}
56
57
	public function setFocusable($value=true) {
58
		if ($value === true)
59
			$this->setProperty("tabindex", "0");
60
		else {
61
			$this->removeProperty("tabindex");
62
		}
63
		return $this;
64
	}
65
66
	public function setAnimated($content, $animation="") {
67
		$this->setTagName("div");
68
		$this->addToProperty("class", "animated " . $animation);
69
		$visible=new HtmlSemDoubleElement("visible-" . $this->identifier, "div");
70
		$visible->setClass("visible content");
71
		$visible->setContent($this->content);
72
		$hidden=new HtmlSemDoubleElement("hidden-" . $this->identifier, "div");
73
		$hidden->setClass("hidden content");
74
		$hidden->setContent($content);
75
		$this->content=array ($visible,$hidden );
76
		return $hidden;
77
	}
78
79
	/**
80
	 *
81
	 * @param string|HtmlIcon $icon
82
	 * @return HtmlButton
83
	 */
84
	public function asIcon($icon) {
85
		$iconO=$icon;
86
		if (\is_string($icon)) {
87
			$iconO=new HtmlIcon("icon-" . $this->identifier, $icon);
88
		}
89
		$this->addToProperty("class", "icon");
90
		$this->content=$iconO;
91
		return $this;
92
	}
93
94
	public function asSubmit() {
95
		$this->setProperty("type", "submit");
96
		return $this->setTagName("button");
97
	}
98
99
	/**
100
	 * Add and return a button label
101
	 * @param string $label
102
	 * @param boolean $before
103
	 * @param string $icon
104
	 * @return HtmlLabel
105
	 */
106
	public function addLabel($label, $before=false, $icon=NULL) {
107
		$this->tagName="div";$prefix="";
108
		if($before)
109
			$prefix="left ";
110
		$this->addToProperty("class", $prefix."labeled");
111
		$isIcon=(isset($this->content[0]) && $this->content[0] instanceof HtmlIcon);
112
		$this->content=new HtmlButton("button-" . $this->identifier, $this->content);
113
		if($isIcon){
114
			$this->content->addClass("icon");
115
		}
116
		$this->content->setTagName("div");
117
		$label=new HtmlLabel("label-" . $this->identifier, $label, $icon,"a");
118
		$label->setBasic();
119
		$this->addContent($label, $before);
120
		return $label;
121
	}
122
123
	/*
124
	 * (non-PHPdoc)
125
	 * @see \Ajax\common\html\BaseHtml::fromArray()
126
	 */
127
	public function fromArray($array) {
128
		$array=parent::fromArray($array);
129
		foreach ( $array as $key => $value ) {
130
			$this->setProperty($key, $value);
131
		}
132
		return $array;
133
	}
134
135
	/**
136
	 * hint towards a positive consequence
137
	 * @return HtmlButton
138
	 */
139
	public function setPositive() {
140
		return $this->addToProperty("class", "positive");
141
	}
142
143
	public function setColor($color){
144
		if(\is_array($this->content)){
145
			foreach ($this->content as $content){
146
				if($content instanceof HtmlButton)
147
					$content->setColor($color);
148
			}
149
		}
150
		else
151
			parent::setColor($color);
152
		return $this;
153
	}
154
155
	/**
156
	 * hint towards a negative consequence
157
	 * @return HtmlButton
158
	 */
159
	public function setNegative() {
160
		return $this->addToProperty("class", "negative");
161
	}
162
163
	/**
164
	 * formatted to toggle on/off
165
	 * @return HtmlButton
166
	 */
167
	public function setToggle() {
168
		$this->onCreate("$('#".$this->identifier."').state();");
169
		return $this->addToProperty("class", "toggle");
170
	}
171
172
	/**
173
	 *
174
	 * @return HtmlButton
175
	 */
176
	public function setCircular() {
177
		return $this->addToProperty("class", "circular");
178
	}
179
180
	/**
181
	 * button is less pronounced
182
	 * @return HtmlButton
183
	 */
184
	public function setBasic() {
185
		return $this->addToProperty("class", "basic");
186
	}
187
188
	public function setEmphasis($value) {
189
		return $this->addToPropertyCtrl("class", $value, Emphasis::getConstants());
190
	}
191
192
	public function setLoading() {
193
		return $this->addToProperty("class", "loading");
194
	}
195
196
	/**
197
	 * Returns a new social Button
198
	 * @param string $identifier
199
	 * @param string $social
200
	 * @param string $value
201
	 * @return HtmlButton
202
	 */
203
	public static function social($identifier, $social, $value=NULL) {
204
		if ($value === NULL)
205
			$value=\ucfirst($social);
206
		$return=new HtmlButton($identifier, $value);
207
		$return->addIcon($social);
208
		return $return->addToPropertyCtrl("class", $social, Social::getConstants());
209
	}
210
211
	/**
212
	 * Returns a new labeled Button
213
	 * @param string $identifier
214
	 * @param string $value
215
	 * @param string $icon
216
	 * @param boolean $before
217
	 * @return \Ajax\semantic\html\elements\HtmlButton
218
	 */
219
	public static function labeled($identifier, $value, $icon, $before=true) {
220
		$result=new HtmlButton($identifier, $value);
221
		$result->addIcon($icon, $before, true);
222
		return $result;
223
	}
224
225
	/**
226
	 * Returns a new icon Button
227
	 * @param string $identifier
228
	 * @param string $icon
229
	 * @return HtmlButton
230
	 */
231
	public static function icon($identifier, $icon) {
232
		$result=new HtmlButton($identifier);
233
		$result->asIcon($icon);
234
		return $result;
235
	}
236
237
	/**
238
	 * {@inheritDoc}
239
	 * @see HtmlSemDoubleElement::asLink()
240
	 */
241
	public function asLink($href=NULL,$target=NULL) {
242
		parent::asLink($href,$target);
243
		return $this;
244
	}
245
246
	/**
247
	 * Returns a button with a dropdown button
248
	 * @param string $identifier
249
	 * @param string $value
250
	 * @param array $items
251
	 * @param boolean $asCombo
252
	 * @param string $icon
253
	 * @return HtmlButtonGroups
254
	 */
255
	public static function dropdown($identifier,$value,$items=[],$asCombo=false,$icon=null){
256
		$result=new HtmlButtonGroups($identifier,[$value]);
257
		$result->addDropdown($items,$asCombo);
258
		if(isset($icon))
259
			$result->setIcon($icon);
0 ignored issues
show
Bug introduced by
The method setIcon() does not seem to exist on object<Ajax\semantic\htm...ments\HtmlButtonGroups>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
260
		return $result;
261
	}
262
}
263