diff js/dnd.js @ 15:7a5f74482ee3

More cross browser work, garbage collection for timers, etc.
author David Eads <eads@chicagotech.org>
date Mon, 02 Mar 2009 23:22:37 -0600
parents ef7ad7b5baa4
children bb68dc3ad56f
line wrap: on
line diff
--- a/js/dnd.js	Fri Feb 27 12:30:42 2009 -0600
+++ b/js/dnd.js	Mon Mar 02 23:22:37 2009 -0600
@@ -31,7 +31,7 @@
  *   the best experience.
  *
  * idSelector:  
- *   A callback that parses a unique id out of a droppable element. B y default
+ *   A callback that parses a unique id out of a droppable element. By default
  *   this uses the id of the element, but one could parse out an ID based on 
  *   any part of the URL, interior markup, etc.
  *
@@ -79,16 +79,15 @@
 (function($) {
   $.fn.dnd = function(opt) {
     opt = $.extend({}, {
-      dropWrapper: '<p class="dnd-dropped"></p>',
+      interval: 250,
+      dropWrapper: '<p class="dnd-dropped-wrapper"></p>',
       insertBefore: false,
       insertAfter: false,
       processedClass:  'dnd-processed',
-      interval: 100,
-
+      ignoreClass: 'dnd-dropped',
       processTargets: function(targets) {
         return targets.each(function() {
-          //$('head', $(this).contents()).append('<style type="text/css">.dnd-processed { display: none; }</style>');
-          //@TODO use jQuery.rules()
+          $('head', $(this).contents()).append('<style type="text/css">img { display: none; } img.dnd-dropped {display: block; }</style>');
           return this;
         });
       },
@@ -117,24 +116,7 @@
         return '<span id="dnd-' + representation_id +'-'+ count +'">' + representation_id + '</span>';
       },
 
-      // Back out markup to render in place after parent container
-      preprocessDrop: function(target, drop) {
-        return drop;
-        var old_parent = false;
-        var element_id = '';
-
-        var parents = drop.parents();
-        for (var i=0; i < parents.length; i++) {
-          if ($(parents[i]).is('body')) {
-            element_id = $(drop).get(0).id;
-            $(old_parent).after(drop.clone());
-            drop.remove();
-          }
-          old_parent = parents[i];
-        }
-        return $('#'+ element_id, $(target).contents());
-      },
-
+      preprocessDrop: function(target, drop) { return drop; },
       postprocessDrop: function(target, drop, element) { 
         $(element).addClass('dnd-inserted'); 
       }
@@ -165,18 +147,20 @@
         targets.each(function() {
           if ($(this).is('iframe')) {
             var target = this;
-            var selector = 'img[src='+ element.src +']';
+
+            var selector = 'img:not(.' + opt.ignoreClass + ')';
 
             // Watch the iframe for changes
             var t = setInterval(function() {              
-              $('img', $(target).contents()).each(function() {
+              $(selector, $(target).contents()).each(function() {
                 if (opt.idSelector(this) == representation_id) {
                   var drop = opt.preprocessDrop(target, $(this)); // Must return a jquery object
                   var representation = opt.renderRepresentation(target, drop, representation_id);
                   if (representation) {
-                    if (opt.dropWrapper) {
+                    // Breaks IE 7!
+                    /*if (opt.dropWrapper) {
                       drop.wrap(opt.dropWrapper);
-                    }
+                    }*/
                     if (opt.insertBefore) {
                       drop.before(opt.insertBefore);
                     }
@@ -186,10 +170,22 @@
                     drop.replaceWith(representation);
                     opt.postprocessDrop(target, drop, element);
                   }
-                }
+                } 
               });
             }, opt.interval);
-            // @TODO track the timer with $.data() so we can clear it?
+
+            // Track current active timers -- this means you can implement
+            // your own garbage collection for specific interactions, such
+            // as paging.
+            var data = $(document).data('dnd_timers');
+            if (data) {
+              data[data.length] = t;
+            } else {
+              data = new Array();
+              data[0] = t;
+            }
+            $(document).data('dnd_timers', data);
+
           } else if ($(this).is('textarea')) {
             //console.log('@TODO handle textareas via.... regexp?');
           }