Completed
Push — master ( b6875a...29801f )
by Jean-Christophe
03:06
created

Widget::jsDisabled()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Ajax\common;
4
5
use Ajax\common\html\HtmlDoubleElement;
6
use Ajax\semantic\html\elements\HtmlButton;
7
use Ajax\semantic\widgets\datatable\PositionInTable;
8
use Ajax\semantic\html\collections\menus\HtmlMenu;
9
use Ajax\semantic\widgets\base\FieldAsTrait;
10
use Ajax\semantic\html\elements\HtmlButtonGroups;
11
use Ajax\semantic\widgets\base\InstanceViewer;
12
use Ajax\semantic\html\modules\HtmlDropdown;
13
use Ajax\service\JArray;
14
use Ajax\service\Javascript;
15
16
abstract class Widget extends HtmlDoubleElement {
17
	use FieldAsTrait;
18
19
	/**
20
	 * @var string classname
21
	 */
22
	protected $_model;
23
	protected $_modelInstance;
24
	/**
25
	 * @var InstanceViewer
26
	 */
27
	protected $_instanceViewer;
28
	/**
29
	 * @var HtmlMenu
30
	 */
31
	protected $_toolbar;
32
	/**
33
	 * @var PositionInTable
34
	 */
35
	protected $_toolbarPosition;
36
37
	protected $_edition;
38
39
40
	public function __construct($identifier,$model,$modelInstance=NULL) {
41
		parent::__construct($identifier);
42
		$this->_template="%wrapContentBefore%%content%%wrapContentAfter%";
43
		$this->setModel($model);
44
		if(isset($modelInstance));
45
			$this->show($modelInstance);
46
	}
47
48
	protected function _init($instanceViewer,$contentKey,$content,$edition){
49
		$this->_instanceViewer=$instanceViewer;
50
		$this->content=[$contentKey=>$content];
51
		$this->_toolbarPosition=PositionInTable::BEFORETABLE;
52
		$this->_edition=$edition;
53
	}
54
55
	protected function _getFieldIdentifier($prefix){
56
		return $this->identifier."-{$prefix}-".$this->_instanceViewer->getIdentifier();
57
	}
58
59
	abstract protected  function _setToolbarPosition($table,$captions=NULL);
60
61
	public function show($modelInstance){
62
		$this->_modelInstance=$modelInstance;
63
	}
64
65
	public function getModel() {
66
		return $this->_model;
67
	}
68
69
	public function setModel($_model) {
70
		$this->_model=$_model;
71
		return $this;
72
	}
73
74
	public function getInstanceViewer() {
75
		return $this->_instanceViewer;
76
	}
77
78
	public function setInstanceViewer($_instanceViewer) {
79
		$this->_instanceViewer=$_instanceViewer;
80
		return $this;
81
	}
82
83
	abstract public function getHtmlComponent();
84
85
	public function setColor($color){
86
		return $this->getHtmlComponent()->setColor($color);
87
	}
88
89
90
	public function setCaptions($captions){
91
		$this->_instanceViewer->setCaptions($captions);
92
		return $this;
93
	}
94
95
	public function setFields($fields){
96
		$this->_instanceViewer->setVisibleProperties($fields);
97
		return $this;
98
	}
99
100
	public function addField($field){
101
		$this->_instanceViewer->addField($field);
102
		return $this;
103
	}
104
105
	public function insertField($index,$field){
106
		$this->_instanceViewer->insertField($index, $field);
107
		return $this;
108
	}
109
110
	public function insertInField($index,$field){
111
		$this->_instanceViewer->insertInField($index, $field);
112
		return $this;
113
	}
114
115
	public function setValueFunction($index,$callback){
116
		$this->_instanceViewer->setValueFunction($index, $callback);
117
		return $this;
118
	}
119
120
	public function setIdentifierFunction($callback){
121
		$this->_instanceViewer->setIdentifierFunction($callback);
122
		return $this;
123
	}
124
125
	/**
126
	 * @return \Ajax\semantic\html\collections\menus\HtmlMenu
127
	 */
128
	public function getToolbar(){
129
		if(isset($this->_toolbar)===false){
130
			$this->_toolbar=new HtmlMenu("toolbar-".$this->identifier);
131
			//$this->_toolbar->setSecondary();
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
132
		}
133
		return $this->_toolbar;
134
	}
135
136
	/**
137
	 * Adds a new element in toolbar
138
	 * @param mixed $element
139
	 * @param callable $callback function to call on $element
140
	 * @return \Ajax\common\html\HtmlDoubleElement
141
	 */
142
	public function addInToolbar($element,$callback=NULL){
143
		$tb=$this->getToolbar();
144
		if(isset($callback)){
145
			if(\is_callable($callback)){
146
				$callback($element);
147
			}
148
		}
149
		return $tb->addItem($element);
150
	}
151
152
	/**
153
	 * @param string $caption
154
	 * @param string $icon
155
	 * @param callable $callback function($element)
156
	 * @return \Ajax\common\html\HtmlDoubleElement
157
	 */
158
	public function addItemInToolbar($caption,$icon=NULL,$callback=NULL){
159
		$result=$this->addInToolbar($caption,$callback);
160
		if(isset($icon))
161
			$result->addIcon($icon);
162
		return $result;
163
	}
164
165
	/**
166
	 * @param array $items
167
	 * @param callable $callback function($element)
168
	 * @return \Ajax\common\Widget
169
	 */
170
	public function addItemsInToolbar(array $items,$callback=NULL){
171
		if(JArray::isAssociative($items)){
172
			foreach ($items as $icon=>$item){
173
				$this->addItemInToolbar($item,$icon,$callback);
174
			}
175
		}else{
176
			foreach ($items as $item){
177
				$this->addItemInToolbar($item,null,$callback);
178
			}
179
		}
180
		return $this;
181
	}
182
183
	/**
184
	 * @param string $value
185
	 * @param array $items
186
	 * @param callable $callback function($element)
187
	 * @return \Ajax\common\html\HtmlDoubleElement
188
	 */
189 View Code Duplication
	public function addDropdownInToolbar($value,$items,$callback=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...
190
		$dd=$value;
191
		if (\is_string($value)) {
192
			$dd=new HtmlDropdown("dropdown-". $this->identifier."-".$value, $value, $items);
193
		}
194
		return $this->addInToolbar($dd,$callback);
195
	}
196
197
	/**
198
	 * @param unknown $caption
199
	 * @param callable $callback function($element)
200
	 * @return \Ajax\common\html\HtmlDoubleElement
201
	 */
202
	public function addButtonInToolbar($caption,$callback=NULL){
203
		$bt=new HtmlButton("",$caption);
204
		return $this->addInToolbar($bt,$callback);
205
	}
206
207
	/**
208
	 * @param array $captions
209
	 * @param boolean $asIcon
210
	 * @param callable $callback function($element)
211
	 * @return \Ajax\common\html\HtmlDoubleElement
212
	 */
213
	public function addButtonsInToolbar(array $captions,$asIcon=false,$callback=NULL){
214
		$bts=new HtmlButtonGroups("",$captions,$asIcon);
215
		return $this->addInToolbar($bts,$callback);
216
	}
217
218
	/**
219
	 * @param string $caption
220
	 * @param string $icon
221
	 * @param boolean $before
222
	 * @param boolean $labeled
223
	 * @return \Ajax\common\html\HtmlDoubleElement
224
	 */
225
	public function addLabelledIconButtonInToolbar($caption,$icon,$before=true,$labeled=false){
226
		$bt=new HtmlButton("",$caption);
227
		$bt->addIcon($icon,$before,$labeled);
228
		return $this->addInToolbar($bt);
229
	}
230
231
	/**
232
	 * Defines a callback function to call for modifying captions
233
	 * function parameters are $captions: the captions to modify and $instance: the active model instance
234
	 * @param callable $captionCallback
235
	 * @return Widget
236
	 */
237
	public function setCaptionCallback($captionCallback) {
238
		$this->_instanceViewer->setCaptionCallback($captionCallback);
239
		return $this;
240
	}
241
242
	/**
243
	 * Makes the input fields editable
244
	 * @param boolean $_edition
245
	 * @return \Ajax\common\Widget
246
	 */
247
	public function setEdition($_edition=true) {
248
		$this->_edition=$_edition;
249
		return $this;
250
	}
251
252
	/**
253
	 * Defines the default function which displays fields value
254
	 * @param callable $defaultValueFunction
255
	 * @return \Ajax\common\Widget
256
	 */
257
	public function setDefaultValueFunction($defaultValueFunction){
258
		$this->_instanceViewer->setDefaultValueFunction($defaultValueFunction);
259
		return $this;
260
	}
261
262
	public function jsDisabled($disable=true){
263
		return "$('#".$this->identifier." .ui.input').toggleClass('disabled',".$disable.");";
264
	}
265
266
	/**
267
	 * @param unknown $caption
268
	 * @param callable $callback function($element)
269
	 * @return \Ajax\common\html\HtmlDoubleElement
270
	 */
271
	public function addEditButtonInToolbar($caption,$callback=NULL){
272
		$bt=new HtmlButton($this->identifier."-editBtn",$caption);
273
		$bt->setToggle();
274
		$bt->onClick($this->jsDisabled(Javascript::prep_value("$(event.target).hasClass('active')")));
0 ignored issues
show
Documentation introduced by
\Ajax\service\Javascript....hasClass(\'active\')') is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
275
		return $this->addInToolbar($bt,$callback);
276
	}
277
278
}