changeset 25:e71df38143d1

Added element counting, but there is a bug in textarea click counting -- it only works properly once a count has already been started via another mechanism.
author David Eads <eads@chicagotech.org>
date Wed, 11 Mar 2009 14:07:50 -0500
parents 4f58fa0a9a6d
children 45c6c48c2a88
files js/dnd-library.js js/jquery.draganddrop.js
diffstat 2 files changed, 32 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/js/dnd-library.js	Wed Mar 11 01:47:57 2009 -0500
+++ b/js/dnd-library.js	Wed Mar 11 14:07:50 2009 -0500
@@ -153,9 +153,14 @@
 Drupal.behaviors.dndLibrary.attach_none = function(data, settings) {
   settings = $.extend({
     targets: $('#'+ data.field),
-    processTextAreaClick: function(target, clicked, representation_id, e, data) {
+    processTextAreaClick: function(clicked, representation_id, e, data) {
+      var target = this, $target = $(target);
+
+      // Update element count
+      Drupal.behaviors.dndLibrary.countElements.call(target, representation_id);
+
       var snippet = '<p class="dnd-dropped-wrapper">' + Drupal.settings.dndEditorRepresentations[representation_id].body + '</p>';
-      $(target).replaceSelection(snippet, true);
+      $target.replaceSelection(snippet, true);
     }
   }, settings);
   $(settings.drop_selector).dnd(settings);
@@ -199,17 +204,8 @@
       var representation = Drupal.settings.dndEditorRepresentations[representation_id].body;
       var target = this, $target = $(target), $drop = $(drop), block;
 
-      // Keep a counter
-      var counter = $target.data('representation_counter');      
-      if (!counter) {
-        counter = {}
-        counter[representation_id] = 1;
-      } else if (counter && !counter[representation_id]) {
-        counter[representation_id] = 1;
-      } else {
-        counter[representation_id] = counter[representation_id] + 1;
-      }
-      $target.data('representation_counter', counter);
+      // Update element count
+      Drupal.behaviors.dndLibrary.countElements.call(target, representation_id);
 
       // Search through block level parents
       $drop.parents().each(function() {
@@ -291,3 +287,21 @@
 
   $(settings.drop_selector).dnd(settings);
 }
+
+
+// Keep a counter of times a representation ID has been used
+Drupal.behaviors.dndLibrary.countElements = function(representation_id) {
+  // We need to track element usage betwen all editors, so we look for the
+  // parent form item
+  $target = $(this).parents('.form-item');
+  var counter = $target.data('representation_counter');      
+  if (!counter) {
+    counter = {}
+    counter[representation_id] = 1;
+  } else if (counter && !counter[representation_id]) {
+    counter[representation_id] = 1;
+  } else {
+    counter[representation_id] = counter[representation_id] + 1;
+  }
+  $target.data('representation_counter', counter);
+}
--- a/js/jquery.draganddrop.js	Wed Mar 11 01:47:57 2009 -0500
+++ b/js/jquery.draganddrop.js	Wed Mar 11 14:07:50 2009 -0500
@@ -162,6 +162,9 @@
         // Add a special class
         $(element).addClass(opt.processedClass);
 
+        // Data for custom event
+        var event_data = {'drop': element, 'representation_id': representation_id};
+
         // We need to differentiate behavior based on the targets
         targets.each(function() {
           var target = this, $target = $(target);
@@ -169,11 +172,13 @@
             $(element).addClass(opt.iframeTargetClass);
             $(element).click(function() {
               opt.processIframeClick.call(target, element, representation_id);
+              $(target).trigger('dnd_drop', event_data);
             });
           } else if ($target.is('textarea')) {
             $(element).addClass(opt.textareaTargetClass);
             $(element).click(function() {
               opt.processTextAreaClick.call(target, element, representation_id);
+              $(target).trigger('dnd_drop', event_data);
             });
           }
         });