Mercurial > defr > drupal > scald > dnd
comparison 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 |
comparison
equal
deleted
inserted
replaced
| 3:5df0783706f7 | 4:c2eb995212bf |
|---|---|
| 1 Drupal.behaviors.dndLibrary = function(context) { | 1 Drupal.behaviors.dndLibrary = function(context) { |
| 2 var libraries = Drupal.settings.dndEnabledLibraries; | 2 $('.dnd-library-wrapper:not(.dnd-processed)', context).each(function() { |
| 3 var $this = $(this); | |
| 3 | 4 |
| 4 // Loop over libraries keyed by the textarea they are associated with | 5 // This is a bad hack to lop off '-dnd-library' from the id to get the editor name |
| 5 for (var textarea_id in libraries) { | 6 var editor = this.id.slice(0, -12); |
| 6 | 7 |
| 7 var library = $('#' + textarea_id + '-dnd-library'); | 8 // Attempt to attach library |
| 9 //Drupal.behaviors.dndLibrary.attach_library(false, Drupal.wysiwyg.instances[editor]); | |
| 8 | 10 |
| 9 // Ajax pager: This seems like it is bad to be here | 11 // Bind Drag and Drop plugin invocation to wywsiwygAttach event |
| 10 $('.pager a', library).click(function(e, data) { | 12 $('#' + editor).bind('wysiwygAttach', Drupal.behaviors.dndLibrary.attach_library); |
| 13 | |
| 14 // @TODO track and clear intervals to save memory at the cost of | |
| 15 // more processing? | |
| 16 $('#' + editor).bind('wysiwygDetach', Drupal.behaviors.dndLibrary.detach_library); | |
| 17 | |
| 18 // Ajax pager | |
| 19 $('.pager a', $this).click(function(e, data) { | |
| 11 $.getJSON(this.href, function(data) { | 20 $.getJSON(this.href, function(data) { |
| 12 $('.library', library).html(''); | 21 $('.header', $this).html(data.header); |
| 13 for (item in data.library) { | 22 $('.library', $this).html(data.library); |
| 14 $('.library', library).append(data.library[item].library); | 23 //$('.footer', $this).html(data.footer); |
| 15 Drupal.settings.dndEditorRepresentations[item] = data.library[item].editor; | 24 for (editor_id in data.editor_representations) { |
| 16 Drupal.behaviors.dndLibrary(context); // Durn event reattachment | 25 Drupal.settings.dndEditorRepresentations[editor_id] = data.editor_representations[editor_id]; |
| 17 } | 26 } |
| 27 var params = Drupal.wysiwyg.instances[editor]; | |
| 28 $('#' + editor).trigger('wysiwygAttach', params); | |
| 18 }); | 29 }); |
| 19 return false; | 30 return false; |
| 20 }); | 31 }); |
| 21 | 32 |
| 22 // Bind Drag and Drop plugin invocation to wywsiwygAttach event | 33 $this.addClass('dnd-processed'); |
| 23 $('#' + textarea_id).bind('wysiwygAttach', function(e, data) { | 34 }); |
| 24 var settings = { | 35 } |
| 25 renderRepresentation: function(target, drop, representation_id) { | |
| 26 console.log(representation_id); | |
| 27 return Drupal.settings.dndEditorRepresentations[representation_id]; | |
| 28 } | |
| 29 } | |
| 30 settings = $.extend(settings, libraries[textarea_id]); | |
| 31 | 36 |
| 32 // Get editor attachment function | 37 Drupal.behaviors.dndLibrary.attach_library = function(e, data) { |
| 33 var editor_fn = 'attach_' + data.editor; | 38 var settings = { |
| 34 if ($.isFunction(window.Drupal.behaviors.dndLibrary[editor_fn])) { | 39 renderRepresentation: function(target, drop, representation_id) { |
| 35 window.Drupal.behaviors.dndLibrary[editor_fn](data, settings); | 40 return Drupal.settings.dndEditorRepresentations[representation_id]; |
| 36 } | 41 } |
| 37 }); | 42 } |
| 43 settings = $.extend(settings, Drupal.settings.dndEnabledLibraries[data.field]); | |
| 38 | 44 |
| 39 // @TODO track and clear intervals to save memory at the cost of | 45 var editor_fn = 'attach_' + data.editor; |
| 40 // more processing? | 46 if ($.isFunction(window.Drupal.behaviors.dndLibrary[editor_fn])) { |
| 41 $('#' + textarea_id).bind('wysiwygDetach', function(e, data) { | 47 window.Drupal.behaviors.dndLibrary[editor_fn](data, settings); |
| 42 console.log('detach'); | |
| 43 }); | |
| 44 } | 48 } |
| 45 } | 49 } |
| 46 | 50 |
| 51 Drupal.behaviors.dndLibrary.detach_library = function(e, data) { | |
| 52 console.log('detach'); | |
| 53 } | |
| 54 | |
| 55 | |
| 47 Drupal.behaviors.dndLibrary.attach_tinymce = function(data, settings) { | 56 Drupal.behaviors.dndLibrary.attach_tinymce = function(data, settings) { |
| 48 var tiny = tinyMCE.getInstanceById(data.field); | 57 |
| 49 $('#'+ data.field +'-wrapper iframe').load(function() { | 58 var tiny_instance = tinyMCE.getInstanceById(data.field); |
| 50 settings = $.extend({ | 59 |
| 51 targets: $('#'+ data.field +'-wrapper iframe'), | 60 // If the Tiny instance exists, attach directly, otherwise wait until Tiny |
| 52 insertAfter: '<p><span id="__caret">_</span></p>', | 61 // has registered a new instance. |
| 53 postprocessDrop: function() { | 62 if (tiny_instance) { |
| 54 // Get our special span, select it, delete it, and hope the caret | 63 Drupal.behaviors.dndLibrary._attach_tinymce(data, settings, tiny_instance); |
| 55 // resets correctly. | 64 } else { |
| 56 tiny.selection.select(tiny.dom.get('__caret')); | 65 var t = setInterval(function() { |
| 57 tiny.execCommand('Delete', false, null); | 66 var tiny_instance = tinyMCE.getInstanceById(data.field); |
| 58 tiny.dom.remove('__caret'); | 67 if (tiny_instance) { |
| 68 Drupal.behaviors.dndLibrary._attach_tinymce(data, settings, tiny_instance); | |
| 69 clearInterval(t); | |
| 59 } | 70 } |
| 60 }, settings); | 71 }, 100); |
| 72 } | |
| 73 } | |
| 61 | 74 |
| 62 $(settings.drop_selector).dnd(settings); | 75 Drupal.behaviors.dndLibrary._attach_tinymce = function(data, settings, tiny_instance) { |
| 63 }); | 76 settings = $.extend({ |
| 77 targets: $('#'+ data.field +'-wrapper iframe'), | |
| 78 insertAfter: '<p><span id="__caret">_</span></p>', | |
| 79 postprocessDrop: function(target, drop, element) { | |
| 80 // Get our special span, select it, delete it, and hope the caret | |
| 81 // resets correctly. | |
| 82 tiny_instance.selection.select(tiny_instance.dom.get('__caret')); | |
| 83 tiny_instance.execCommand('Delete', false, null); | |
| 84 tiny_instance.dom.remove('__caret'); | |
| 85 | |
| 86 // Add some classes to the library items | |
| 87 $(element).addClass('dnd-inserted'); | |
| 88 $(element).parents('.editor-item').addClass('dnd-child-inserted'); | |
| 89 } | |
| 90 }, settings); | |
| 91 | |
| 92 $(settings.drop_selector).dnd(settings); | |
| 64 } | 93 } |
