annotate popups_reference.js @ 10:108ced764a40 tip

Fix compatibility with CCK Multigroup
author Franck Deroche <franck@defr.org>
date Thu, 26 Mar 2009 14:59:27 +0100
parents 990f71344a66
children
rev   line source
franck@5 1 // $Id: popups_reference.js,v 1.1.2.3 2009/03/06 01:54:08 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@5 41 var title = decodeURIComponent(popups_reference_get_cookie_value('PopupRefTitle'));
franck@0 42 $wrapper.find('select').val(nid); // Select
franck@0 43 $wrapper.find(':radio[value=' + nid + ']').select(); // Radio buttons
franck@5 44
franck@5 45 // Get the first empty autocomplete field to fill (http://drupal.org/node/388406).
franck@5 46 $emptyAutos = $wrapper.find('input.form-autocomplete').filter(function(i) {
franck@5 47 return !$(this).val();
franck@5 48 });
franck@5 49 if ($emptyAutos.length) {
franck@5 50 $emptyAutos.eq(0).val(title);
franck@5 51 }
franck@5 52 else { // There are no empty fields, use the first one.
franck@5 53 $wrapper.find('input.form-autocomplete:first').val(title);
franck@5 54 }
franck@0 55 });
franck@0 56 });
franck@0 57 });
franck@0 58 };
franck@0 59