Mercurial > defr > drupal > scald > dnd
diff js/dnd-library.js @ 30:2d49adbd8992
Start of applying a last layer of polish: starting the process of cleaning up the library code.
author | David Eads <eads@chicagotech.org> |
---|---|
date | Tue, 17 Mar 2009 21:29:00 -0500 |
parents | 7d6bf2dca269 |
children | 767ebf925654 |
line wrap: on
line diff
--- a/js/dnd-library.js Tue Mar 17 10:59:50 2009 -0500 +++ b/js/dnd-library.js Tue Mar 17 21:29:00 2009 -0500 @@ -20,11 +20,15 @@ 'empty' : function(a, i, m) { return !$(a).filter(function(i) { return !$(this).is('br'); - }).length && !$.trim(a.textContent || a.innerText||$(a).text()||""); + }).length && !$.trim(a.textContent || a.innerText||$(a).text() || ""); } }); }) (jQuery); +/** + * Initialize and load drag and drop library and pass off rendering and + * behavior attachment. + */ Drupal.behaviors.dndLibrary = function(context) { $('.dnd-library-wrapper', context).each(function() { var $this = $(this); @@ -32,8 +36,8 @@ // This is a bad hack to lop off '-dnd-library' from the id to get the editor name var $editor = $('#' + this.id.slice(0, -12)); - var settings = Drupal.settings.dndEnabledLibraries[$editor.get(0).id]; - var bt_settings = $.extend({ + // Set up some initial settings for BeautyTips + var settings = Drupal.settings.dndEnabledLibraries[$editor.get(0).id] = $.extend({ 'trigger': 'none', 'width': 375, 'spikeLength': 7, @@ -42,81 +46,32 @@ 'strokeWidth': 1, 'fill': '#ffd', 'strokeStyle': '#555' - }, settings.bt_settings); + }, Drupal.settings.dndEnabledLibraries[$editor.get(0).id]); // Bind Drag and Drop plugin invocation to events emanating from Wysiwyg $editor.bind('wysiwygAttach', Drupal.behaviors.dndLibrary.attach_library); $editor.bind('wysiwygDetach', Drupal.behaviors.dndLibrary.detach_library); - // Add preview behavior to editor items (thanks, BeautyTips!) - $('.editor-item', $this).each(function () { - $(this).bt(Drupal.settings.dndLibraryPreviews[this.id], bt_settings); - var hover_opts = $.extend({ - 'interval': 500, - 'timeout' : 0, - 'over': function() { - var $this = $(this); - this.btOn(); - // Remove the preview once dragging of any image has commenced - $('img', $this).bind('drag', function(e) { - $this.btOff(); - }); - $('img', $this).bind('click', function(e) { - $this.btOff(); - }); - }, - 'out': function() { this.btOff(); } - }, settings.libraryHoverIntentOpts); - $(this).hoverIntent(hover_opts); + // Set up empty objects to keep track of things + Drupal.settings.dndEditorRepresentations = {}; + Drupal.settings.dndLibraryPreviews = {}; + + // Populate + $.getJSON(Drupal.settings.basePath + settings.url, function(data) { + Drupal.behaviors.dndLibrary.renderLibrary.call($this.get(0), data, $editor); }); - // Simple AJAX pager - $('.pager a', $this).click(function() { - $.getJSON(this.href, function(data) { - Drupal.behaviors.dndLibrary.refreshLibrary.call($this.get(0), data, $editor); - }); - Drupal.behaviors.dndLibrary(); - return false; - }); - - $('.view-filters input[type=submit]', $this).click(function() { - $(this).ajaxSubmit({ - 'url' : Drupal.settings.basePath + Drupal.settings.dndEnabledLibraries[$editor.get(0).id].url, - 'dataType': 'json', - 'success': function(responsetext, statustext) { - Drupal.behaviors.dndLibrary.refreshLibrary.call($this.get(0), responsetext, $editor); - Drupal.behaviors.dndLibrary(); - } - }); - return false; - }); - - // Preload images in editor representations - var cached = $.data($editor, 'dnd_preload') || {}; - for (editor_id in Drupal.settings.dndEditorRepresentations) { - if (!cached[editor_id]) { - $representation = $(Drupal.settings.dndEditorRepresentations[editor_id].body); - if ($representation.is('img') && $representation.get(0).src) { - $representation.attr('src', $representation.get(0).src); - } else { - $('img', $representation).each(function() { - this.attr('src', this.src); - }); - } - } - } - $.data($editor, 'dnd_preload', cached); }); } -Drupal.behaviors.dndLibrary.refreshLibrary = function(data, editor) { +Drupal.behaviors.dndLibrary.renderLibrary = function(data, editor) { $this = $(this); - $('.header', $this).html(data.header); - $('.library', $this).html(data.library); - $('.footer', $this).html(data.footer); + $this.html(data.library); + var settings = Drupal.settings.dndEnabledLibraries[editor.get(0).id]; var params = Drupal.wysiwyg.instances[editor.get(0).id]; + editor.trigger('wysiwygDetach', params); editor.trigger('wysiwygAttach', params); @@ -127,8 +82,44 @@ Drupal.settings.dndLibraryPreviews[preview_id] = data.library_previews[preview_id]; } - // Reattach behaviors - Drupal.behaviors.dndLibrary(); + // Add preview behavior to editor items (thanks, BeautyTips!) + $('.editor-item', $this).each(function () { + $(this).bt(Drupal.settings.dndLibraryPreviews[this.id], settings.bt_settings); + var hover_opts = $.extend({ + 'interval': 500, + 'timeout' : 0, + 'over': function() { + var $this = $(this); + this.btOn(); + // Remove the preview once dragging of any image has commenced + $('img', $this).bind('drag', function(e) { + $this.btOff(); + }); + $('img', $this).bind('click', function(e) { + $this.btOff(); + }); + }, + 'out': function() { this.btOff(); } + }, settings.libraryHoverIntentOpts); + $(this).hoverIntent(hover_opts); + }); + + // Preload images in editor representations + var cached = $.data($(editor), 'dnd_preload') || {}; + /*for (editor_id in Drupal.settings.dndEditorRepresentations) { + if (!cached[editor_id]) { + $representation = $(Drupal.settings.dndEditorRepresentations[editor_id].body); + if ($representation.is('img') && $representation.get(0).src) { + $representation.attr('src', $representation.get(0).src); + } else { + $('img', $representation).each(function() { + this.attr('src', this.src); + }); + } + } + } + $.data($(editor), 'dnd_preload', cached);*/ + }