Completed
Push — master ( e37d9a...63bf31 )
by Jean-Christophe
03:30
created

JqueryActionsTrait::_toggle()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 6

Duplication

Lines 8
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 8
loc 8
rs 9.4285
cc 2
eloc 6
nc 2
nop 2
1
<?php
2
3
namespace Ajax\common\traits;
4
5
trait JqueryActionsTrait {
6
	public abstract function _prep_element($element);
7
	public abstract function _prep_value($value);
8
9
	/**
10
	 * 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.
11
	 * @param string $element
12
	 * @param string $attributeName
13
	 * @param string $value
14
	 * @param boolean $immediatly delayed if false
15
	 */
16 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...
17
		$element=$this->_prep_element($element);
18
		if (isset($value)) {
19
			$value=$this->_prep_value($value);
20
			$str="$({$element}).attr(\"$attributeName\",{$value});";
21
		} else
22
			$str="$({$element}).attr(\"$attributeName\");";
23
			if ($immediatly)
24
				$this->jquery_code_for_compile[]=$str;
0 ignored issues
show
Bug introduced by
The property jquery_code_for_compile does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
25
				return $str;
26
	}
27
28
	/**
29
	 * Insert content, specified by the parameter, after each element in the set of matched elements
30
	 * @param string $element
31
	 * @param string $value
32
	 * @param boolean $immediatly defers the execution if set to false
33
	 * @return string
34
	 */
35
	public function after($element='this', $value='', $immediatly=false){
36
		$element=$this->_prep_element($element);
37
		$value=$this->_prep_value($value);
38
		$str="$({$element}).after({$value});";
39
		if ($immediatly)
40
			$this->jquery_code_for_compile[]=$str;
41
			return $str;
42
	}
43
44
	/**
45
	 * Execute a jQuery animate action
46
	 *
47
	 * @param string $element element
48
	 * @param string|array $params One of 'slow', 'normal', 'fast', or time in milliseconds
49
	 * @param string $speed
50
	 * @param string $extra
51
	 * @param boolean $immediatly delayed if false
52
	 * @return string
53
	 */
54
	public function _animate($element='this', $params=array(), $speed='', $extra='', $immediatly=false) {
55
		$element=$this->_prep_element($element);
56
		$speed=$this->_validate_speed($speed);
57
58
		$animations="\t\t\t";
59
		if (is_array($params)) {
60
			foreach ( $params as $param => $value ) {
61
				$animations.=$param.': \''.$value.'\', ';
62
			}
63
		}
64
		$animations=substr($animations, 0, -2); // remove the last ", "
65
66
		if ($speed!='') {
67
			$speed=', '.$speed;
68
		}
69
70
		if ($extra!='') {
71
			$extra=', '.$extra;
72
		}
73
74
		$str="$({$element}).animate({\n$animations\n\t\t}".$speed.$extra.");";
75
76
		if ($immediatly)
77
			$this->jquery_code_for_compile[]=$str;
78
			return $str;
79
	}
80
81
	// --------------------------------------------------------------------
82
83
	/**
84
	 * Execute a jQuery hide action
85
	 *
86
	 * @param string $element element
87
	 * @param string $speed One of 'slow', 'normal', 'fast', or time in milliseconds
88
	 * @param string $callback Javascript callback function
89
	 * @param boolean $immediatly delayed if false
90
	 * @return string
91
	 */
92 View Code Duplication
	public function _fadeIn($element='this', $speed='', $callback='', $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...
93
		$element=$this->_prep_element($element);
94
		$speed=$this->_validate_speed($speed);
95
96
		if ($callback!='') {
97
			$callback=", function(){\n{$callback}\n}";
98
		}
99
100
		$str="$({$element}).fadeIn({$speed}{$callback});";
101
102
		if ($immediatly)
103
			$this->jquery_code_for_compile[]=$str;
104
			return $str;
105
	}
106
107
	// --------------------------------------------------------------------
108
109
	/**
110
	 * Execute a jQuery fadeOut action
111
	 *
112
	 * @param string $element element
113
	 * @param string $speed One of 'slow', 'normal', 'fast', or time in milliseconds
114
	 * @param string $callback Javascript callback function
115
	 * @param boolean $immediatly delayed if false
116
	 * @return string
117
	 */
118 View Code Duplication
	public function _fadeOut($element='this', $speed='', $callback='', $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...
119
		$element=$this->_prep_element($element);
120
		$speed=$this->_validate_speed($speed);
121
122
		if ($callback!='') {
123
			$callback=", function(){\n{$callback}\n}";
124
		}
125
126
		$str="$({$element}).fadeOut({$speed}{$callback});";
127
128
		if ($immediatly)
129
			$this->jquery_code_for_compile[]=$str;
130
			return $str;
131
	}
132
133
	// --------------------------------------------------------------------
134
135
	/**
136
	 * Execute a jQuery hide action
137
	 *
138
	 * @param string $element element
139
	 * @param string $speed One of 'slow', 'normal', 'fast', or time in milliseconds
140
	 * @param string $callback Javascript callback function
141
	 * @param boolean $immediatly delayed if false
142
	 * @return string
143
	 */
144 View Code Duplication
	public function _hide($element='this', $speed='', $callback='', $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...
145
		$element=$this->_prep_element($element);
146
		$speed=$this->_validate_speed($speed);
147
148
		if ($callback!='') {
149
			$callback=", function(){\n{$callback}\n}";
150
		}
151
152
		$str="$({$element}).hide({$speed}{$callback});";
153
154
		if ($immediatly)
155
			$this->jquery_code_for_compile[]=$str;
156
			return $str;
157
	}
158
159
	// --------------------------------------------------------------------
160
161
	// --------------------------------------------------------------------
162
163
	/**
164
	 * Execute a jQuery slideUp action
165
	 *
166
	 * @param string $element element
167
	 * @param string $speed One of 'slow', 'normal', 'fast', or time in milliseconds
168
	 * @param string $callback Javascript callback function
169
	 * @param boolean $immediatly delayed if false
170
	 * @return string
171
	 */
172 View Code Duplication
	public function _slideUp($element='this', $speed='', $callback='', $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...
173
		$element=$this->_prep_element($element);
174
		$speed=$this->_validate_speed($speed);
175
176
		if ($callback!='') {
177
			$callback=", function(){\n{$callback}\n}";
178
		}
179
180
		$str="$({$element}).slideUp({$speed}{$callback});";
181
182
		if ($immediatly)
183
			$this->jquery_code_for_compile[]=$str;
184
			return $str;
185
	}
186
187
	// --------------------------------------------------------------------
188
189
	/**
190
	 * Execute a jQuery slideDown action
191
	 *
192
	 * @param string $element element
193
	 * @param string $speed One of 'slow', 'normal', 'fast', or time in milliseconds
194
	 * @param string $callback Javascript callback function
195
	 * @param boolean $immediatly delayed if false
196
	 * @return string
197
	 */
198 View Code Duplication
	public function _slideDown($element='this', $speed='', $callback='', $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...
199
		$element=$this->_prep_element($element);
200
		$speed=$this->_validate_speed($speed);
201
202
		if ($callback!='') {
203
			$callback=", function(){\n{$callback}\n}";
204
		}
205
206
		$str="$({$element}).slideDown({$speed}{$callback});";
207
208
		if ($immediatly)
209
			$this->jquery_code_for_compile[]=$str;
210
			return $str;
211
	}
212
213
	// --------------------------------------------------------------------
214
215
	/**
216
	 * Execute a jQuery slideToggle action
217
	 *
218
	 * @param string $element element
219
	 * @param string $speed One of 'slow', 'normal', 'fast', or time in milliseconds
220
	 * @param string $callback Javascript callback function
221
	 * @param boolean $immediatly delayed if false
222
	 * @return string
223
	 */
224 View Code Duplication
	public function _slideToggle($element='this', $speed='', $callback='', $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...
225
		$element=$this->_prep_element($element);
226
		$speed=$this->_validate_speed($speed);
227
228
		if ($callback!='') {
229
			$callback=", function(){\n{$callback}\n}";
230
		}
231
232
		$str="$({$element}).slideToggle({$speed}{$callback});";
233
234
		if ($immediatly)
235
			$this->jquery_code_for_compile[]=$str;
236
			return $str;
237
	}
238
239
	// --------------------------------------------------------------------
240
241
	/**
242
	 * Outputs a jQuery toggle event
243
	 *
244
	 * @param string $element element
245
	 * @param boolean $immediatly delayed if false
246
	 * @return string
247
	 */
248 View Code Duplication
	public function _toggle($element='this', $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...
249
		$element=$this->_prep_element($element);
250
		$str="$({$element}).toggle();";
251
252
		if ($immediatly)
253
			$this->jquery_code_for_compile[]=$str;
254
			return $str;
255
	}
256
257
	// --------------------------------------------------------------------
258
259
	/**
260
	 * Execute all handlers and behaviors attached to the matched elements for the given event.
261
	 * @param string $element
262
	 * @param string $event
263
	 * @param boolean $immediatly delayed if false
264
	 */
265 View Code Duplication
	public function _trigger($element='this', $event='click', $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...
266
		$element=$this->_prep_element($element);
267
		$str="$({$element}).trigger(\"$event\");";
268
269
		if ($immediatly)
270
			$this->jquery_code_for_compile[]=$str;
271
			return $str;
272
	}
273
274
	// --------------------------------------------------------------------
275
276
	/**
277
	 * Execute a jQuery show action
278
	 *
279
	 * @param string $element element
280
	 * @param string $speed One of 'slow', 'normal', 'fast', or time in milliseconds
281
	 * @param string $callback Javascript callback function
282
	 * @param boolean $immediatly delayed if false
283
	 * @return string
284
	 */
285 View Code Duplication
	public function _show($element='this', $speed='', $callback='', $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...
286
		$element=$this->_prep_element($element);
287
		$speed=$this->_validate_speed($speed);
288
289
		if ($callback!='') {
290
			$callback=", function(){\n{$callback}\n}";
291
		}
292
293
		$str="$({$element}).show({$speed}{$callback});";
294
295
		if ($immediatly)
296
			$this->jquery_code_for_compile[]=$str;
297
			return $str;
298
	}
299
300
	/**
301
	 * Places a condition
302
	 * @param string $condition
303
	 * @param string $jsCodeIfTrue
304
	 * @param string $jsCodeIfFalse
305
	 * @param boolean $immediatly delayed if false
306
	 * @return string
307
	 */
308
	public function _condition($condition, $jsCodeIfTrue, $jsCodeIfFalse=null, $immediatly=false) {
309
		$str="if(".$condition."){".$jsCodeIfTrue."}";
310
		if (isset($jsCodeIfFalse)) {
311
			$str.="else{".$jsCodeIfFalse."}";
312
		}
313
314
		if ($immediatly)
315
			$this->jquery_code_for_compile[]=$str;
316
			return $str;
317
	}
318
319
	// ------------------------------------------------------------------------
320
	/**
321
	 * Call the JQuery method $jqueryCall on $element with parameters $param
322
	 * @param string $element
323
	 * @param string $jqueryCall
324
	 * @param mixed $param
325
	 * @param string $jsCallback javascript code to execute after the jquery call
326
	 * @param boolean $immediatly
327
	 * @return string
328
	 */
329
	public function _doJQuery($element, $jqueryCall, $param="", $jsCallback="", $immediatly=false) {
330
		$param=$this->_prep_value($param);
331
		$callback="";
332
		if ($jsCallback!="")
333
			$callback=", function(event){\n{$jsCallback}\n}";
334
			$script="$(".$this->_prep_element($element).").".$jqueryCall."(".$param.$callback.");\n";
335
			if ($immediatly)
336
				$this->jquery_code_for_compile[]=$script;
337
				return $script;
338
	}
339
340
	/**
341
	 *
342
	 * @param string $event
343
	 * @param string $element
344
	 * @param string $elementToModify
345
	 * @param string $jqueryCall
346
	 * @param string|array $param
347
	 * @param boolean $preventDefault
348
	 * @param boolean $stopPropagation
349
	 * @param string $jsCallback javascript code to execute after the jquery call
350
	 * @param boolean $immediatly
351
	 * @return string
352
	 */
353
	public function _doJQueryOn($event, $element, $elementToModify, $jqueryCall, $param="", $preventDefault=false, $stopPropagation=false, $jsCallback="",$immediatly=true) {
354
		return $this->_add_event($element, $this->_doJQuery($elementToModify, $jqueryCall, $param, $jsCallback), $event, $preventDefault, $stopPropagation,$immediatly);
0 ignored issues
show
Bug introduced by
It seems like _add_event() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
355
	}
356
357
	/**
358
	 * Execute the code $js
359
	 * @param string $js Code to execute
360
	 * @param boolean $immediatly diffère l'exécution si false
361
	 * @return String
362
	 */
363
	public function _exec($js, $immediatly=false) {
364
		$script=$js."\n";
365
		if ($immediatly)
366
			$this->jquery_code_for_compile[]=$script;
367
			return $script;
368
	}
369
370
	/**
371
	 *
372
	 * @param string $element
373
	 * @param string $event
374
	 * @param string $js Code to execute
375
	 * @param boolean $preventDefault
376
	 * @param boolean $stopPropagation
377
	 * @param boolean $immediatly
378
	 * @return String
379
	 */
380
	public function _execOn($element, $event, $js, $preventDefault=false, $stopPropagation=false,$immediatly=true) {
381
		return $this->_add_event($element, $this->_exec($js), $event, $preventDefault, $stopPropagation,$immediatly);
0 ignored issues
show
Bug introduced by
It seems like _add_event() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
382
	}
383
384
	/**
385
	 * Ensures the speed parameter is valid for jQuery
386
	 * @param string|int $speed
387
	 * @return string
388
	 */
389
	private function _validate_speed($speed) {
390
		if (in_array($speed, array (
391
				'slow','normal','fast'
392
		))) {
393
			$speed='"'.$speed.'"';
394
		} elseif (preg_match("/[^0-9]/", $speed)) {
395
			$speed='';
396
		}
397
398
		return $speed;
399
	}
400
}