franck@0: // $Id: popups_reference.js,v 1.1.2.1 2009/01/18 22:40:33 starbow Exp $ franck@0: franck@0: /** franck@0: * Popups: Add and Reference behavior franck@0: * franck@0: * Adds the behavior of selecting the newly created node. franck@0: */ franck@0: franck@0: /** franck@0: * Parse the cookies to find a value. franck@0: * franck@0: * @param name of cookie value. franck@0: */ franck@0: function popups_reference_get_cookie_value(name) { franck@0: name += '='; franck@0: var cookies = document.cookie.split(';'); franck@0: for (var i=0; i < cookies.length; i++) { franck@0: var cookie = jQuery.trim(cookies[i]); franck@0: if (cookie.indexOf(name) === 0) { franck@0: return cookie.substring(name.length, cookie.length); franck@0: } franck@0: } franck@0: } franck@0: franck@0: /** franck@0: * Attach the behavior. franck@0: */ franck@0: Drupal.behaviors.popups_reference = function(context) { franck@0: $('.popups-reference', context).not('.popups-reference-processed').each(function() { franck@0: $(this).addClass('popups-reference-processed'); // Don't re-add to processed links. franck@0: $(this).click(function() { franck@0: var rel = $(this).attr('rel'); // Rel attribute of the clicked link is the wrapper id. franck@0: var $wrapper = $('#' + rel); franck@0: // Unbind namespaced event, so bindings don't pile up every click. franck@0: $(document).unbind('popups_form_success.popups_reference'); franck@0: franck@0: // Bind to the popups API custom form_success event. franck@0: $(document).bind('popups_form_success.popups_reference', function() { franck@0: // Info about the new node was placed in a cookie when it was created. franck@0: var nid = popups_reference_get_cookie_value('PopupRefNid'); franck@0: var title = popups_reference_get_cookie_value('PopupRefTitle'); franck@1: title = decodeURIComponent(title); franck@0: $wrapper.find('select').val(nid); // Select franck@0: $wrapper.find('input.form-autocomplete').val(title); // Autocomplete franck@0: $wrapper.find(':radio[value=' + nid + ']').select(); // Radio buttons franck@0: }); franck@0: }); franck@0: }); franck@0: }; franck@0: