eads@16: /**
eads@16: * Drag and Drop Library For Drupal
eads@16: *
eads@16: * This builds on the DnD jQuery plugin written to provide drag and drop media
eads@16: * handling to Rich Text Editors to consume, display, and attach behavior to
eads@16: * a "media library" provided via JSON and implemented for Drupal running
eads@16: * the Wysiwyg plugin.
eads@16: */
eads@16:
eads@16: /**
eads@16: * Extend jQuery a bit
eads@16: *
eads@16: * We add a selector to look for "empty" elements (empty elements in TinyMCE
eads@17: * often have non-breaking spaces and
tags). An exception is required
eads@17: * to make this work in IE.
eads@16: */
eads@16: (function($) {
eads@16: // Custom selectors
eads@16: $.extend($.expr[":"], {
eads@16: 'empty' : function(a, i, m) {
eads@17: return !$(a).filter(function(i) {
eads@17: return !$(this).is('br');
eads@17: }).length && !$.trim(a.textContent || a.innerText||$(a).text()||"");
eads@16: }
eads@16: });
eads@16: }) (jQuery);
eads@16:
eads@2: Drupal.behaviors.dndLibrary = function(context) {
eads@16: $('.dnd-library-wrapper', context).each(function() {
eads@4: var $this = $(this);
eads@2:
eads@4: // This is a bad hack to lop off '-dnd-library' from the id to get the editor name
eads@17: var $editor = $('#' + this.id.slice(0, -12));
eads@17:
eads@2:
eads@16: // Bind Drag and Drop plugin invocation to events emanating from Wysiwyg
eads@17: $editor.bind('wysiwygAttach', Drupal.behaviors.dndLibrary.attach_library);
eads@17: $editor.bind('wysiwygDetach', Drupal.behaviors.dndLibrary.detach_library);
eads@17:
eads@17: // Add basic hover behavior to editor items
eads@17: $('.editor-item', context).hover(function() {
eads@17: var $this = $(this);
eads@17: var p = $('#edit-body-wrapper').position();
eads@17: var preview = $(Drupal.settings.dndLibraryPreviews[this.id]).css({
eads@17: 'position' : 'absolute',
eads@17: 'top' : p.top + 150,
eads@17: 'left' : p.left + $('#edit-body-wrapper').width() + 150,
eads@17: 'display' : 'none',
eads@17: 'width' : '300px',
eads@17: 'background-color' : '#ddd',
eads@17: 'border' : '3px solid #999',
eads@17: 'padding' : '4px',
eads@17: 'z-index' : 10
eads@17: });
eads@17: $('body').prepend(preview);
eads@17: preview.fadeIn('slow');
eads@17: }, function() {
eads@17: $('#' + this.id.replace(/test/,'preview')).fadeOut('fast', function() { $(this).remove(); });
eads@17: });
eads@4:
eads@4: // Ajax pager
eads@17: $('.pager a', $this).click(function() {
eads@2: $.getJSON(this.href, function(data) {
eads@17: Drupal.behaviors.dndLibrary.refreshLibrary.call($this.get(0), data, $editor);
eads@2: });
eads@17:
eads@17: // Reattach behaviors
eads@17: Drupal.behaviors.dndLibrary();
eads@17:
eads@2: return false;
eads@2: });
eads@17:
eads@17: // Preload images in editor representations
eads@17: var cached = $.data($editor, 'dnd_preload') || {};
eads@17: for (editor_id in Drupal.settings.dndEditorRepresentations) {
eads@17: if (!cached[editor_id]) {
eads@17: $representation = $(Drupal.settings.dndEditorRepresentations[editor_id]);
eads@17: if ($representation.is('img') && $representation.get(0).src) {
eads@17: $representation.attr('src', $representation.get(0).src);
eads@17: } else {
eads@17: $('img', $representation).each(function() {
eads@17: this.attr('src', this.src);
eads@17: });
eads@17: }
eads@17: }
eads@17: }
eads@17: $.data($editor, 'dnd_preload', cached);
eads@4: });
eads@4: }
eads@2:
eads@17: Drupal.behaviors.dndLibrary.refreshLibrary = function(data, editor) {
eads@17: $this = $(this);
eads@17:
eads@17: $('.header', $this).html(data.header);
eads@17: $('.library', $this).html(data.library);
eads@17: $('.footer', $this).html(data.footer);
eads@17:
eads@17: var params = Drupal.wysiwyg.instances[editor.get(0).id];
eads@17: editor.trigger('wysiwygDetach', params);
eads@17: editor.trigger('wysiwygAttach', params);
eads@17:
eads@17: for (editor_id in data.editor_representations) {
eads@17: Drupal.settings.dndEditorRepresentations[editor_id] = data.editor_representations[editor_id];
eads@17: }
eads@17: for (preview_id in data.library_previews) {
eads@17: Drupal.settings.dndLibraryPreviews[preview_id] = data.library_previews[preview_id];
eads@17: }
eads@17: }
eads@17:
eads@17:
eads@16: // Dynamically compose a callback based on the editor name
eads@4: Drupal.behaviors.dndLibrary.attach_library = function(e, data) {
eads@17: var settings = $.extend({idSelector: Drupal.behaviors.dndLibrary.idSelector}, Drupal.settings.dndEnabledLibraries[data.field]);
eads@4: var editor_fn = 'attach_' + data.editor;
eads@4: if ($.isFunction(window.Drupal.behaviors.dndLibrary[editor_fn])) {
eads@17: window.Drupal.behaviors.dndLibrary[editor_fn](data, settings);
eads@2: }
eads@2: }
eads@2:
eads@16: // Do garbage collection on detach
eads@4: Drupal.behaviors.dndLibrary.detach_library = function(e, data) {
eads@15: for (t in $(document).data('dnd_timers')) {
eads@15: clearInterval(t);
eads@15: }
eads@15: $(document).removeData('dnd_timers');
eads@4: }
eads@4:
eads@16: // Basic textareas
eads@16: Drupal.behaviors.dndLibrary.attach_none = function(data, settings) {
eads@16: settings = $.extend({
eads@16: targets: $('#'+ data.field),
eads@17: processTextAreaDrop: function(target, clicked, representation_id, e, data) {
eads@17: var snippet = '
' + Drupal.settings.dndEditorRepresentations[representation_id] + '
'; eads@16: $(target).replaceSelection(snippet, true); eads@16: } eads@16: }, settings); eads@16: $(settings.drop_selector).dnd(settings); eads@16: } eads@4: eads@16: // Attach TinyMCE eads@2: Drupal.behaviors.dndLibrary.attach_tinymce = function(data, settings) { eads@4: var tiny_instance = tinyMCE.getInstanceById(data.field); eads@4: eads@4: // If the Tiny instance exists, attach directly, otherwise wait until Tiny eads@4: // has registered a new instance. eads@4: if (tiny_instance) { eads@4: Drupal.behaviors.dndLibrary._attach_tinymce(data, settings, tiny_instance); eads@4: } else { eads@4: var t = setInterval(function() { eads@4: var tiny_instance = tinyMCE.getInstanceById(data.field); eads@4: if (tiny_instance) { eads@4: Drupal.behaviors.dndLibrary._attach_tinymce(data, settings, tiny_instance); eads@4: clearInterval(t); eads@2: } eads@4: }, 100); eads@4: } eads@4: } eads@2: eads@17: Drupal.behaviors.dndLibrary.idSelector = function(element) { eads@17: if ($(element).is('img')) { eads@17: return $.url.setUrl(element.src).param('dnd_id'); eads@17: } eads@17: return false; eads@17: } eads@17: eads@16: // Really attach TinyMCE eads@4: Drupal.behaviors.dndLibrary._attach_tinymce = function(data, settings, tiny_instance) { eads@16: var ed = tiny_instance, dom = ed.dom, s = ed.selection; eads@16: eads@4: settings = $.extend({ eads@4: targets: $('#'+ data.field +'-wrapper iframe'), eads@16: interval: 100, eads@16: processIframeDrop: function(target, dragged, dropped, representation_id) { eads@16: var representation = Drupal.settings.dndEditorRepresentations[representation_id]; eads@16: var $target = $(target), $dropped = $(dropped), $dragged = $(dragged), block; eads@15: eads@16: // Search through block level parents eads@16: $dropped.parents().each(function() { eads@16: var $this = $(this); eads@16: if ($this.css('display') == 'block') { eads@16: block = this; eads@16: return false; eads@16: } eads@16: }); eads@16: eads@16: // Remove dropped item eads@16: $dropped.remove(); eads@16: eads@16: // Create an element to insert eads@16: var insert = dom.create('p', {'class' : 'dnd-dropped-wrapper', 'id' : 'dnd-inserted'}, representation); eads@16: eads@16: // The no-parent case eads@16: if ($(block).is('body')) { eads@16: s.setNode(insert); eads@16: } eads@16: else { eads@16: var old_id = block.id; eads@16: block.id = 'target-block'; eads@16: $block = $('#target-block', $target.contents()); eads@16: eads@16: // @TODO is finding the parent broken in safari?? eads@16: $block.after('' + representation + '
'); eads@16: eads@16: // The active target block should be empty eads@16: if ($('#target-block:empty', $target.contents()).length > 0) { eads@17: $('#target-block', $target.contents()).remove(); eads@16: } else if (old_id) { eads@16: block.id = old_id; eads@16: } else { eads@16: $block.removeAttr('id'); eads@16: } eads@16: } eads@16: eads@16: var $inserted = $('#dnd-inserted', $target.contents()); eads@16: var inserted = $inserted.get(0); eads@16: eads@16: // Look behind in the DOM eads@16: var previous = $inserted.prev().get(0); eads@16: eads@16: // If the previous element is also an editor representation, we need to eads@16: // put a dummy paragraph between the elements to prevent editor errors. eads@16: if (previous && $(previous).hasClass('dnd-dropped-wrapper')) { eads@16: $inserted.before('_
'); eads@16: c = dom.get('__spacer'); eads@16: s.select(c); eads@16: ed.execCommand('Delete', false, null); eads@16: dom.remove(c); eads@16: } eads@16: eads@16: // Look ahead in the DOM eads@16: var next = $inserted.next().get(0); eads@16: eads@16: // If the next item exists and isn't an editor representation, drop the eads@16: // caret at the beginning of the element, otherwise make a new paragraph eads@16: // to advance the caret to. eads@16: if (next && !$(next).hasClass('dnd-dropped-wrapper')) { eads@16: $(next).prepend('_'); eads@16: } eads@16: else { eads@16: var after = dom.create('p', {}, '_'); eads@16: dom.insertAfter(after, 'dnd-inserted'); eads@16: } eads@16: eads@16: // Clear the ID for the next drop eads@16: $inserted.removeAttr('id'); eads@16: eads@16: // Force selection to reset the caret eads@16: var c = dom.get('__caret'); eads@16: s.select(c); eads@16: ed.execCommand('Delete', false, null); eads@16: dom.remove(c); eads@4: eads@4: // Add some classes to the library items eads@16: $(dragged).addClass('dnd-inserted'); eads@16: drag_parents = $(dragged).parents('.editor-item'); eads@16: drag_parents.addClass('dnd-child-inserted'); eads@4: } eads@4: }, settings); eads@4: eads@4: $(settings.drop_selector).dnd(settings); eads@2: }