view popups_reference.js @ 1:ece8e4be4d6f

Fix node titles after the creation
author Franck Deroche <franck@defr.org>
date Wed, 21 Jan 2009 11:34:38 +0100
parents 56772e0a00ae
children 92ed2a629336
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').val(title); // Autocomplete
        $wrapper.find(':radio[value=' + nid + ']').select(); // Radio buttons
      });
    });
  });
};