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 | if ($expression == null) { |
||
26 | return false; |
||
27 | } |
||
28 | return \strrpos($expression, 'this') !== false || \strrpos($expression, 'event') !== false || \strrpos($expression, 'self') !== false; |
||
29 | } |
||
30 | |||
31 | public static function isFunction($jsCode) { |
||
32 | return JString::startswith($jsCode, "function"); |
||
33 | } |
||
34 | |||
35 | public static function fileUploadBehavior($id = '') { |
||
36 | return "$('input:text, .ui.button', '#{$id}').on('click', function (e) {e.preventDefault();\$('input:file', '#{$id}').click();}); |
||
37 | $('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);}});"; |
||
38 | } |
||
39 | |||
40 | /** |
||
41 | * Puts HTML element in quotes for use in jQuery code |
||
42 | * unless the supplied element is the Javascript 'this' |
||
43 | * object, in which case no quotes are added |
||
44 | * |
||
45 | * @param string $element |
||
46 | * @return string |
||
47 | */ |
||
48 | public static function prep_element($element) { |
||
49 | if (self::containsCode($element) === false) { |
||
50 | $element = '"' . addslashes($element ?? '') . '"'; |
||
51 | } |
||
52 | return $element; |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * Puts HTML values in quotes for use in jQuery code |
||
57 | * unless the supplied value contains the Javascript 'this' or 'event' |
||
58 | * object, in which case no quotes are added |
||
59 | * |
||
60 | * @param string $value |
||
61 | * @return string |
||
62 | */ |
||
63 | public static function prep_value($value) { |
||
64 | if (\is_array($value)) { |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
65 | $value = implode(",", $value); |
||
66 | } |
||
67 | if (self::containsCode($value) === false) { |
||
68 | $value = \str_replace([ |
||
69 | "\\", |
||
70 | "\"" |
||
71 | ], [ |
||
72 | "\\\\", |
||
73 | "\\\"" |
||
74 | ], $value); |
||
75 | $value = '"' . $value . '"'; |
||
76 | } |
||
77 | return trim($value, "%"); |
||
78 | } |
||
79 | |||
80 | public static function prep_jquery_selector($value) { |
||
81 | if (JString::startswith($value, '$(') === false) { |
||
82 | return '$(' . self::prep_value($value) . ')'; |
||
83 | } |
||
84 | return $value; |
||
85 | } |
||
86 | } |
||
87 |