Passed
Push — master ( e24b4a...52e3ac )
by Jean-Christophe
02:06
created

JsUtilsInternalTrait::minify()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

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