From 4b761efbcd80953b3956f452855880246be3bd53 Mon Sep 17 00:00:00 2001
From: James Elliott <james.elliott@acquia.com>
Date: Mon, 25 Apr 2011 20:06:29 -0400
Subject: [PATCH 14/15] woohoo got WYSIWYG up and functioning

---
 includes/media.filter.inc |   14 +++++++--
 js/media.format_form.js   |   72 +++++++++++++++++++++++++--------------------
 js/wysiwyg-media.js       |   42 ++++++++++++++++++--------
 3 files changed, 80 insertions(+), 48 deletions(-)

diff --git a/includes/media.filter.inc b/includes/media.filter.inc
index cdc9a39..414900b 100644
--- a/includes/media.filter.inc
+++ b/includes/media.filter.inc
@@ -198,6 +198,7 @@ function media_pre_render_text_format($element) {
   if (isset($tagmap)) {
     drupal_add_js(array('tagmap' => array_unique($tagmap)), 'setting');
   }
+
   return $element;
 }
 
@@ -249,8 +250,15 @@ function media_format($media) {
       'data' => drupal_render(drupal_get_form('media_format_form', $media)),
     );
   } else {
+    list($form, $form_state) = ajax_get_form();
+    drupal_process_form($form['#form_id'], $form, $form_state);
+    
     $commands[] = array(
       'command' => 'mediaFormatSelected',
+      'file' => $media,
+      'viewMode' => $form_state['values']['format'],
+      'formattedMedia' => drupal_render(media_get_file_without_label($media, $form_state['values']['format'])),
+      'instanceId' => $form_state['values']['trigger'],
     );
   }
   
@@ -311,7 +319,7 @@ function media_format_form($form, $form_state, $media) {
   drupal_add_library('system', 'form');
 
   $path = drupal_get_path('module', 'media');
-  $form['#attached']['js'][] = $path . '/js/media.format_form.js';
+  #$form['#attached']['js'][] = $path . '/js/media.format_form.js';
   $form['#attached']['css'][] = $path . '/css/media-format-form.css';
 
   $form['heading'] = array(
@@ -354,7 +362,7 @@ function media_format_form($form, $form_state, $media) {
   }
   
   $form['actions'] = array(
-    $form_id . '-submit' => array(
+    'submit' => array(
       '#type' => 'submit',
       '#value' => t('Okay'),
       '#ajax' => array(
@@ -362,7 +370,7 @@ function media_format_form($form, $form_state, $media) {
       ),
       '#name' => 'process',
     ),
-    $form_id . '-cancel' => array(
+    'cancel' => array(
       '#type' => 'button',
       '#value' => t('Cancel'),
       '#name' => 'cancel',
diff --git a/js/media.format_form.js b/js/media.format_form.js
index 43ea6e0..d9cae89 100644
--- a/js/media.format_form.js
+++ b/js/media.format_form.js
@@ -6,37 +6,45 @@
  */
 
 (function ($) {
-
-Drupal.media = Drupal.media || {};
-Drupal.media.formatForm = Drupal.media.formatForm || {};
-
-Drupal.media.mediaFormatSelected = {};
-
-Drupal.behaviors.mediaFormatForm = {
-  attach: function (context, settings) {
-    
-  }
-};
-
-Drupal.media.formatForm.getOptions = function () {
-  // Get all the values
-  var ret = {}; $.each($('#media-format-form fieldset#edit-options *').serializeArray(), function (i, field) { ret[field.name] = field.value; });
-  return ret;
-};
-
-Drupal.media.formatForm.getFormattedMedia = function () {
-  var formatType = $("select#edit-format option:selected").val();
-  return { type: formatType, options: Drupal.media.formatForm.getOptions(), html: Drupal.settings.media.formatFormFormats[formatType] };
-};
-
-Drupal.media.formatForm.submit = function () {
-  // @see Drupal.behaviors.mediaFormatForm.attach().
-  var buttons = $(parent.window.document.body).find('#mediaStyleSelector').parent('.ui-dialog').find('.ui-dialog-buttonpane button');
-  if ($(this).hasClass('fake-cancel')) {
-    buttons[1].click();
-  } else {
-    buttons[0].click();
-  }
-}
+  
+//Drupal.media = Drupal.media || {};
+//Drupal.media.formatForm = Drupal.media.formatForm || {};
+//
+//Drupal.media.mediaFormatSelected = {};
+//
+//Drupal.behaviors.mediaFormatForm = {
+//  attach: function (context, settings) {
+//    // Add "Submit" and "Cancel" buttons inside the IFRAME that trigger the
+//    // behavior of the hidden "OK" and "Cancel" buttons that are outside the
+//    // IFRAME. See Drupal.media.browser.validateButtons() for more details.
+//    $('<a class="button fake-ok">Submit</a>').appendTo($('#media-format-form')).bind('click', Drupal.media.formatForm.submit);
+//    $('<a class="button fake-cancel">Cancel</a>').appendTo($('#media-format-form')).bind('click', Drupal.media.formatForm.submit);
+//
+//    if (Drupal.settings.media_format_form.autosubmit) {
+//      $('.button.fake-ok').click();
+//    }
+//  }
+//};
+//
+//Drupal.media.formatForm.getOptions = function () {
+//  // Get all the values
+//  var ret = {}; $.each($('#media-format-form fieldset#edit-options *').serializeArray(), function (i, field) { ret[field.name] = field.value; });
+//  return ret;
+//};
+//
+//Drupal.media.formatForm.getFormattedMedia = function () {
+//  var formatType = $("select#edit-format option:selected").val();
+//  return { type: formatType, options: Drupal.media.formatForm.getOptions(), html: Drupal.settings.media.formatFormFormats[formatType] };
+//};
+//
+//Drupal.media.formatForm.submit = function () {
+//  // @see Drupal.behaviors.mediaFormatForm.attach().
+//  var buttons = $(parent.window.document.body).find('#mediaStyleSelector').parent('.ui-dialog').find('.ui-dialog-buttonpane button');
+//  if ($(this).hasClass('fake-cancel')) {
+//    buttons[1].click();
+//  } else {
+//    buttons[0].click();
+//  }
+//}
 
 })(jQuery);
diff --git a/js/wysiwyg-media.js b/js/wysiwyg-media.js
index 97dadf2..bd51f0e 100644
--- a/js/wysiwyg-media.js
+++ b/js/wysiwyg-media.js
@@ -8,6 +8,11 @@
 
 Drupal.media = Drupal.media || {};
 
+Drupal.ajax.prototype.commands.mediaFormatSelected = function (ajax, response, status) {
+  Drupal.wysiwyg.plugins.media.insertMediaFile(response.file, response.viewMode, response.formattedMedia, {}, response.instanceId);
+  $('#media-browser-wrapper').dialog('close');
+};
+
 // Define the behavior.
 Drupal.wysiwyg.plugins.media = {
 
@@ -25,16 +30,7 @@ Drupal.wysiwyg.plugins.media = {
    */
   invoke: function (data, settings, instanceId) {
     if (data.format == 'html') {
-//      if (typeof Drupal.ajax[instanceId] == 'undefined') {
-//        var element_settings = Drupal.media.dialog.ajaxDefaults();
-//        element_settings.event = 'launchWYSIWYG';
-//        
-//        settings.trigger = instanceId;
-//        element_settings.submit = settings;
-//        
-//        Drupal.ajax[instanceId] = new Drupal.ajax(instanceId, $('#' + instanceId), element_settings);
-//      }
-//      $('#' + instanceId).bind('mediaBrowserSelect', Drupal.wysiwyg.plugins.media.mediaBrowserOnSelect).trigger('launchWYSIWYG');
+      $('#' + instanceId).trigger('launchBrowser');
     }
   },
 
@@ -44,11 +40,18 @@ Drupal.wysiwyg.plugins.media = {
    */
   mediaBrowserOnSelect: function (event, data) {
     var mediaFile = data.files[0];
-    // Need to figure out some way of launching the format form    
+    // Need to figure out some way of launching the format form
+    var instanceId = $(this).attr('id');
+    
+    Drupal.ajax[instanceId+'-ff'].element_settings.url = '/media/' + mediaFile.fid + '/format-form';
+    Drupal.ajax[instanceId+'-ff'].options.url = '/media/' + mediaFile.fid + '/format-form';
+    
+    $(this).trigger('launchFF');
+    
     return;
   },
 
-  insertMediaFile: function (mediaFile, viewMode, formattedMedia, options, wysiwygInstance) {
+  insertMediaFile: function (mediaFile, viewMode, formattedMedia, options, instanceId) {
 
     this.initializeTagMap();
     // @TODO: the folks @ ckeditor have told us that there is no way
@@ -65,7 +68,7 @@ Drupal.wysiwyg.plugins.media = {
     var inlineTag = Drupal.wysiwyg.plugins.media.createTag(imgElement);
     // Add it to the tag map in case the user switches input formats
     Drupal.settings.tagmap[inlineTag] = toInsert;
-    wysiwygInstance.insert(toInsert);
+    CKEDITOR.instances[instanceId].insertHtml(toInsert);
   },
 
   /**
@@ -161,6 +164,19 @@ Drupal.wysiwyg.plugins.media = {
         }
       }
     }
+    // Attach behaviors for dialogs
+    if (typeof Drupal.ajax[instanceId] == 'undefined') {
+      $('#' + instanceId).bind('mediaBrowserSelect', Drupal.wysiwyg.plugins.media.mediaBrowserOnSelect);
+      var element_settings = Drupal.media.dialog.ajaxDefaults();
+      element_settings.event = 'launchBrowser';
+      settings.trigger = instanceId;
+      element_settings.submit = settings;
+      Drupal.ajax[instanceId] = new Drupal.ajax(instanceId, $('#' + instanceId), element_settings);
+
+      element_settings.event = 'launchFF';
+      Drupal.ajax[instanceId+'-ff'] = new Drupal.ajax(instanceId, $('#' + instanceId), element_settings);
+    }
+
     return content;
   },
 
-- 
1.7.4.msysgit.0

