1
|
|
|
<?php |
2
|
|
|
namespace Ajax\service; |
3
|
|
|
|
4
|
|
|
class Javascript { |
5
|
|
|
|
6
|
|
|
public static $preventDefault = "\nif(event && event.preventDefault) event.preventDefault();\n"; |
7
|
|
|
|
8
|
|
|
public static $stopPropagation = "\nif(event && event.stopPropagation) event.stopPropagation();\n"; |
9
|
|
|
|
10
|
|
|
public static function draggable($attr = "id") { |
11
|
|
|
return 'var dt=event.dataTransfer || event.originalEvent.dataTransfer;dt.setData("text/plain",JSON.stringify({id:$(event.target).attr("id"),data:$(event.target).attr("' . $attr . '")}));'; |
12
|
|
|
} |
13
|
|
|
|
14
|
|
|
public static function dropZone($jqueryDone, $jsCallback = "") { |
15
|
|
|
$done = ($jqueryDone != null) ? '$(event.target).' . $jqueryDone . '($("#"+_data.id));' : ''; |
16
|
|
|
return 'var dt=event.dataTransfer || event.originalEvent.dataTransfer;var _data=JSON.parse(dt.getData("text/plain"));' . $done . 'var data=_data.data;' . $jsCallback; |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
public static function fileDropZone($jsCallback = "") { |
20
|
|
|
$done = 'event.target.upload=formData;$(event.target).trigger("upload");'; |
21
|
|
|
return 'var dt=event.dataTransfer || event.originalEvent.dataTransfer;var files=dt.files;var formData = new FormData();for (var i = 0; i < files.length; i++) {formData.append("file-"+i,files[i]);}' . $done . $jsCallback; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public static function containsCode($expression) { |
25
|
|
|
return \strrpos($expression, 'this') !== false || \strrpos($expression, 'event') !== false || \strrpos($expression, 'self') !== false; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public static function isFunction($jsCode) { |
29
|
|
|
return JString::startswith($jsCode, "function"); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public static function fileUploadBehavior($id = '') { |
33
|
|
|
return "$('input:text, .ui.button', '#{$id}').on('click', function (e) {e.preventDefault();\$('input:file', '#{$id}').click();}); |
34
|
|
|
$('input:file', '#{$id}').on('change', function (e) {if(e.target.files.length){var name = e.target.files[0].name;$('input:text', $(e.target).parent()).val(name);}});"; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Puts HTML element in quotes for use in jQuery code |
39
|
|
|
* unless the supplied element is the Javascript 'this' |
40
|
|
|
* object, in which case no quotes are added |
41
|
|
|
* |
42
|
|
|
* @param string $element |
43
|
|
|
* @return string |
44
|
|
|
*/ |
45
|
|
|
public static function prep_element($element) { |
46
|
|
|
if (self::containsCode($element) === false) { |
47
|
|
|
$element = '"' . addslashes($element) . '"'; |
48
|
|
|
} |
49
|
|
|
return $element; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Puts HTML values in quotes for use in jQuery code |
54
|
|
|
* unless the supplied value contains the Javascript 'this' or 'event' |
55
|
|
|
* object, in which case no quotes are added |
56
|
|
|
* |
57
|
|
|
* @param string $value |
58
|
|
|
* @return string |
59
|
|
|
*/ |
60
|
|
|
public static function prep_value($value) { |
61
|
|
|
if (\is_array($value)) { |
|
|
|
|
62
|
|
|
$value = implode(",", $value); |
63
|
|
|
} |
64
|
|
|
if (self::containsCode($value) === false) { |
65
|
|
|
$value = \str_replace([ |
66
|
|
|
"\\", |
67
|
|
|
"\"" |
68
|
|
|
], [ |
69
|
|
|
"\\\\", |
70
|
|
|
"\\\"" |
71
|
|
|
], $value); |
72
|
|
|
$value = '"' . $value . '"'; |
73
|
|
|
} |
74
|
|
|
return trim($value, "%"); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public static function prep_jquery_selector($value) { |
78
|
|
|
if (JString::startswith($value, '$(') === false) { |
79
|
|
|
return '$(' . self::prep_value($value) . ')'; |
80
|
|
|
} |
81
|
|
|
return $value; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|