Adds experimental support for local config files.

This commit is contained in:
Lars Jung 2012-09-19 14:47:52 +02:00
parent bcc9f21fc1
commit df208faf25
5 changed files with 88 additions and 21 deletions

View file

@ -58,7 +58,8 @@ To create an fresh zipball run
* PHP ignore patterns might include paths now * PHP ignore patterns might include paths now
* adds optional binary prefixes for file sizes * adds optional binary prefixes for file sizes
* improves filter: autofocus on keypress, clear on `ESC` * 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 * temp download packages are now stored in the `_h5ai/cache` and deleted as soon as possible
* localization now in separate files * localization now in separate files
* updates translations * updates translations

View file

@ -7,6 +7,9 @@ modulejs.define('parser/generic-json', ['_', '$', 'core/mode', 'core/settings',
mode.serverName = json.serverName; mode.serverName = json.serverName;
mode.serverVersion = json.serverVersion; mode.serverVersion = json.serverVersion;
if (!settings.custom) {
settings.custom = {};
}
if (_.has(json, 'customHeader')) { if (_.has(json, 'customHeader')) {
settings.custom.header = json.customHeader; settings.custom.header = json.customHeader;
} }

View file

@ -21,13 +21,26 @@
// @include "inc/**/*.js" // @include "inc/**/*.js"
$.ajax({ var $scriptTag = $('script[src$="scripts.js"]'),
url: $('script[src$="scripts.js"]').attr('src').replace(/scripts.js$/, '../config.json'), globalConfigHref = $scriptTag.attr('src').replace(/scripts.js$/, '../config.json'),
complete: function (data) { localConfigHref = $scriptTag.data('config') || './_h5ai.config.json',
var config = JSON.parse(data.responseText.replace(/\/\*[\s\S]*?\*\/|\/\/.*?(\n|$)/g, '')); ajaxOpts = {dataType: 'text'},
$(function () { 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, _ */ /*global amplify, Base64, jQuery, Modernizr, moment, _ */
// `jQuery`, `moment` and `underscore` are itself functions, // `jQuery`, `moment` and `underscore` are itself functions,
@ -40,9 +53,31 @@
modulejs.define('moment', function () { return moment; }); modulejs.define('moment', function () { return moment; });
modulejs.define('_', function () { return _; }); modulejs.define('_', function () { return _; });
$(function () {
modulejs.require($('body').attr('id')); 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)); }(jQuery));

View file

@ -19,6 +19,7 @@
- var h5ai = "<?php echo $h5aiAbsHref; ?>" - var h5ai = "<?php echo $h5aiAbsHref; ?>"
- var json = "<?php if (!$isHeadRequest) { echo $h5ai->getGenericJson(); }?>" - var json = "<?php if (!$isHeadRequest) { echo $h5ai->getGenericJson(); }?>"
- var fallback = "<?php if (!$isHeadRequest) { echo $h5ai->getNoJsFallback(); }?>" - var fallback = "<?php if (!$isHeadRequest) { echo $h5ai->getNoJsFallback(); }?>"
- var config = "<?php if (!$isHeadRequest) { echo $h5ai->getCustomConfig(); }?>"
doctype 5 doctype 5
//if lt IE 9 //if lt IE 9
@ -62,4 +63,4 @@ html.no-js( lang="en" )
div#data-php-no-js-fallback.hideOnJs !{fallback} div#data-php-no-js-fallback.hideOnJs !{fallback}
script( src!="#{h5ai}js/scripts.js" ) script( src!="#{h5ai}js/scripts.js", data-config!="#{config}" )

View file

@ -26,13 +26,28 @@ class H5ai {
private static final function load_config($file) { 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 // remove comments to get pure json
$str = preg_replace("/\/\*.*?\*\/|\/\/.*?(\n|$)/s", "", $str); $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->ignore_patterns = $H5AI_CONFIG["IGNORE_PATTERNS"];
$this->index_files = $H5AI_CONFIG["INDEX_FILES"]; $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->options = $this->config["options"];
$this->h5aiAbsHref = H5ai::normalize_path($this->options["h5aiAbsHref"], true); $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->absHref = H5ai::normalize_path(preg_replace('/[^\\/]*$/', '', getenv("REQUEST_URI")), true);
$this->absPath = $this->getAbsPath($this->absHref); $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) { public function getEntries($absHref, $content) {
$folder = Entry::get($this, $this->getAbsPath($absHref), $absHref); $folder = Entry::get($this, $this->getAbsPath($absHref), $absHref);