TCallbackClientScript::highlight()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
1
<?php
2
3
/**
4
 * TCallbackClientScript class file
5
 *
6
 * @author Wei Zhuo <weizhuo[at]gamil[dot]com>
7
 * @link https://github.com/pradosoft/prado
8
 * @license https://github.com/pradosoft/prado/blob/master/LICENSE
9
 */
10
11
namespace Prado\Web\UI\ActiveControls;
12
13
use Prado\Collections\TList;
14
use Prado\TPropertyValue;
15
use Prado\Web\UI\ISurroundable;
16
use Prado\Web\UI\TControl;
17
use Prado\Web\UI\THtmlWriter;
18
use Prado\Web\UI\WebControls\TCheckBox;
19
use Prado\Web\UI\WebControls\TCheckBoxList;
20
use Prado\Web\UI\WebControls\TListControl;
21
22
/**
23
 * TCallbackClientScript class.
24
 *
25
 * The TCallbackClientScript class provides corresponding methods that can be
26
 * executed on the client-side (i.e. the browser client that is viewing
27
 * the page) during a callback response.
28
 *
29
 * The available methods includes setting/clicking input elements, changing Css
30
 * styles, hiding/showing elements, and adding visual effects to elements on the
31
 * page. The client-side methods can be access through the CallbackClient
32
 * property available in TPage.
33
 *
34
 * For example, to hide "$myTextBox" element during callback response, do
35
 * ```php
36
 * $this->getPage()->getCallbackClient()->hide($myTextBox);
37
 * ```
38
 *
39
 * To call a specific jQuery method on an element, use the {@see jQuery} method:
40
 * ```php
41
 * // simple example: focus a textbox
42
 * $this->getCallbackClient()->jQuery($myTextBox, 'focus');
43
 *
44
 * // complex example: resize a textbox using an animation
45
 * 	$this->getCallbackClient()->jQuery($myTextBox, 'animate', array(
46
 *		array(	'width' => '+=100',
47
 *				'height' => '+=50'
48
 *			),
49
 *		array('duration' => 1000)
50
 *		));
51
 * ```
52
 *
53
 * To call a jQueryUI effect on an element, use the {@see juiEffect} method:
54
 * ```php
55
 * // simple example: focus a textbox
56
 * $this->getCallbackClient()->juiEffect($myTextBox, 'highlight');
57
 * ```
58
 *
59
 * In order to use the jQueryUI effects, the jqueryui script must be registered:
60
 * ```php
61
 * $this->getPage()->getClientScript()->registerPradoScript('jqueryui');
62
 * ```
63
 *
64
 * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
65
 * @author Fabio Bas <ctrlaltca[at]gmail[dot]com>
66
 * @since 3.1
67
 */
68
class TCallbackClientScript extends \Prado\TApplicationComponent
69
{
70
	/**
71
	 * @var TList list of client functions to execute.
72
	 */
73
	private $_actions;
74
75
	/**
76
	 * Constructor.
77
	 */
78
	public function __construct()
79
	{
80
		$this->_actions = new TList();
81
		parent::__construct();
82
	}
83
84
	/**
85
	 * @return array list of client function to be executed during callback
86
	 * response.
87
	 */
88
	public function getClientFunctionsToExecute()
89
	{
90
		return $this->_actions->toArray();
91
	}
92
93
	/**
94
	 * Executes a client-side statement.
95
	 * @param string $function javascript function name
96
	 * @param array $params list of arguments for the function
97
	 */
98
	public function callClientFunction($function, $params = [])
99
	{
100
		if (!is_array($params)) {
0 ignored issues
show
introduced by
The condition is_array($params) is always true.
Loading history...
101
			$params = [$params];
102
		}
103
104
		if (count($params) > 0) {
105
			if ($params[0] instanceof ISurroundable) {
106
				$params[0] = $params[0]->getSurroundingTagID();
107
			} elseif ($params[0] instanceof TControl) {
108
				$params[0] = $params[0]->getClientID();
109
			}
110
		}
111
		$this->_actions->add([$function => $params]);
112
	}
113
114
	/**
115
	 * Executes a jQuery client-side method over an element.
116
	 * @param string $element control or element id
117
	 * @param string $method jQuery method name
118
	 * @param array $params list of arguments for the function
119
	 */
120
	public function jQuery($element, $method, $params = [])
121
	{
122
		if ($element instanceof ISurroundable) {
0 ignored issues
show
introduced by
$element is never a sub-type of Prado\Web\UI\ISurroundable.
Loading history...
123
			$element = $element->getSurroundingTagID();
124
		} elseif ($element instanceof TControl) {
0 ignored issues
show
introduced by
$element is never a sub-type of Prado\Web\UI\TControl.
Loading history...
125
			$element = $element->getClientID();
126
		}
127
128
		if (!is_array($params)) {
0 ignored issues
show
introduced by
The condition is_array($params) is always true.
Loading history...
129
			$params = [$params];
130
		}
131
132
		$this->_actions->add(['Prado.Element.j' => [$element, $method, $params]]);
133
	}
134
135
	/**
136
	 * Client script to set the value of a particular input element.
137
	 * @param \Prado\Web\UI\TControl $input control element to set the new value
138
	 * @param string $text new value
139
	 */
140
	public function setValue($input, $text)
141
	{
142
		$this->jQuery($input, 'val', $text);
0 ignored issues
show
Bug introduced by
$text of type string is incompatible with the type array expected by parameter $params of Prado\Web\UI\ActiveContr...kClientScript::jQuery(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

142
		$this->jQuery($input, 'val', /** @scrutinizer ignore-type */ $text);
Loading history...
Bug introduced by
$input of type Prado\Web\UI\TControl is incompatible with the type string expected by parameter $element of Prado\Web\UI\ActiveContr...kClientScript::jQuery(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

142
		$this->jQuery(/** @scrutinizer ignore-type */ $input, 'val', $text);
Loading history...
143
	}
144
145
	/**
146
	 * Client script to select/clear/check a drop down list, check box list,
147
	 * or radio button list.
148
	 * The second parameter determines the selection method. Valid methods are
149
	 *  - <b>Value</b>, select or check by value
150
	 *  - <b>Values</b>, select or check by a list of values
151
	 *  - <b>Index</b>, select or check by index (zero based index)
152
	 *  - <b>Indices</b>, select or check by a list of index (zero based index)
153
	 *  - <b>Clear</b>, clears or selections or checks in the list
154
	 *  - <b>All</b>, select all
155
	 *  - <b>Invert</b>, invert the selection.
156
	 * @param \Prado\Web\UI\WebControls\TListControl $control list control
157
	 * @param string $method selection method
158
	 * @param null|int|string $value the value or index to select/check.
159
	 * @param null|string $type selection control type, either 'check' or 'select'
160
	 */
161
	public function select($control, $method = 'Value', $value = null, $type = null)
162
	{
163
		$method = TPropertyValue::ensureEnum(
164
			$method,
165
			'Value',
166
			'Index',
167
			'Clear',
168
			'Indices',
169
			'Values',
170
			'All',
171
			'Invert'
172
		);
173
		$type = ($type === null) ? $this->getSelectionControlType($control) : $type;
174
		$total = $this->getSelectionControlIsListType($control) ? $control->getItemCount() : 1;
175
176
		// pass the ID to avoid getting the surrounding elements (ISurroundable)
177
		if ($control instanceof TControl) {
0 ignored issues
show
introduced by
$control is always a sub-type of Prado\Web\UI\TControl.
Loading history...
178
			$control = $control->getClientID();
179
		}
180
181
		$this->callClientFunction(
182
			'Prado.Element.select',
183
			[$control, $type . $method, $value, $total]
184
		);
185
	}
186
187
	private function getSelectionControlType($control)
188
	{
189
		if (is_string($control)) {
190
			return 'check';
191
		}
192
		if ($control instanceof TCheckBoxList) {
193
			return 'check';
194
		}
195
		if ($control instanceof TCheckBox) {
196
			return 'check';
197
		}
198
		return 'select';
199
	}
200
201
	private function getSelectionControlIsListType($control)
202
	{
203
		return $control instanceof TListControl;
204
	}
205
206
	/**
207
	 * Client script to click on an element. <b>This client-side function is unpredictable.</b>
208
	 *
209
	 * @param \Prado\Web\UI\TControl $control control element or element id
210
	 */
211
	public function click($control)
212
	{
213
		$this->jQuery($control, 'trigger', 'click');
0 ignored issues
show
Bug introduced by
'click' of type string is incompatible with the type array expected by parameter $params of Prado\Web\UI\ActiveContr...kClientScript::jQuery(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

213
		$this->jQuery($control, 'trigger', /** @scrutinizer ignore-type */ 'click');
Loading history...
Bug introduced by
$control of type Prado\Web\UI\TControl is incompatible with the type string expected by parameter $element of Prado\Web\UI\ActiveContr...kClientScript::jQuery(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

213
		$this->jQuery(/** @scrutinizer ignore-type */ $control, 'trigger', 'click');
Loading history...
214
	}
215
216
	/**
217
	 * Client script to check or uncheck a checkbox or radio button.
218
	 * @param \Prado\Web\UI\TControl $checkbox control element or element id
219
	 * @param bool $checked check or uncheck the checkbox or radio button.
220
	 */
221
	public function check($checkbox, $checked = true)
222
	{
223
		$this->select($checkbox, "Value", $checked);
224
	}
225
226
	/**
227
	 * Raise the client side event (given by $eventName) on a particular element.
228
	 * @param \Prado\Web\UI\TControl $control control element or element id
229
	 * @param string $eventName Event name, e.g. "click"
230
	 */
231
	public function raiseClientEvent($control, $eventName)
232
	{
233
		$this->jQuery($control, 'trigger', $eventName);
0 ignored issues
show
Bug introduced by
$control of type Prado\Web\UI\TControl is incompatible with the type string expected by parameter $element of Prado\Web\UI\ActiveContr...kClientScript::jQuery(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

233
		$this->jQuery(/** @scrutinizer ignore-type */ $control, 'trigger', $eventName);
Loading history...
Bug introduced by
$eventName of type string is incompatible with the type array expected by parameter $params of Prado\Web\UI\ActiveContr...kClientScript::jQuery(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

233
		$this->jQuery($control, 'trigger', /** @scrutinizer ignore-type */ $eventName);
Loading history...
234
	}
235
236
	/**
237
	 * Sets the attribute of a particular control.
238
	 * @param \Prado\Web\UI\TControl $control control element or element id
239
	 * @param string $name attribute name
240
	 * @param string $value attribute value
241
	 */
242
	public function setAttribute($control, $name, $value)
243
	{
244
		// Attributes should be applied on Surrounding tag, except for 'disabled' attribute
245
		if ($control instanceof ISurroundable && strtolower($name) !== 'disabled') {
246
			$control = $control->getSurroundingTagID();
0 ignored issues
show
Bug introduced by
The method getSurroundingTagID() does not exist on Prado\Web\UI\TControl. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

246
			/** @scrutinizer ignore-call */ 
247
   $control = $control->getSurroundingTagID();
Loading history...
247
		}
248
		$this->callClientFunction('Prado.Element.setAttribute', [$control, $name, $value]);
249
	}
250
251
	/**
252
	 * Sets the options of a select input element.
253
	 * @param \Prado\Web\UI\TControl $control control element or element id
254
	 * @param array|TListControl $items a list of new options
255
	 */
256
	public function setListItems($control, $items)
257
	{
258
		$options = [];
259
		if ($control instanceof TListControl) {
260
			$promptText = $control->getPromptText();
261
			$promptValue = $control->getPromptValue();
262
263
			if ($promptValue === '') {
264
				$promptValue = $promptText;
265
			}
266
267
			if ($promptValue !== '') {
268
				$options[] = [$promptText, $promptValue];
269
			}
270
		}
271
272
		foreach ($items as $item) {
273
			if ($item->getHasAttributes()) {
274
				$options[] = [$item->getText(), $item->getValue(), $item->getAttributes()->itemAt('Group')];
275
			} else {
276
				$options[] = [$item->getText(), $item->getValue()];
277
			}
278
		}
279
		$this->callClientFunction('Prado.Element.setOptions', [$control, $options]);
280
	}
281
282
	/**
283
	 * Shows an element by changing its CSS display style as empty.
284
	 * @param \Prado\Web\UI\TControl $element control element or element id
285
	 */
286
	public function show($element)
287
	{
288
		$this->jQuery($element, 'show');
0 ignored issues
show
Bug introduced by
$element of type Prado\Web\UI\TControl is incompatible with the type string expected by parameter $element of Prado\Web\UI\ActiveContr...kClientScript::jQuery(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

288
		$this->jQuery(/** @scrutinizer ignore-type */ $element, 'show');
Loading history...
289
	}
290
291
	/**
292
	 * Hides an element by changing its CSS display style to "none".
293
	 * @param \Prado\Web\UI\TControl $element control element or element id
294
	 */
295
	public function hide($element)
296
	{
297
		$this->jQuery($element, 'hide');
0 ignored issues
show
Bug introduced by
$element of type Prado\Web\UI\TControl is incompatible with the type string expected by parameter $element of Prado\Web\UI\ActiveContr...kClientScript::jQuery(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

297
		$this->jQuery(/** @scrutinizer ignore-type */ $element, 'hide');
Loading history...
298
	}
299
300
	/**
301
	 * Toggles the visibility of the element.
302
	 * @param \Prado\Web\UI\TControl $element control element or element id
303
	 * @param null|string $effect visual effect, such as, 'fade' or 'slide'.
304
	 * @param array $options additional options.
305
	 */
306
	public function toggle($element, $effect = null, $options = [])
307
	{
308
		switch (strtolower($effect)) {
0 ignored issues
show
Bug introduced by
It seems like $effect can also be of type null; however, parameter $string of strtolower() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

308
		switch (strtolower(/** @scrutinizer ignore-type */ $effect)) {
Loading history...
309
			case 'fade':
310
				$method = 'fadeToggle';
311
				break;
312
			case 'slide':
313
				$method = 'slideToggle';
314
				break;
315
			default:
316
				$method = 'toggle';
317
				// avoid fancy effect by default
318
				if (!array_key_exists('duration', $options)) {
319
					$options['duration'] = 0;
320
				}
321
				break;
322
		}
323
		$this->jQuery($element, $method, $options);
0 ignored issues
show
Bug introduced by
$element of type Prado\Web\UI\TControl is incompatible with the type string expected by parameter $element of Prado\Web\UI\ActiveContr...kClientScript::jQuery(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

323
		$this->jQuery(/** @scrutinizer ignore-type */ $element, $method, $options);
Loading history...
324
	}
325
326
	/**
327
	 * Removes an element from the HTML page.
328
	 * @param \Prado\Web\UI\TControl $element control element or element id
329
	 */
330
	public function remove($element)
331
	{
332
		$this->jQuery($element, 'remove');
0 ignored issues
show
Bug introduced by
$element of type Prado\Web\UI\TControl is incompatible with the type string expected by parameter $element of Prado\Web\UI\ActiveContr...kClientScript::jQuery(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

332
		$this->jQuery(/** @scrutinizer ignore-type */ $element, 'remove');
Loading history...
333
	}
334
335
	/**
336
	 * Update the element's innerHTML with new content.
337
	 * @param \Prado\Web\UI\TControl $element control element or element id
338
	 * @param \Prado\Web\UI\TControl $content new HTML content, if content is of a TControl, the
339
	 * controls render method is called.
340
	 */
341
	public function update($element, $content)
342
	{
343
		$this->jQuery($element, 'html', $content);
0 ignored issues
show
Bug introduced by
$content of type Prado\Web\UI\TControl is incompatible with the type array expected by parameter $params of Prado\Web\UI\ActiveContr...kClientScript::jQuery(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

343
		$this->jQuery($element, 'html', /** @scrutinizer ignore-type */ $content);
Loading history...
Bug introduced by
$element of type Prado\Web\UI\TControl is incompatible with the type string expected by parameter $element of Prado\Web\UI\ActiveContr...kClientScript::jQuery(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

343
		$this->jQuery(/** @scrutinizer ignore-type */ $element, 'html', $content);
Loading history...
344
	}
345
346
	/**
347
	 * Add a Css class name to the element.
348
	 * @param \Prado\Web\UI\TControl $element control element or element id
349
	 * @param string $cssClass CssClass name to add.
350
	 */
351
	public function addCssClass($element, $cssClass)
352
	{
353
		$this->jQuery($element, 'addClass', $cssClass);
0 ignored issues
show
Bug introduced by
$element of type Prado\Web\UI\TControl is incompatible with the type string expected by parameter $element of Prado\Web\UI\ActiveContr...kClientScript::jQuery(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

353
		$this->jQuery(/** @scrutinizer ignore-type */ $element, 'addClass', $cssClass);
Loading history...
Bug introduced by
$cssClass of type string is incompatible with the type array expected by parameter $params of Prado\Web\UI\ActiveContr...kClientScript::jQuery(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

353
		$this->jQuery($element, 'addClass', /** @scrutinizer ignore-type */ $cssClass);
Loading history...
354
	}
355
356
	/**
357
	 * Remove a Css class name from the element.
358
	 * @param \Prado\Web\UI\TControl $element control element or element id
359
	 * @param string $cssClass CssClass name to remove.
360
	 */
361
	public function removeCssClass($element, $cssClass)
362
	{
363
		$this->jQuery($element, 'removeClass', $cssClass);
0 ignored issues
show
Bug introduced by
$element of type Prado\Web\UI\TControl is incompatible with the type string expected by parameter $element of Prado\Web\UI\ActiveContr...kClientScript::jQuery(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

363
		$this->jQuery(/** @scrutinizer ignore-type */ $element, 'removeClass', $cssClass);
Loading history...
Bug introduced by
$cssClass of type string is incompatible with the type array expected by parameter $params of Prado\Web\UI\ActiveContr...kClientScript::jQuery(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

363
		$this->jQuery($element, 'removeClass', /** @scrutinizer ignore-type */ $cssClass);
Loading history...
364
	}
365
366
	/**
367
	 * Scroll the top of the browser viewing area to the location of the
368
	 * element.
369
	 *
370
	 * @param \Prado\Web\UI\TControl $element control element or element id
371
	 * @param array $options additional options: 'duration' in ms, 'offset' from the top in pixels
372
	 */
373
	public function scrollTo($element, $options = [])
374
	{
375
		$this->callClientFunction('Prado.Element.scrollTo', [$element, $options]);
376
	}
377
378
	/**
379
	 * Focus on a particular element.
380
	 * @param \Prado\Web\UI\TControl $element control element or element id.
381
	 */
382
	public function focus($element)
383
	{
384
		$this->jQuery($element, 'trigger', 'focus');
0 ignored issues
show
Bug introduced by
'focus' of type string is incompatible with the type array expected by parameter $params of Prado\Web\UI\ActiveContr...kClientScript::jQuery(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

384
		$this->jQuery($element, 'trigger', /** @scrutinizer ignore-type */ 'focus');
Loading history...
Bug introduced by
$element of type Prado\Web\UI\TControl is incompatible with the type string expected by parameter $element of Prado\Web\UI\ActiveContr...kClientScript::jQuery(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

384
		$this->jQuery(/** @scrutinizer ignore-type */ $element, 'trigger', 'focus');
Loading history...
385
	}
386
387
	/**
388
	 * Sets the style of element. The style must be a key-value array where the
389
	 * key is the style property and the value is the style value.
390
	 * @param \Prado\Web\UI\TControl $element control element or element id
391
	 * @param array $styles list of key-value pairs as style property and style value.
392
	 */
393
	public function setStyle($element, $styles)
394
	{
395
		$this->jQuery($element, 'css', [$styles]);
0 ignored issues
show
Bug introduced by
$element of type Prado\Web\UI\TControl is incompatible with the type string expected by parameter $element of Prado\Web\UI\ActiveContr...kClientScript::jQuery(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

395
		$this->jQuery(/** @scrutinizer ignore-type */ $element, 'css', [$styles]);
Loading history...
396
	}
397
398
	/**
399
	 * Append a HTML fragement to the element.
400
	 * @param \Prado\Web\UI\TControl $element control element or element id
401
	 * @param string $content HTML fragement or the control to be rendered
402
	 */
403
	public function appendContent($element, $content)
404
	{
405
		$this->jQuery($element, 'append', $content);
0 ignored issues
show
Bug introduced by
$element of type Prado\Web\UI\TControl is incompatible with the type string expected by parameter $element of Prado\Web\UI\ActiveContr...kClientScript::jQuery(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

405
		$this->jQuery(/** @scrutinizer ignore-type */ $element, 'append', $content);
Loading history...
Bug introduced by
$content of type string is incompatible with the type array expected by parameter $params of Prado\Web\UI\ActiveContr...kClientScript::jQuery(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

405
		$this->jQuery($element, 'append', /** @scrutinizer ignore-type */ $content);
Loading history...
406
	}
407
408
	/**
409
	 * Prepend a HTML fragement to the element.
410
	 * @param \Prado\Web\UI\TControl $element control element or element id
411
	 * @param string $content HTML fragement or the control to be rendered
412
	 */
413
	public function prependContent($element, $content)
414
	{
415
		$this->jQuery($element, 'prepend', $content);
0 ignored issues
show
Bug introduced by
$element of type Prado\Web\UI\TControl is incompatible with the type string expected by parameter $element of Prado\Web\UI\ActiveContr...kClientScript::jQuery(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

415
		$this->jQuery(/** @scrutinizer ignore-type */ $element, 'prepend', $content);
Loading history...
Bug introduced by
$content of type string is incompatible with the type array expected by parameter $params of Prado\Web\UI\ActiveContr...kClientScript::jQuery(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

415
		$this->jQuery($element, 'prepend', /** @scrutinizer ignore-type */ $content);
Loading history...
416
	}
417
418
	/**
419
	 * Insert a HTML fragement after the element.
420
	 * @param \Prado\Web\UI\TControl $element control element or element id
421
	 * @param string $content HTML fragement or the control to be rendered
422
	 */
423
	public function insertContentAfter($element, $content)
424
	{
425
		$this->jQuery($element, 'after', $content);
0 ignored issues
show
Bug introduced by
$content of type string is incompatible with the type array expected by parameter $params of Prado\Web\UI\ActiveContr...kClientScript::jQuery(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

425
		$this->jQuery($element, 'after', /** @scrutinizer ignore-type */ $content);
Loading history...
Bug introduced by
$element of type Prado\Web\UI\TControl is incompatible with the type string expected by parameter $element of Prado\Web\UI\ActiveContr...kClientScript::jQuery(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

425
		$this->jQuery(/** @scrutinizer ignore-type */ $element, 'after', $content);
Loading history...
426
	}
427
428
	/**
429
	 * Insert a HTML fragement in before the element.
430
	 * @param \Prado\Web\UI\TControl $element control element or element id
431
	 * @param string $content HTML fragement or the control to be rendered
432
	 */
433
	public function insertContentBefore($element, $content)
434
	{
435
		$this->jQuery($element, 'before', $content);
0 ignored issues
show
Bug introduced by
$content of type string is incompatible with the type array expected by parameter $params of Prado\Web\UI\ActiveContr...kClientScript::jQuery(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

435
		$this->jQuery($element, 'before', /** @scrutinizer ignore-type */ $content);
Loading history...
Bug introduced by
$element of type Prado\Web\UI\TControl is incompatible with the type string expected by parameter $element of Prado\Web\UI\ActiveContr...kClientScript::jQuery(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

435
		$this->jQuery(/** @scrutinizer ignore-type */ $element, 'before', $content);
Loading history...
436
	}
437
438
	/**
439
	 * Replace the content of an element with new content. The new content can
440
	 * be a string or a TControl component. If the <tt>content</tt> parameter is
441
	 * a TControl component, its rendered method will be called and its contents
442
	 * will be used for replacement.
443
	 * @param \Prado\Web\UI\TControl $element control element or HTML element id.
444
	 * @param string $content HTML fragement or the control to be rendered.
445
	 * @param bool $self whether to fully replace the element or just its inner content.
446
	 * @see insertAbout
447
	 * @see insertBelow
448
	 * @see insertBefore
449
	 * @see insertAfter
450
	 */
451
	protected function replace($element, $content, $self)
452
	{
453
		if ($content instanceof TControl) {
0 ignored issues
show
introduced by
$content is never a sub-type of Prado\Web\UI\TControl.
Loading history...
454
			$boundary = $this->getRenderedContentBoundary($content);
455
			$content = null;
456
		} elseif ($content instanceof THtmlWriter) {
0 ignored issues
show
introduced by
$content is never a sub-type of Prado\Web\UI\THtmlWriter.
Loading history...
457
			$boundary = $this->getResponseContentBoundary($content);
458
			$content = null;
459
		} else {
460
			$boundary = null;
461
		}
462
463
		$this->callClientFunction('Prado.Element.replace', [$element, $content, $boundary, $self]);
464
	}
465
466
	/**
467
	 * Replace the content of an element with new content contained in writer.
468
	 * @param \Prado\Web\UI\TControl $element control element or HTML element id.
469
	 * @param string $content HTML fragement or the control to be rendered.
470
	 * @param bool $self whether to fully replace the element or just its inner content, defaults to true.
471
	 */
472
	public function replaceContent($element, $content, $self = true)
473
	{
474
		$this->replace($element, $content, $self);
475
	}
476
477
478
	/**
479
	 * Evaluate a block of javascript enclosed in a boundary.
480
	 * @param \Prado\Web\UI\THtmlWriter|string $writer writer for the content.
481
	 */
482
	public function evaluateScript($writer)
483
	{
484
		if ($writer instanceof THtmlWriter) {
485
			$boundary = $this->getResponseContentBoundary($writer);
486
			$content = null;
487
		} else {
488
			$boundary = null;
489
			$content = $writer;
490
		}
491
492
		$this->callClientFunction('Prado.Element.evaluateScript', [$content, $boundary]);
493
	}
494
495
	/**
496
	 * Appends a block of inline javascript enclosed in a boundary.
497
	 * Similar to to evaluateScript(), but functions declared in the
498
	 * inline block will be available to page elements.
499
	 * @param \Prado\Web\UI\THtmlWriter $content writer for the content.
500
	 */
501
	public function appendScriptBlock($content)
502
	{
503
		if ($content instanceof TControl) {
0 ignored issues
show
introduced by
$content is never a sub-type of Prado\Web\UI\TControl.
Loading history...
504
			$boundary = $this->getRenderedContentBoundary($content);
505
		} elseif ($content instanceof THtmlWriter) {
0 ignored issues
show
introduced by
$content is always a sub-type of Prado\Web\UI\THtmlWriter.
Loading history...
506
			$boundary = $this->getResponseContentBoundary($content);
507
		} else {
508
			$boundary = null;
509
		}
510
511
		$this->callClientFunction('Prado.Element.appendScriptBlock', [$boundary]);
512
	}
513
514
	/**
515
	 * Renders the control and return the content boundary from
516
	 * TCallbackResponseWriter. This method should only be used by framework
517
	 * component developers. The render() method is defered to be called in the
518
	 * TActivePageAdapter class.
519
	 * @param \Prado\Web\UI\TControl $control control to be rendered on callback response.
520
	 * @return string the boundary for which the rendered content is wrapped.
521
	 */
522
	private function getRenderedContentBoundary($control)
523
	{
524
		$writer = $this->getResponse()->createHtmlWriter();
525
		$adapter = $control->getPage()->getAdapter();
526
		$adapter->registerControlToRender($control, $writer);
527
		return $writer->getWriter()->getBoundary();
528
	}
529
530
	/**
531
	 * @param \Prado\Web\UI\THtmlWriter $html the writer responsible for rendering html content.
532
	 * @return string content boundary.
533
	 */
534
	private function getResponseContentBoundary($html)
535
	{
536
		if ($html instanceof THtmlWriter) {
0 ignored issues
show
introduced by
$html is always a sub-type of Prado\Web\UI\THtmlWriter.
Loading history...
537
			if ($html->getWriter() instanceof TCallbackResponseWriter) {
538
				return $html->getWriter()->getBoundary();
539
			}
540
		}
541
		return null;
542
	}
543
544
	/* VISUAL EFFECTS */
545
546
	/**
547
	 * Add a visual effect the element.
548
	 * @param string $type visual effect function name.
549
	 * @param \Prado\Web\UI\TControl $element control element or element id
550
	 * @param array $options visual effect key-value pair options.
551
	 */
552
	public function visualEffect($type, $element, $options = [])
553
	{
554
		$this->jQuery($element, $type, $options);
0 ignored issues
show
Bug introduced by
$element of type Prado\Web\UI\TControl is incompatible with the type string expected by parameter $element of Prado\Web\UI\ActiveContr...kClientScript::jQuery(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

554
		$this->jQuery(/** @scrutinizer ignore-type */ $element, $type, $options);
Loading history...
555
	}
556
557
	/* BASIC EFFECTS (JQUERY CORE) */
558
559
	/**
560
	 * Visual Effect: Gradually make the element appear.
561
	 * This effect doesn't need jQueryUI.
562
	 * @param \Prado\Web\UI\TControl $element control element or element id
563
	 * @param array $options visual effect key-value pair options.
564
	 */
565
	public function fadeIn($element, $options = [])
566
	{
567
		$this->visualEffect('fadeIn', $element, $options);
568
	}
569
570
	/**
571
	 * Visual Effect: Gradually fade the element.
572
	 * This effect doesn't need jQueryUI.
573
	 * @param \Prado\Web\UI\TControl $element control element or element id
574
	 * @param array $options visual effect key-value pair options.
575
	 */
576
	public function fadeOut($element, $options = [])
577
	{
578
		$this->visualEffect('fadeOut', $element, $options);
579
	}
580
581
	/**
582
	 * Set the opacity on a html element or control.
583
	 * This effect doesn't need jQueryUI.
584
	 * @param \Prado\Web\UI\TControl $element control element or element id
585
	 * @param float $value opacity value between 1 and 0
586
	 * @param int $duration animation duration in ms
587
	 */
588
	public function fadeTo($element, $value, $duration = 500)
589
	{
590
		$value = TPropertyValue::ensureFloat($value);
591
		$this->visualEffect('fadeTo', $element, [$duration, $value]);
592
	}
593
594
	/**
595
	 * Visual Effect: Slide down.
596
	 * This effect doesn't need jQueryUI.
597
	 * @param \Prado\Web\UI\TControl $element control element or element id
598
	 * @param array $options visual effect key-value pair options.
599
	 */
600
	public function slideDown($element, $options = [])
601
	{
602
		$this->visualEffect('slideDown', $element, $options);
603
	}
604
605
	/**
606
	 * Visual Effect: Slide up.
607
	 * This effect doesn't need jQueryUI.
608
	 * @param \Prado\Web\UI\TControl $element control element or element id
609
	 * @param array $options visual effect key-value pair options.
610
	 */
611
	public function slideUp($element, $options = [])
612
	{
613
		$this->visualEffect('slideUp', $element, $options);
614
	}
615
616
	/* OLD METHODS, DEPRECATED, BACKWARDS-COMPATIBILITY */
617
618
	/**
619
	 * Alias of fadeIn()
620
	 * @deprecated since 3.4
621
	 * @param \Prado\Web\UI\TControl $element control element or element id
622
	 * @param array $options visual effect key-value pair options.
623
	 */
624
	public function appear($element, $options = [])
625
	{
626
		$this->fadeIn($element, $options);
627
	}
628
629
	/**
630
	 * Alias of fadeOut()
631
	 * @deprecated since 3.4
632
	 * @param \Prado\Web\UI\TControl $element control element or element id
633
	 * @param array $options visual effect key-value pair options.
634
	 */
635
	public function fade($element, $options = [])
636
	{
637
		$this->fadeOut($element, $options);
638
	}
639
640
	/**
641
	 * Alias of fadeTo()
642
	 * @deprecated since 3.4
643
	 * @param \Prado\Web\UI\TControl $element control element or element id
644
	 * @param float $value opacity value between 1 and 0
645
	 */
646
	public function setOpacity($element, $value)
647
	{
648
		$this->fadeTo($element, $value);
649
	}
650
651
	/* JQUERY UI EFFECTS */
652
653
	/**
654
	 * Add a jQuery-ui effect the element.
655
	 * This method needs jQueryUI.
656
	 * @param \Prado\Web\UI\TControl $element control element or element id
657
	 * @param string $effect visual effect function name.
658
	 * @param array $options effect options.
659
	 */
660
	public function juiEffect($element, $effect, $options = [])
661
	{
662
		$options['effect'] = $effect;
663
		$this->jQuery($element, 'effect', [$options]);
0 ignored issues
show
Bug introduced by
$element of type Prado\Web\UI\TControl is incompatible with the type string expected by parameter $element of Prado\Web\UI\ActiveContr...kClientScript::jQuery(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

663
		$this->jQuery(/** @scrutinizer ignore-type */ $element, 'effect', [$options]);
Loading history...
664
	}
665
666
	/**
667
	 * Visual Effect: Blind.
668
	 * This effect needs jQueryUI.
669
	 * @param \Prado\Web\UI\TControl $element control element or element id
670
	 * @param array $options visual effect key-value pair options.
671
	 */
672
	public function blind($element, $options = [])
673
	{
674
		$this->juiEffect($element, 'blind', $options);
675
	}
676
677
	/**
678
	 * Visual Effect: Drop out.
679
	 * This effect needs jQueryUI.
680
	 * @param \Prado\Web\UI\TControl $element control element or element id
681
	 * @param array $options visual effect key-value pair options.
682
	 */
683
	public function drop($element, $options = [])
684
	{
685
		$this->juiEffect($element, 'drop', $options);
686
	}
687
688
	/**
689
	 * Visual Effect: Fold.
690
	 * This effect needs jQueryUI.
691
	 * @param \Prado\Web\UI\TControl $element control element or element id
692
	 * @param array $options visual effect key-value pair options.
693
	 */
694
	public function fold($element, $options = [])
695
	{
696
		$this->juiEffect($element, 'fold', $options);
697
	}
698
699
	/**
700
	 * Visual Effect: Gradually make an element grow to a predetermined size.
701
	 * This effect needs jQueryUI.
702
	 * @param \Prado\Web\UI\TControl $element control element or element id
703
	 * @param array $options visual effect key-value pair options.
704
	 */
705
	public function size($element, $options = [])
706
	{
707
		$this->juiEffect($element, 'size', $options);
708
	}
709
710
	/**
711
	 * Visual Effect: Gradually grow and fade the element.
712
	 * This effect needs jQueryUI.
713
	 * @param \Prado\Web\UI\TControl $element control element or element id
714
	 * @param array $options visual effect key-value pair options.
715
	 */
716
	public function puff($element, $options = [])
717
	{
718
		$this->juiEffect($element, 'puff', $options);
719
	}
720
721
	/**
722
	 * Visual Effect: Pulsate.
723
	 * This effect needs jQueryUI.
724
	 * @param \Prado\Web\UI\TControl $element control element or element id
725
	 * @param array $options visual effect key-value pair options.
726
	 */
727
	public function pulsate($element, $options = [])
728
	{
729
		$this->juiEffect($element, 'pulsate', $options);
730
	}
731
732
	/**
733
	 * Visual Effect: Shake the element.
734
	 * This effect needs jQueryUI.
735
	 * @param \Prado\Web\UI\TControl $element control element or element id
736
	 * @param array $options visual effect key-value pair options.
737
	 */
738
	public function shake($element, $options = [])
739
	{
740
		$this->juiEffect($element, 'shake', $options);
741
	}
742
743
	/**
744
	 * Visual Effect: Scale the element.
745
	 * This effect needs jQueryUI.
746
	 * @param \Prado\Web\UI\TControl $element control element or element id
747
	 * @param array $options visual effect key-value pair options.
748
	 */
749
	public function scale($element, $options = [])
750
	{
751
		$this->juiEffect($element, 'scale', $options);
752
	}
753
754
	/**
755
	 * Visual Effect: High light the element for about 2 seconds.
756
	 * This effect needs jQueryUI.
757
	 * @param \Prado\Web\UI\TControl $element control element or element id
758
	 * @param array $options visual effect key-value pair options.
759
	 */
760
	public function highlight($element, $options = [])
761
	{
762
		$this->juiEffect($element, 'highlight', $options);
763
	}
764
765
	/* jui - OLD METHODS, DEPRECATED, BACKWARDS-COMPATIBILITY */
766
767
	/**
768
	 * Alias of blind(), presets the direction to 'down'
769
	 * @deprecated since 3.4
770
	 * @param \Prado\Web\UI\TControl $element control element or element id
771
	 * @param array $options visual effect key-value pair options.
772
	 */
773
	public function blindDown($element, $options = [])
774
	{
775
		$options['direction'] = 'down';
776
		$this->blind($element, $options);
777
	}
778
779
	/**
780
	 * Alias of blind(), presets the direction to 'up'
781
	 * @deprecated since 3.4
782
	 * @param \Prado\Web\UI\TControl $element control element or element id
783
	 * @param array $options visual effect key-value pair options.
784
	 */
785
	public function blindUp($element, $options = [])
786
	{
787
		$options['direction'] = 'up';
788
		$this->blind($element, $options);
789
	}
790
791
	/**
792
	 * Alias of drop()
793
	 * @deprecated since 3.4
794
	 * @param \Prado\Web\UI\TControl $element control element or element id
795
	 * @param array $options visual effect key-value pair options.
796
	 */
797
	public function dropOut($element, $options = [])
798
	{
799
		$this->drop($element, $options);
800
	}
801
802
	/**
803
	 * Alias of size()
804
	 * @deprecated since 3.4
805
	 * @param \Prado\Web\UI\TControl $element control element or element id
806
	 * @param array $options visual effect key-value pair options.
807
	 */
808
	public function grow($element, $options = [])
809
	{
810
		$this->size($element, $options);
811
	}
812
813
	/**
814
	 * Alias of scale()
815
	 * @deprecated since 3.4
816
	 * @param \Prado\Web\UI\TControl $element control element or element id
817
	 * @param array $options visual effect key-value pair options.
818
	 */
819
	public function shrink($element, $options = [])
820
	{
821
		$options['percent'] = 0;
822
		$this->scale($element, $options);
823
	}
824
825
	/**
826
	 * Alias of scale()
827
	 * @deprecated since 3.4
828
	 * @param \Prado\Web\UI\TControl $element control element or element id
829
	 * @param array $options visual effect key-value pair options.
830
	 */
831
	public function squish($element, $options = [])
832
	{
833
		$options['origin'] = ['top', 'left'];
834
		$options['percent'] = 0;
835
		$this->scale($element, $options);
836
	}
837
838
	/**
839
	 * Alias of scale()
840
	 * @deprecated since 3.4
841
	 * @param \Prado\Web\UI\TControl $element control element or element id
842
	 * @param array $options visual effect key-value pair options.
843
	 */
844
	public function switchOff($element, $options = [])
845
	{
846
		$options['direction'] = 'vertical';
847
		$options['percent'] = 0;
848
		$this->scale($element, $options);
849
	}
850
}
851