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

Javascript::prep_jquery_selector()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Ajax\service;
4
5
class Javascript {
6
	public static $preventDefault="\nif(event && event.preventDefault) event.preventDefault();\n";
7
	public static $stopPropagation="\nif(event && event.stopPropagation) event.stopPropagation();\n";
8
9
	public static function draggable($attr="id"){
10
		return 'var dt=event.dataTransfer || event.originalEvent.dataTransfer;dt.setData("text/plain",JSON.stringify({id:$(event.target).attr("id"),data:$(event.target).attr("'.$attr.'")}));';
11
	}
12
	
13
	public static function dropZone($jqueryDone,$jsCallback=""){
14
		return 'var dt=event.dataTransfer || event.originalEvent.dataTransfer;var _data=JSON.parse(dt.getData("text/plain"));$(event.target).'.$jqueryDone.'($("#"+_data.id));var data=_data.data;'.$jsCallback;
15
	}
16
	
17
	public static function containsCode($expression){
18
		return strrpos($expression, 'this')!==false||strrpos($expression, 'event')!==false||strrpos($expression, 'self')!==false;
19
	}
20
	
21
	public static function isFunction($jsCode){
22
		return JString::startswith($jsCode, "function");
23
	}
24
25
	/**
26
	 * Puts HTML element in quotes for use in jQuery code
27
	 * unless the supplied element is the Javascript 'this'
28
	 * object, in which case no quotes are added
29
	 *
30
	 * @param string $element
31
	 * @return string
32
	 */
33
	public static function prep_element($element) {
34
		if (self::containsCode($element)===false) {
35
			$element='"'.addslashes($element).'"';
36
		}
37
		return $element;
38
	}
39
40
	/**
41
	 * Puts HTML values in quotes for use in jQuery code
42
	 * unless the supplied value contains the Javascript 'this' or 'event'
43
	 * object, in which case no quotes are added
44
	 *
45
	 * @param string $value
46
	 * @return string
47
	 */
48
	public static function prep_value($value) {
49
		if (\is_array($value)) {
0 ignored issues
show
introduced by
The condition is_array($value) is always false.
Loading history...
50
			$value=implode(",", $value);
51
		}
52
		if (self::containsCode($value)===false) {
53
			$value=\str_replace(["\\","\""], ["\\\\","\\\""], $value);
54
			$value='"'.$value.'"';
55
		}
56
		return trim($value,"%");
57
	}
58
59
	public static function prep_jquery_selector($value){
60
		if(JString::startswith($value, '$(')===false){
61
			return '$('.self::prep_value($value).')';
62
		}
63
		return $value;
64
	}
65
}
66