Completed
Push — master ( e9bdcb...b0560a )
by Jean-Christophe
04:52
created

HtmlButton::setLoading()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
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://semantic-ui.com/elements/button.html
13
 * @author jc
14
 * @version 1.001
15
 */
16
class HtmlButton extends HtmlSemDoubleElement {
17
	use LabeledIconTrait;
18
19
	/**
20
	 * Constructs an HTML Semantic button
21
	 * @param string $identifier HTML id
22
	 * @param string $value value of the Button
23
	 * @param string $cssStyle btn-default, btn-primary...
24
	 * @param string $onClick JS Code for click event
25
	 */
26 View Code Duplication
	public function __construct($identifier, $value="", $cssStyle=null, $onClick=null) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
27
		parent::__construct($identifier, "button", "ui button");
28
		$this->content=$value;
29
		if (isset($cssStyle)) {
30
			$this->setStyle($cssStyle);
31
		}
32
		if (isset($onClick)) {
33
			$this->onClick($onClick);
34
		}
35
	}
36
37
	/**
38
	 * Set the button value
39
	 * @param string $value
40
	 * @return \Ajax\semantic\html\HtmlButton
41
	 */
42
	public function setValue($value) {
43
		$this->content=$value;
44
		return $this;
45
	}
46
47
	/**
48
	 * define the button style
49
	 * @param string|int $cssStyle
50
	 * @return \Ajax\semantic\html\HtmlButton default : ""
51
	 */
52
	public function setStyle($cssStyle) {
53
		return $this->addToProperty("class", $cssStyle);
54
	}
55
56
	public function setFocusable($value=true) {
57
		if ($value === true)
58
			$this->setProperty("tabindex", "0");
59
		else {
60
			$this->removeProperty("tabindex");
61
		}
62
		return $this;
63
	}
64
65
	public function setAnimated($content, $animation="") {
66
		$this->setTagName("div");
67
		$this->addToProperty("class", "animated " . $animation);
68
		$visible=new HtmlSemDoubleElement("visible-" . $this->identifier, "div");
69
		$visible->setClass("visible content");
70
		$visible->setContent($this->content);
71
		$hidden=new HtmlSemDoubleElement("hidden-" . $this->identifier, "div");
72
		$hidden->setClass("hidden content");
73
		$hidden->setContent($content);
74
		$this->content=array ($visible,$hidden );
75
		return $hidden;
76
	}
77
78
	/**
79
	 *
80
	 * @param string|HtmlIcon $icon
81
	 * @return \Ajax\semantic\html\elements\HtmlButton
82
	 */
83
	public function asIcon($icon) {
84
		$iconO=$icon;
85
		if (\is_string($icon)) {
86
			$iconO=new HtmlIcon("icon-" . $this->identifier, $icon);
87
		}
88
		$this->addToProperty("class", "icon");
89
		$this->content=$iconO;
90
		return $this;
91
	}
92
93
	public function asSubmit() {
94
		$this->setProperty("type", "submit");
95
		return $this->setTagName("button");
96
	}
97
98
	/**
99
	 * Add and return a button label
100
	 * @param string $caption
101
	 * @param string $before
102
	 * @return \Ajax\semantic\html\elements\HtmlLabel
103
	 */
104
	public function addLabel($caption, $before=false) {
105
		$this->tagName="div";
106
		$this->addToProperty("class", "labeled");
107
		$this->content=new HtmlButton("button-" . $this->identifier, $this->content);
108
		$this->content->setTagName("div");
109
		$label=new HtmlLabel("label-" . $this->identifier, $caption, "a");
110
		$label->setBasic();
111
		$this->addContent($label, $before);
0 ignored issues
show
Bug introduced by
It seems like $before defined by parameter $before on line 104 can also be of type string; however, Ajax\common\html\HtmlDoubleElement::addContent() does only seem to accept boolean, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
112
		return $label;
113
	}
114
115
	/*
116
	 * (non-PHPdoc)
117
	 * @see \Ajax\common\html\BaseHtml::fromArray()
118
	 */
119
	public function fromArray($array) {
120
		$array=parent::fromArray($array);
121
		foreach ( $array as $key => $value ) {
122
			$this->setProperty($key, $value);
123
		}
124
		return $array;
125
	}
126
127
	/**
128
	 * show it is currently the active user selection
129
	 * @return \Ajax\semantic\html\elements\HtmlButton
130
	 */
131
	public function setActive() {
132
		return $this->addToProperty("class", "active");
133
	}
134
135
	/**
136
	 * hint towards a positive consequence
137
	 * @return \Ajax\semantic\html\elements\HtmlButton
138
	 */
139
	public function setPositive() {
140
		return $this->addToProperty("class", "positive");
141
	}
142
143
	/**
144
	 * hint towards a negative consequence
145
	 * @return \Ajax\semantic\html\elements\HtmlButton
146
	 */
147
	public function setNegative() {
148
		return $this->addToProperty("class", "negative");
149
	}
150
151
	/**
152
	 * formatted to toggle on/off
153
	 * @return \Ajax\semantic\html\elements\HtmlButton
154
	 */
155
	public function setToggle() {
156
		return $this->addToProperty("class", "toggle");
157
	}
158
159
	/**
160
	 *
161
	 * @return \Ajax\semantic\html\elements\HtmlButton
162
	 */
163
	public function setCircular() {
164
		return $this->addToProperty("class", "circular");
165
	}
166
167
	/**
168
	 * button is less pronounced
169
	 * @return \Ajax\semantic\html\elements\HtmlButton
170
	 */
171
	public function setBasic() {
172
		return $this->addToProperty("class", "basic");
173
	}
174
175
	public function setEmphasis($value) {
176
		return $this->addToPropertyCtrl("class", $value, Emphasis::getConstants());
177
	}
178
179
	public function setLoading() {
180
		return $this->addToProperty("class", "loading");
181
	}
182
183
	public static function social($identifier, $social, $value=NULL) {
184
		if ($value === NULL)
185
			$value=\ucfirst($social);
186
		$return=new HtmlButton($identifier, $value);
187
		$return->addIcon($social);
188
		return $return->addToPropertyCtrl("class", $social, Social::getConstants());
189
	}
190
191
	public static function labeled($identifier, $value, $icon, $before=true) {
192
		$result=new HtmlButton($identifier, $value);
193
		$result->addIcon($icon, $before, true);
194
		return $result;
195
	}
196
197
	public static function icon($identifier, $icon) {
198
		$result=new HtmlButton($identifier);
199
		$result->asIcon($icon);
200
		return $result;
201
	}
202
}