diff popups_reference.js @ 0:56772e0a00ae

Import of dev version (2009-01-19)
author Franck Deroche <franck@defr.org>
date Wed, 21 Jan 2009 11:19:01 +0100
parents
children ece8e4be4d6f e1318a313b1d
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/popups_reference.js	Wed Jan 21 11:19:01 2009 +0100
@@ -0,0 +1,49 @@
+// $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');
+        $wrapper.find('select').val(nid); // Select
+        $wrapper.find('input.form-autocomplete').val(title); // Autocomplete
+        $wrapper.find(':radio[value=' + nid + ']').select(); // Radio buttons
+      });
+    });
+  });
+};
+