Merge pull request #2190 from splitbrain/betterJSINFO

feat: canonically provide $ACT to javascript
This commit is contained in:
Andreas Gohr 2018-04-05 10:31:27 +02:00 committed by GitHub
commit d9e82b09c1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 21 additions and 9 deletions

View File

@ -90,10 +90,6 @@ if($DATE_AT) {
//make infos about the selected page available
$INFO = pageinfo();
//export minimal info to JS, plugins can add more
$JSINFO['id'] = $ID;
$JSINFO['namespace'] = (string) $INFO['namespace'];
// handle debugging
if($conf['allowdebug'] && $ACT == 'debug') {
html_debug();

View File

@ -300,6 +300,23 @@ function pageinfo() {
return $info;
}
/**
* Initialize and/or fill global $JSINFO with some basic info to be given to javascript
*/
function jsinfo() {
global $JSINFO, $ID, $INFO, $ACT;
if (!is_array($JSINFO)) {
$JSINFO = [];
}
//export minimal info to JS, plugins can add more
$JSINFO['id'] = $ID;
$JSINFO['namespace'] = (string) $INFO['namespace'];
$JSINFO['ACT'] = act_clean($ACT);
$JSINFO['useHeadingNavigation'] = (int) useHeading('navigation');
$JSINFO['useHeadingContent'] = (int) useHeading('content');
}
/**
* Return information about the current media item as an associative array.
*

View File

@ -335,13 +335,12 @@ function tpl_metaheaders($alt = true) {
'href'=> DOKU_BASE.'lib/exe/css.php?t='.rawurlencode($conf['template']).'&tseed='.$tseed
);
// make $INFO and other vars available to JavaScripts
$json = new JSON();
$script = "var NS='".$INFO['namespace']."';";
if($conf['useacl'] && $INPUT->server->str('REMOTE_USER')) {
$script .= "var SIG='".toolbar_signature()."';";
}
$script .= 'var JSINFO = '.$json->encode($JSINFO).';';
jsinfo();
$script .= 'var JSINFO = ' . json_encode($JSINFO).';';
$head['script'][] = array('type'=> 'text/javascript', '_data'=> $script);
// load jquery

View File

@ -100,8 +100,8 @@ function js_out(){
'secure' => $conf['securecookie'] && is_ssl()
)).";";
// FIXME: Move those to JSINFO
print "var DOKU_UHN = ".((int) useHeading('navigation')).";";
print "var DOKU_UHC = ".((int) useHeading('content')).";";
print "Object.defineProperty(window, 'DOKU_UHN', { get: function() { console.warn('Using DOKU_UHN is deprecated. Please use JSINFO.useHeadingNavigation instead'); return JSINFO.useHeadingNavigation; } });";
print "Object.defineProperty(window, 'DOKU_UHC', { get: function() { console.warn('Using DOKU_UHC is deprecated. Please use JSINFO.useHeadingContent instead'); return JSINFO.useHeadingContent; } });";
// load JS specific translations
$lang['js']['plugins'] = js_pluginstrings();