1
|
|
|
<?php |
2
|
|
|
namespace Ajax\common\traits; |
3
|
|
|
use Ajax\common\BaseGui; |
4
|
|
|
|
5
|
|
|
trait JsUtilsInternalTrait{ |
6
|
|
|
|
7
|
|
|
protected $jquery_code_for_compile=array (); |
8
|
|
|
|
9
|
|
|
protected function _addToCompile($jsScript) { |
10
|
|
|
$this->jquery_code_for_compile[]=$jsScript; |
11
|
|
|
} |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @param BaseGui $library |
15
|
|
|
* @param mixed $view |
16
|
|
|
*/ |
17
|
|
|
private function _compileLibrary($library, &$view=NULL){ |
|
|
|
|
18
|
|
|
if ($library!=NULL) { |
19
|
|
|
if(isset($view)) |
20
|
|
|
$library->compileHtml($this, $view); |
21
|
|
|
if ($library->isAutoCompile()) { |
22
|
|
|
$library->compile(true); |
23
|
|
|
} |
24
|
|
|
} |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
private function defer($script){ |
|
|
|
|
28
|
|
|
$result="window.defer=function (method) {if (window.jQuery) method(); else setTimeout(function() { defer(method) }, 50);};"; |
29
|
|
|
$result.="window.defer(function(){".$script."})"; |
30
|
|
|
return $result; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
private function ready($script){ |
|
|
|
|
34
|
|
|
$result='$(document).ready(function() {'."\n"; |
35
|
|
|
$result.=$script.'})'; |
36
|
|
|
return $result; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
private function minify($input) { |
|
|
|
|
40
|
|
|
if(trim($input) === "") return $input; |
41
|
|
|
return preg_replace( |
42
|
|
|
array( |
43
|
|
|
// Remove comment(s) |
44
|
|
|
'#\s*("(?:[^"\\\]++|\\\.)*+"|\'(?:[^\'\\\\]++|\\\.)*+\')\s*|\s*\/\*(?!\!|@cc_on)(?>[\s\S]*?\*\/)\s*|\s*(?<![\:\=])\/\/.*(?=[\n\r]|$)|^\s*|\s*$#', |
45
|
|
|
// Remove white-space(s) outside the string and regex |
46
|
|
|
'#("(?:[^"\\\]++|\\\.)*+"|\'(?:[^\'\\\\]++|\\\.)*+\'|\/\*(?>.*?\*\/)|\/(?!\/)[^\n\r]*?\/(?=[\s.,;]|[gimuy]|$))|\s*([!%&*\(\)\-=+\[\]\{\}|;:,.<>?\/])\s*#s', |
47
|
|
|
// Remove the last semicolon |
48
|
|
|
'#;+\}#', |
49
|
|
|
// Minify object attribute(s) except JSON attribute(s). From `{'foo':'bar'}` to `{foo:'bar'}` |
50
|
|
|
'#([\{,])([\'])(\d+|[a-z_][a-z0-9_]*)\2(?=\:)#i', |
51
|
|
|
// --ibid. From `foo['bar']` to `foo.bar` |
52
|
|
|
'#([a-z0-9_\)\]])\[([\'"])([a-z_][a-z0-9_]*)\2\]#i' |
53
|
|
|
), |
54
|
|
|
array( |
55
|
|
|
'$1', |
56
|
|
|
'$1$2', |
57
|
|
|
'}', |
58
|
|
|
'$1$3', |
59
|
|
|
'$1.$3' |
60
|
|
|
), |
61
|
|
|
$input); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Outputs an opening <script> |
66
|
|
|
* |
67
|
|
|
* @param string $src |
68
|
|
|
* @return string |
69
|
|
|
*/ |
70
|
|
|
private function _open_script($src='') { |
|
|
|
|
71
|
|
|
$str='<script type="text/javascript" '; |
72
|
|
|
$str.=($src=='') ? '>' : ' src="'.$src.'">'; |
73
|
|
|
return $str; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Outputs an closing </script> |
78
|
|
|
* |
79
|
|
|
* @param string $extra |
80
|
|
|
* @return string |
81
|
|
|
*/ |
82
|
|
|
private function _close_script($extra="\n") { |
|
|
|
|
83
|
|
|
return "</script>$extra"; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
private function conflict() { |
|
|
|
|
87
|
|
|
$this->_addToCompile("var btn = $.fn.button.noConflict();$.fn.btn = btn;"); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function addToCompile($jsScript) { |
91
|
|
|
$this->_addToCompile($jsScript); |
92
|
|
|
} |
93
|
|
|
} |