You are here

dnd.module in Scald: Media Management made easy 6

File

dnd/dnd.module
View source
<?php

// A suffix for auto generated IDs
define('DND_ID_SUFFIX', '-dnd-library');

/**
 * Implementation of hook_menu().
 */
function dnd_menu() {
  $items = array();
  $items['admin/settings/dnd'] = array(
    'title' => 'Drag and Drop Library',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'dnd_admin_form',
    ),
    'description' => 'Configure drag-and-drop enabled textareas.',
    'access arguments' => array(
      'administer dnd',
    ),
    'file' => 'dnd.admin.inc',
  );
  return $items;
}

/**
 * Implementation of hook_perm().
 */
function dnd_perm() {
  return array(
    'administer dnd',
  );
}

/**
 * Implementation of hook_theme().
 */
function dnd_theme() {
  return array(
    'dnd_library_wrapper' => array(
      'arguments' => array(
        'settings' => NULL,
        'element' => NULL,
      ),
    ),
  );
}

/**
 * Implementation of hook_elements().
 *
 * Overload textareas.
 */
function dnd_elements() {
  $type = array();
  $type['textarea'] = array(
    '#dnd-enabled' => FALSE,
    '#dnd-settings' => NULL,
    '#process' => array(
      'form_expand_ahah',
      'dnd_process_textarea',
    ),
  );
  return $type;
}

/**
 * Settings array:
 * What should it take, if anything?  Probably a source * maybe editor specific configuration shit?
 *
 * - source for library json/ajax shit
 * - target selector
 * - item selector
 *
 * perhaps like so:
 *
 * global =>
 *   droppable targets
 *   library source for textarea
 *
 * tinymce/othereditor =>
 *   target selector logic
 *   configuration options
 *   callback should be smart about attachment and detachment
 */
function dnd_process_textarea($element, $edit, $form_state, $form) {
  if (isset($element['#dnd-enabled']) && $element['#dnd-enabled']) {
    dnd_add_library();
    $settings = array(
      'library_id' => $element['#id'] . DND_ID_SUFFIX,
      'url' => dnd_get_library(),
    );

    // We take a string or an object or an array
    if (is_object($element['#dnd-settings'])) {
      $settings = (array) $element['#dnd-settings'] + $settings;
    }
    else {
      if (is_array($element['#dnd-settings'])) {
        $settings = $element['#dnd-settings'] + $settings;
      }
    }
    drupal_add_js(array(
      'dndDropAreas' => array(
        $element['#id'] => $settings,
      ),
    ), 'setting');
  }
  return $element;
}

/**
 * Adds all the JS libraries for the library to the page.
 */
function dnd_add_library() {
  static $included = FALSE;
  if (!$included) {
    $included = TRUE;

    // BeautyTips
    drupal_add_js(drupal_get_path('module', 'dnd') . '/js/bt/other_libs/excanvas_0002/excanvas-compressed.js');
    drupal_add_js(drupal_get_path('module', 'dnd') . '/js/bt/other_libs/jquery.hoverIntent.minified.js');
    drupal_add_js(drupal_get_path('module', 'dnd') . '/js/bt/jquery.bt.js');

    // Dependencies
    drupal_add_js(drupal_get_path('module', 'dnd') . '/js/jquery.url.packed.js');
    drupal_add_js(drupal_get_path('module', 'dnd') . '/js/jquery.fieldselection.js');
    drupal_add_js('misc/jquery.form.js');

    // Drag and drop
    drupal_add_js(drupal_get_path('module', 'dnd') . '/js/jquery.draganddrop.js');
    drupal_add_js(drupal_get_path('module', 'dnd') . '/js/dnd-library.js');

    // Contains the library theming
    drupal_add_css(drupal_get_path('module', 'dnd') . '/css/dnd-library.css');

    // Settings for the library url
    drupal_add_js(array(
      'dnd' => array(
        'url' => url(dnd_get_library()),
      ),
    ), 'setting');
  }
}

/**
 * Tells DnD that the library shouldn't be displayed on this page.
 *
 * This function should be called whenever the library shouldn't be
 * displayed.
 *
 * @param boolean $set
 *   If FALSE, no change to the suppression status will be done, allowing
 *   other functions to query the suppression state. Defaults to TRUE.
 *
 * @return boolean
 *   TRUE if the library output has been suppressed, FALSE otherwise.
 */
function dnd_suppress_library($set = TRUE) {
  static $suppress = FALSE;
  if ($set && !$suppress) {
    $suppress = TRUE;
    drupal_add_js(array(
      'dnd' => array(
        'suppress' => 1,
      ),
    ), 'setting');
  }
  return $suppress;
}

/**
 * Implementation of hook_wywiwyg_plugin().
 */
function dnd_wysiwyg_plugin($editor, $version = 0) {
  $plugins = array();
  switch ($editor) {
    case 'tinymce':
      if ($version > 3) {
        $plugins['forcecontainer'] = array(
          'title' => t('Force Container Plugin'),
          'description' => t('A custom plugin to forces a selection up to the outer container of a given element.'),
          'extensions' => array(
            'forcecontainer' => t('Force Container'),
          ),
          'path' => drupal_get_path('module', 'dnd') . '/js/tinymce/forcecontainer/editor_plugin_src.js',
          'load' => TRUE,
          'options' => array(
            'forcecontainer_class' => 'dnd-drop-wrapper',
            'forcecontainer_trigger_dnd' => TRUE,
          ),
        );
      }
      break;
  }
  return $plugins;
}

/**
 * Theme the markup that will surround a library loaded via JSON.
 */
function theme_dnd_library_wrapper($settings, $element = NULL) {
  return '<div id="' . $settings['library_id'] . '" class="dnd-library-wrapper"></div>';
}

/**
 * Return the list of all the available libraries.
 * @return array
 *   An associative array, where the keys are the library paths, and
 *   the value is an associated label.
 */
function dnd_get_libraries() {
  static $libraries = NULL;
  if (is_null($libraries)) {
    $libraries = module_invoke_all('dnd_libraries_info');
    drupal_alter('dnd_libraries_info', $libraries);
  }
  return $libraries;
}

/**
 * Return the default library.
 */
function dnd_get_library() {
  $libraries = dnd_get_libraries();
  $default = variable_get('dnd_callback_url', '');
  if (isset($libraries[$default])) {
    $library = $default;
  }
  else {
    reset($libraries);
    $library = key($libraries);
  }
  return $library;
}

Functions

Namesort descending Description
dnd_add_library Adds all the JS libraries for the library to the page.
dnd_elements Implementation of hook_elements().
dnd_get_libraries Return the list of all the available libraries.
dnd_get_library Return the default library.
dnd_menu Implementation of hook_menu().
dnd_perm Implementation of hook_perm().
dnd_process_textarea Settings array: What should it take, if anything? Probably a source * maybe editor specific configuration shit?
dnd_suppress_library Tells DnD that the library shouldn't be displayed on this page.
dnd_theme Implementation of hook_theme().
dnd_wysiwyg_plugin Implementation of hook_wywiwyg_plugin().
theme_dnd_library_wrapper Theme the markup that will surround a library loaded via JSON.

Constants

Namesort descending Description
DND_ID_SUFFIX