annotate 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
rev   line source
franck@0 1 // $Id: popups_reference.js,v 1.1.2.1 2009/01/18 22:40:33 starbow Exp $
franck@0 2
franck@0 3 /**
franck@0 4 * Popups: Add and Reference behavior
franck@0 5 *
franck@0 6 * Adds the behavior of selecting the newly created node.
franck@0 7 */
franck@0 8
franck@0 9 /**
franck@0 10 * Parse the cookies to find a value.
franck@0 11 *
franck@0 12 * @param name of cookie value.
franck@0 13 */
franck@0 14 function popups_reference_get_cookie_value(name) {
franck@0 15 name += '=';
franck@0 16 var cookies = document.cookie.split(';');
franck@0 17 for (var i=0; i < cookies.length; i++) {
franck@0 18 var cookie = jQuery.trim(cookies[i]);
franck@0 19 if (cookie.indexOf(name) === 0) {
franck@0 20 return cookie.substring(name.length, cookie.length);
franck@0 21 }
franck@0 22 }
franck@0 23 }
franck@0 24
franck@0 25 /**
franck@0 26 * Attach the behavior.
franck@0 27 */
franck@0 28 Drupal.behaviors.popups_reference = function(context) {
franck@0 29 $('.popups-reference', context).not('.popups-reference-processed').each(function() {
franck@0 30 $(this).addClass('popups-reference-processed'); // Don't re-add to processed links.
franck@0 31 $(this).click(function() {
franck@0 32 var rel = $(this).attr('rel'); // Rel attribute of the clicked link is the wrapper id.
franck@0 33 var $wrapper = $('#' + rel);
franck@0 34 // Unbind namespaced event, so bindings don't pile up every click.
franck@0 35 $(document).unbind('popups_form_success.popups_reference');
franck@0 36
franck@0 37 // Bind to the popups API custom form_success event.
franck@0 38 $(document).bind('popups_form_success.popups_reference', function() {
franck@0 39 // Info about the new node was placed in a cookie when it was created.
franck@0 40 var nid = popups_reference_get_cookie_value('PopupRefNid');
franck@0 41 var title = popups_reference_get_cookie_value('PopupRefTitle');
franck@1 42 title = decodeURIComponent(title);
franck@0 43 $wrapper.find('select').val(nid); // Select
franck@0 44 $wrapper.find('input.form-autocomplete').val(title); // Autocomplete
franck@0 45 $wrapper.find(':radio[value=' + nid + ']').select(); // Radio buttons
franck@0 46 });
franck@0 47 });
franck@0 48 });
franck@0 49 };
franck@0 50