Advertisement
Guest User

Plone jQueryUI RelationChoice Widget fix

a guest
May 14th, 2013
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function formwidget_autocomplete_new_value(input_box, value, label) {
  2.     console.log(input_box, value, label);
  3.    
  4.     (function($) { 
  5.     /* Hack around bug as described on
  6.      * http://stackoverflow.com/questions/15651455/plone-dexterity-relationchoice-widget-clashes-with-jqueryui/16516223#16516223
  7.      *
  8.      * Fortunately, the autocomplete widget is easy to detect, because it
  9.      * leaves the "label" argument undefined.
  10.      * */
  11.     if (!label) {
  12.         label = value["item"]["label"];
  13.         value = value["item"]["value"];
  14.         input_box = $(input_box.target);
  15.     }
  16.  
  17.         var base_id = input_box[0].id.replace(/-widgets-query$/,"");
  18.         var base_name = input_box[0].name.replace(/\.widgets\.query$/,"");
  19.         var widget_base = $('#'+base_id+"-input-fields");
  20.  
  21.         var all_fields = widget_base.find('input:radio, input:checkbox');
  22.        
  23.         // Clear query box and uncheck any radio boxes
  24.         input_box.val("");
  25.         widget_base.find('input:radio').attr('checked', '');
  26.        
  27.         // If a radio/check box for this value already exists, check it.
  28.         var selected_field = widget_base.find('input[value="' + value + '"]');
  29.         if(selected_field.length) {
  30.             selected_field.each(function() { this.checked = true; });
  31.             return;
  32.         }
  33.  
  34.         widget_base, base_name, base_id
  35.         // Create the box for this value
  36.         var idx = all_fields.length;
  37.         var klass = widget_base.data('klass');
  38.         var title = widget_base.data('title');
  39.         var type = widget_base.data('input_type');
  40.         var span = $('<span/>').attr("id",base_id+"-"+idx+"-wrapper").attr("class","option");
  41.         span.append($("<label/>").attr("for",base_id+"-"+idx)
  42.                                  .append($('<input>').attr("type",type)
  43.                                                      .attr("id",base_id+"-"+idx)
  44.                                                      .attr("name",base_name+":list")
  45.                                                      .attr("class",klass)
  46.                                                      .attr("title",title)
  47.                                                      .attr("checked","checked")
  48.                                                      .attr("value",value)
  49.                                                      )
  50.                                  .append(" ")
  51.                                  .append($("<span>").attr("class","label").text(label))
  52.                                  );
  53.         widget_base.append(span);
  54.     // In respect of the source.
  55.     if (!label) {
  56.         // data["item"]["value"] = ""  
  57.         label = "";
  58.     }
  59.    
  60.     }(jQuery));
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement