Passed
Push — master ( 238ca7...e7f105 )
by Jean-Christophe
04:32
created

Javascript::dropZone()   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 2
dl 0
loc 2
rs 10
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",$(event.target).attr("'.$attr.'"));';
11
	}
12
	
13
	public static function dropZone($jqueryDone,$jsCallback=""){
14
		return 'var dt=event.dataTransfer || event.originalEvent.dataTransfer;var data=dt.getData("Text");$(event.target).'.$jqueryDone.'($("#"+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
	/**
22
	 * Puts HTML element in quotes for use in jQuery code
23
	 * unless the supplied element is the Javascript 'this'
24
	 * object, in which case no quotes are added
25
	 *
26
	 * @param string $element
27
	 * @return string
28
	 */
29
	public static function prep_element($element) {
30
		if (self::containsCode($element)===false) {
31
			$element='"'.addslashes($element).'"';
32
		}
33
		return $element;
34
	}
35
36
	/**
37
	 * Puts HTML values in quotes for use in jQuery code
38
	 * unless the supplied value contains the Javascript 'this' or 'event'
39
	 * object, in which case no quotes are added
40
	 *
41
	 * @param string $value
42
	 * @return string
43
	 */
44
	public static function prep_value($value) {
45
		if (\is_array($value)) {
0 ignored issues
show
introduced by
The condition is_array($value) is always false.
Loading history...
46
			$value=implode(",", $value);
47
		}
48
		if (self::containsCode($value)===false) {
49
			$value=\str_replace(["\\","\""], ["\\\\","\\\""], $value);
50
			$value='"'.$value.'"';
51
		}
52
		return $value;
53
	}
54
55
	public static function prep_jquery_selector($value){
56
		if(JString::startswith($value, '$(')===false){
57
			return '$('.$value.')';
58
		}
59
		return $value;
60
	}
61
}
62