Passed
Push — master ( cb22e7...489ec7 )
by Jean-Christophe
02:21
created

JsUtilsActionsTrait::_doJQueryOn()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 9
dl 0
loc 2
rs 10
c 0
b 0
f 0

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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

534
	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...
535
		$retour="var data={$json};"
536
				."\n\tdata=($.isPlainObject(data))?data:JSON.parse(data);"
537
				."\n\tvar pk=data['pk'];var object=data['object'];"
538
				."\n\tfor(var field in object){"
539
				."\n\tif($('[data-field='+field+']',$('._element[data-ajax='+pk+']')).length){"
540
				."\n\t\t$('[data-field='+field+']',$('._element[data-ajax='+pk+']')).each(function(){"
541
						."\n\t\t\tif($(this).is('[value]')) { $(this).val(object[field]);} else { $(this).html(object[field]); }"
542
				."\n\t});"
543
				."\n}};\n";
544
		$retour.="\t$(document).trigger('jsonReady',[data]);\n";
545
		return $this->exec($retour,$immediatly);
546
	}
547
	
548
	/**
549
	 * Sets an element draggable (HTML5 drag and drop)
550
	 * @param string $element The element selector
551
	 * @param array $parameters default : array("attr"=>"id","preventDefault"=>false,"stopPropagation"=>false,"immediatly"=>true)
552
	 */
553
	public function setDraggable($element,$parameters=[]){
554
		$attr="id";
555
		extract($parameters);
556
		$script=$this->_add_event($element, Javascript::draggable($attr), "dragstart",$parameters);
557
		return $script;
558
	}
559
	
560
	/**
561
	 * Declares an element as a drop zone (HTML5 drag and drop)
562
	 * @param string $element The element selector
563
	 * @param array $parameters default : array("attr"=>"id","stopPropagation"=>false,"immediatly"=>true,"jqueryDone"=>"append")
564
	 * @param string $jsCallback the js script to call when element is dropped
565
	 */
566
	public function asDropZone($element,$jsCallback="",$parameters=[]){
567
		$stopPropagation=false;
568
		$immediatly=true;
569
		$jqueryDone="append";
570
		$script=$this->_add_event($element, '', "dragover",true,$stopPropagation,$immediatly);
571
		extract($parameters);
572
		$script.=$this->_add_event($element, Javascript::dropZone($jqueryDone,$jsCallback), "drop",true,$stopPropagation,$immediatly);
573
		return $script;
574
	}
575
	
576
	public function interval($jsCode,$time,$globalName=null,$immediatly=true){
577
		if(!Javascript::isFunction($jsCode)){
578
			$jsCode="function(){\n".$jsCode."\n}";
579
		}
580
		if(isset($globalName)){
581
			$script="if(window.{$globalName}){clearInterval(window.{$globalName});}\nwindow.{$globalName}=setInterval({$jsCode},{$time});";
582
		}else{
583
			$script="setInterval({$jsCode},{$time});";
584
		}
585
		return $this->exec($script,$immediatly);
586
	}
587
	
588
	public function clearInterval($globalName,$immediatly=true){
589
		return $this->exec("if(window.{$globalName}){clearInterval(window.{$globalName});}",$immediatly);
590
	}
591
}
592