diff dnd.module @ 2:5a44c430b7ac

Added dnd_test module, tons and tons of fixes and changes.
author David Eads <eads@chicagotech.org>
date Tue, 17 Feb 2009 15:34:56 -0600
parents cee053085755
children 5df0783706f7
line wrap: on
line diff
--- a/dnd.module	Mon Feb 16 01:39:54 2009 -0600
+++ b/dnd.module	Tue Feb 17 15:34:56 2009 -0600
@@ -1,5 +1,8 @@
 <?php
 
+// A suffix for auto generated IDs
+define(DND_ID_SUFFIX, '-dnd-library');
+
 /**
  * Implementation of hook_menu().
  */
@@ -21,3 +24,120 @@
 function dnd_perm() {
   return array('administer dnd');
 }
+
+/**
+ * Implementation of hook_theme().
+ */
+function dnd_theme() {
+  return array(
+    'dnd_library' => array('arguments' => array('element' => NULL, 'settings' => NULL), 'template' => 'dnd-library'),
+  );
+}
+
+
+/**
+ * Implementation of hook_elements().
+ *
+ * Overload textareas.
+ */
+function dnd_elements() {
+  $type = array();
+  $type['textarea'] = array(
+    '#input' => TRUE, 
+    '#cols' => 60, 
+    '#rows' => 5, 
+    '#resizable' => TRUE,
+    '#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, $form_state) {
+  if ($element['#dnd-enabled']) {
+
+    drupal_add_js(drupal_get_path('module', 'dnd') .'/dnd/dnd.js');
+    drupal_add_js(drupal_get_path('module', 'dnd') .'/js/dnd-library.js', 'footer');
+
+    $settings = array();
+
+    // We take a string or an 
+    if (is_string($element['#dnd-settings'])) {
+      // @TODO load settings
+    } 
+    else if (is_object($element['#dnd-settings'])) {
+      $settings = (array) $element['#dnd-settings'];
+    }
+    else if (is_array($element['#dnd-settings'])) {
+      $settings = $element['#dnd-settings'];
+    }
+
+    // Set some important defaults
+    $settings = array('library_id' => $element['#id'] . DND_ID_SUFFIX) + $settings;
+
+    // Add enabled libraries to settings for tracking
+    drupal_add_js(array(
+      'dndEnabledLibraries' => array($element['#id'] => $settings),
+    ), 'setting');
+
+    $element['#prefix'] = theme('dnd_library', $element, $settings);
+  }
+  return $element;
+}
+
+function template_preprocess_dnd_library(&$variables) {
+  global $base_url;
+  list($element, $settings) = array($variables['element'], $variables['settings']);
+
+  // Get library via a backdoor HTTP request.  This is plenty fast for this
+  // application and keeps things nice and consistent.
+  if (!($url = parse_url($settings['url']))) {
+    return t('This library is not available');
+  }
+
+  // Handle both relative and absolute urls
+  if (!isset($url['scheme'])) {
+    $settings['url'] = $base_url .'/'. $settings['url'];
+  }
+
+  $request = drupal_http_request($settings['url']);
+
+  // We must remove some Drupal escaping
+  $json = json_decode(str_replace(array('\x3c', '\x3e', '\x26'), array("<", ">", "&"), $request->data), TRUE);
+
+  foreach ($json['library'] as $id=>$item) {
+    $editor_representations[$id] = filter_xss_admin($item['editor']);
+    $library_representations[$id] = filter_xss_admin($item['library']);
+  }
+
+  $variables['library_id'] = $settings['library_id'];
+  $variables['header'] = filter_xss_admin($json['header']);
+  $variables['items'] = $library_representations;
+  $variables['footer'] = filter_xss_admin($json['footer']);
+
+  // Store editor representations in Drupal setting
+  drupal_add_js(array(
+    'dndEditorRepresentations' => $editor_representations, 
+  ), 'setting');
+}