Completed
Push — master ( cfc18c...89eac8 )
by Jean-Christophe
03:42
created

JsUtilsActionsTrait::doJQuery()   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 4
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
20
	 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
21
	 * @param string - 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 View Code Duplication
	public function _genericCallValue($jQueryCall,$element='this', $param="", $immediatly=false) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 View Code Duplication
	public function attr($element='this', $attributeName, $value="", $immediatly=false) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 $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
224
	 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
225
	 * @param string - 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
237
	 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
238
	 * @param string - 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
250
	 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
251
	 * @param string - 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
263
	 * @param string - 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->_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...
269
	}
270
271
	/**
272
	 * Execute a javascript library slideDown action
273
	 *
274
	 * @param string - element
275
	 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
276
	 * @param string - 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
288
	 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
289
	 * @param string - 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
301
	 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
302
	 * @param string - 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
314
	 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
315
	 * @param string - 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
327
	 * @param boolean $immediatly defers the execution if set to false
328
	 * @return string
329
	 */
330
	public function toggleClass($element='this', $class='', $immediatly=false) {
331
		return $this->_genericCallValue('toggleClass',$element, $class, $immediatly);
332
	}
333
334
	/**
335
	 * Execute all handlers and behaviors attached to the matched elements for the given event.
336
	 * @param string $element
337
	 * @param string $event
338
	 * @param boolean $immediatly defers the execution if set to false
339
	 */
340
	public function trigger($element='this', $event='click', $immediatly=false) {
341
		$element=Javascript::prep_element($element);
342
		$str="$({$element}).trigger(\"$event\");";
343
344
		if ($immediatly)
345
			$this->jquery_code_for_compile[]=$str;
346
		return $str;
347
	}
348
349
	/**
350
	 * Execute a javascript library show action
351
	 *
352
	 * @param string - element
353
	 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
354
	 * @param string - Javascript callback function
355
	 * @param boolean $immediatly defers the execution if set to false
356
	 * @return string
357
	 */
358
	public function show($element='this', $speed='', $callback='', $immediatly=false) {
359
		return $this->_showHideWithEffect("show",$element,$speed,$callback,$immediatly);
360
	}
361
362
	/**
363
	 * Creates a jQuery sortable
364
	 *
365
	 * @param string $element
366
	 * @param array $options
367
	 * @return void
368
	 */
369
	public function sortable($element, $options=array()) {
370
		if (count($options)>0) {
371
			$sort_options=array ();
372
			foreach ( $options as $k => $v ) {
373
				$sort_options[]="\n\t\t".$k.': '.$v."";
374
			}
375
			$sort_options=implode(",", $sort_options);
376
		} else {
377
			$sort_options='';
378
		}
379
380
		return "$(".Javascript::prep_element($element).").sortable({".$sort_options."\n\t});";
381
	}
382
383
	/**
384
	 * Table Sorter Plugin
385
	 *
386
	 * @param string $table table name
387
	 * @param string $options plugin location
388
	 * @return string
389
	 */
390
	public function tablesorter($table='', $options='') {
391
		$this->jquery_code_for_compile[]="\t$(".Javascript::prep_element($table).").tablesorter($options);\n";
392
	}
393
394
	/**
395
	 * Allows to attach a condition
396
	 * @param string $condition
397
	 * @param string $jsCodeIfTrue
398
	 * @param string $jsCodeIfFalse
399
	 * @param boolean $immediatly defers the execution if set to false
400
	 */
401
	public function condition($condition, $jsCodeIfTrue, $jsCodeIfFalse=null, $immediatly=false) {
402
		$str="if(".$condition."){".$jsCodeIfTrue."}";
403
		if (isset($jsCodeIfFalse)) {
404
			$str.="else{".$jsCodeIfFalse."}";
405
		}
406
407
		if ($immediatly)
408
			$this->jquery_code_for_compile[]=$str;
409
		return $str;
410
	}
411
412
	/**
413
	 * Call the JQuery method $jqueryCall on $element with parameters $param
414
	 * @param string $element
415
	 * @param string $jqueryCall
416
	 * @param mixed $param
417
	 * @param string $jsCallback javascript code to execute after the jquery call
418
	 * @param boolean $immediatly
419
	 * @return string
420
	 */
421
	private function _doJQuery($element, $jqueryCall, $param="", $jsCallback="", $immediatly=false) {
422
		$param=Javascript::prep_value($param);
423
		$callback="";
424
		if ($jsCallback!="")
425
			$callback=", function(event){\n{$jsCallback}\n}";
426
			$script="$(".Javascript::prep_element($element).").".$jqueryCall."(".$param.$callback.");\n";
427
			if ($immediatly)
428
				$this->jquery_code_for_compile[]=$script;
429
				return $script;
430
	}
431
432
	/**
433
	 * Calls the JQuery callback $someThing on $element with facultative parameter $param
434
	 * @param string $element the element
435
	 * @param string $jqueryCall the JQuery callback
436
	 * @param mixed $param array or string parameters
437
	 * @param string $jsCallback javascript code to execute after the jquery call
438
	 * @return mixed
439
	 */
440
	public function doJQuery($element, $jqueryCall, $param="", $jsCallback="") {
441
		return $this->_doJQuery($element, $jqueryCall, $param, $jsCallback, true);
442
	}
443
444
	/**
445
	 * Calls the JQuery callback $someThing on $element with facultative parameter $param
446
	 * @param string $element the element
447
	 * @param string $jqueryCall the JQuery callback
448
	 * @param mixed $param array or string parameters
449
	 * @param string $jsCallback javascript code to execute after the jquery call
450
	 * @return mixed
451
	 */
452
	public function doJQueryDeferred($element, $jqueryCall, $param="", $jsCallback="") {
453
		return $this->_doJQuery($element, $jqueryCall, $param, $jsCallback, false);
454
	}
455
456
	/**
457
	 *
458
	 * @param string $event
459
	 * @param string $element
460
	 * @param string $elementToModify
461
	 * @param string $jqueryCall
462
	 * @param string|array $param
463
	 * @param boolean $preventDefault
464
	 * @param boolean $stopPropagation
465
	 * @param string $jsCallback javascript code to execute after the jquery call
466
	 * @param boolean $immediatly
467
	 * @return string
468
	 */
469
	private function _doJQueryOn($event, $element, $elementToModify, $jqueryCall, $param="", $preventDefault=false, $stopPropagation=false, $jsCallback="",$immediatly=true) {
470
		return $this->_add_event($element, $this->_doJQuery($elementToModify, $jqueryCall, $param, $jsCallback), $event, $preventDefault, $stopPropagation,$immediatly);
471
	}
472
473
	/**
474
	 * Calls the JQuery callback $jqueryCall on $element with facultative parameter $param in response to an event $event
475
	 * @param string $event
476
	 * @param string $element
477
	 * @param string $elementToModify
478
	 * @param string $jqueryCall
479
	 * @param string $param
480
	 * @param array $parameters default : array("preventDefault"=>false,"stopPropagation"=>false,"jsCallback"=>'',"immediatly"=>true)
481
	 */
482
	public function doJQueryOn($event, $element, $elementToModify, $jqueryCall, $param="", $parameters=array()) {
483
		$jsCallback="";
484
		$stopPropagation=false;
485
		$preventDefault=false;
486
		$immediatly=true;
487
		extract($parameters);
488
		return $this->_doJQueryOn($event, $element, $elementToModify, $jqueryCall, $param, $preventDefault, $stopPropagation, $jsCallback,$immediatly);
489
	}
490
491
	/**
492
	 * Executes the code $js
493
	 * @param string $js Code to execute
494
	 * @param boolean $immediatly delayed if false
495
	 * @return String
496
	 */
497
	public function exec($js, $immediatly=false) {
498
		$script=$js."\n";
499
		if ($immediatly)
500
			$this->jquery_code_for_compile[]=$script;
501
		return $script;
502
	}
503
504
	/**
505
	 * Executes the code $js
506
	 * @param string $js Code to execute
507
	 * @param boolean $immediatly delayed if false
0 ignored issues
show
Bug introduced by
There is no parameter named $immediatly. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
508
	 * @return String
509
	 */
510
	public function execAtLast($js) {
511
		$script=$js."\n";
512
		$this->jquery_code_for_compile_at_last[]=$script;
513
		return $script;
514
	}
515
516
	/**
517
	 * Executes the javascript code $js when $event fires on $element
518
	 * @param string $event
519
	 * @param string $element
520
	 * @param string $js Code to execute
521
	 * @param array $parameters default : array("preventDefault"=>false,"stopPropagation"=>false,"immediatly"=>true)
522
	 * @return String
523
	 */
524
	public function execOn($event, $element, $js, $parameters=array()) {
525
		$stopPropagation=false;
526
		$preventDefault=false;
527
		$immediatly=true;
528
		extract($parameters);
529
		$script=$this->_add_event($element, $this->exec($js), $event, $preventDefault, $stopPropagation,$immediatly);
530
		return $script;
531
	}
532
}
533