From 1acd8f50f7c2345828ac832729eec27794c03af1 Mon Sep 17 00:00:00 2001 From: tjhunt Date: Wed, 5 Nov 2008 02:16:07 +0000 Subject: [PATCH] user selector: MDL-16966 Work round IE bug. Also, deal with reload case. Listen you morons in Redmond: getElementById does not mean getElementByIdOrNameOrAnythingElseRandomWeFeelLike. Oh well. I have done a work-around. --- user/selector/lib.php | 12 +++++++----- user/selector/script.js | 26 +++++++++++++++++--------- 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/user/selector/lib.php b/user/selector/lib.php index ea9ec7dba3..2a6ad70e37 100644 --- a/user/selector/lib.php +++ b/user/selector/lib.php @@ -205,7 +205,7 @@ abstract class user_selector_base { $output .= "\n\n\n"; // Initialise the ajax functionality. - $output .= $this->initialise_javascript(); + $output .= $this->initialise_javascript($search); // Return or output it. if ($return) { @@ -539,7 +539,9 @@ abstract class user_selector_base { } $name = 'userselector_' . $name; $output = '

' . - ' ' . + // For the benefit of brain-dead IE, the id must be different from the name of the hidden form field above. + // It seems that document.getElementById('frog') in IE will return and element with name="frog". + ' ' . '

\n"; user_preference_allow_ajax_update($name, PARAM_BOOL); return $output; @@ -549,7 +551,7 @@ abstract class user_selector_base { * @param boolean $optiontracker if true, initialise JavaScript for updating the user prefs. * @return any HTML needed here. */ - protected function initialise_javascript() { + protected function initialise_javascript($search) { global $USER; $output = ''; @@ -559,8 +561,8 @@ abstract class user_selector_base { $USER->userselectors[$hash] = $options; // Initialise the selector. - $output .= print_js_call('new user_selector', array($this->name, $hash, - $this->extrafields, get_string('previouslyselectedusers', '', '%%SEARCHTERM%%'), + $output .= print_js_call('new user_selector', array($this->name, $hash, $this->extrafields, + $search, get_string('previouslyselectedusers', '', '%%SEARCHTERM%%'), get_string('nomatchingusers', '', '%%SEARCHTERM%%')), true); return $output; diff --git a/user/selector/script.js b/user/selector/script.js index 46671893a0..dd8bac2978 100644 --- a/user/selector/script.js +++ b/user/selector/script.js @@ -11,7 +11,7 @@ * @param Array extrafields extra fields we are displaying for each user in addition to fullname. * @param String label used for the optgroup of users who are selected but who do not match the current search. */ -function user_selector(name, hash, extrafields, strprevselected, strnomatchingusers) { +function user_selector(name, hash, extrafields, lastsearch, strprevselected, strnomatchingusers) { this.name = name; this.extrafields = extrafields; this.strprevselected = strprevselected; @@ -40,7 +40,7 @@ function user_selector(name, hash, extrafields, strprevselected, strnomatchingus // Hook up the event handler for when the search text changes. var oself = this; YAHOO.util.Event.addListener(this.searchfield, "keyup", function(e) { oself.handle_keyup(e) }); - this.lastsearch = this.get_search_text(); + this.lastsearch = lastsearch; // Define our custom event. this.createEvent('selectionchanged'); @@ -52,8 +52,8 @@ function user_selector(name, hash, extrafields, strprevselected, strnomatchingus YAHOO.util.Event.addListener(this.listbox, "click", function(e) { oself.handle_selection_change() }); YAHOO.util.Event.addListener(this.listbox, "change", function(e) { oself.handle_selection_change() }); - // And when the search any substring preference changes. Do an immediate research. - YAHOO.util.Event.addListener('userselector_searchanywhere', 'click', function(e) { oself.handle_searchanywhere_change() }); + // And when the search any substring preference changes. Do an immediate re-search. + YAHOO.util.Event.addListener('userselector_searchanywhereid', 'click', function(e) { oself.handle_searchanywhere_change() }); // Replace the Clear submit button with a clone that is not a submit button. var oldclearbutton = document.getElementById(this.name + '_clearbutton'); @@ -70,6 +70,14 @@ function user_selector(name, hash, extrafields, strprevselected, strnomatchingus // Hook up the event handler for the clear button. YAHOO.util.Event.addListener(this.clearbutton, "click", function(e) { oself.handle_clear() }); + + // If the contents of the search box is different from the search that was + // done on the server, reload the options. (This happens, for example, + // in Firefox. Go to the role assign page - the search box will be blank. + // Type something in the search box. Then click reload. The text will stay + // in the search box, but the server does not know about the new search term, + // so without this line, the list would contain all the users. + this.send_query(false); } /** @@ -277,7 +285,7 @@ user_selector.prototype.get_search_text = function() { * @return the value of one of the option checkboxes. */ user_selector.prototype.get_option = function(name) { - var checkbox = document.getElementById('userselector_' + name); + var checkbox = document.getElementById('userselector_' + name + 'id'); if (checkbox) { return checkbox.checked; } else { @@ -474,14 +482,14 @@ YAHOO.lang.augmentProto(user_selector, YAHOO.util.EventProvider); */ function user_selector_options_tracker() { var oself = this; - YAHOO.util.Event.addListener('userselector_preserveselected', 'click', + YAHOO.util.Event.addListener('userselector_preserveselectedid', 'click', function(e) { oself.handle_option_change('userselector_preserveselected') }); - YAHOO.util.Event.addListener('userselector_autoselectunique', 'click', + YAHOO.util.Event.addListener('userselector_autoselectuniqueid', 'click', function(e) { oself.handle_option_change('userselector_autoselectunique') }); - YAHOO.util.Event.addListener('userselector_searchanywhere', 'click', + YAHOO.util.Event.addListener('userselector_searchanywhereid', 'click', function(e) { oself.handle_option_change('userselector_searchanywhere') }); } user_selector_options_tracker.prototype.handle_option_change = function(option) { - set_user_preference(option, document.getElementById(option).checked); + set_user_preference(option, document.getElementById(option + 'id').checked); } \ No newline at end of file -- 2.39.5