Fix for AJAX/AHAH event handlers for Wysiwyg integration in D6

  1. // $Id$
  2.  
  3. /*
  4.   drupal_add_js(this_file) on the form for which this is needed.
  5.   Customize the CHANGE ME parts to deal with the relevant fields.
  6. */
  7.  
  8. /*
  9.  * Attach AJAX/AHAH event handlers for Wysiwyg integration.
  10.  *
  11.  * This code takes care of Wysiwyg integration in D6.
  12.  * Wysiwyg module needs to be able to detach any attached editor before the
  13.  * AJAX request is made or it cannot be re-attached afterwards.
  14.  * Drupal 6 does not have detach behaviors so we need to manually call
  15.  * Drupal.wysiwygDetach(). Wysiwyg's own behavior won't be able to
  16.  * re-attach the editor either because the context doesn't include the
  17.  * format selector so we call Drupal.wysiwygAttach() manually too.
  18.  * Drupal's jquery.form.js does not trigger 'form-pre-serialize' so we must
  19.  * re-serialize the form after detaching the editor in 'ajaxSend'.
  20.  *
  21.  * @param context
  22.  *   A DOM element, supplied by Drupal.attachBehaviors().
  23.  *
  24.  * This code is based on Drupal.behaviors.attachWysiwyg from Wysiwyg module.
  25.  * @see Drupal.behaviors.attachWysiwyg in Wysiwyg module.
  26.  */
  27. Drupal.behaviors.markup_snippets = function (context) {
  28.   var params;
  29.   // Add a global AJAX listener for attaching/detaching Wysiwyg module.
  30.   $('#edit-markup-snippets-select-wrapper:not(.markup-snippets-processed)', context).each(function () { // CHANGE ME: Replace markup-snippets.... with any other element (prefereably one created by your module) which will not be modified by the AHAH call. This is just an anchor point for the event handler.
  31.     $(this).addClass('markup-snippets-processed').bind('ajaxSend', function (event, XMLHttpRequest, ajaxOptions) { // CHANGE ME: See above, add class to not bind twice if behaviors are run again.
  32.       // Detach the Wysiwyg module if this is our request.
  33.       if (ajaxOptions.url == '/markup_snippets/js') { // CHANGE ME: Replace with your AHAH URL to catch only the requests going there.
  34.         params = $.extend({}, Drupal.wysiwyg.instances['edit-body']); // CHANGE ME: Replace 'edit-body' with the id of the Wysiwyged:ed field.
  35.         if (params.editor == 'none') {
  36.           // No editor instance, get parameters from the selected input format.
  37.           params = Drupal.wysiwyg.getParams($(':input.wysiwyg-field-edit-body:checked, div.wysiwyg-field-edit-body', context).get(0)); // CHANGE ME: Replace 'edit-body' with the name of the Wysiwyg:ed field.
  38.           // Override status to keep the editor disabled when re-attaching.
  39.           params.status = false;
  40.         }
  41.         Drupal.wysiwygDetach(context, params);
  42.         // Re-serialize the form. Fields must be enabled to be included in POST data.
  43.         $("#edit-markup-snippets-select).attr('disabled', false); // CHANGE ME: The id of the element triggering AHAH.
  44.        ajaxOptions.data = $('#edit-body').parents('form').formSerialize();
  45.        $("#edit-markup-snippets-select").attr('disabled', true); // CHANGE ME: The id of the element triggering AHAH.
  46.      }
  47.    }).bind('ajaxComplete', function (event, XMLHttpRequest, ajaxOptions) {
  48.      // Re-attach the Wysiwyg module if this is our response.
  49.      if (ajaxOptions.url == '/markup_snippets/js') { // CHANGE ME: See above.
  50.        Drupal.wysiwygAttach(context, params);
  51.      }
  52.    });
  53.  });
  54. }

Submit Fix

Any tags you'd like to associate with your code, delimitered by commas (example: Views, CCK, Module, etc).
Select the syntax highlighting mode to use.