Passed
Push — master ( ef609f...421c61 )
by Jean-Christophe
02:48
created

JsUtilsInternalTrait::_close_script()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 2
rs 10
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=NULL, &$view=NULL){
19
		if(isset($view))
20
			$library->compileHtml($this, $view);
0 ignored issues
show
Bug introduced by
The method compileHtml() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

20
			$library->/** @scrutinizer ignore-call */ 
21
             compileHtml($this, $view);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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
		return 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
	}
62
63
	/**
64
	 * Outputs an opening <script>
65
	 *
66
	 * @param string $src
67
	 * @return string
68
	 */
69
	protected function _open_script($src='') {
70
		$str='<script type="text/javascript" ';
71
		$str.=($src=='') ? '>' : ' src="'.$src.'">';
72
		return $str;
73
	}
74
75
	/**
76
	 * Outputs an closing </script>
77
	 *
78
	 * @param string $extra
79
	 * @return string
80
	 */
81
	protected function _close_script($extra="\n") {
82
		return "</script>$extra";
83
	}
84
85
	protected function conflict() {
86
		$this->_addToCompile("var btn = $.fn.button.noConflict();$.fn.btn = btn;");
87
	}
88
89
	public function addToCompile($jsScript) {
90
		$this->_addToCompile($jsScript);
91
	}
92
}
93