Skip to content

Commit

Permalink
Merge pull request #2190 from splitbrain/betterJSINFO
Browse files Browse the repository at this point in the history
feat: canonically provide $ACT to javascript
  • Loading branch information
splitbrain committed Apr 5, 2018
2 parents 874fc8d + 0c39d46 commit d9e82b0
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 9 deletions.
4 changes: 0 additions & 4 deletions doku.php
Expand Up @@ -90,10 +90,6 @@
//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();
Expand Down
17 changes: 17 additions & 0 deletions inc/common.php
Expand Up @@ -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.
*
Expand Down
5 changes: 2 additions & 3 deletions inc/template.php
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lib/exe/js.php
Expand Up @@ -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();
Expand Down

0 comments on commit d9e82b0

Please sign in to comment.