diff js/dnd-library.js @ 4:c2eb995212bf

TONS of fixes in this commit.
author David Eads <eads@chicagotech.org>
date Thu, 19 Feb 2009 12:33:19 -0600
parents 5a44c430b7ac
children 99ba5941779c
line wrap: on
line diff
--- a/js/dnd-library.js	Tue Feb 17 15:46:36 2009 -0600
+++ b/js/dnd-library.js	Thu Feb 19 12:33:19 2009 -0600
@@ -1,64 +1,93 @@
 Drupal.behaviors.dndLibrary = function(context) {
-  var libraries = Drupal.settings.dndEnabledLibraries;
+  $('.dnd-library-wrapper:not(.dnd-processed)', context).each(function() {
+    var $this = $(this);
 
-  // Loop over libraries keyed by the textarea they are associated with
-  for (var textarea_id in libraries) {
+    // This is a bad hack to lop off '-dnd-library' from the id to get the editor name
+    var editor = this.id.slice(0, -12); 
 
-    var library = $('#' + textarea_id + '-dnd-library');
+    // Attempt to attach library
+    //Drupal.behaviors.dndLibrary.attach_library(false, Drupal.wysiwyg.instances[editor]);
 
-    // Ajax pager:  This seems like it is bad to be here
-    $('.pager a', library).click(function(e, data) {
+    // Bind Drag and Drop plugin invocation to wywsiwygAttach event
+    $('#' + editor).bind('wysiwygAttach', Drupal.behaviors.dndLibrary.attach_library);
+
+    // @TODO track and clear intervals to save memory at the cost of 
+    // more processing?
+    $('#' + editor).bind('wysiwygDetach', Drupal.behaviors.dndLibrary.detach_library);
+
+    // Ajax pager
+    $('.pager a', $this).click(function(e, data) {
       $.getJSON(this.href, function(data) {
-        $('.library', library).html('');
-        for (item in data.library) {
-          $('.library', library).append(data.library[item].library);
-          Drupal.settings.dndEditorRepresentations[item] = data.library[item].editor;
-          Drupal.behaviors.dndLibrary(context);  // Durn event reattachment
+        $('.header', $this).html(data.header);
+        $('.library', $this).html(data.library);
+        //$('.footer', $this).html(data.footer);
+        for (editor_id in data.editor_representations) {
+          Drupal.settings.dndEditorRepresentations[editor_id] = data.editor_representations[editor_id];
         }
+        var params = Drupal.wysiwyg.instances[editor];
+        $('#' + editor).trigger('wysiwygAttach', params);
       });
       return false;
     });
 
-    // Bind Drag and Drop plugin invocation to wywsiwygAttach event
-    $('#' + textarea_id).bind('wysiwygAttach', function(e, data) {
-      var settings = {
-        renderRepresentation: function(target, drop, representation_id) {
-          console.log(representation_id);
-          return Drupal.settings.dndEditorRepresentations[representation_id];
-        }
-      }
-      settings = $.extend(settings, libraries[textarea_id]);
+    $this.addClass('dnd-processed');
+  });
+}
 
-      // Get editor attachment function
-      var editor_fn = 'attach_' + data.editor;
-      if ($.isFunction(window.Drupal.behaviors.dndLibrary[editor_fn])) {
-        window.Drupal.behaviors.dndLibrary[editor_fn](data, settings); 
-      }
-    });
+Drupal.behaviors.dndLibrary.attach_library = function(e, data) {
+  var settings = {
+    renderRepresentation: function(target, drop, representation_id) {
+      return Drupal.settings.dndEditorRepresentations[representation_id];
+    }
+  }
+  settings = $.extend(settings, Drupal.settings.dndEnabledLibraries[data.field]);
 
-    // @TODO track and clear intervals to save memory at the cost of 
-    // more processing?
-    $('#' + textarea_id).bind('wysiwygDetach', function(e, data) {
-      console.log('detach');
-    });
+  var editor_fn = 'attach_' + data.editor;
+  if ($.isFunction(window.Drupal.behaviors.dndLibrary[editor_fn])) {
+    window.Drupal.behaviors.dndLibrary[editor_fn](data, settings); 
   }
 }
 
+Drupal.behaviors.dndLibrary.detach_library = function(e, data) {
+  console.log('detach');
+}
+
+
 Drupal.behaviors.dndLibrary.attach_tinymce = function(data, settings) {
-  var tiny = tinyMCE.getInstanceById(data.field);
-  $('#'+ data.field +'-wrapper iframe').load(function() {
-    settings = $.extend({
-      targets: $('#'+ data.field +'-wrapper iframe'),
-      insertAfter: '<p><span id="__caret">_</span></p>',
-      postprocessDrop: function() {
-        // Get our special span, select it, delete it, and hope the caret
-        // resets correctly.
-        tiny.selection.select(tiny.dom.get('__caret'));
-        tiny.execCommand('Delete', false, null);
-				tiny.dom.remove('__caret');
+
+  var tiny_instance = tinyMCE.getInstanceById(data.field);
+
+  // If the Tiny instance exists, attach directly, otherwise wait until Tiny
+  // has registered a new instance.
+  if (tiny_instance) { 
+    Drupal.behaviors.dndLibrary._attach_tinymce(data, settings, tiny_instance);
+  } else {
+    var t = setInterval(function() {
+      var tiny_instance = tinyMCE.getInstanceById(data.field);
+      if (tiny_instance) {
+        Drupal.behaviors.dndLibrary._attach_tinymce(data, settings, tiny_instance);
+        clearInterval(t);
       }
-    }, settings);
+    }, 100);
+  }
+}
 
-    $(settings.drop_selector).dnd(settings);
-  });
+Drupal.behaviors.dndLibrary._attach_tinymce = function(data, settings, tiny_instance) {
+  settings = $.extend({
+    targets: $('#'+ data.field +'-wrapper iframe'),
+    insertAfter: '<p><span id="__caret">_</span></p>',
+    postprocessDrop: function(target, drop, element) {
+      // Get our special span, select it, delete it, and hope the caret
+      // resets correctly.
+      tiny_instance.selection.select(tiny_instance.dom.get('__caret'));
+      tiny_instance.execCommand('Delete', false, null);
+      tiny_instance.dom.remove('__caret');
+
+      // Add some classes to the library items
+      $(element).addClass('dnd-inserted'); 
+      $(element).parents('.editor-item').addClass('dnd-child-inserted');
+    }
+  }, settings);
+
+  $(settings.drop_selector).dnd(settings);
 }