Completed
Push — master ( 63bca8...69044f )
by Jean-Christophe
04:31
created

JsUtilsActionsTrait::_doJQueryOn()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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

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
 */
11
trait JsUtilsActionsTrait {
12
13
	abstract public function _add_event($element, $js, $event, $preventDefault=false, $stopPropagation=false,$immediatly=true);
14
	/**
15
	 * show or hide with effect
16
	 *
17
	 * @param string $action
18
	 * @param string - element
19
	 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
20
	 * @param string - Javascript callback function
21
	 * @param boolean $immediatly defers the execution if set to false
22
	 * @return string
23
	 */
24
	protected function _showHideWithEffect($action,$element='this', $speed='', $callback='', $immediatly=false) {
25
		$element=Javascript::prep_element($element);
26
		$speed=$this->_validate_speed($speed);
27
		if ($callback!='') {
28
			$callback=", function(){\n{$callback}\n}";
29
		}
30
		$str="$({$element}).{$action}({$speed}{$callback});";
31
		if ($immediatly)
32
			$this->jquery_code_for_compile[]=$str;
33
			return $str;
34
	}
35
	/**
36
	 * Ensures the speed parameter is valid for jQuery
37
	 * @param string|int $speed
38
	 * @return string
39
	 */
40
	private function _validate_speed($speed) {
41
		if (in_array($speed, array (
42
				'slow','normal','fast'
43
		))) {
44
			$speed='"'.$speed.'"';
45
		} elseif (preg_match("/[^0-9]/", $speed)) {
46
			$speed='';
47
		}
48
49
		return $speed;
50
	}
51
52
	/**
53
	 * Execute a generic jQuery call with a value.
54
	 * @param string $jQueryCall
55
	 * @param string $element
56
	 * @param string $param
57
	 * @param boolean $immediatly delayed if false
58
	 */
59 View Code Duplication
	public function _genericCallValue($jQueryCall,$element='this', $param="", $immediatly=false) {
60
		$element=Javascript::prep_element($element);
61
		if (isset($param)) {
62
			$param=Javascript::prep_value($param);
63
			$str="$({$element}).{$jQueryCall}({$param});";
64
		} else
65
			$str="$({$element}).{$jQueryCall}();";
66
		if ($immediatly)
67
			$this->jquery_code_for_compile[]=$str;
68
		return $str;
69
	}
70
71
	/**
72
	 * Execute a generic jQuery call with 2 elements.
73
	 * @param string $jQueryCall
74
	 * @param string $to
75
	 * @param string $element
76
	 * @param boolean $immediatly delayed if false
77
	 * @return string
78
	 */
79
	public function _genericCallElement($jQueryCall,$to='this', $element, $immediatly=false) {
80
		$to=Javascript::prep_element($to);
81
		$element=Javascript::prep_element($element);
82
		$str="$({$to}).{$jQueryCall}({$element});";
83
		if ($immediatly)
84
			$this->jquery_code_for_compile[]=$str;
85
		return $str;
86
	}
87
	/**
88
	 * add class to element
89
	 *
90
	 * @param string $element
91
	 * @param string $class to add
92
	 * @param boolean $immediatly defers the execution if set to false
93
	 * @return string
94
	 */
95
	public function addClass($element='this', $class='', $immediatly=false) {
96
		return $this->_genericCallValue('addClass',$element, $class, $immediatly);
97
	}
98
99
	/**
100
	 * Insert content, specified by the parameter, after each element in the set of matched elements
101
	 * @param string $to
102
	 * @param string $element
103
	 * @param boolean $immediatly defers the execution if set to false
104
	 * @return string
105
	 */
106
	public function after($to, $element, $immediatly=false){
107
		return $this->_genericCallElement('after',$to, $element, $immediatly);
108
	}
109
110
	/**
111
	 * Insert content, specified by the parameter, before each element in the set of matched elements
112
	 * @param string $to
113
	 * @param string $element
114
	 * @param boolean $immediatly defers the execution if set to false
115
	 * @return string
116
	 */
117
	public function before($to, $element, $immediatly=false){
118
		return $this->_genericCallElement('before',$to, $element, $immediatly);
119
	}
120
121
	/**
122
	 * 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.
123
	 * @param string $element
124
	 * @param string $attributeName
125
	 * @param string $value
126
	 * @param boolean $immediatly delayed if false
127
	 */
128 View Code Duplication
	public function attr($element='this', $attributeName, $value="", $immediatly=false) {
129
		$element=Javascript::prep_element($element);
130
		if (isset($value)) {
131
			$value=Javascript::prep_value($value);
132
			$str="$({$element}).attr(\"$attributeName\",{$value});";
133
		} else
134
			$str="$({$element}).attr(\"$attributeName\");";
135
		if ($immediatly)
136
			$this->jquery_code_for_compile[]=$str;
137
		return $str;
138
	}
139
140
	/**
141
	 * 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.
142
	 * @param string $element
143
	 * @param string $value
144
	 * @param boolean $immediatly defers the execution if set to false
145
	 */
146
	public function val($element='this',$value='',$immediatly=false){
147
		return $this->_genericCallValue('val',$element,$value,$immediatly);
148
	}
149
150
	/**
151
	 * Get or set the html of an attribute for the first element in the set of matched elements.
152
	 * @param string $element
153
	 * @param string $value
154
	 * @param boolean $immediatly defers the execution if set to false
155
	 */
156
	public function html($element='this', $value='', $immediatly=false) {
157
		return $this->_genericCallValue('html',$element, $value, $immediatly);
158
	}
159
160
	/**
161
	 * Outputs a javascript library animate event
162
	 *
163
	 * @param string $element element
164
	 * @param array $params
165
	 * @param string $speed One of 'slow', 'normal', 'fast', or time in milliseconds
166
	 * @param string $extra
167
	 * @param boolean $immediatly defers the execution if set to false
168
	 * @return string
169
	 */
170
	public function animate($element='this', $params=array(), $speed='', $extra='', $immediatly=false) {
171
		$element=Javascript::prep_element($element);
172
		$speed=$this->_validate_speed($speed);
173
174
		$animations="\t\t\t";
175
		if (\is_array($params)) {
176
			foreach ( $params as $param => $value ) {
177
				$animations.=$param.': \''.$value.'\', ';
178
			}
179
		}
180
		$animations=substr($animations, 0, -2); // remove the last ", "
181
182
		if ($speed!='') {
183
			$speed=', '.$speed;
184
		}
185
186
		if ($extra!='') {
187
			$extra=', '.$extra;
188
		}
189
190
		$str="$({$element}).animate({\n$animations\n\t\t}".$speed.$extra.");";
191
192
		if ($immediatly)
193
			$this->jquery_code_for_compile[]=$str;
194
		return $str;
195
	}
196
197
	/**
198
	 * Insert content, specified by the parameter $element, to the end of each element in the set of matched elements $to.
199
	 * @param string $to
200
	 * @param string $element
201
	 * @param boolean $immediatly defers the execution if set to false
202
	 * @return string
203
	 */
204
	public function append($to, $element, $immediatly=false) {
205
		return $this->_genericCallElement('append',$to, $element, $immediatly);
206
	}
207
208
	/**
209
	 * Insert content, specified by the parameter $element, to the beginning of each element in the set of matched elements $to.
210
	 * @param string $to
211
	 * @param string $element
212
	 * @param boolean $immediatly defers the execution if set to false
213
	 * @return string
214
	 */
215
	public function prepend($to, $element, $immediatly=false) {
216
		return $this->_genericCallElement('prepend',$to, $element, $immediatly);
217
	}
218
219
	/**
220
	 * Execute a javascript library hide action
221
	 *
222
	 * @param string - element
223
	 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
224
	 * @param string - Javascript callback function
225
	 * @param boolean $immediatly defers the execution if set to false
226
	 * @return string
227
	 */
228
	public function fadeIn($element='this', $speed='', $callback='', $immediatly=false) {
229
		return $this->_showHideWithEffect("fadeIn",$element,$speed,$callback,$immediatly);
230
	}
231
232
	/**
233
	 * Execute a javascript library hide action
234
	 *
235
	 * @param string - element
236
	 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
237
	 * @param string - Javascript callback function
238
	 * @param boolean $immediatly defers the execution if set to false
239
	 * @return string
240
	 */
241
	public function fadeOut($element='this', $speed='', $callback='', $immediatly=false) {
242
		return $this->_showHideWithEffect("fadeOut",$element,$speed,$callback,$immediatly);
243
	}
244
245
	/**
246
	 * Execute a javascript library slideUp action
247
	 *
248
	 * @param string - element
249
	 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
250
	 * @param string - Javascript callback function
251
	 * @param boolean $immediatly defers the execution if set to false
252
	 * @return string
253
	 */
254
	public function slideUp($element='this', $speed='', $callback='', $immediatly=false) {
255
		return $this->_showHideWithEffect("slideUp",$element,$speed,$callback,$immediatly);
256
	}
257
258
	/**
259
	 * Execute a javascript library removeClass action
260
	 *
261
	 * @param string - element
262
	 * @param string - Class to add
263
	 * @param boolean $immediatly defers the execution if set to false
264
	 * @return string
265
	 */
266
	public function removeClass($element='this', $class='', $immediatly=false) {
267
		return $this->_genericCall('removeClass',$element, $class, $immediatly);
0 ignored issues
show
Bug introduced by
The method _genericCall() does not exist on Ajax\common\traits\JsUtilsActionsTrait. Did you maybe mean _genericCallValue()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
268
	}
269
270
	/**
271
	 * Execute a javascript library slideDown action
272
	 *
273
	 * @param string - element
274
	 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
275
	 * @param string - Javascript callback function
276
	 * @param boolean $immediatly defers the execution if set to false
277
	 * @return string
278
	 */
279
	public function slideDown($element='this', $speed='', $callback='', $immediatly=false) {
280
		return $this->_showHideWithEffect("slideDown",$element,$speed,$callback,$immediatly);
281
	}
282
283
	/**
284
	 * Execute a javascript library slideToggle action
285
	 *
286
	 * @param string - element
287
	 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
288
	 * @param string - Javascript callback function
289
	 * @param boolean $immediatly defers the execution if set to false
290
	 * @return string
291
	 */
292
	public function slideToggle($element='this', $speed='', $callback='', $immediatly=false) {
293
		return $this->_showHideWithEffect("slideToggle",$element,$speed,$callback,$immediatly);
294
	}
295
296
	/**
297
	 * Execute a javascript library hide action
298
	 *
299
	 * @param string - element
300
	 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
301
	 * @param string - Javascript callback function
302
	 * @param boolean $immediatly defers the execution if set to false
303
	 * @return string
304
	 */
305
	public function hide($element='this', $speed='', $callback='', $immediatly=false) {
306
		return $this->_showHideWithEffect("hide",$element,$speed,$callback,$immediatly);
307
	}
308
309
	/**
310
	 * Execute a javascript library toggle action
311
	 *
312
	 * @param string - element
313
	 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
314
	 * @param string - Javascript callback function
315
	 * @param boolean $immediatly defers the execution if set to false
316
	 * @return string
317
	 */
318
	public function toggle($element='this', $speed='', $callback='', $immediatly=false) {
319
		return $this->_showHideWithEffect("toggle",$element,$speed,$callback,$immediatly);
320
	}
321
322
	/**
323
	 * Execute a javascript library toggle class action
324
	 *
325
	 * @param string - element
326
	 * @param boolean $immediatly defers the execution if set to false
327
	 * @return string
328
	 */
329
	public function toggleClass($element='this', $class='', $immediatly=false) {
330
		return $this->_genericCallValue('toggleClass',$element, $class, $immediatly);
331
	}
332
333
	/**
334
	 * Execute all handlers and behaviors attached to the matched elements for the given event.
335
	 * @param string $element
336
	 * @param string $event
337
	 * @param boolean $immediatly defers the execution if set to false
338
	 */
339
	public function trigger($element='this', $event='click', $immediatly=false) {
340
		$element=Javascript::prep_element($element);
341
		$str="$({$element}).trigger(\"$event\");";
342
343
		if ($immediatly)
344
			$this->jquery_code_for_compile[]=$str;
345
		return $str;
346
	}
347
348
	/**
349
	 * Execute a javascript library show action
350
	 *
351
	 * @param string - element
352
	 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
353
	 * @param string - Javascript callback function
354
	 * @param boolean $immediatly defers the execution if set to false
355
	 * @return string
356
	 */
357
	public function show($element='this', $speed='', $callback='', $immediatly=false) {
358
		return $this->_showHideWithEffect("show",$element,$speed,$callback,$immediatly);
359
	}
360
361
	/**
362
	 * Creates a jQuery sortable
363
	 *
364
	 * @param string $element
365
	 * @param array $options
366
	 * @return void
367
	 */
368
	public function sortable($element, $options=array()) {
369
		if (count($options)>0) {
370
			$sort_options=array ();
371
			foreach ( $options as $k => $v ) {
372
				$sort_options[]="\n\t\t".$k.': '.$v."";
373
			}
374
			$sort_options=implode(",", $sort_options);
375
		} else {
376
			$sort_options='';
377
		}
378
379
		return "$(".Javascript::prep_element($element).").sortable({".$sort_options."\n\t});";
380
	}
381
382
	/**
383
	 * Table Sorter Plugin
384
	 *
385
	 * @param string $table table name
386
	 * @param string $options plugin location
387
	 * @return string
388
	 */
389
	public function tablesorter($table='', $options='') {
390
		$this->jquery_code_for_compile[]="\t$(".Javascript::prep_element($table).").tablesorter($options);\n";
391
	}
392
393
	/**
394
	 * Allows to attach a condition
395
	 * @param string $condition
396
	 * @param string $jsCodeIfTrue
397
	 * @param string $jsCodeIfFalse
398
	 * @param boolean $immediatly defers the execution if set to false
399
	 */
400
	public function condition($condition, $jsCodeIfTrue, $jsCodeIfFalse=null, $immediatly=false) {
401
		$str="if(".$condition."){".$jsCodeIfTrue."}";
402
		if (isset($jsCodeIfFalse)) {
403
			$str.="else{".$jsCodeIfFalse."}";
404
		}
405
406
		if ($immediatly)
407
			$this->jquery_code_for_compile[]=$str;
408
		return $str;
409
	}
410
411
	/**
412
	 * Call the JQuery method $jqueryCall on $element with parameters $param
413
	 * @param string $element
414
	 * @param string $jqueryCall
415
	 * @param mixed $param
416
	 * @param string $jsCallback javascript code to execute after the jquery call
417
	 * @param boolean $immediatly
418
	 * @return string
419
	 */
420
	private function _doJQuery($element, $jqueryCall, $param="", $jsCallback="", $immediatly=false) {
421
		$param=Javascript::prep_value($param);
422
		$callback="";
423
		if ($jsCallback!="")
424
			$callback=", function(event){\n{$jsCallback}\n}";
425
			$script="$(".Javascript::prep_element($element).").".$jqueryCall."(".$param.$callback.");\n";
426
			if ($immediatly)
427
				$this->jquery_code_for_compile[]=$script;
428
				return $script;
429
	}
430
431
	/**
432
	 * Calls the JQuery callback $someThing on $element with facultative parameter $param
433
	 * @param string $element the element
434
	 * @param string $jqueryCall the JQuery callback
435
	 * @param mixed $param array or string parameters
436
	 * @param string $jsCallback javascript code to execute after the jquery call
437
	 * @return mixed
438
	 */
439
	public function doJQuery($element, $jqueryCall, $param="", $jsCallback="") {
440
		return $this->_doJQuery($element, $jqueryCall, $param, $jsCallback, true);
441
	}
442
443
	/**
444
	 * Calls the JQuery callback $someThing on $element with facultative parameter $param
445
	 * @param string $element the element
446
	 * @param string $jqueryCall the JQuery callback
447
	 * @param mixed $param array or string parameters
448
	 * @param string $jsCallback javascript code to execute after the jquery call
449
	 * @return mixed
450
	 */
451
	public function doJQueryDeferred($element, $jqueryCall, $param="", $jsCallback="") {
452
		return $this->_doJQuery($element, $jqueryCall, $param, $jsCallback, false);
453
	}
454
455
	/**
456
	 *
457
	 * @param string $event
458
	 * @param string $element
459
	 * @param string $elementToModify
460
	 * @param string $jqueryCall
461
	 * @param string|array $param
462
	 * @param boolean $preventDefault
463
	 * @param boolean $stopPropagation
464
	 * @param string $jsCallback javascript code to execute after the jquery call
465
	 * @param boolean $immediatly
466
	 * @return string
467
	 */
468
	private function _doJQueryOn($event, $element, $elementToModify, $jqueryCall, $param="", $preventDefault=false, $stopPropagation=false, $jsCallback="",$immediatly=true) {
469
		return $this->_add_event($element, $this->_doJQuery($elementToModify, $jqueryCall, $param, $jsCallback), $event, $preventDefault, $stopPropagation,$immediatly);
470
	}
471
472
	/**
473
	 * Calls the JQuery callback $jqueryCall on $element with facultative parameter $param in response to an event $event
474
	 * @param string $event
475
	 * @param string $element
476
	 * @param string $elementToModify
477
	 * @param string $jqueryCall
478
	 * @param string $param
479
	 * @param array $parameters default : array("preventDefault"=>false,"stopPropagation"=>false,"jsCallback"=>'',"immediatly"=>true)
480
	 */
481
	public function doJQueryOn($event, $element, $elementToModify, $jqueryCall, $param="", $parameters=array()) {
482
		$jsCallback="";
483
		$stopPropagation=false;
484
		$preventDefault=false;
485
		$immediatly=true;
486
		extract($parameters);
487
		return $this->_doJQueryOn($event, $element, $elementToModify, $jqueryCall, $param, $preventDefault, $stopPropagation, $jsCallback,$immediatly);
488
	}
489
490
	/**
491
	 * Executes the code $js
492
	 * @param string $js Code to execute
493
	 * @param boolean $immediatly delayed if false
494
	 * @return String
495
	 */
496
	public function exec($js, $immediatly=false) {
497
		$script=$js."\n";
498
		if ($immediatly)
499
			$this->jquery_code_for_compile[]=$script;
500
		return $script;
501
	}
502
503
	/**
504
	 * Executes the javascript code $js when $event fires on $element
505
	 * @param string $event
506
	 * @param string $element
507
	 * @param string $js Code to execute
508
	 * @param array $parameters default : array("preventDefault"=>false,"stopPropagation"=>false,"immediatly"=>true)
509
	 * @return String
510
	 */
511
	public function execOn($event, $element, $js, $parameters=array()) {
512
		$stopPropagation=false;
513
		$preventDefault=false;
514
		$immediatly=true;
515
		extract($parameters);
516
		$script=$this->_add_event($element, $this->exec($js), $event, $preventDefault, $stopPropagation,$immediatly);
517
		return $script;
518
	}
519
}
520