2014-03-04 16:15:08 +01:00
|
|
|
<?php
|
2015-02-04 10:46:36 +01:00
|
|
|
/* Icinga Web 2 | (c) 2013-2015 Icinga Development Team | GPLv2+ */
|
2014-03-04 16:15:08 +01:00
|
|
|
|
|
|
|
namespace Icinga\Web;
|
|
|
|
|
|
|
|
use Icinga\Application\Icinga;
|
2014-09-02 16:25:44 +02:00
|
|
|
use Icinga\Web\FileCache;
|
2014-03-04 16:15:08 +01:00
|
|
|
use JShrink\Minifier;
|
|
|
|
|
|
|
|
class JavaScript
|
|
|
|
{
|
|
|
|
protected static $jsFiles = array(
|
|
|
|
'js/helpers.js',
|
|
|
|
'js/icinga.js',
|
|
|
|
'js/icinga/logger.js',
|
|
|
|
'js/icinga/utils.js',
|
|
|
|
'js/icinga/ui.js',
|
|
|
|
'js/icinga/timer.js',
|
|
|
|
'js/icinga/loader.js',
|
2014-09-12 09:09:21 +02:00
|
|
|
'js/icinga/eventlistener.js',
|
2014-03-04 16:15:08 +01:00
|
|
|
'js/icinga/events.js',
|
|
|
|
'js/icinga/history.js',
|
|
|
|
'js/icinga/module.js',
|
2014-09-05 13:17:40 +02:00
|
|
|
'js/icinga/timezone.js',
|
2015-11-16 14:19:33 +01:00
|
|
|
'js/icinga/behavior/application-state.js',
|
2014-09-10 10:45:34 +02:00
|
|
|
'js/icinga/behavior/tooltip.js',
|
|
|
|
'js/icinga/behavior/sparkline.js',
|
|
|
|
'js/icinga/behavior/tristate.js',
|
2014-09-19 15:48:44 +02:00
|
|
|
'js/icinga/behavior/navigation.js',
|
2015-06-29 18:45:14 +02:00
|
|
|
'js/icinga/behavior/form.js',
|
2015-08-04 14:06:33 +02:00
|
|
|
'js/icinga/behavior/actiontable.js',
|
2015-09-28 15:44:25 +02:00
|
|
|
'js/icinga/behavior/selectable.js'
|
2014-03-04 16:15:08 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
protected static $vendorFiles = array(
|
2014-03-27 08:32:02 +01:00
|
|
|
'js/vendor/jquery-2.1.0',
|
2014-09-02 17:51:07 +02:00
|
|
|
'js/vendor/jquery.sparkline',
|
|
|
|
'js/vendor/jquery.tipsy'
|
2014-03-04 16:15:08 +01:00
|
|
|
);
|
|
|
|
|
2014-06-12 19:33:28 +02:00
|
|
|
protected static $ie8VendorFiles = array(
|
|
|
|
'js/vendor/jquery-1.11.0',
|
2014-09-02 17:51:07 +02:00
|
|
|
'js/vendor/jquery.sparkline',
|
|
|
|
'js/vendor/jquery.tipsy'
|
2014-06-12 19:33:28 +02:00
|
|
|
);
|
|
|
|
|
2014-03-04 16:15:08 +01:00
|
|
|
public static function sendMinified()
|
2014-03-27 08:32:02 +01:00
|
|
|
{
|
2015-11-27 16:48:38 +01:00
|
|
|
self::send(true);
|
2014-03-27 08:32:02 +01:00
|
|
|
}
|
|
|
|
|
2014-06-12 19:33:28 +02:00
|
|
|
public static function sendForIe8()
|
|
|
|
{
|
|
|
|
self::$vendorFiles = self::$ie8VendorFiles;
|
2015-11-27 16:48:38 +01:00
|
|
|
self::send();
|
2014-06-12 19:33:28 +02:00
|
|
|
}
|
|
|
|
|
2014-03-27 08:32:02 +01:00
|
|
|
public static function send($minified = false)
|
2014-03-04 16:15:08 +01:00
|
|
|
{
|
|
|
|
header('Content-Type: application/javascript');
|
2014-11-12 17:14:09 +01:00
|
|
|
$basedir = Icinga::app()->getBootstrapDirectory();
|
2014-03-04 16:15:08 +01:00
|
|
|
|
|
|
|
$js = $out = '';
|
2014-03-27 08:32:02 +01:00
|
|
|
$min = $minified ? '.min' : '';
|
2014-03-04 16:15:08 +01:00
|
|
|
|
2014-09-02 16:25:44 +02:00
|
|
|
// Prepare vendor file list
|
|
|
|
$vendorFiles = array();
|
2014-03-04 16:15:08 +01:00
|
|
|
foreach (self::$vendorFiles as $file) {
|
2014-09-02 16:25:44 +02:00
|
|
|
$vendorFiles[] = $basedir . '/' . $file . $min . '.js';
|
2014-03-04 16:15:08 +01:00
|
|
|
}
|
|
|
|
|
2014-09-02 16:25:44 +02:00
|
|
|
// Prepare Icinga JS file list
|
|
|
|
$jsFiles = array();
|
2014-03-04 16:15:08 +01:00
|
|
|
foreach (self::$jsFiles as $file) {
|
2014-09-02 16:25:44 +02:00
|
|
|
$jsFiles[] = $basedir . '/' . $file;
|
2014-03-04 16:15:08 +01:00
|
|
|
}
|
2014-03-26 10:24:27 +01:00
|
|
|
|
|
|
|
foreach (Icinga::app()->getModuleManager()->getLoadedModules() as $name => $module) {
|
|
|
|
if ($module->hasJs()) {
|
2015-08-06 15:15:02 +02:00
|
|
|
foreach ($module->getJsFiles() as $path) {
|
|
|
|
if (file_exists($path)) {
|
|
|
|
$jsFiles[] = $path;
|
|
|
|
}
|
|
|
|
}
|
2014-03-26 10:24:27 +01:00
|
|
|
}
|
|
|
|
}
|
2014-09-02 16:25:44 +02:00
|
|
|
$files = array_merge($vendorFiles, $jsFiles);
|
|
|
|
|
|
|
|
if ($etag = FileCache::etagMatchesFiles($files)) {
|
|
|
|
header("HTTP/1.1 304 Not Modified");
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
$etag = FileCache::etagForFiles($files);
|
|
|
|
}
|
|
|
|
header('Cache-Control: public');
|
|
|
|
header('ETag: "' . $etag . '"');
|
|
|
|
header('Content-Type: application/javascript');
|
|
|
|
|
|
|
|
$cacheFile = 'icinga-' . $etag . $min . '.js';
|
|
|
|
$cache = FileCache::instance();
|
|
|
|
if ($cache->has($cacheFile)) {
|
|
|
|
$cache->send($cacheFile);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We do not minify vendor files
|
|
|
|
foreach ($vendorFiles as $file) {
|
|
|
|
$out .= file_get_contents($file);
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($jsFiles as $file) {
|
2015-08-06 15:15:02 +02:00
|
|
|
$js .= file_get_contents($file) . "\n\n\n";
|
2014-09-02 16:25:44 +02:00
|
|
|
}
|
|
|
|
|
2014-03-27 08:32:02 +01:00
|
|
|
if ($minified) {
|
2014-11-14 11:55:06 +01:00
|
|
|
require_once 'JShrink/Minifier.php';
|
2014-03-27 08:32:02 +01:00
|
|
|
$out .= Minifier::minify($js, array('flaggedComments' => false));
|
|
|
|
} else {
|
|
|
|
$out .= $js;
|
|
|
|
}
|
2014-09-02 16:25:44 +02:00
|
|
|
$cache->store($cacheFile, $out);
|
2014-03-04 16:15:08 +01:00
|
|
|
echo $out;
|
|
|
|
}
|
|
|
|
}
|