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)) { |
|
|
|
|
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
|
|
|
|