Asset.php
3.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php
namespace Nextend\Framework\Asset\Js;
use Nextend\Framework\Asset\AbstractAsset;
use Nextend\Framework\Localization\Localization;
use Nextend\Framework\Platform\Platform;
use Nextend\Framework\Plugin;
use Nextend\Framework\Settings;
use Nextend\Framework\Url\Url;
use Nextend\Framework\View\Html;
use Nextend\SmartSlider3\SmartSlider3Info;
class Asset extends AbstractAsset {
public function __construct() {
$this->cache = new Cache();
}
public function getOutput() {
$output = "";
$needProtocol = !Settings::get('protocol-relative', '1');
$globalInline = $this->getGlobalInlineScripts();
if (!empty($globalInline)) {
$output .= Html::script(self::minify_js($globalInline . "\n"));
}
$async = !Platform::isAdmin();
$scriptAttributes = array();
if ($async) {
$scriptAttributes['defer'] = 1;
$scriptAttributes['async'] = 1;
}
foreach ($this->urls as $url) {
$output .= Html::scriptFile($this->filterSrc($url), $scriptAttributes) . "\n";
}
foreach ($this->getFiles() as $file) {
if (substr($file, 0, 2) == '//') {
$output .= Html::scriptFile($this->filterSrc($file), $scriptAttributes) . "\n";
} else {
$output .= Html::scriptFile($this->filterSrc(Url::pathToUri($file, $needProtocol) . '?ver=' . SmartSlider3Info::$revisionShort), $scriptAttributes) . "\n";
}
}
$output .= Html::script(self::minify_js(Localization::toJS() . "\n" . $this->getInlineScripts() . "\n"));
return $output;
}
private function filterSrc($src) {
return Plugin::applyFilters('n2_script_loader_src', $src);
}
public function get() {
return array(
'url' => $this->urls,
'files' => $this->getFiles(),
'inline' => $this->getInlineScripts(),
'globalInline' => $this->getGlobalInlineScripts()
);
}
public function getAjaxOutput() {
$output = $this->getInlineScripts();
return $output;
}
private function getGlobalInlineScripts() {
return implode('', $this->globalInline);
}
private function getInlineScripts() {
$scripts = '';
foreach ($this->firstCodes as $script) {
$scripts .= $script . "\n";
}
foreach ($this->inline as $script) {
$scripts .= $script . "\n";
}
return $this->serveJquery($scripts);
}
public static function serveJquery($script) {
if (empty($script)) {
return "";
}
$inline = "_N2.r('documentReady', function(){\n";
$inline .= $script;
$inline .= "});\n";
return $inline;
}
public static function minify_js($input) {
if (trim($input) === "") return $input;
return preg_replace(array(
// Remove comment(s)
'#\s*("(?:[^"\\\]++|\\\.)*+"|\'(?:[^\'\\\\]++|\\\.)*+\')\s*|\s*\/\*(?!\!|@cc_on)(?>[\s\S]*?\*\/)\s*|\s*(?<![\:\=])\/\/.*(?=[\n\r]|$)|^\s*|\s*$#',
// Remove white-space(s) outside the string and regex
'#("(?:[^"\\\]++|\\\.)*+"|\'(?:[^\'\\\\]++|\\\.)*+\'|\/\*(?>.*?\*\/)|\/(?!\/)[^\n\r]*?\/(?=[\s.,;]|[gimuy]|$))|\s*([!%&*\(\)\-=+\[\]\{\}|;:,.<>?\/])\s*#s',
// Remove the last semicolon
'#;+\}#',
// Minify object attribute(s) except JSON attribute(s). From `{'foo':'bar'}` to `{foo:'bar'}`
'#([\{,])([\'])(\d+|[a-z_][a-z0-9_]*)\2(?=\:)#i',
// --ibid. From `foo['bar']` to `foo.bar`
'#([a-z0-9_\)\]])\[([\'"])([a-z_][a-z0-9_]*)\2\]#i'
), array(
'$1',
'$1$2',
'}',
'$1$3',
'$1.$3'
), $input);
}
}