2014-03-04 16:15:08 +01:00
|
|
|
<?php
|
2014-07-15 13:39:22 +02:00
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
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',
|
2014-09-10 10:45:34 +02:00
|
|
|
'js/icinga/behavior/tooltip.js',
|
|
|
|
'js/icinga/behavior/sparkline.js',
|
|
|
|
'js/icinga/behavior/tristate.js',
|
|
|
|
'js/icinga/behavior/navigation.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-19 17:06:21 +01:00
|
|
|
public static function listModuleFiles()
|
|
|
|
{
|
|
|
|
$list = array();
|
|
|
|
foreach (Icinga::app()->getModuleManager()->getLoadedModules() as $name => $module) {
|
|
|
|
if ($module->hasJs()) {
|
|
|
|
$list[] = 'js/' . $name . '/module.js';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $list;
|
2014-03-04 16:15:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public static function sendMinified()
|
2014-03-27 08:32:02 +01:00
|
|
|
{
|
|
|
|
return self::send(true);
|
|
|
|
}
|
|
|
|
|
2014-06-12 19:33:28 +02:00
|
|
|
public static function sendForIe8()
|
|
|
|
{
|
|
|
|
self::$vendorFiles = self::$ie8VendorFiles;
|
|
|
|
return self::send();
|
|
|
|
}
|
|
|
|
|
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');
|
|
|
|
$basedir = Icinga::app()->getBootstrapDirecory();
|
|
|
|
|
|
|
|
$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()) {
|
2014-09-02 16:25:44 +02:00
|
|
|
$jsFiles[] = $module->getJsFilename();
|
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) {
|
|
|
|
$js .= file_get_contents($file);
|
|
|
|
}
|
|
|
|
|
2014-03-27 08:32:02 +01:00
|
|
|
if ($minified) {
|
2014-06-05 02:10:49 +02:00
|
|
|
require_once 'IcingaVendor/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;
|
|
|
|
}
|
|
|
|
}
|