view popups_reference.js @ 2:92ed2a629336

Only update one autocomplete field after successful node creation Before the patch, the title of the newly created node would appear in every inputs of the field, which wasn't all that usefull. The code now only updates one field, and /tries/ to be smart when making the choice of which one to update: if there's an empty field, we'll put the value there. If there isn't, then we'll use the first one. Ideally, the code would check if the field is configured for "Unlimited" values, and if it is, try to add a new input instead of blindly overwritting potentially valuable user input.
author Franck Deroche <franck@defr.org>
date Wed, 21 Jan 2009 14:17:45 +0100
parents ece8e4be4d6f
children 990f71344a66
line wrap: on
line source
// $Id: popups_reference.js,v 1.1.2.1 2009/01/18 22:40:33 starbow Exp $

/**
 * Popups: Add and Reference behavior
 * 
 * Adds the behavior of selecting the newly created node.
 */

/**
 * Parse the cookies to find a value.
 *
 * @param name of cookie value.
 */ 
function popups_reference_get_cookie_value(name) {
  name += '=';
  var cookies = document.cookie.split(';');
  for (var i=0; i < cookies.length; i++) {
    var cookie = jQuery.trim(cookies[i]);
    if (cookie.indexOf(name) === 0) {
      return cookie.substring(name.length, cookie.length);
    }
  }
}

/**
 * Attach the behavior.
 */
Drupal.behaviors.popups_reference = function(context) {
  $('.popups-reference', context).not('.popups-reference-processed').each(function() {
    $(this).addClass('popups-reference-processed'); // Don't re-add to processed links.
    $(this).click(function() {
      var rel = $(this).attr('rel'); // Rel attribute of the clicked link is the wrapper id.
      var $wrapper = $('#' + rel);
      // Unbind namespaced event, so bindings don't pile up every click.
      $(document).unbind('popups_form_success.popups_reference');
      
      // Bind to the popups API custom form_success event.
      $(document).bind('popups_form_success.popups_reference', function() {
        // Info about the new node was placed in a cookie when it was created.
        var nid = popups_reference_get_cookie_value('PopupRefNid');
        var title = popups_reference_get_cookie_value('PopupRefTitle');
        title = decodeURIComponent(title);
        $wrapper.find('select').val(nid); // Select
        $wrapper
	  .find('input.form-autocomplete:not([value]), input.form-autocomplete')
	  .filter(':first') // Only update the first item in the set
	  .val(title); // Autocomplete
        $wrapper.find(':radio[value=' + nid + ']').select(); // Radio buttons
      });
    });
  });
};