annotate dnd.module @ 21:69db20fdbac2

Cleanup and refactoring.
author David Eads <eads@chicagotech.org>
date Tue, 10 Mar 2009 13:46:49 -0500
parents 89fe0aca43d4
children 9a92410be362
rev   line source
eads@1 1 <?php
eads@1 2
eads@2 3 // A suffix for auto generated IDs
eads@2 4 define(DND_ID_SUFFIX, '-dnd-library');
eads@2 5
eads@1 6 /**
eads@1 7 * Implementation of hook_menu().
eads@1 8 */
eads@1 9 function dnd_menu() {
eads@1 10 $items = array();
eads@1 11 $items['admin/settings/dnd'] = array(
eads@1 12 'title' => 'Drag and Drop Library',
eads@1 13 'page callback' => 'dnd_admin',
eads@1 14 'description' => 'Configure drag-and-drop enabled textareas.',
eads@1 15 'access arguments' => array('administer dnd'),
eads@1 16 'file' => 'dnd.admin.inc',
eads@1 17 );
eads@1 18 return $items;
eads@1 19 }
eads@1 20
eads@1 21 /**
eads@1 22 * Implementation of hook_perm().
eads@1 23 */
eads@1 24 function dnd_perm() {
eads@1 25 return array('administer dnd');
eads@1 26 }
eads@2 27
eads@2 28 /**
eads@2 29 * Implementation of hook_theme().
eads@2 30 */
eads@2 31 function dnd_theme() {
eads@2 32 return array(
eads@2 33 'dnd_library' => array('arguments' => array('element' => NULL, 'settings' => NULL), 'template' => 'dnd-library'),
eads@2 34 );
eads@2 35 }
eads@2 36
eads@2 37
eads@2 38 /**
eads@2 39 * Implementation of hook_elements().
eads@2 40 *
eads@2 41 * Overload textareas.
eads@2 42 */
eads@2 43 function dnd_elements() {
eads@2 44 $type = array();
eads@2 45 $type['textarea'] = array(
eads@2 46 '#input' => TRUE,
eads@2 47 '#cols' => 60,
eads@2 48 '#rows' => 5,
eads@2 49 '#resizable' => TRUE,
eads@2 50 '#dnd-enabled' => FALSE,
eads@2 51 '#dnd-settings' => NULL,
eads@2 52 '#process' => array('form_expand_ahah', 'dnd_process_textarea'),
eads@2 53 );
eads@2 54 return $type;
eads@2 55 }
eads@2 56
eads@2 57 /**
eads@2 58 * Settings array:
eads@21 59 * What should it take, if anything? Probably a source * maybe editor specific configuration shit?
eads@2 60 *
eads@2 61 * - source for library json/ajax shit
eads@2 62 * - target selector
eads@2 63 * - item selector
eads@2 64 *
eads@2 65 * perhaps like so:
eads@2 66 *
eads@2 67 * global =>
eads@2 68 * droppable targets
eads@2 69 * library source for textarea
eads@2 70 *
eads@2 71 * tinymce/othereditor =>
eads@2 72 * target selector logic
eads@2 73 * configuration options
eads@2 74 * callback should be smart about attachment and detachment
eads@2 75 */
eads@21 76 function dnd_process_textarea($element, $edit, $form_state, $form) {
eads@2 77 if ($element['#dnd-enabled']) {
eads@20 78
eads@20 79 // BeautyTips
eads@19 80 drupal_add_js(drupal_get_path('module', 'dnd') .'/js/bt/other_libs/excanvas_0002/excanvas-compressed.js');
eads@20 81 drupal_add_js(drupal_get_path('module', 'dnd') .'/js/bt/other_libs/jquery.hoverIntent.minified.js');
eads@20 82 drupal_add_js(drupal_get_path('module', 'dnd') .'/js/bt/jquery.bt.js');
eads@20 83
eads@20 84 // Dependencies
eads@18 85 drupal_add_js(drupal_get_path('module', 'dnd') .'/js/jquery.url.packed.js');
eads@18 86 drupal_add_js(drupal_get_path('module', 'dnd') .'/js/jquery.fieldselection.js');
eads@20 87 drupal_add_js('misc/jquery.form.js');
eads@20 88
eads@20 89 // Drag and drop
eads@18 90 drupal_add_js(drupal_get_path('module', 'dnd') .'/js/jquery.draganddrop.js');
eads@18 91 drupal_add_js(drupal_get_path('module', 'dnd') .'/js/dnd-library.js');
eads@2 92
eads@2 93 $settings = array();
eads@2 94
eads@18 95 // We take a string or an object or an array
eads@2 96 if (is_string($element['#dnd-settings'])) {
eads@2 97 // @TODO load settings
eads@2 98 }
eads@2 99 else if (is_object($element['#dnd-settings'])) {
eads@2 100 $settings = (array) $element['#dnd-settings'];
eads@2 101 }
eads@2 102 else if (is_array($element['#dnd-settings'])) {
eads@2 103 $settings = $element['#dnd-settings'];
eads@2 104 }
eads@2 105
eads@2 106 // Set some important defaults
eads@2 107 $settings = array('library_id' => $element['#id'] . DND_ID_SUFFIX) + $settings;
eads@2 108
eads@2 109 // Add enabled libraries to settings for tracking
eads@2 110 drupal_add_js(array(
eads@2 111 'dndEnabledLibraries' => array($element['#id'] => $settings),
eads@2 112 ), 'setting');
eads@2 113
eads@21 114 $element['#suffix'] = theme('dnd_library', $element, $settings) . $element['#suffix'];
eads@2 115 }
eads@2 116 return $element;
eads@2 117 }
eads@2 118
eads@18 119 /**
eads@18 120 * Drag and drop library template preprocessor.
eads@18 121 */
eads@2 122 function template_preprocess_dnd_library(&$variables) {
eads@2 123 global $base_url;
eads@2 124 list($element, $settings) = array($variables['element'], $variables['settings']);
eads@2 125
eads@2 126 // Get library via a backdoor HTTP request. This is plenty fast for this
eads@2 127 // application and keeps things nice and consistent.
eads@2 128 if (!($url = parse_url($settings['url']))) {
eads@2 129 return t('This library is not available');
eads@2 130 }
eads@2 131
eads@2 132 // Handle both relative and absolute urls
eads@2 133 if (!isset($url['scheme'])) {
eads@2 134 $settings['url'] = $base_url .'/'. $settings['url'];
eads@2 135 }
eads@2 136
eads@2 137 $request = drupal_http_request($settings['url']);
eads@2 138
eads@2 139 // We must remove some Drupal escaping
eads@2 140 $json = json_decode(str_replace(array('\x3c', '\x3e', '\x26'), array("<", ">", "&"), $request->data), TRUE);
eads@2 141
eads@4 142 // Generate an array of editor representations to add
eads@7 143 if (is_array($json['editor_representations'])) {
eads@7 144 foreach ($json['editor_representations'] as $editor_id=>$editor_item) {
eads@20 145 $editor_representations[$editor_id] = $editor_item;
eads@7 146 }
eads@2 147 }
eads@2 148
eads@17 149 // Generate an array of library previews to add
eads@17 150 if (is_array($json['library_previews'])) {
eads@17 151 foreach ($json['library_previews'] as $preview_id=>$preview_item) {
eads@20 152 $library_previews[$preview_id] = $preview_item;
eads@17 153 }
eads@17 154 }
eads@17 155
eads@4 156 // Store editor representations in Drupal setting
eads@17 157 drupal_add_js(array(
eads@17 158 'dndEditorRepresentations' => $editor_representations,
eads@17 159 'dndLibraryPreviews' => $library_previews,
eads@17 160 ), 'setting');
eads@4 161
eads@2 162 $variables['library_id'] = $settings['library_id'];
eads@20 163 $variables['header'] = $json['header'];
eads@20 164 $variables['library'] = $json['library'];
eads@20 165 $variables['footer'] = $json['footer'];
eads@2 166 }