phpMv -UI toolkit 2.4.12
jQuery, jQuery UI, Twitter Bootstrap and Semantic-UI library for php & php MVC Frameworks
Loading...
Searching...
No Matches
Javascript.php
Go to the documentation of this file.
1<?php
2namespace Ajax\service;
3
4class 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
48 public static function prep_element($element) {
49 if (self::containsCode($element) === false) {
50 $element = '"' . addslashes($element ?? '') . '"';
51 }
52 return $element;
53 }
54
63 public static function prep_value($value) {
64 if (\is_array($value)) {
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}
static startswith($hay, $needle)
Definition JString.php:10
static draggable($attr="id")
static containsCode($expression)
static fileUploadBehavior($id='')
static isFunction($jsCode)
static prep_jquery_selector($value)
static dropZone($jqueryDone, $jsCallback="")
static prep_value($value)
Puts HTML values in quotes for use in jQuery code unless the supplied value contains the Javascript '...
static prep_element($element)
Puts HTML element in quotes for use in jQuery code unless the supplied element is the Javascript 'thi...
static fileDropZone($jsCallback="")