view js/dnd-library.js @ 8:b9cd179a30a8

Use user session for the drupal_http_request requesting the library. By default, drupal_http_request runs in a sandbox environment, thus the request doesn't have any idea about the current user. This in turn means that the request on the library is performed as an anonymous user, who may not have appropriate credentials to access the library.
author Franck Deroche <franck@defr.org>
date Wed, 01 Apr 2009 15:49:44 +0200
parents c2eb995212bf
children 99ba5941779c
line wrap: on
line source
Drupal.behaviors.dndLibrary = function(context) {
  $('.dnd-library-wrapper:not(.dnd-processed)', context).each(function() {
    var $this = $(this);

    // 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); 

    // Attempt to attach library
    //Drupal.behaviors.dndLibrary.attach_library(false, Drupal.wysiwyg.instances[editor]);

    // Bind Drag and Drop plugin invocation to wywsiwygAttach event
    $('#' + editor).bind('wysiwygAttach', Drupal.behaviors.dndLibrary.attach_library);

    // @TODO track and clear intervals to save memory at the cost of 
    // more processing?
    $('#' + editor).bind('wysiwygDetach', Drupal.behaviors.dndLibrary.detach_library);

    // Ajax pager
    $('.pager a', $this).click(function(e, data) {
      $.getJSON(this.href, function(data) {
        $('.header', $this).html(data.header);
        $('.library', $this).html(data.library);
        //$('.footer', $this).html(data.footer);
        for (editor_id in data.editor_representations) {
          Drupal.settings.dndEditorRepresentations[editor_id] = data.editor_representations[editor_id];
        }
        var params = Drupal.wysiwyg.instances[editor];
        $('#' + editor).trigger('wysiwygAttach', params);
      });
      return false;
    });

    $this.addClass('dnd-processed');
  });
}

Drupal.behaviors.dndLibrary.attach_library = function(e, data) {
  var settings = {
    renderRepresentation: function(target, drop, representation_id) {
      return Drupal.settings.dndEditorRepresentations[representation_id];
    }
  }
  settings = $.extend(settings, Drupal.settings.dndEnabledLibraries[data.field]);

  var editor_fn = 'attach_' + data.editor;
  if ($.isFunction(window.Drupal.behaviors.dndLibrary[editor_fn])) {
    window.Drupal.behaviors.dndLibrary[editor_fn](data, settings); 
  }
}

Drupal.behaviors.dndLibrary.detach_library = function(e, data) {
  console.log('detach');
}


Drupal.behaviors.dndLibrary.attach_tinymce = function(data, settings) {

  var tiny_instance = tinyMCE.getInstanceById(data.field);

  // If the Tiny instance exists, attach directly, otherwise wait until Tiny
  // has registered a new instance.
  if (tiny_instance) { 
    Drupal.behaviors.dndLibrary._attach_tinymce(data, settings, tiny_instance);
  } else {
    var t = setInterval(function() {
      var tiny_instance = tinyMCE.getInstanceById(data.field);
      if (tiny_instance) {
        Drupal.behaviors.dndLibrary._attach_tinymce(data, settings, tiny_instance);
        clearInterval(t);
      }
    }, 100);
  }
}

Drupal.behaviors.dndLibrary._attach_tinymce = function(data, settings, tiny_instance) {
  settings = $.extend({
    targets: $('#'+ data.field +'-wrapper iframe'),
    insertAfter: '<p><span id="__caret">_</span></p>',
    postprocessDrop: function(target, drop, element) {
      // Get our special span, select it, delete it, and hope the caret
      // resets correctly.
      tiny_instance.selection.select(tiny_instance.dom.get('__caret'));
      tiny_instance.execCommand('Delete', false, null);
      tiny_instance.dom.remove('__caret');

      // Add some classes to the library items
      $(element).addClass('dnd-inserted'); 
      $(element).parents('.editor-item').addClass('dnd-child-inserted');
    }
  }, settings);

  $(settings.drop_selector).dnd(settings);
}