JsUtilsActionsTrait   F
last analyzed

Complexity

Total Complexity 72

Size/Duplication

Total Lines 816
Duplicated Lines 0 %

Importance

Changes 8
Bugs 0 Features 0
Metric Value
wmc 72
eloc 171
c 8
b 0
f 0
dl 0
loc 816
rs 2.64

44 Methods

Rating   Name   Duplication   Size   Complexity  
A setJsonToElement() 0 4 1
A interval() 0 10 3
A clearInterval() 0 2 1
A asDropZone() 0 8 1
A counterOn() 0 2 1
A counter() 0 35 4
A setDraggable() 0 8 1
A _activateLink() 0 9 2
A activateLink() 0 2 1
A condition() 0 9 3
A doJQueryDeferred() 0 2 1
A _showHideWithEffect() 0 10 3
A hide() 0 2 1
A _genericCallValue() 0 10 3
A tablesorter() 0 2 1
A toggleClass() 0 2 1
A slideUp() 0 2 1
A execAtLast() 0 4 1
A before() 0 2 1
A exec() 0 5 2
A show() 0 2 1
A sortable() 0 12 3
A attr() 0 10 3
A html() 0 2 1
A trigger() 0 7 2
A fadeOut() 0 2 1
A _validate_speed() 0 12 3
A _doJQuery() 0 9 3
A addClass() 0 2 1
A doJQueryOn() 0 7 1
A val() 0 2 1
A after() 0 2 1
A prepend() 0 2 1
A _doJQueryOn() 0 2 1
A toggle() 0 2 1
A execOn() 0 7 1
A append() 0 2 1
A removeClass() 0 2 1
A doJQuery() 0 2 1
A slideDown() 0 2 1
A _genericCallElement() 0 7 2
A animate() 0 25 6
A fadeIn() 0 2 1
A slideToggle() 0 2 1

How to fix   Complexity   

Complex Class

Complex classes like JsUtilsActionsTrait often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use JsUtilsActionsTrait, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace Ajax\common\traits;
4
5
use Ajax\service\Javascript;
6
7
/**
8
 *
9
 * @author jc
10
 * @property array $jquery_code_for_compile
11
 * @property array $jquery_code_for_compile_at_last
12
 */
13
trait JsUtilsActionsTrait {
14
15
	abstract public function _add_event($element, $js, $event, $preventDefault = false, $stopPropagation = false, $immediatly = true, $listenerOn=false);
16
17
	/**
18
	 * show or hide with effect
19
	 *
20
	 * @param string $action
21
	 * @param string $element
22
	 *        	element
23
	 * @param string $speed
24
	 *        	One of 'slow', 'normal', 'fast', or time in milliseconds
25
	 * @param string $callback
26
	 *        	Javascript callback function
27
	 * @param boolean $immediatly
28
	 *        	defers the execution if set to false
29
	 * @return string
30
	 */
31
	protected function _showHideWithEffect($action, $element = 'this', $speed = '', $callback = '', $immediatly = false) {
32
		$element = Javascript::prep_element ( $element );
33
		$speed = $this->_validate_speed ( $speed );
34
		if ($callback != '') {
35
			$callback = ", function(){\n{$callback}\n}";
36
		}
37
		$str = "$({$element}).{$action}({$speed}{$callback});";
38
		if ($immediatly)
39
			$this->jquery_code_for_compile [] = $str;
40
		return $str;
41
	}
42
43
	/**
44
	 * Ensures the speed parameter is valid for jQuery
45
	 *
46
	 * @param string|int $speed
47
	 * @return string
48
	 */
49
	private function _validate_speed($speed) {
50
		if (in_array ( $speed, array (
51
				'slow',
52
				'normal',
53
				'fast'
54
		) )) {
55
			$speed = '"' . $speed . '"';
56
		} elseif (preg_match ( "/[^0-9]/", $speed )) {
57
			$speed = '';
58
		}
59
60
		return $speed;
61
	}
62
63
	/**
64
	 * Execute a generic jQuery call with a value.
65
	 *
66
	 * @param string $jQueryCall
67
	 * @param string $element
68
	 * @param string $param
69
	 * @param boolean $immediatly
70
	 *        	delayed if false
71
	 */
72
	public function _genericCallValue($jQueryCall, $element = 'this', $param = "", $immediatly = false) {
73
		$element = Javascript::prep_element ( $element );
74
		if (isset ( $param )) {
75
			$param = Javascript::prep_value ( $param );
76
			$str = "$({$element}).{$jQueryCall}({$param});";
77
		} else
78
			$str = "$({$element}).{$jQueryCall}();";
79
		if ($immediatly)
80
			$this->jquery_code_for_compile [] = $str;
81
		return $str;
82
	}
83
84
	/**
85
	 * Execute a generic jQuery call with 2 elements.
86
	 *
87
	 * @param string $jQueryCall
88
	 * @param string $to
89
	 * @param string $element
90
	 * @param boolean $immediatly
91
	 *        	delayed if false
92
	 * @return string
93
	 */
94
	public function _genericCallElement($jQueryCall, $to = 'this', $element = '', $immediatly = false) {
95
		$to = Javascript::prep_element ( $to );
96
		$element = Javascript::prep_element ( $element );
97
		$str = "$({$to}).{$jQueryCall}({$element});";
98
		if ($immediatly)
99
			$this->jquery_code_for_compile [] = $str;
100
		return $str;
101
	}
102
103
	/**
104
	 * add class to element
105
	 *
106
	 * @param string $element
107
	 * @param string $class
108
	 *        	to add
109
	 * @param boolean $immediatly
110
	 *        	defers the execution if set to false
111
	 * @return string
112
	 */
113
	public function addClass($element = 'this', $class = '', $immediatly = false) {
114
		return $this->_genericCallValue ( 'addClass', $element, $class, $immediatly );
115
	}
116
117
	/**
118
	 * Insert content, specified by the parameter, after each element in the set of matched elements
119
	 *
120
	 * @param string $to
121
	 * @param string $element
122
	 * @param boolean $immediatly
123
	 *        	defers the execution if set to false
124
	 * @return string
125
	 */
126
	public function after($to, $element, $immediatly = false) {
127
		return $this->_genericCallElement ( 'after', $to, $element, $immediatly );
128
	}
129
130
	/**
131
	 * Insert content, specified by the parameter, before each element in the set of matched elements
132
	 *
133
	 * @param string $to
134
	 * @param string $element
135
	 * @param boolean $immediatly
136
	 *        	defers the execution if set to false
137
	 * @return string
138
	 */
139
	public function before($to, $element, $immediatly = false) {
140
		return $this->_genericCallElement ( 'before', $to, $element, $immediatly );
141
	}
142
143
	/**
144
	 * Get or set the value of an attribute for the first element in the set of matched elements or set one or more attributes for every matched element.
145
	 *
146
	 * @param string $element
147
	 * @param string $attributeName
148
	 * @param string $value
149
	 * @param boolean $immediatly
150
	 *        	delayed if false
151
	 */
152
	public function attr($element = 'this', $attributeName = 'id', $value = "", $immediatly = false) {
153
		$element = Javascript::prep_element ( $element );
154
		if (isset ( $value )) {
155
			$value = Javascript::prep_value ( $value );
156
			$str = "$({$element}).attr(\"$attributeName\",{$value});";
157
		} else
158
			$str = "$({$element}).attr(\"$attributeName\");";
159
		if ($immediatly)
160
			$this->jquery_code_for_compile [] = $str;
161
		return $str;
162
	}
163
164
	/**
165
	 * Get or set the value of the first element in the set of matched elements or set one or more attributes for every matched element.
166
	 *
167
	 * @param string $element
168
	 * @param string $value
169
	 * @param boolean $immediatly
170
	 *        	defers the execution if set to false
171
	 */
172
	public function val($element = 'this', $value = '', $immediatly = false) {
173
		return $this->_genericCallValue ( 'val', $element, $value, $immediatly );
174
	}
175
176
	/**
177
	 * Get or set the html of an attribute for the first element in the set of matched elements.
178
	 *
179
	 * @param string $element
180
	 * @param string $value
181
	 * @param boolean $immediatly
182
	 *        	defers the execution if set to false
183
	 */
184
	public function html($element = 'this', $value = '', $immediatly = false) {
185
		return $this->_genericCallValue ( 'html', $element, $value, $immediatly );
186
	}
187
188
	/**
189
	 * Outputs a javascript library animate event
190
	 *
191
	 * @param string $element
192
	 *        	element
193
	 * @param array|string $params
194
	 * @param string $speed
195
	 *        	One of 'slow', 'normal', 'fast', or time in milliseconds
196
	 * @param string $extra
197
	 * @param boolean $immediatly
198
	 *        	defers the execution if set to false
199
	 * @return string
200
	 */
201
	public function animate($element = 'this', $params = array (), $speed = '', $extra = '', $immediatly = false) {
202
		$element = Javascript::prep_element ( $element );
203
		$speed = $this->_validate_speed ( $speed );
204
205
		$animations = "\t\t\t";
206
		if (\is_array ( $params )) {
207
			foreach ( $params as $param => $value ) {
208
				$animations .= $param . ': \'' . $value . '\', ';
209
			}
210
		}
211
		$animations = substr ( $animations, 0, - 2 ); // remove the last ", "
212
213
		if ($speed != '') {
214
			$speed = ', ' . $speed;
215
		}
216
217
		if ($extra != '') {
218
			$extra = ', ' . $extra;
219
		}
220
221
		$str = "$({$element}).animate({\n$animations\n\t\t}" . $speed . $extra . ");";
222
223
		if ($immediatly)
224
			$this->jquery_code_for_compile [] = $str;
225
		return $str;
226
	}
227
228
	/**
229
	 * Insert content, specified by the parameter $element, to the end of each element in the set of matched elements $to.
230
	 *
231
	 * @param string $to
232
	 * @param string $element
233
	 * @param boolean $immediatly
234
	 *        	defers the execution if set to false
235
	 * @return string
236
	 */
237
	public function append($to, $element, $immediatly = false) {
238
		return $this->_genericCallElement ( 'append', $to, $element, $immediatly );
239
	}
240
241
	/**
242
	 * Insert content, specified by the parameter $element, to the beginning of each element in the set of matched elements $to.
243
	 *
244
	 * @param string $to
245
	 * @param string $element
246
	 * @param boolean $immediatly
247
	 *        	defers the execution if set to false
248
	 * @return string
249
	 */
250
	public function prepend($to, $element, $immediatly = false) {
251
		return $this->_genericCallElement ( 'prepend', $to, $element, $immediatly );
252
	}
253
254
	/**
255
	 * Execute a javascript library hide action
256
	 *
257
	 * @param string $element
258
	 *        	element
259
	 * @param string $speed
260
	 *        	One of 'slow', 'normal', 'fast', or time in milliseconds
261
	 * @param string $callback
262
	 *        	Javascript callback function
263
	 * @param boolean $immediatly
264
	 *        	defers the execution if set to false
265
	 * @return string
266
	 */
267
	public function fadeIn($element = 'this', $speed = '', $callback = '', $immediatly = false) {
268
		return $this->_showHideWithEffect ( "fadeIn", $element, $speed, $callback, $immediatly );
269
	}
270
271
	/**
272
	 * Execute a javascript library hide action
273
	 *
274
	 * @param string $element
275
	 *        	element
276
	 * @param string $speed
277
	 *        	One of 'slow', 'normal', 'fast', or time in milliseconds
278
	 * @param string $callback
279
	 *        	Javascript callback function
280
	 * @param boolean $immediatly
281
	 *        	defers the execution if set to false
282
	 * @return string
283
	 */
284
	public function fadeOut($element = 'this', $speed = '', $callback = '', $immediatly = false) {
285
		return $this->_showHideWithEffect ( "fadeOut", $element, $speed, $callback, $immediatly );
286
	}
287
288
	/**
289
	 * Execute a javascript library slideUp action
290
	 *
291
	 * @param string $element
292
	 *        	element
293
	 * @param string $speed
294
	 *        	One of 'slow', 'normal', 'fast', or time in milliseconds
295
	 * @param string $callback
296
	 *        	Javascript callback function
297
	 * @param boolean $immediatly
298
	 *        	defers the execution if set to false
299
	 * @return string
300
	 */
301
	public function slideUp($element = 'this', $speed = '', $callback = '', $immediatly = false) {
302
		return $this->_showHideWithEffect ( "slideUp", $element, $speed, $callback, $immediatly );
303
	}
304
305
	/**
306
	 * Execute a javascript library removeClass action
307
	 *
308
	 * @param string $element
309
	 *        	element
310
	 * @param string $class
311
	 *        	Class to add
312
	 * @param boolean $immediatly
313
	 *        	defers the execution if set to false
314
	 * @return string
315
	 */
316
	public function removeClass($element = 'this', $class = '', $immediatly = false) {
317
		return $this->_genericCallValue ( 'removeClass', $element, $class, $immediatly );
318
	}
319
320
	/**
321
	 * Execute a javascript library slideDown action
322
	 *
323
	 * @param string $element
324
	 *        	element
325
	 * @param string $speed
326
	 *        	One of 'slow', 'normal', 'fast', or time in milliseconds
327
	 * @param string $callback
328
	 *        	Javascript callback function
329
	 * @param boolean $immediatly
330
	 *        	defers the execution if set to false
331
	 * @return string
332
	 */
333
	public function slideDown($element = 'this', $speed = '', $callback = '', $immediatly = false) {
334
		return $this->_showHideWithEffect ( "slideDown", $element, $speed, $callback, $immediatly );
335
	}
336
337
	/**
338
	 * Execute a javascript library slideToggle action
339
	 *
340
	 * @param string $element
341
	 *        	element
342
	 * @param string $speed
343
	 *        	One of 'slow', 'normal', 'fast', or time in milliseconds
344
	 * @param string $callback
345
	 *        	Javascript callback function
346
	 * @param boolean $immediatly
347
	 *        	defers the execution if set to false
348
	 * @return string
349
	 */
350
	public function slideToggle($element = 'this', $speed = '', $callback = '', $immediatly = false) {
351
		return $this->_showHideWithEffect ( "slideToggle", $element, $speed, $callback, $immediatly );
352
	}
353
354
	/**
355
	 * Execute a javascript library hide action
356
	 *
357
	 * @param string $element
358
	 *        	element
359
	 * @param string $speed
360
	 *        	One of 'slow', 'normal', 'fast', or time in milliseconds
361
	 * @param string $callback
362
	 *        	Javascript callback function
363
	 * @param boolean $immediatly
364
	 *        	defers the execution if set to false
365
	 * @return string
366
	 */
367
	public function hide($element = 'this', $speed = '', $callback = '', $immediatly = false) {
368
		return $this->_showHideWithEffect ( "hide", $element, $speed, $callback, $immediatly );
369
	}
370
371
	/**
372
	 * Execute a javascript library toggle action
373
	 *
374
	 * @param string $element
375
	 *        	element
376
	 * @param string $speed
377
	 *        	One of 'slow', 'normal', 'fast', or time in milliseconds
378
	 * @param string $callback
379
	 *        	Javascript callback function
380
	 * @param boolean $immediatly
381
	 *        	defers the execution if set to false
382
	 * @return string
383
	 */
384
	public function toggle($element = 'this', $speed = '', $callback = '', $immediatly = false) {
385
		return $this->_showHideWithEffect ( "toggle", $element, $speed, $callback, $immediatly );
386
	}
387
388
	/**
389
	 * Execute a javascript library toggle class action
390
	 *
391
	 * @param string $element
392
	 *        	element
393
	 * @param string $class
394
	 * @param boolean $immediatly
395
	 *        	defers the execution if set to false
396
	 * @return string
397
	 */
398
	public function toggleClass($element = 'this', $class = '', $immediatly = false) {
399
		return $this->_genericCallValue ( 'toggleClass', $element, $class, $immediatly );
400
	}
401
402
	/**
403
	 * Execute all handlers and behaviors attached to the matched elements for the given event.
404
	 *
405
	 * @param string $element
406
	 * @param string $event
407
	 * @param boolean $immediatly
408
	 *        	defers the execution if set to false
409
	 */
410
	public function trigger($element = 'this', $event = 'click', $immediatly = false) {
411
		$element = Javascript::prep_element ( $element );
412
		$str = "$({$element}).trigger(\"$event\");";
413
414
		if ($immediatly)
415
			$this->jquery_code_for_compile [] = $str;
416
		return $str;
417
	}
418
419
	/**
420
	 * Execute a javascript library show action
421
	 *
422
	 * @param string $element
423
	 *        	element
424
	 * @param string $speed
425
	 *        	One of 'slow', 'normal', 'fast', or time in milliseconds
426
	 * @param string $callback
427
	 *        	Javascript callback function
428
	 * @param boolean $immediatly
429
	 *        	defers the execution if set to false
430
	 * @return string
431
	 */
432
	public function show($element = 'this', $speed = '', $callback = '', $immediatly = false) {
433
		return $this->_showHideWithEffect ( "show", $element, $speed, $callback, $immediatly );
434
	}
435
436
	/**
437
	 * Creates a jQuery sortable
438
	 *
439
	 * @param string $element
440
	 * @param array $options
441
	 * @return string
442
	 */
443
	public function sortable($element, $options = array ()) {
444
		if (count ( $options ) > 0) {
445
			$sort_options = array ();
446
			foreach ( $options as $k => $v ) {
447
				$sort_options [] = "\n\t\t" . $k . ': ' . $v . "";
448
			}
449
			$sort_options = implode ( ",", $sort_options );
450
		} else {
451
			$sort_options = '';
452
		}
453
454
		return "$(" . Javascript::prep_element ( $element ) . ").sortable({" . $sort_options . "\n\t});";
455
	}
456
457
	/**
458
	 * Table Sorter Plugin
459
	 *
460
	 * @param string $table
461
	 *        	table name
462
	 * @param string $options
463
	 *        	plugin location
464
	 * @return string
465
	 */
466
	public function tablesorter($table = '', $options = '') {
467
		$this->jquery_code_for_compile [] = "\t$(" . Javascript::prep_element ( $table ) . ").tablesorter($options);\n";
468
	}
469
470
	/**
471
	 * Allows to attach a condition
472
	 *
473
	 * @param string $condition
474
	 * @param string $jsCodeIfTrue
475
	 * @param string $jsCodeIfFalse
476
	 * @param boolean $immediatly
477
	 *        	defers the execution if set to false
478
	 */
479
	public function condition($condition, $jsCodeIfTrue, $jsCodeIfFalse = null, $immediatly = false) {
480
		$str = "if(" . $condition . "){" . $jsCodeIfTrue . "}";
481
		if (isset ( $jsCodeIfFalse )) {
482
			$str .= "else{" . $jsCodeIfFalse . "}";
483
		}
484
485
		if ($immediatly)
486
			$this->jquery_code_for_compile [] = $str;
487
		return $str;
488
	}
489
490
	/**
491
	 * Call the JQuery method $jqueryCall on $element with parameters $param
492
	 *
493
	 * @param string $element
494
	 * @param string $jqueryCall
495
	 * @param mixed $param
496
	 * @param string $jsCallback
497
	 *        	javascript code to execute after the jquery call
498
	 * @param boolean $immediatly
499
	 * @return string
500
	 */
501
	private function _doJQuery($element, $jqueryCall, $param = "", $jsCallback = "", $immediatly = false) {
502
		$param = Javascript::prep_value ( $param );
503
		$callback = "";
504
		if ($jsCallback != "")
505
			$callback = ", function(event){\n{$jsCallback}\n}";
506
		$script = "$(" . Javascript::prep_element ( $element ) . ")." . $jqueryCall . "(" . $param . $callback . ");\n";
507
		if ($immediatly)
508
			$this->jquery_code_for_compile [] = $script;
509
		return $script;
510
	}
511
512
	/**
513
	 * Calls the JQuery callback $someThing on $element with facultative parameter $param
514
	 *
515
	 * @param string $element
516
	 *        	the element
517
	 * @param string $jqueryCall
518
	 *        	the JQuery callback
519
	 * @param mixed $param
520
	 *        	array or string parameters
521
	 * @param string $jsCallback
522
	 *        	javascript code to execute after the jquery call
523
	 * @return mixed
524
	 */
525
	public function doJQuery($element, $jqueryCall, $param = "", $jsCallback = "") {
526
		return $this->_doJQuery ( $element, $jqueryCall, $param, $jsCallback, true );
527
	}
528
529
	/**
530
	 * Calls the JQuery callback $someThing on $element with facultative parameter $param
531
	 *
532
	 * @param string $element
533
	 *        	the element
534
	 * @param string $jqueryCall
535
	 *        	the JQuery callback
536
	 * @param mixed $param
537
	 *        	array or string parameters
538
	 * @param string $jsCallback
539
	 *        	javascript code to execute after the jquery call
540
	 * @return mixed
541
	 */
542
	public function doJQueryDeferred($element, $jqueryCall, $param = "", $jsCallback = "") {
543
		return $this->_doJQuery ( $element, $jqueryCall, $param, $jsCallback, false );
544
	}
545
546
	/**
547
	 *
548
	 * @param string $event
549
	 * @param string $element
550
	 * @param string $elementToModify
551
	 * @param string $jqueryCall
552
	 * @param string|array $param
553
	 * @param boolean $preventDefault
554
	 * @param boolean $stopPropagation
555
	 * @param string $jsCallback
556
	 *        	javascript code to execute after the jquery call
557
	 * @param boolean $immediatly
558
	 * @return string
559
	 */
560
	private function _doJQueryOn($event, $element, $elementToModify, $jqueryCall, $param = "", $preventDefault = false, $stopPropagation = false, $jsCallback = "", $immediatly = true) {
561
		return $this->_add_event ( $element, $this->_doJQuery ( $elementToModify, $jqueryCall, $param, $jsCallback ), $event, $preventDefault, $stopPropagation, $immediatly );
562
	}
563
564
	/**
565
	 * Calls the JQuery callback $jqueryCall on $element with facultative parameter $param in response to an event $event
566
	 *
567
	 * @param string $event
568
	 * @param string $element
569
	 * @param string $elementToModify
570
	 * @param string $jqueryCall
571
	 * @param string $param
572
	 * @param array $parameters
573
	 *        	default : array("preventDefault"=>false,"stopPropagation"=>false,"jsCallback"=>'',"immediatly"=>true)
574
	 */
575
	public function doJQueryOn($event, $element, $elementToModify, $jqueryCall, $param = "", $parameters = array ()) {
576
		$jsCallback = "";
577
		$stopPropagation = false;
578
		$preventDefault = false;
579
		$immediatly = true;
580
		extract ( $parameters );
581
		return $this->_doJQueryOn ( $event, $element, $elementToModify, $jqueryCall, $param, $preventDefault, $stopPropagation, $jsCallback, $immediatly );
582
	}
583
584
	/**
585
	 * Executes the code $js
586
	 *
587
	 * @param string $js
588
	 *        	Code to execute
589
	 * @param boolean $immediatly
590
	 *        	delayed if false
591
	 * @return String
592
	 */
593
	public function exec($js, $immediatly = false) {
594
		$script = $js . "\n";
595
		if ($immediatly)
596
			$this->jquery_code_for_compile [] = $script;
597
		return $script;
598
	}
599
600
	/**
601
	 * Executes the code $js
602
	 *
603
	 * @param string $js
604
	 *        	Code to execute
605
	 * @param boolean $immediatly
606
	 *        	delayed if false
607
	 * @return String
608
	 */
609
	public function execAtLast($js) {
610
		$script = $js . "\n";
611
		$this->jquery_code_for_compile_at_last [] = $script;
612
		return $script;
613
	}
614
615
	/**
616
	 * Executes the javascript code $js when $event fires on $element
617
	 *
618
	 * @param string $event
619
	 * @param string $element
620
	 * @param string $js
621
	 *        	Code to execute
622
	 * @param array $parameters
623
	 *        	default : array("preventDefault"=>false,"stopPropagation"=>false,"immediatly"=>true)
624
	 * @return String
625
	 */
626
	public function execOn($event, $element, $js, $parameters = array ()) {
627
		$stopPropagation = false;
628
		$preventDefault = false;
629
		$immediatly = true;
630
		extract ( $parameters );
631
		$script = $this->_add_event ( $element, $this->exec ( $js ), $event, $preventDefault, $stopPropagation, $immediatly );
632
		return $script;
633
	}
634
	public function setJsonToElement($json, $elementClass = "_element", $immediatly = true) {
0 ignored issues
show
Unused Code introduced by
The parameter $elementClass is not used and could be removed. ( Ignorable by Annotation )

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

634
	public function setJsonToElement($json, /** @scrutinizer ignore-unused */ $elementClass = "_element", $immediatly = true) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
635
		$retour = "var data={$json};" . "\n\tdata=($.isPlainObject(data))?data:JSON.parse(data);" . "\n\tvar pk=data['pk'];var object=data['object'];" . "\n\tfor(var field in object){" . "\n\tif($('[data-field='+field+']',$('._element[data-ajax='+pk+']')).length){" . "\n\t\t$('[data-field='+field+']',$('._element[data-ajax='+pk+']')).each(function(){" . "\n\t\t\tif($(this).is('[value]')) { $(this).val(object[field]);} else { $(this).html(object[field]); }" . "\n\t});" . "\n}};\n";
636
		$retour .= "\t$(document).trigger('jsonReady',[data]);\n";
637
		return $this->exec ( $retour, $immediatly );
638
	}
639
640
	/**
641
	 * Sets an element draggable (HTML5 drag and drop)
642
	 *
643
	 * @param string $element
644
	 *        	The element selector
645
	 * @param array $parameters
646
	 *        	default : array("attr"=>"id","preventDefault"=>false,"stopPropagation"=>false,"immediatly"=>true)
647
	 */
648
	public function setDraggable($element, $parameters = [ ]) {
649
		$attr = "id";
650
		$preventDefault = false;
651
		$stopPropagation = false;
652
		$immediatly = true;
653
		extract ( $parameters );
654
		$script = $this->_add_event ( $element, Javascript::draggable ( $attr ), "dragstart", $preventDefault, $stopPropagation, $immediatly );
655
		return $script;
656
	}
657
658
	/**
659
	 * Declares an element as a drop zone (HTML5 drag and drop)
660
	 *
661
	 * @param string $element
662
	 *        	The element selector
663
	 * @param array $parameters
664
	 *        	default : array("attr"=>"id","stopPropagation"=>false,"immediatly"=>true,"jqueryDone"=>"append")
665
	 * @param string $jsCallback
666
	 *        	the js script to call when element is dropped
667
	 */
668
	public function asDropZone($element, $jsCallback = "", $parameters = [ ]) {
669
		$stopPropagation = false;
670
		$immediatly = true;
671
		$jqueryDone = "append";
672
		$script = $this->_add_event ( $element, '', "dragover", true, $stopPropagation, $immediatly );
673
		extract ( $parameters );
674
		$script .= $this->_add_event ( $element, Javascript::dropZone ( $jqueryDone, $jsCallback ), "drop", true, $stopPropagation, $immediatly );
675
		return $script;
676
	}
677
678
	/**
679
	 * Calls a function or evaluates an expression at specified intervals (in milliseconds)
680
	 *
681
	 * @param string $jsCode
682
	 *        	The code to execute
683
	 * @param int $time
684
	 *        	The time interval in milliseconds
685
	 * @param string $globalName
686
	 *        	The global name of the interval, used to clear it
687
	 * @param boolean $immediatly
688
	 *        	delayed if false
689
	 * @return string
690
	 */
691
	public function interval($jsCode, $time, $globalName = null, $immediatly = true) {
692
		if (! Javascript::isFunction ( $jsCode )) {
693
			$jsCode = "function(){\n" . $jsCode . "\n}";
694
		}
695
		if (isset ( $globalName )) {
696
			$script = "if(window.{$globalName}){clearInterval(window.{$globalName});}\nwindow.{$globalName}=setInterval({$jsCode},{$time});";
697
		} else {
698
			$script = "setInterval({$jsCode},{$time});";
699
		}
700
		return $this->exec ( $script, $immediatly );
701
	}
702
703
	/**
704
	 * Clears an existing interval
705
	 *
706
	 * @param string $globalName
707
	 *        	The interval global name
708
	 * @param boolean $immediatly
709
	 *        	delayed if false
710
	 * @return string
711
	 */
712
	public function clearInterval($globalName, $immediatly = true) {
713
		return $this->exec ( "if(window.{$globalName}){clearInterval(window.{$globalName});}", $immediatly );
714
	}
715
716
	/**
717
	 * Associates a counter to the element designated by $counterSelector
718
	 * Triggers the events counter-start and counter-end on finished with the parameters value and limit
719
	 *
720
	 * @param string $counterSelector
721
	 *        	Selector of the existing element wich display the counter
722
	 * @param integer $value
723
	 *        	The initial value of the counter
724
	 * @param integer $limit
725
	 *        	The limit of the counter (minimum if countDown is true, maximum if not)
726
	 * @param string $globalName
727
	 *        	The global name of the counter, to use with the clearInterval method
728
	 * @param boolean $countDown
729
	 *        	count down if true or elapse if false
730
	 * @param boolean $immediatly
731
	 *        	delayed if false
732
	 * @return string
733
	 */
734
	public function counter($counterSelector, $value = 0, $limit = 0, $globalName = null, $countDown = true, $immediatly = true) {
735
		$stop = "";
736
		if ($countDown) {
737
			$stop = "if (--timer < " . $limit . ") {clearInterval(interval);display.trigger({type:'counter-end',value: timer,limit:" . $limit . "});}";
738
		} else {
739
			if ($limit != 0) {
740
				$stop = "if (++timer > " . $limit . ") {clearInterval(interval);display.trigger({type:'counter-end',value: timer,limit:" . $limit . "});}";
741
			}
742
		}
743
		$global = "";
744
		if (isset ( $globalName )) {
745
			$global = "\nwindow.{$globalName}=interval;";
746
		}
747
		$timer = "var startTimer=function(duration, display) {var timer = duration, days, hours, minutes, seconds;
748
											var sh=function(disp,v){if(disp.is('[value]')){disp.val(v);} else {disp.html(v);};};
749
											var shHide=function(v,k,kBefore){if(v==0 && display.find(k).closest('.timer').is(':visible') && (!kBefore || !display.find(kBefore).closest('.timer').is(':visible'))){display.find(k).closest('.timer').hide();}else{sh(display.find(k),v);}};
750
											var pl=function(v,text){return (v>1)?v+' '+text+'s':(v>0)?v+' '+text:'';};
751
											var d0=function(v){return v < 10 ? '0' + v : v;};
752
											var shortSh=function(d,h,m,s){sh(display,pl(d,'day')+' '+[h,m,s].join(':'));};
753
											var longSh=function(d,h,m,s){shHide(d,'.day');shHide(h,'.hour','.day');shHide(m,'.minute','.hour');shHide(s,'.second','.minute');};
754
											var mainSh=(display.find('.hour').first().length)?longSh:shortSh;
755
											display.trigger('counter-start',timer);
756
											display.show();
757
											var interval=setInterval(function () {
758
												days = parseInt(timer / 86400, 10);
759
												hours = d0(parseInt((timer%86400) / 3600, 10));
760
												minutes = d0(parseInt((timer%3600) / 60, 10));
761
												seconds = d0(parseInt(timer%60, 10));
762
												mainSh(days,hours,minutes,seconds);
763
												" . $stop . "
764
    										}, 1000);
765
										" . $global . "
766
										}";
767
		$element = '$("' . $counterSelector . '")';
768
		return $this->exec ( $timer . "\nstartTimer(" . $value . "," . $element . ");", $immediatly );
769
	}
770
771
	/**
772
	 * Associates a counter to the element designated by $counterSelector when $event is triggered on $element
773
	 *
774
	 * @param string $element
775
	 *        	The triggering element
776
	 * @param string $event
777
	 *        	The triggering event
778
	 * @param string $counterSelector
779
	 *        	Selector of the existing element wich display the counter
780
	 * @param integer $value
781
	 *        	The initial value of the counter
782
	 * @param integer $limit
783
	 *        	The limit of the counter (minimum if countDown is true, maximum if not)
784
	 * @param string $globalName
785
	 *        	The global name of the counter, to use with the clearInterval method
786
	 * @param boolean $countDown
787
	 *        	count down if true or elapse if false
788
	 * @return string
789
	 */
790
	public function counterOn($element, $event, $counterSelector, $value = 0, $limit = 0, $globalName = null, $countDown = true) {
791
		return $this->execOn ( $event, $element, $this->counter ( $counterSelector, $value, $limit, $globalName, $countDown, false ) );
792
	}
793
794
	/**
795
	 * Activates an element if it is active (add the class active)
796
	 *
797
	 * @param string $target
798
	 *        	the container element
799
	 * @param string $property
800
	 *        	default: href
801
	 * @param string $href
802
	 *        	the active href (if null, window.location.href is used)
803
	 * @return string
804
	 */
805
	public function activateLink($target, $property = 'href', $href = null) {
806
		return $this->execAtLast ( $this->_activateLink ( $target, $property, $href ) );
807
	}
808
809
	/**
810
	 * Returns the javascript code for activate an element if it is active (add the class active)
811
	 *
812
	 * @param string $target
813
	 *        	the container element
814
	 * @param string $property
815
	 *        	default: href
816
	 * @param string $href
817
	 *        	the active href (if null, window.location.href is used)
818
	 * @return string
819
	 */
820
	public function _activateLink($target, $property = 'href', $href = null) {
821
		$js = '$("' . $target . ' [' . $property . ']").removeClass("active");';
822
		if (isset ( $href )) {
823
			$js .= 'var href="' . $href . '";';
824
		} else {
825
			$js .= 'var href=window.location.href;';
826
		}
827
		$js .= '$("' . $target . ' [' . $property . ']").each(function(){if(href.includes($(this).attr("' . $property . '"))) $(this).addClass("active");});';
828
		return $js;
829
	}
830
}
831