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@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@20: // Add preview behavior to editor items (thanks, BeautyTips!) eads@18: $('.editor-item', context).each(function () { eads@18: $(this).bt(Drupal.settings.dndLibraryPreviews[this.id], { eads@20: 'trigger': 'none', eads@20: 'width': 300, eads@20: 'spikeLength': 7, eads@20: 'spikeGirth': 9, eads@18: 'corner-radius' : 3, eads@20: 'strokeWidth': 1, eads@20: 'fill': '#ffd', eads@18: 'strokeStyle': '#555' eads@17: }); eads@20: $(this).hoverIntent({ eads@20: 'interval': 500, eads@20: 'timeout' : 0, eads@20: 'over': function() { eads@20: var $this = $(this); eads@20: this.btOn(); eads@20: // Remove the preview once dragging of any image has commenced eads@20: $('img', $this).bind('drag', function(e) { eads@20: $this.btOff(); eads@20: }); eads@20: $('img', $this).bind('click', function(e) { eads@20: $this.btOff(); eads@20: }); eads@20: }, eads@20: 'out': function() { this.btOff(); } eads@18: }); eads@17: }); eads@4: eads@20: // Simple 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: Drupal.behaviors.dndLibrary(); eads@2: return false; eads@2: }); eads@17: eads@20: $('.view-filters form', $this).submit(function() { eads@20: $(this).ajaxSubmit({ eads@20: // @TODO add URL from settings and target more specifically... eads@20: 'dataType': 'json', eads@20: 'success': function(responsetext, statustext) { eads@20: drupal.behaviors.dndlibrary.responsetext.call($this.get(0), responsetext, $editor); eads@20: drupal.behaviors.dndlibrary(); eads@20: } eads@20: }); eads@20: return false; eads@20: }); eads@20: 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@19: eads@19: // Reattach behaviors eads@19: Drupal.behaviors.dndLibrary(); 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@20: processTextAreaClick: 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@20: processIframeDrop: function(drop, id_selector) { eads@20: var representation_id = id_selector.call(this, drop); eads@16: var representation = Drupal.settings.dndEditorRepresentations[representation_id]; eads@20: var target = this, $target = $(target), $drop = $(drop), block; eads@15: eads@16: // Search through block level parents eads@20: $drop.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@20: $drop.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@20: if (next && !$(next).hasClass('dnd-droped-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: }, settings); eads@4: eads@4: $(settings.drop_selector).dnd(settings); eads@2: }