Passed
Push — master ( fa0f64...7ebbb8 )
by Jean-Christophe
01:58
created

Javascript::prep_jquery_selector()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 5
rs 10
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/plain",JSON.stringify({id:$(event.target).attr("id"),data:$(event.target).attr("'.$attr.'")}));';
11
	}
12
	
13
	public static function dropZone($jqueryDone,$jsCallback=""){
14
		return 'var dt=event.dataTransfer || event.originalEvent.dataTransfer;var _data=JSON.parse(dt.getData("text/plain"));$(event.target).'.$jqueryDone.'($("#"+_data.id));var data=_data.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
	public static function isFunction($jsCode){
22
		return JString::startswith($jsCode, "function");
23
	}
24
	
25
	public static function fileUploadBehavior($id=''){
26
		return "$('input:text, .ui.button', '#{$id}').on('click', function (e) {e.preventDefault();\$('input:file', '#{$id}').click();});
27
				$('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);}});";
28
	}
29
30
	/**
31
	 * Puts HTML element in quotes for use in jQuery code
32
	 * unless the supplied element is the Javascript 'this'
33
	 * object, in which case no quotes are added
34
	 *
35
	 * @param string $element
36
	 * @return string
37
	 */
38
	public static function prep_element($element) {
39
		if (self::containsCode($element)===false) {
40
			$element='"'.addslashes($element).'"';
41
		}
42
		return $element;
43
	}
44
45
	/**
46
	 * Puts HTML values in quotes for use in jQuery code
47
	 * unless the supplied value contains the Javascript 'this' or 'event'
48
	 * object, in which case no quotes are added
49
	 *
50
	 * @param string $value
51
	 * @return string
52
	 */
53
	public static function prep_value($value) {
54
		if (\is_array($value)) {
0 ignored issues
show
introduced by
The condition is_array($value) is always false.
Loading history...
55
			$value=implode(",", $value);
56
		}
57
		if (self::containsCode($value)===false) {
58
			$value=\str_replace(["\\","\""], ["\\\\","\\\""], $value);
59
			$value='"'.$value.'"';
60
		}
61
		return trim($value,"%");
62
	}
63
64
	public static function prep_jquery_selector($value){
65
		if(JString::startswith($value, '$(')===false){
66
			return '$('.self::prep_value($value).')';
67
		}
68
		return $value;
69
	}
70
}
71