Passed
Push — master ( db96c7...0c9a9b )
by Jean-Christophe
03:42
created

JsUtilsInternalTrait::_addToCompile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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