From 428acddb72e2e4bc169ad6db97428679ec4a64f4 Mon Sep 17 00:00:00 2001 From: samhemelryk Date: Wed, 8 Jul 2009 09:01:20 +0000 Subject: [PATCH] outputlib MDL-19740 Fixed up outputlib focus handling to deal with the three different ways of requesting focus. --- lib/javascript-static.js | 6 +++--- lib/outputlib.php | 11 +++++++---- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/lib/javascript-static.js b/lib/javascript-static.js index 6f7d951981..2c3bc83ab4 100644 --- a/lib/javascript-static.js +++ b/lib/javascript-static.js @@ -1048,9 +1048,9 @@ function focuscontrol(controlid) { * Transfers keyboard focus to an HTML element based on the old style style of focus * This function should be removed as soon as it is no longer used */ -function old_onload_focus(parentname, controlname) { - if (window[parentname]) { - window[parentname][controlname].focus(); +function old_onload_focus(formid, controlname) { + if (document.forms[formid]) { + document.forms[formid].elements[controlname].focus(); } } diff --git a/lib/outputlib.php b/lib/outputlib.php index 3b356f9aad..877b0aed16 100644 --- a/lib/outputlib.php +++ b/lib/outputlib.php @@ -1573,11 +1573,14 @@ class moodle_core_renderer extends moodle_renderer_base { $focus = $this->page->focuscontrol; if (!empty($focus)) { - $pos = strpos($focus, '.'); - if($pos !== false) { + if(preg_match("#forms\['([a-zA-Z0-9]+)'\].elements\['([a-zA-Z0-9]+)'\]#", $focus, $matches)) { + // This is a horrifically bad way to handle focus but it is passed in + // through messy formslib::moodleform + $this->page->requires->js_function_call('old_onload_focus', Array($matches[1], $matches[2])); + } else if (strpos($focus, '.')!==false) { // Old style of focus, bad way to do it - debugging('This code is using the old style focus event, Please update this code to focus on an element id', DEBUG_DEVELOPER); - $this->page->requires->js_function_call('old_onload_focus', Array(substr($focus, 0, $pos), substr($focus, $pos))); + debugging('This code is using the old style focus event, Please update this code to focus on an element id or the moodleform focus method.', DEBUG_DEVELOPER); + $this->page->requires->js_function_call('old_onload_focus', explode('.', $focus, 2)); } else { // Focus element with given id $this->page->requires->js_function_call('focuscontrol', Array($focus)); -- 2.39.5