Completed
Push — master ( 8e32f8...32ad89 )
by Jean-Christophe
03:26
created

HtmlButton::setLoading()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
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
9
/**
10
 * Semantic Button component
11
 * @see http://semantic-ui.com/elements/button.html
12
 * @author jc
13
 * @version 1.001
14
 */
15
class HtmlButton extends HtmlSemDoubleElement {
16
	use LabeledIconTrait;
17
	/**
18
	 * Constructs an HTML Semantic button
19
	 * @param string $identifier HTML id
20
	 * @param string $value value of the Button
21
	 * @param string $cssStyle btn-default, btn-primary...
22
	 * @param string $onClick JS Code for click event
23
	 */
24 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...
25
		parent::__construct($identifier, "button","ui button");
26
		$this->content=$value;
27
		if (isset($cssStyle)) {
28
			$this->setStyle($cssStyle);
29
		}
30
		if (isset($onClick)) {
31
			$this->onClick($onClick);
32
		}
33
	}
34
35
	/**
36
	 * Set the button value
37
	 * @param string $value
38
	 * @return \Ajax\semantic\html\HtmlButton
39
	 */
40
	public function setValue($value) {
41
		$this->content=$value;
42
		return $this;
43
	}
44
45
	/**
46
	 * define the button style
47
	 * @param string|int $cssStyle
48
	 * @return \Ajax\semantic\html\HtmlButton default : ""
49
	 */
50
	public function setStyle($cssStyle) {
51
		return $this->addToProperty("class",$cssStyle);
52
	}
53
54
	public function setFocusable($value=true){
55
		if($value===true)
56
			$this->setProperty("tabindex", "0");
57
		else{
58
			$this->removeProperty("tabindex");
59
		}
60
		return $this;
61
	}
62
63
	public function setAnimated($content,$animation=""){
64
		$this->setTagName("div");
65
		$this->addToProperty("class", "animated ".$animation);
66
		$visible=new HtmlSemDoubleElement("visible-".$this->identifier,"div");
67
		$visible->setClass("visible content");
68
		$visible->setContent($this->content);
69
		$hidden=new HtmlSemDoubleElement("hidden-".$this->identifier,"div");
70
		$hidden->setClass("hidden content");
71
		$hidden->setContent($content);
72
		$this->content=array($visible,$hidden);
73
		return $hidden;
74
	}
75
76
	/**
77
	 * @param string|HtmlIcon $icon
78
	 * @return \Ajax\semantic\html\elements\HtmlButton
79
	 */
80
	public function asIcon($icon){
81
		$iconO=$icon;
82
		if(\is_string($icon)){
83
			$iconO=new HtmlIcon("icon-".$this->identifier, $icon);
84
		}
85
		$this->addToProperty("class", "icon");
86
		$this->content=$iconO;
87
		return $this;
88
	}
89
90
	/**
91
	 * Add and return a button label
92
	 * @param string $caption
93
	 * @param string $before
94
	 * @return \Ajax\semantic\html\elements\HtmlLabel
95
	 */
96
	public function addLabel($caption,$before=false){
97
		$this->tagName="div";
98
		$this->addToProperty("class", "labeled");
99
		$this->content=new HtmlButton("button-".$this->identifier,$this->content);
100
		$this->content->setTagName("div");
101
		$label=new HtmlLabel("label-".$this->identifier,$caption,"a");
102
		$label->setBasic();
103
		$this->addContent($label,$before);
0 ignored issues
show
Bug introduced by
It seems like $before defined by parameter $before on line 96 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...
104
		return $label;
105
	}
106
	/*
107
	 * (non-PHPdoc)
108
	 * @see \Ajax\common\html\BaseHtml::fromArray()
109
	 */
110
	public function fromArray($array) {
111
		$array=parent::fromArray($array);
112
		foreach ( $array as $key => $value ) {
113
			$this->setProperty($key, $value);
114
		}
115
		return $array;
116
	}
117
118
	/**
119
	 *  show it is currently the active user selection
120
	 * @return \Ajax\semantic\html\elements\HtmlButton
121
	 */
122
	public function setActive(){
123
		return $this->addToProperty("class", "active");
124
	}
125
126
	/**
127
	 * hint towards a positive consequence
128
	 * @return \Ajax\semantic\html\elements\HtmlButton
129
	 */
130
	public function setPositive(){
131
		return $this->addToProperty("class", "positive");
132
	}
133
134
	/**
135
	 * hint towards a negative consequence
136
	 * @return \Ajax\semantic\html\elements\HtmlButton
137
	 */
138
	public function setNegative(){
139
		return $this->addToProperty("class", "negative");
140
	}
141
142
	/**
143
	 * formatted to toggle on/off
144
	 * @return \Ajax\semantic\html\elements\HtmlButton
145
	 */
146
	public function setToggle(){
147
		return $this->addToProperty("class", "toggle");
148
	}
149
150
	/**
151
	 * @return \Ajax\semantic\html\elements\HtmlButton
152
	 */
153
	public function setCircular(){
154
		return $this->addToProperty("class", "circular");
155
	}
156
157
	/**
158
	 *  button is less pronounced
159
	 * @return \Ajax\semantic\html\elements\HtmlButton
160
	 */
161
	public function setBasic(){
162
		return $this->addToProperty("class", "basic");
163
	}
164
165
	public function setEmphasis($value){
166
		return $this->addToPropertyCtrl("class", $value, Emphasis::getConstants());
167
	}
168
169
	public function setLoading(){
170
		return $this->addToProperty("class", "loading");
171
	}
172
}