view sites/all/modules/custom/solrsearch_autocomplete/solrsearch_autocomplete.js @ 0:015d06b10d37 default tip

initial
author dwinter
date Wed, 31 Jul 2013 13:49:13 +0200
parents
children
line wrap: on
line source


/**
 * Adds the custom autocomplete widget behavior.
 */
Drupal.behaviors.solrsearch_autocomplete = {
  attach: function(context) {
	
    jQuery(".solrsearch-autocomplete.unprocessed", context).add(".solrsearch-autocomplete.unprocessed input", context).autocomplete(Drupal.settings.solrsearch_autocomplete.path,
    {
      // Classnames for the widget.
      extraParams : {fieldName:function(){
    	  classes = document.activeElement.getAttribute("class");
    	  splitted =classes.split(" ");
    	  for (var i=0; i <splitted.length; i++){
    		    if (splitted[i].match("^solrsearch-autocomplete.field.")){
    		    	return splitted[i].replace("solrsearch-autocomplete.field.","");
    		    }
      			};
    	  return "IM_author";
      }},
      inputClass: "",
      loadingClass: "throbbing",
      // Do not select first suggestion by default.
      selectFirst: false,
      // Specify no matching as it wil be done on server-side.
      matchContains: false,
      matchSubset: false,
      // Maximum number of items to show in widget.
      max: 50,
      scroll: true,
      scrollHeight: 360,
      // Data returned from server is JSON-encoded.
      dataType: "json",
      // Function to parse returned json into elements.
      parse: function(data) {
        return jQuery.map(data, function(item) {
          return {
            data: item,          // Echo the input data.
            value: item.display, // This will be shown in the options widget.
            result: item.key     // The actual value to put into the form element.
          }
        });
      },
      // Return the HTML to display in the options widget.
      formatItem: function(item) {
        return item.display;
      }
    }).result(function(item, element) {
      // Handle selection of an element in the autocomplete widget.
      // We should submit the widget's parent form.
      jQuery(this).get(0).form.submit();
    }).addClass('form-autocomplete'); // Add Drupal autocomplete widget's style.
  }
};