annotate js/dnd-library.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 99ba5941779c
children bb68dc3ad56f
rev   line source
eads@2 1 Drupal.behaviors.dndLibrary = function(context) {
eads@4 2 $('.dnd-library-wrapper:not(.dnd-processed)', context).each(function() {
eads@4 3 var $this = $(this);
eads@2 4
eads@4 5 // This is a bad hack to lop off '-dnd-library' from the id to get the editor name
eads@4 6 var editor = this.id.slice(0, -12);
eads@2 7
eads@4 8 // Attempt to attach library
eads@4 9 //Drupal.behaviors.dndLibrary.attach_library(false, Drupal.wysiwyg.instances[editor]);
eads@2 10
eads@4 11 // Bind Drag and Drop plugin invocation to wywsiwygAttach event
eads@4 12 $('#' + editor).bind('wysiwygAttach', Drupal.behaviors.dndLibrary.attach_library);
eads@4 13
eads@4 14 // @TODO track and clear intervals to save memory at the cost of
eads@4 15 // more processing?
eads@4 16 $('#' + editor).bind('wysiwygDetach', Drupal.behaviors.dndLibrary.detach_library);
eads@4 17
eads@4 18 // Ajax pager
eads@4 19 $('.pager a', $this).click(function(e, data) {
eads@2 20 $.getJSON(this.href, function(data) {
eads@4 21 $('.header', $this).html(data.header);
eads@4 22 $('.library', $this).html(data.library);
eads@4 23 //$('.footer', $this).html(data.footer);
eads@4 24 for (editor_id in data.editor_representations) {
eads@4 25 Drupal.settings.dndEditorRepresentations[editor_id] = data.editor_representations[editor_id];
eads@2 26 }
eads@4 27 var params = Drupal.wysiwyg.instances[editor];
eads@15 28 $('#' + editor).trigger('wysiwygDetach', params);
eads@4 29 $('#' + editor).trigger('wysiwygAttach', params);
eads@2 30 });
eads@2 31 return false;
eads@2 32 });
eads@2 33
eads@4 34 $this.addClass('dnd-processed');
eads@4 35 });
eads@4 36 }
eads@2 37
eads@4 38 Drupal.behaviors.dndLibrary.attach_library = function(e, data) {
eads@4 39 var settings = {
eads@4 40 renderRepresentation: function(target, drop, representation_id) {
eads@4 41 return Drupal.settings.dndEditorRepresentations[representation_id];
eads@4 42 }
eads@4 43 }
eads@4 44 settings = $.extend(settings, Drupal.settings.dndEnabledLibraries[data.field]);
eads@2 45
eads@4 46 var editor_fn = 'attach_' + data.editor;
eads@4 47 if ($.isFunction(window.Drupal.behaviors.dndLibrary[editor_fn])) {
eads@4 48 window.Drupal.behaviors.dndLibrary[editor_fn](data, settings);
eads@2 49 }
eads@2 50 }
eads@2 51
eads@4 52 Drupal.behaviors.dndLibrary.detach_library = function(e, data) {
eads@15 53 for (t in $(document).data('dnd_timers')) {
eads@15 54 clearInterval(t);
eads@15 55 }
eads@15 56 $(document).removeData('dnd_timers');
eads@4 57 }
eads@4 58
eads@4 59
eads@2 60 Drupal.behaviors.dndLibrary.attach_tinymce = function(data, settings) {
eads@4 61
eads@4 62 var tiny_instance = tinyMCE.getInstanceById(data.field);
eads@4 63
eads@4 64 // If the Tiny instance exists, attach directly, otherwise wait until Tiny
eads@4 65 // has registered a new instance.
eads@4 66 if (tiny_instance) {
eads@4 67 Drupal.behaviors.dndLibrary._attach_tinymce(data, settings, tiny_instance);
eads@4 68 } else {
eads@4 69 var t = setInterval(function() {
eads@4 70 var tiny_instance = tinyMCE.getInstanceById(data.field);
eads@4 71 if (tiny_instance) {
eads@4 72 Drupal.behaviors.dndLibrary._attach_tinymce(data, settings, tiny_instance);
eads@4 73 clearInterval(t);
eads@2 74 }
eads@4 75 }, 100);
eads@4 76 }
eads@4 77 }
eads@2 78
eads@4 79 Drupal.behaviors.dndLibrary._attach_tinymce = function(data, settings, tiny_instance) {
eads@4 80 settings = $.extend({
eads@4 81 targets: $('#'+ data.field +'-wrapper iframe'),
eads@4 82 insertAfter: '<p><span id="__caret">_</span></p>',
eads@15 83
eads@15 84 // Back out markup to render in place after parent container
eads@15 85 preprocessDrop: function(target, drop) {
eads@15 86 drop.id = 'DND-TMP-' + $.data(drop);
eads@15 87 // Do some native tiny manipulations
eads@15 88 return drop;
eads@15 89 },
eads@4 90 postprocessDrop: function(target, drop, element) {
eads@4 91 // Get our special span, select it, delete it, and hope the caret
eads@4 92 // resets correctly.
eads@4 93 tiny_instance.selection.select(tiny_instance.dom.get('__caret'));
eads@4 94 tiny_instance.execCommand('Delete', false, null);
eads@4 95 tiny_instance.dom.remove('__caret');
eads@4 96
eads@4 97 // Add some classes to the library items
eads@4 98 $(element).addClass('dnd-inserted');
eads@4 99 $(element).parents('.editor-item').addClass('dnd-child-inserted');
eads@4 100 }
eads@4 101 }, settings);
eads@4 102
eads@4 103 $(settings.drop_selector).dnd(settings);
eads@2 104 }