diff --git a/README.md b/README.md index 6cd7cd56..e20c8ffb 100644 --- a/README.md +++ b/README.md @@ -58,7 +58,8 @@ To create an fresh zipball run * PHP ignore patterns might include paths now * adds optional binary prefixes for file sizes * improves filter: autofocus on keypress, clear on `ESC` -* add ctrl-click file selection +* adds ctrl-click file selection +* adds Piwik analytics support * temp download packages are now stored in the `_h5ai/cache` and deleted as soon as possible * localization now in separate files * updates translations diff --git a/src/_h5ai/js/inc/parser/generic-json.js b/src/_h5ai/js/inc/parser/generic-json.js index df7bcd5d..3debafb9 100644 --- a/src/_h5ai/js/inc/parser/generic-json.js +++ b/src/_h5ai/js/inc/parser/generic-json.js @@ -7,6 +7,9 @@ modulejs.define('parser/generic-json', ['_', '$', 'core/mode', 'core/settings', mode.serverName = json.serverName; mode.serverVersion = json.serverVersion; + if (!settings.custom) { + settings.custom = {}; + } if (_.has(json, 'customHeader')) { settings.custom.header = json.customHeader; } diff --git a/src/_h5ai/js/scripts.js b/src/_h5ai/js/scripts.js index 4e522dbe..b5078066 100644 --- a/src/_h5ai/js/scripts.js +++ b/src/_h5ai/js/scripts.js @@ -21,28 +21,63 @@ // @include "inc/**/*.js" - $.ajax({ - url: $('script[src$="scripts.js"]').attr('src').replace(/scripts.js$/, '../config.json'), - complete: function (data) { + var $scriptTag = $('script[src$="scripts.js"]'), + globalConfigHref = $scriptTag.attr('src').replace(/scripts.js$/, '../config.json'), + localConfigHref = $scriptTag.data('config') || './_h5ai.config.json', - var config = JSON.parse(data.responseText.replace(/\/\*[\s\S]*?\*\/|\/\/.*?(\n|$)/g, '')); + ajaxOpts = {dataType: 'text'}, + + parse = function (response) { + + return response.replace ? JSON.parse(response.replace(/\/\*[\s\S]*?\*\/|\/\/.*?(\n|$)/g, '')) : {}; + }, + + extendLevel1 = function (a, b) { + + $.each(b, function (key) { + + $.extend(a[key], b[key]); + }); + }, + + run = function (config) { + /*global amplify, Base64, jQuery, Modernizr, moment, _ */ + + // `jQuery`, `moment` and `underscore` are itself functions, + // so they have to be wrapped to not be handled as constructors. + modulejs.define('config', config); + modulejs.define('amplify', amplify); + modulejs.define('base64', Base64); + modulejs.define('$', function () { return jQuery; }); + modulejs.define('modernizr', Modernizr); + modulejs.define('moment', function () { return moment; }); + modulejs.define('_', function () { return _; }); $(function () { - /*global amplify, Base64, jQuery, Modernizr, moment, _ */ - - // `jQuery`, `moment` and `underscore` are itself functions, - // so they have to be wrapped to not be handled as constructors. - modulejs.define('config', config); - modulejs.define('amplify', amplify); - modulejs.define('base64', Base64); - modulejs.define('$', function () { return jQuery; }); - modulejs.define('modernizr', Modernizr); - modulejs.define('moment', function () { return moment; }); - modulejs.define('_', function () { return _; }); modulejs.require($('body').attr('id')); }); + }, + + config = { + options: {}, + types: {}, + langs: {} + }; + + $.ajax(globalConfigHref, ajaxOpts).always(function (g) { + + extendLevel1(config, parse(g)); + if (localConfigHref === 'ignore') { + run(config); + return; } + + $.ajax(localConfigHref, ajaxOpts).always(function (l) { + + extendLevel1(config, parse(l)); + run(config); + }); }); }(jQuery)); diff --git a/src/_h5ai/php/h5ai-index.php.jade b/src/_h5ai/php/h5ai-index.php.jade index d8030fdc..c5fe7de4 100644 --- a/src/_h5ai/php/h5ai-index.php.jade +++ b/src/_h5ai/php/h5ai-index.php.jade @@ -19,6 +19,7 @@ - var h5ai = "" - var json = "getGenericJson(); }?>" - var fallback = "getNoJsFallback(); }?>" +- var config = "getCustomConfig(); }?>" doctype 5 //if lt IE 9 @@ -62,4 +63,4 @@ html.no-js( lang="en" ) div#data-php-no-js-fallback.hideOnJs !{fallback} - script( src!="#{h5ai}js/scripts.js" ) + script( src!="#{h5ai}js/scripts.js", data-config!="#{config}" ) diff --git a/src/_h5ai/php/inc/H5ai.php b/src/_h5ai/php/inc/H5ai.php index c31a1a08..f785bd6c 100644 --- a/src/_h5ai/php/inc/H5ai.php +++ b/src/_h5ai/php/inc/H5ai.php @@ -26,13 +26,28 @@ class H5ai { private static final function load_config($file) { - $str = file_exists($file) ? file_get_contents($file) : ""; + if (!file_exists($file)) { + return array(); + } + + $str = file_get_contents($file); // remove comments to get pure json $str = preg_replace("/\/\*.*?\*\/|\/\/.*?(\n|$)/s", "", $str); - $config = json_decode($str, true); - return $config; + return json_decode($str, true); + } + + + private static final function merge_config($a, $b) { + + $result = array_merge(array(), $a); + + foreach ($b as $key => $value) { + $result[$key] = array_merge($result[$key], $b[$key]); + } + + return $result; } @@ -61,7 +76,8 @@ class H5ai { $this->ignore_patterns = $H5AI_CONFIG["IGNORE_PATTERNS"]; $this->index_files = $H5AI_CONFIG["INDEX_FILES"]; - $this->config = H5ai::load_config($this->h5aiAbsPath . "/config.json"); + $this->config = array("options" => array(), "types" => array(), "langs" => array()); + $this->config = H5ai::merge_config($this->config, H5ai::load_config($this->h5aiAbsPath . "/config.json")); $this->options = $this->config["options"]; $this->h5aiAbsHref = H5ai::normalize_path($this->options["h5aiAbsHref"], true); @@ -69,6 +85,9 @@ class H5ai { $this->absHref = H5ai::normalize_path(preg_replace('/[^\\/]*$/', '', getenv("REQUEST_URI")), true); $this->absPath = $this->getAbsPath($this->absHref); + + $this->config = H5ai::merge_config($this->config, H5ai::load_config($this->absPath . "/_h5ai.config.json")); + $this->options = $this->config["options"]; } @@ -285,6 +304,14 @@ class H5ai { } + public function getCustomConfig() { + + $config = "_h5ai.config.json"; + $config = $this->fileExists($config ? $this->absPath . "/" . $config : "ignore") ? $config : "ignore"; + return $config; + } + + public function getEntries($absHref, $content) { $folder = Entry::get($this, $this->getAbsPath($absHref), $absHref);