From bef45cd3fd301f27a1b96e85864737270e4c4ff9 Mon Sep 17 00:00:00 2001 From: tjhunt Date: Tue, 23 Sep 2008 09:59:18 +0000 Subject: [PATCH] MDL-16630 - Change admintree JavaScript to move the code out of the page, and into a separate .js file --- blocks/admin_tree/admintree.js | 75 +++++++++++++++++ blocks/admin_tree/block_admin_tree.php | 109 ++++--------------------- lib/weblib.php | 6 +- 3 files changed, 96 insertions(+), 94 deletions(-) create mode 100644 blocks/admin_tree/admintree.js diff --git a/blocks/admin_tree/admintree.js b/blocks/admin_tree/admintree.js new file mode 100644 index 0000000000..bbf92a4d48 --- /dev/null +++ b/blocks/admin_tree/admintree.js @@ -0,0 +1,75 @@ +admin_tree = { + numdivs: null, + hiddennodes: null, + closedimg: null, + openimg: null, + closedalt: null, + openalt: null, + + init: function(numdivs, expandnodes, pixpath, openalt, closedalt) { + // Store the information we were passed in. + admin_tree.closedimg = pixpath + '/i/closed.gif'; + admin_tree.openimg = pixpath + '/i/open.gif'; + admin_tree.closedalt = closedalt; + admin_tree.openalt = openalt; + admin_tree.numdivs = numdivs; + + // Initialise the hiddennodes array. + admin_tree.hiddennodes = new Array(); + for (var i = 1; i <= admin_tree.numdivs; i++) { + admin_tree.hiddennodes[i] = null; + } + + // Collapse everything while adding the event handlers. + for (var i = admin_tree.numdivs; i > 0; i--) { + admin_tree.collapse(i); + var togglelink = document.getElementById("vh_div" + i + "indicator").parentNode; + togglelink.href = '#'; + YAHOO.util.Event.addListener(togglelink, 'click', admin_tree.toggle, i); + } + + // Re-expand the bits we want expanded. + for (var i = 0; i < expandnodes.length; i++) { + admin_tree.expand(expandnodes[i]); + } + }, + + toggle: function(e, i) { + if (admin_tree.hiddennodes[i] === null) { + admin_tree.collapse(i); + } else { + admin_tree.expand(i); + } + YAHOO.util.Event. preventDefault(e); + }, + + collapse: function(i) { + if (admin_tree.hiddennodes[i] !== null) { + return; + } + var obj = document.getElementById("vh_div" + i); + if (obj === null) { + return; + } + var nothing = document.createElement("span"); + nothing.setAttribute("id", "vh_div" + i); + admin_tree.hiddennodes[i] = obj; + obj.parentNode.replaceChild(nothing, obj); + var icon = document.getElementById("vh_div" + i + "indicator"); + icon.src = admin_tree.closedimg; + icon.alt = admin_tree.closedalt; + }, + + expand: function(i) { + if (admin_tree.hiddennodes[i] === null) { + return; + } + var nothing = document.getElementById("vh_div" + i); + var obj = admin_tree.hiddennodes[i]; + admin_tree.hiddennodes[i] = null; + nothing.parentNode.replaceChild(obj, nothing); + var icon = document.getElementById("vh_div" + i + "indicator"); + icon.src = admin_tree.openimg; + icon.alt = admin_tree.openalt; + } +}; diff --git a/blocks/admin_tree/block_admin_tree.php b/blocks/admin_tree/block_admin_tree.php index 14cd47ee5c..d7ac10ecf4 100644 --- a/blocks/admin_tree/block_admin_tree.php +++ b/blocks/admin_tree/block_admin_tree.php @@ -18,7 +18,7 @@ class block_admin_tree extends block_base { $this->tempcontent = ''; $this->section = (isset($PAGE->section) ? $PAGE->section : ''); $this->pathtosection = array(); - $this->expandjavascript = ''; + $this->expandnodes = array(); } function applicable_formats() { @@ -37,8 +37,8 @@ class block_admin_tree extends block_base { global $CFG; $strfolderopened = s(get_string('folderopened')); - $this->tempcontent .= '
'; - $this->tempcontent .= ''.$strfolderopened.' '; + $this->tempcontent .= '
'."\n"; $this->currentdepth++; $this->divcounter++; @@ -52,7 +52,7 @@ class block_admin_tree extends block_base { function create_item($visiblename,$link,$icon,$class) { global $CFG; $this->tempcontent .= ''."\n"; + $visiblename.'
'."\n"; } function build_tree (&$content) { @@ -86,7 +86,7 @@ class block_admin_tree extends block_base { // check if the category we're currently printing is a parent category for the current page; if it is, we // make a note (in the javascript) that it has to be expanded after the page has loaded if ($this->section != '' and $this->pathtosection[count($this->pathtosection) - 1] == $content->name) { - $this->expandjavascript .= 'expand('.$this->divcounter.');'."\n"; + $this->expandnodes[] = $this->divcounter; array_pop($this->pathtosection); } @@ -104,7 +104,6 @@ class block_admin_tree extends block_base { } function get_content() { - global $CFG; if ($this->content !== NULL) { @@ -127,100 +126,30 @@ class block_admin_tree extends block_base { // we need to do this instead of $this->build_tree($adminroot) because the top-level folder // is redundant (and ideally ignored). (the top-level folder is "administration".) - $entries = array_keys($adminroot->children); - asort($entries); - foreach ($entries as $entry) { $this->build_tree($adminroot->children[$entry]); } if ($this->tempcontent !== '') { - $closedimg = ''.s(get_string('folderclosed')).''; - $openedimg = ''.s(get_string('folderopened')).''; - + require_js(array('yui_yahoo','yui_event')); + require_js($CFG->wwwroot . '/blocks/admin_tree/admintree.js'); $this->content = new object(); - $this->content->text = ' - -
- -'.$this->tempcontent.' - -
-'; + $this->content->text = '
' . $this->tempcontent . "
\n"; + $this->content->text .= print_js_call('admin_tree.init', + array($this->divcounter - 1, $this->expandnodes, $CFG->pixpath, + get_string('folderopened'), get_string('folderclosed')), true); // only do search if you have moodle/site:config if (has_capability('moodle/site:config',get_context_instance(CONTEXT_SYSTEM)) ) { - $this->content->footer = '
'. - '
'. - ''. - ''. - '
'. - '
'; + $this->content->footer = + '
'. + '
'. + ''. + ''. + '
'. + '
'; } else { $this->content->footer = ''; } @@ -230,8 +159,6 @@ collapseall(); } return $this->content; - } } - ?> diff --git a/lib/weblib.php b/lib/weblib.php index 1d1884d342..1ac2faf4cc 100644 --- a/lib/weblib.php +++ b/lib/weblib.php @@ -2742,7 +2742,7 @@ function get_require_js_code($loadlibs) { function print_js_call($function, $args = array(), $return = false) { $quotedargs = array(); foreach ($args as $arg) { - $quotedargs[] = "'" . addslashes_js($arg) . "'"; + $quotedargs[] = json_encode($arg); } $html = ''; $html .= '