]> git.mjollnir.org Git - moodle.git/commitdiff
user selector: MDL-16966 Work round IE bug. Also, deal with reload case.
authortjhunt <tjhunt>
Wed, 5 Nov 2008 02:16:07 +0000 (02:16 +0000)
committertjhunt <tjhunt>
Wed, 5 Nov 2008 02:16:07 +0000 (02:16 +0000)
Listen you morons in Redmond: getElementById does not mean getElementByIdOrNameOrAnythingElseRandomWeFeelLike. Oh well. I have done a work-around.

user/selector/lib.php
user/selector/script.js

index ea9ec7dba3f1c9d53b3da211c0a0eebd6dc767b7..2a6ad70e37f031e756e8f65dcb03d98550bfdeb7 100644 (file)
@@ -205,7 +205,7 @@ abstract class user_selector_base {
         $output .= "</div>\n</div>\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 = '<p><input type="hidden" name="' . $name . '" value="0" />' .
-                '<input type="checkbox" id="' . $name . '" name="' . $name . '" value="1"' . $checked . ' /> ' .
+                // 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".
+                '<input type="checkbox" id="' . $name . 'id" name="' . $name . '" value="1"' . $checked . ' /> ' .
                 '<label for="' . $name . '">' . $label . "</label></p>\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;
index 46671893a0b59dbdb6f2fff269ba6a2c96116680..dd8bac2978f56bec23999ba64c1bb26e04aadd14 100644 (file)
@@ -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.<b> 
  */
 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