changeset 28:7d6bf2dca269

Completely broken library registration, fixed a typo.
author David Eads <eads@chicagotech.org>
date Tue, 17 Mar 2009 06:25:47 -0500
parents 9a92410be362
children 37ca57016cbe
files dnd-library.tpl.php dnd.module js/dnd-library.js modules/dnd_test/dnd_test.module
diffstat 4 files changed, 43 insertions(+), 99 deletions(-) [+]
line wrap: on
line diff
--- a/dnd-library.tpl.php	Fri Mar 13 19:34:05 2009 -0500
+++ b/dnd-library.tpl.php	Tue Mar 17 06:25:47 2009 -0500
@@ -1,11 +1,3 @@
 <div id="<?php print $library_id; ?>" class="dnd-library-wrapper">
-  <div class="header">
-    <?php print $header; ?>
-  </div>
-  <div class="library">
-    <?php print $library; ?>
-  </div>
-  <div class="footer">
-    <?php print $footer; ?>
-  </div>
+  <?php print $library; ?>
 </div>
--- a/dnd.module	Fri Mar 13 19:34:05 2009 -0500
+++ b/dnd.module	Tue Mar 17 06:25:47 2009 -0500
@@ -30,7 +30,7 @@
  */
 function dnd_theme() {
   return array(
-    'dnd_library' => array('arguments' => array('element' => NULL, 'settings' => NULL), 'template' => 'dnd-library'),
+    'dnd_library' => array('arguments' => array('library' => NULL, 'library_id' => NULL), 'template' => 'dnd-library'),
   );
 }
 
@@ -76,20 +76,6 @@
 function dnd_process_textarea($element, $edit, $form_state, $form) {
   if ($element['#dnd-enabled']) {
 
-    // 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');
-
     $settings = array();
 
     // We take a string or an object or an array
@@ -104,63 +90,39 @@
     }
 
     // Set some important defaults
-    $settings = array('library_id' => $element['#id'] . DND_ID_SUFFIX) + $settings;
+    if (function_exists($settings['callback']) && ($library = $settings['callback']($element))) {
+      $settings = array('library_id' => $element['#id'] . DND_ID_SUFFIX) + $settings;
+      unset($settings['callback']);
 
-    // Add enabled libraries to settings for tracking
-    drupal_add_js(array(
-      'dndEnabledLibraries' => array($element['#id'] => $settings),
-    ), 'setting');
+      // 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');
 
-    $element['#suffix'] = theme('dnd_library', $element, $settings) . $element['#suffix'];
+      // 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');
+
+      drupal_add_js(array(
+        'dndEnabledLibraries' => array($element['#id'] => $settings),
+      ), 'setting');
+
+      // Store editor representations in Drupal setting
+      drupal_add_js(array(
+        'dndEditorRepresentations' => $library['editor_representations'], 
+        'dndLibraryPreviews' => $library['library_previews'], 
+      ), 'setting');
+      
+      $element['#suffix'] = theme('dnd_library', $library['library'], $settings['library_id']) . $element['#suffix'];
+    }
+
   }
   return $element;
 }
 
-/**
- * Drag and drop library template preprocessor.
- */
-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);
-
-  // Generate an array of editor representations to add
-  if (is_array($json['editor_representations'])) {
-    foreach ($json['editor_representations'] as $editor_id=>$editor_item) {
-      $editor_representations[$editor_id] = $editor_item;
-    }
-  }
-
-  // Generate an array of library previews to add
-  if (is_array($json['library_previews'])) {
-    foreach ($json['library_previews'] as $preview_id=>$preview_item) {
-      $library_previews[$preview_id] = $preview_item;
-    }
-  }
-
-  // Store editor representations in Drupal setting
-  drupal_add_js(array(
-    'dndEditorRepresentations' => $editor_representations, 
-    'dndLibraryPreviews' => $library_previews, 
-  ), 'setting');
-
-  $variables['library_id'] = $settings['library_id'];
-  $variables['header']     = $json['header'];
-  $variables['library']    = $json['library'];
-  $variables['footer']     = $json['footer'];
-}
+function template_preprocess_dnd_library($library) {}
--- a/js/dnd-library.js	Fri Mar 13 19:34:05 2009 -0500
+++ b/js/dnd-library.js	Tue Mar 17 06:25:47 2009 -0500
@@ -49,7 +49,7 @@
     $editor.bind('wysiwygDetach', Drupal.behaviors.dndLibrary.detach_library);
 
     // Add preview behavior to editor items (thanks, BeautyTips!)
-    $('.editor-item', context).each(function () {
+    $('.editor-item', $this).each(function () {
       $(this).bt(Drupal.settings.dndLibraryPreviews[this.id], bt_settings);
       var hover_opts = $.extend({
         'interval': 500,
@@ -266,7 +266,7 @@
       // If the next item exists and isn't an editor representation, drop the
       // caret at the beginning of the element, otherwise make a new paragraph
       // to advance the caret to.
-      if (next && !$(next).hasClass('dnd-droped-wrapper')) {
+      if (next && !$(next).hasClass('dnd-dropped-wrapper')) {
         $(next).prepend('<span id="__caret">_</span>');
       }
       else {
--- a/modules/dnd_test/dnd_test.module	Fri Mar 13 19:34:05 2009 -0500
+++ b/modules/dnd_test/dnd_test.module	Tue Mar 17 06:25:47 2009 -0500
@@ -7,7 +7,7 @@
   $items = array();
   $items['dnd-test/library'] = array(
     'title' => 'Drag and drop test library',
-    'page callback' => 'dnd_test_library',
+    'page callback' => 'dnd_test_dnd_library',
     'access arguments' => array('access dnd test library'),
   );
   return $items;
@@ -27,11 +27,11 @@
  */
 function dnd_test_form_alter(&$form, &$form_state) {
   if ($form['#id'] == 'node-form' && $form['type']['#value'] == 'page') {
-    drupal_add_css(drupal_get_path('module', 'dnd_test') .'/dnd_test.css');
     $form['body_field']['body']['#dnd-enabled'] = TRUE;
     $form['body_field']['body']['#dnd-settings'] = array(
       'drop_selector' => '#edit-body-dnd-library .drop',
-      'url' => 'dnd-test/library',
+      'callback' => 'dnd_test_dnd_library',
+      'url' => 'dnd-test/library/?json',
     );
     $form['body_field']['body']['#rows'] = 28;
   }
@@ -54,26 +54,16 @@
       'arguments' => array('i' => NULL),
       'template'  => 'dnd-library-preview',
     ),
-    'dnd_library_header' => array(
-      'arguments' => array('page' => NULL),
-      'template'  => 'dnd-library-header',
-    ),
   );
 }
 
 /**
- * Page callback that returns some JSON
+ * Overloaded page callback.
  */
-function dnd_test_library() {
-  $page = ($_GET['page']) ? $_GET['page'] : 1;
-  $test_library = dnd_test_generate_library($page);
-  return drupal_json(array(
-    'header'  => theme('dnd_library_header', $page),
-    'library' => $test_library['library'],
-    'editor_representations' => $test_library['editor_representations'],
-    'library_previews' => $test_library['library_previews'],
-    'footer'  => '<div class="pager">'. l(t('1'), 'dnd-test/library') . ' '. l(t('2'), 'dnd-test/library', array('query' => array('page' => 2))) .'</div>',
-  ));
+function dnd_test_dnd_library($element = NULL) {
+  $page = (array_key_exists('page', $_GET)) ? $_GET['page'] : 1;
+  drupal_add_css(drupal_get_path('module', 'dnd_test') .'/dnd_test.css');
+  drupal_json(dnd_test_generate_library($page));
 }
 
 /**
@@ -83,7 +73,7 @@
   $start = ($page * $limit) - ($limit); 
   $end   = $page * $limit;
 
-  $library = '';
+  $library = theme('dnd_library_header', $page);
   $editor_representations = array();
   $library_previews = array();
   for ($i=$start + 1; $i < $end + 1; $i++) {