Index: modules/block/block.module =================================================================== RCS file: /cvs/drupal/drupal/modules/block/block.module,v retrieving revision 1.284 diff -u -r1.284 block.module --- modules/block/block.module 21 Oct 2007 18:59:01 -0000 1.284 +++ modules/block/block.module 26 Oct 2007 05:18:38 -0000 @@ -242,7 +242,7 @@ if (!empty($old_blocks[$module][$delta])) { $block['status'] = $old_blocks[$module][$delta]->status; $block['weight'] = $old_blocks[$module][$delta]->weight; - $block['region'] = $old_blocks[$module][$delta]->region; + $block['region'] = empty($old_blocks[$module][$delta]->region) ? BLOCK_REGION_NONE : $old_blocks[$module][$delta]->region; $block['visibility'] = $old_blocks[$module][$delta]->visibility; $block['pages'] = $old_blocks[$module][$delta]->pages; $block['custom'] = $old_blocks[$module][$delta]->custom; Index: modules/block/block.admin.inc =================================================================== RCS file: /cvs/drupal/drupal/modules/block/block.admin.inc,v retrieving revision 1.9 diff -u -r1.9 block.admin.inc --- modules/block/block.admin.inc 5 Oct 2007 09:35:09 -0000 1.9 +++ modules/block/block.admin.inc 26 Oct 2007 05:18:38 -0000 @@ -36,15 +36,12 @@ init_theme(); $throttle = module_exists('throttle'); - $block_regions = array(BLOCK_REGION_NONE => '<'. t('none') .'>') + system_region_list($theme_key); + $block_regions = system_region_list($theme_key) + array(BLOCK_REGION_NONE => '<'. t('none') .'>'); // Build form tree $form = array( '#action' => arg(3) ? url('admin/build/block/list/'. $theme_key) : url('admin/build/block'), '#tree' => TRUE, - '#cache' => TRUE, - '#prefix' => '
', - '#suffix' => '
', ); foreach ($blocks as $i => $block) { $key = $block['module'] .'_'. $block['delta']; @@ -69,7 +66,7 @@ ); $form[$key]['region'] = array( '#type' => 'select', - '#default_value' => $block['status'] ? (isset($block['region']) ? $block['region'] : system_default_region($theme_key)) : BLOCK_REGION_NONE, + '#default_value' => $block['region'], '#options' => $block_regions, ); @@ -82,20 +79,9 @@ } } - // Attach the AHAH events to the submit button. Set the AHAH selector to every - // select element in the form. The AHAH event could be attached to every select - // element individually, but using the selector is more efficient, especially - // on a page where hundreds of AHAH enabled elements may be present. $form['submit'] = array( '#type' => 'submit', '#value' => t('Save blocks'), - '#ahah' => array( - 'path' => 'admin/build/block/list/js/'. $theme_key, - 'selector' => '#block-admin-display-form-wrapper select', - 'wrapper' => 'block-admin-display-form-wrapper', - 'event' => 'change', - 'effect' => 'fade', - ), ); return $form; @@ -115,104 +101,28 @@ } /** - * Javascript callback for AHAH replacement. Re-generate the form with the - * updated values and return necessary html. - */ -function block_admin_display_js($theme = NULL) { - // Load the cached form. - $form_cache = cache_get('form_'. $_POST['form_build_id'], 'cache_form'); - - // Set the new weights and regions for each block. - $blocks = array(); - foreach (element_children($form_cache->data) as $key) { - $field = $form_cache->data[$key]; - if (isset($field['info'])) { - $block = array( - 'module' => $field['module']['#value'], - 'delta' => $field['delta']['#value'], - 'info' => html_entity_decode($field['info']['#value'], ENT_QUOTES), - 'region' => $_POST[$key]['region'], - 'weight' => $_POST[$key]['weight'], - 'status' => $_POST[$key]['region'] == BLOCK_REGION_NONE ? 0 : 1, - ); - - $throttle = module_exists('throttle'); - if ($throttle) { - $block['throttle'] = $_POST[$key]['throttle']; - } - - if ($block['weight'] != $form_cache->data[$key]['weight']['#default_value'] || $block['region'] != $form_cache->data[$key]['region']['#default_value']) { - $changed_block = $block['module'] .'_'. $block['delta']; - } - - $blocks[] = $block; - } - } - - // Resort the blocks with the new weights. - usort($blocks, '_block_compare'); - - // Create a form in the new order. - $form_state = array('submitted' => FALSE); - $form = block_admin_display_form($form_state, $blocks, $theme); - - // Maintain classes set on individual blocks. - foreach (element_children($form_cache->data) as $key) { - if (isset($form_cache->data[$key]['#attributes'])) { - $form[$key]['#attributes'] = $form_cache->data[$key]['#attributes']; - } - } - - // Preserve the order of the new form while merging the previous data. - $form_order = array_flip(array_keys($form)); // Save the form order. - $form = array_merge($form_cache->data, $form); // Merge the data. - $form = array_merge($form_order, $form); // Put back into the correct order. - - // Add a permanent class to the changed block. - $form[$changed_block]['#attributes']['class'] = 'block-modified'; - - cache_set('form_'. $_POST['form_build_id'], $form, 'cache_form', $form_cache->expire); - - // Add a temporary class to mark the new AHAH content. - $form[$changed_block]['#attributes']['class'] = empty($form[$changed_block]['#attributes']['class']) ? 'ahah-new-content' : $form[$changed_block]['#attributes']['class'] .' ahah-new-content'; - $form['js_modified'] = array( - '#type' => 'value', - '#value' => TRUE, - ); - - $form['#post'] = $_POST; - $form['#theme'] = 'block_admin_display_form'; - - // Add messages to our output. - drupal_set_message(t('Your settings will not be saved until you click the Save blocks button.'), 'warning'); - - // Render the form. - drupal_alter('form', $form, array(), 'block_admin_display_form'); - $form = form_builder('block_admin_display_form', $form, $form_state); - - // Remove the wrapper from the form to prevent duplicate div IDs. - unset($form['#prefix'], $form['#suffix']); - - $output = drupal_render($form); - - // Return the output in JSON format. - drupal_json(array('status' => TRUE, 'data' => $output)); -} - -/** * Helper function for sorting blocks on admin/build/block. * * Active blocks are sorted by region, then by weight. * Disabled blocks are sorted by name. */ function _block_compare($a, $b) { - $status = $b['status'] - $a['status']; + global $theme_key; + static $regions; + + // We need the region list to correctly order by region. + if (!isset($regions)) { + $regions = array_flip(array_keys(system_region_list($theme_key))); + $regions[BLOCK_REGION_NONE] = count($regions); + } + // Separate enabled from disabled. + $status = $b['status'] - $a['status']; if ($status) { return $status; } - // Sort by region. - $place = strcmp($a['region'], $b['region']); + // Sort by region (in the order defined by theme .info file). + $place = $regions[$a['region']] - $regions[$b['region']]; if ($place) { return $place; } @@ -442,8 +352,9 @@ function template_preprocess_block_admin_display_form(&$variables) { global $theme_key; - $variables['throttle'] = module_exists('throttle'); $block_regions = system_region_list($theme_key); + $variables['throttle'] = module_exists('throttle'); + $variables['block_regions'] = $block_regions + array(BLOCK_REGION_NONE => t('Disabled')); // Highlight regions on page to provide visual reference. foreach ($block_regions as $key => $value) { @@ -460,6 +371,10 @@ // Fetch region for current block. $region = $block['region']['#default_value']; + // Set special classes needed for table drag and drop. + $variables['form'][$i]['region']['#attributes']['class'] = 'block-region-select block-region-'. $region; + $variables['form'][$i]['weight']['#attributes']['class'] = 'block-weight block-weight-'. $region; + // Track first block listing to insert region header inside block_admin_display.tpl.php. $is_region_first = FALSE; if ($last_region != $region) { @@ -473,6 +388,7 @@ } } + $variables['block_listing'][$i]->region = $region; $variables['block_listing'][$i]->is_region_first = $is_region_first; $variables['block_listing'][$i]->row_class = isset($block['#attributes']['class']) ? $block['#attributes']['class'] : ''; $variables['block_listing'][$i]->block_modified = isset($block['#attributes']['class']) && strpos($block['#attributes']['class'], 'block-modified') !== FALSE ? TRUE : FALSE; @@ -483,6 +399,7 @@ $variables['block_listing'][$i]->throttle_check = $variables['throttle'] ? drupal_render($block['throttle']) : ''; $variables['block_listing'][$i]->configure_link = drupal_render($block['configure']); $variables['block_listing'][$i]->delete_link = !empty($block['delete']) ? drupal_render($block['delete']) : ''; + $variables['block_listing'][$i]->printed = FALSE; $last_region = $region; } Index: modules/block/block-admin-display-form.tpl.php =================================================================== RCS file: /cvs/drupal/drupal/modules/block/block-admin-display-form.tpl.php,v retrieving revision 1.1 diff -u -r1.1 block-admin-display-form.tpl.php --- modules/block/block-admin-display-form.tpl.php 5 Oct 2007 09:36:52 -0000 1.1 +++ modules/block/block-admin-display-form.tpl.php 26 Oct 2007 05:18:38 -0000 @@ -25,7 +25,15 @@ * @see theme_block_admin_display() */ ?> - + $title) { + drupal_add_tabledrag('blocks', 'block-region-select', 'match', 'sibling', 'block-region-'. $region, NULL, FALSE); + drupal_add_tabledrag('blocks', 'block-weight', 'order', 'sibling', 'block-weight-'. $region, NULL, FALSE); + } +?> @@ -42,14 +50,19 @@ - + $data): ?> is_region_first): ?> - - - - + $title): ?> + region || $block_regions[$region]): ?> + + + + + region) { $row++; break; } ?> + + - + Index: modules/block/block.css =================================================================== RCS file: /cvs/drupal/drupal/modules/block/block.css,v retrieving revision 1.5 diff -u -r1.5 block.css --- modules/block/block.css 10 Oct 2007 10:24:25 -0000 1.5 +++ modules/block/block.css 26 Oct 2007 05:18:38 -0000 @@ -3,8 +3,8 @@ #blocks td.region { font-weight: bold; } -#blocks td.block { - padding-left: 1.5em; /* LTR */ +#blocks tr.empty-region { + display: none; } .block-region { background-color: #ff6; @@ -12,12 +12,3 @@ margin-bottom: 4px; padding: 3px; } -#blocks select { - margin-right: 24px; /* LTR */ -} -#blocks select.progress-disabled { - margin-right: 0px; /* LTR */ -} -#blocks tr.ahah-new-content { - background-color: #ffd; -} Index: modules/block/block-rtl.css =================================================================== RCS file: modules/block/block-rtl.css diff -N modules/block/block-rtl.css --- modules/block/block-rtl.css 10 Oct 2007 10:24:25 -0000 1.3 +++ /dev/null 1 Jan 1970 00:00:00 -0000 @@ -1,15 +0,0 @@ -/* $Id: block-rtl.css,v 1.3 2007/10/10 10:24:25 goba Exp $ */ - -#blocks td.block { - padding-left: inherit; - padding-right: 1.5em; -} -#blocks select { - margin-left: 24px; -} -#blocks select.progress-disabled { - margin-left: 0px; -} -#blocks .progress .bar { - float: right; -} Index: modules/system/system-rtl.css =================================================================== RCS file: /cvs/drupal/drupal/modules/system/system-rtl.css,v retrieving revision 1.6 diff -u -r1.6 system-rtl.css --- modules/system/system-rtl.css 10 Oct 2007 10:24:25 -0000 1.6 +++ modules/system/system-rtl.css 26 Oct 2007 05:18:38 -0000 @@ -17,6 +17,9 @@ text-align: left; } +div.indentation { + float: right; +} dl.multiselect dt, dl.multiselect dd { float: right; margin: 0 0 0 1em; Index: modules/system/system.css =================================================================== RCS file: /cvs/drupal/drupal/modules/system/system.css,v retrieving revision 1.37 diff -u -r1.37 system.css --- modules/system/system.css 10 Oct 2007 10:24:25 -0000 1.37 +++ modules/system/system.css 26 Oct 2007 05:18:39 -0000 @@ -11,6 +11,9 @@ border-bottom: 1px solid #ccc; padding: 0.1em 0.6em; } +tr.highlight { + background-color: #ffd; +} td.active { background-color: #ddd; } @@ -32,6 +35,21 @@ .breadcrumb { padding-bottom: .5em } +div.indentation { + width: 20px; + margin: -0.4em; + padding: 0.4em; + float: left; /* LTR */ +} +div.tree-child { + background: url(../../misc/tree.png) left center; +} +div.tree-child-last { + background: url(../../misc/tree-bottom.png) left center; +} +div.tree-horizontal { + background: url(../../misc/tree.png) -11px center; +} .error { color: #e55; } @@ -173,7 +191,6 @@ height: 1.75em; margin: 0; } - /* ** Inline items (need to override above) */ @@ -333,6 +350,26 @@ } /* +** Table drag and drop. +*/ +.tabledrag-handle { + cursor: move; + float: left; + height: 1.7em; + margin: -0.42em 0 -0.42em -0.5em; + padding: 0.42em 1.5em 0.42em 0.5em; +} +.tabledrag-handle .handle { + margin-top: 0.3em; + height: 13px; + width: 13px; + background: url(../../misc/draggable.png) no-repeat 0 0; +} +.tabledrag-handle-hover .handle { + background-position: 0 -20px; +} + +/* ** Teaser splitter */ .joined + .grippie { Index: modules/menu/menu.admin.inc =================================================================== RCS file: /cvs/drupal/drupal/modules/menu/menu.admin.inc,v retrieving revision 1.4 diff -u -r1.4 menu.admin.inc --- modules/menu/menu.admin.inc 21 Oct 2007 18:59:02 -0000 1.4 +++ modules/menu/menu.admin.inc 26 Oct 2007 05:18:38 -0000 @@ -21,11 +21,10 @@ } /** - * Shows for one menu the menu items accessible to the current user and relevant operations. + * Form for editing an entire menu tree at once. Shows for one menu the menu + * items accessible to the current user and relevant operations. */ -function menu_overview($menu) { - - $header = array(t('Menu item'), t('Expanded'), array('data' => t('Operations'), 'colspan' => '3')); +function menu_overview_form(&$form_state, $menu) { $sql =" SELECT m.load_functions, m.to_arg_functions, m.access_callback, m.access_arguments, m.page_callback, m.page_arguments, m.title, m.title_callback, m.title_arguments, m.type, ml.* FROM {menu_links} ml LEFT JOIN {menu_router} m ON m.path = ml.router_path @@ -37,63 +36,137 @@ $node_links = array(); menu_tree_collect_node_links($tree, $node_links); menu_tree_check_access($tree, $node_links); - $rows = _menu_overview_tree($tree); - $output = theme('table', $header, $rows); - $output .= theme('pager', NULL, 200, 0); - return $output; + + $form = _menu_overview_tree_form($form_state, $tree); + $form['submit'] = array( + '#type' => 'submit', + '#value' => t('Submit'), + ); + return $form; } /** - * Recursive helper function for menu_overview(). + * Recursive helper function for menu_overview_form(). */ -function _menu_overview_tree($tree) { - static $rows = array(); +function _menu_overview_tree_form(&$form_state, $tree) { + static $form = array('#tree' => TRUE); foreach ($tree as $data) { $title = ''; $item = $data['link']; // Don't show callbacks; these have $item['hidden'] < 0. if ($item && $item['hidden'] >= 0) { - $title = str_repeat('  ', $item['depth'] - 1) . ($item['depth'] > 1 ? '- ' : ''); - $title .= l($item['title'], $item['href'], $item['options']); - // Populate the operations field. + $mlid = $item['mlid']; + $form[$mlid] = array(); + $form[$mlid]['title'] = array( + '#value' => l($item['title'], $item['href'], $item['options']), + ); + $form[$mlid]['expanded'] = array( + '#value' => $item['has_children'] ? (($item['expanded']) ? t('Yes') : t('No')) : '', + ); + $form[$mlid]['weight'] = array( + '#type' => 'weight', + '#default_value' => isset($form_state[$mlid]['weight']) ? $form_state[$mlid]['weight'] : $item['weight'], + ); + $form[$mlid]['depth'] = array( + '#type' => 'hidden', + '#value' => $item['depth'], + ); + $form[$mlid]['mlid'] = array( + '#type' => 'hidden', + '#value' => $item['mlid'], + ); + $form[$mlid]['plid'] = array( + '#type' => 'textfield', + '#default_value' => isset($form_state[$mlid]['plid']) ? $form_state[$mlid]['plid'] : $item['plid'], + '#size' => 6, + ); + + // Build a list of operations. $operations = array(); - // Set the edit column. - $operations[] = array('data' => l(t('edit'), 'admin/build/menu/item/'. $item['mlid'] .'/edit')); + $operations['edit'] = l(t('edit'), 'admin/build/menu/item/'. $item['mlid'] .'/edit'); if ($item['hidden']) { - $title .= ' ('. t('disabled') .')'; - $class = 'menu-disabled'; - $operations[] = array('data' => l(t('enable'), 'admin/build/menu/item/'. $item['mlid'] .'/enable')); + $form[$mlid]['title']['#value'] .= ' ('. t('disabled') .')'; + $form[$mlid]['#attributes']['class'] = 'menu-disabled'; + $operations['enable'] = l(t('enable'), 'admin/build/menu/item/'. $item['mlid'] .'/enable'); } else { - $class = 'menu-enabled'; - $operations[] = array('data' => l(t('disable'), 'admin/build/menu/item/'. $item['mlid'] .'/disable')); + $form[$mlid]['#attributes']['class'] = 'menu-enabled'; + $operations['disable'] = l(t('disable'), 'admin/build/menu/item/'. $item['mlid'] .'/disable'); } // Only items created by the menu module can be deleted. if ($item['module'] == 'menu') { - $operations[] = array('data' => l(t('delete'), 'admin/build/menu/item/'. $item['mlid'] .'/delete')); + $operations['delete'] = l(t('delete'), 'admin/build/menu/item/'. $item['mlid'] .'/delete'); } // Set the reset column. elseif ($item['module'] == 'system' && $item['customized']) { - $operations[] = array('data' => l(t('reset'), 'admin/build/menu/item/'. $item['mlid'] .'/reset')); + $operations['reset'] = l(t('reset'), 'admin/build/menu/item/'. $item['mlid'] .'/reset'); } - else { - $operations[] = array('data' => ''); - } - $row = array( - array('data' => $title, 'class' => $class), - array('data' => ($item['has_children'] ? (($item['expanded']) ? t('Yes') : t('No')) : ''), 'class' => $class), - ); - foreach ($operations as $operation) { - $operation['class'] = $class; - $row[] = $operation; + + $form[$mlid]['operations'] = array(); + foreach ($operations as $op => $value) { + $form[$mlid]['operations'][$op] = array('#value' => $value); } - $rows[] = $row; } + if ($data['below']) { - _menu_overview_tree($data['below']); + _menu_overview_tree_form($form_state, $data['below']); } } - return $rows; + return $form; +} + +/** + * Theme the menu overview form into a sortable drag and drop table. + */ +function theme_menu_overview_form($form) { + // Add the draggable table javascript. + drupal_add_tabledrag('menu-overview', 'menu-plid', 'match', 'parent', 'menu-plid', 'menu-mlid', FALSE); + drupal_add_tabledrag('menu-overview', 'menu-weight', 'order', 'sibling', 'menu-weight', NULL, FALSE); + + $header = array(t('Menu item'), t('Expanded'), t('Parent'), t('Weight'), array('data' => t('Operations'), 'colspan' => '3')); + $rows = array(); + foreach (element_children($form) as $mlid) { + if (isset($form[$mlid]['mlid'])) { + $plid = $form[$mlid]['plid']['#value']; + + // Build a list of operations. + $operations = array(); + foreach (element_children($form[$mlid]['operations']) as $op) { + $operations[] = drupal_render($form[$mlid]['operations'][$op]); + } + while (count($operations) <= 3) { + $operations[] = ''; + } + + // Add special classes to be used for tabledrag.js. + $form[$mlid]['plid']['#attributes']['class'] = 'menu-plid menu-plid-'. $plid; + $form[$mlid]['mlid']['#attributes']['class'] = 'menu-mlid menu-mlid-'. $plid; + $form[$mlid]['weight']['#attributes']['class'] = 'menu-weight menu-weight-'. $plid; + + // Make indentations. + $indentation = ''; + for ($n = 1; $n < $form[$mlid]['depth']['#value']; $n++) { + $indentation .= '
 
'; + } + + $row = array(); + $row[] = $indentation . drupal_render($form[$mlid]['depth']) . drupal_render($form[$mlid]['title']); + $row[] = drupal_render($form[$mlid]['expanded']); + $row[] = drupal_render($form[$mlid]['plid']) . drupal_render($form[$mlid]['mlid']); + $row[] = drupal_render($form[$mlid]['weight']); + $row = array_merge($row, $operations); + + foreach ($row as $n => $data) { + $row[$n] = array('data' => $data); + $row[$n] = array_merge($row[$n], $form[$mlid]['#attributes']); + } + $rows[] = array('data' => $row, 'class' => 'draggable'); + } + } + $output = ''; + $output .= theme('table', $header, $rows, array('id' => 'menu-overview')); + $output .= drupal_render($form); + return $output; } /** Index: modules/menu/menu.module =================================================================== RCS file: /cvs/drupal/drupal/modules/menu/menu.module,v retrieving revision 1.146 diff -u -r1.146 menu.module --- modules/menu/menu.module 21 Oct 2007 18:59:02 -0000 1.146 +++ modules/menu/menu.module 26 Oct 2007 05:18:38 -0000 @@ -80,8 +80,8 @@ ); $items['admin/build/menu-customize/%menu'] = array( 'title' => 'Customize menu', - 'page callback' => 'menu_overview', - 'page arguments' => array(3), + 'page callback' => 'drupal_get_form', + 'page arguments' => array('menu_overview_form', 3), 'title callback' => 'menu_overview_title', 'title arguments' => array(3), 'access arguments' => array('administer menu'), @@ -155,6 +155,18 @@ } /** + * Implemenation of hook_theme(). + */ +function menu_theme() { + return array( + 'menu_overview_form' => array( + 'file' => 'menu.admin.inc', + 'arguments' => array('form' => NULL), + ), + ); +} + +/** * Implementation of hook_enable() * * Add a link for each custom menu. Index: themes/garland/style.css =================================================================== RCS file: /cvs/drupal/drupal/themes/garland/style.css,v retrieving revision 1.27 diff -u -r1.27 style.css --- themes/garland/style.css 11 Oct 2007 09:51:29 -0000 1.27 +++ themes/garland/style.css 26 Oct 2007 05:18:40 -0000 @@ -226,6 +226,10 @@ background-color: #fff; } +tr.highlight { + background-color: #ffd; +} + tr.odd td.active { background-color: #ddecf5; } Index: includes/common.inc =================================================================== RCS file: /cvs/drupal/drupal/includes/common.inc,v retrieving revision 1.704 diff -u -r1.704 common.inc --- includes/common.inc 19 Oct 2007 10:30:54 -0000 1.704 +++ includes/common.inc 26 Oct 2007 05:18:38 -0000 @@ -1915,6 +1915,25 @@ return $output; } +function drupal_add_tabledrag($table_id, $group, $action, $relationship, $target, $source = NULL, $hidden = FALSE) { + static $js_added = FALSE; + if (!$js_added) { + drupal_add_js('misc/tabledrag.js', 'core'); + $js_added = TRUE; + } + + // If source isn't set, assume it is the same as the target. + $source = isset($source) ? $source : $target; + $settings['tabledrag'][$table_id][$group][] = array( + 'target' => $target, + 'source' => $source, + 'relationship' => $relationship, + 'action' => $action, + 'hidden' => $hidden, + ); + drupal_add_js($settings, 'setting'); +} + /** * Aggregate JS files, putting them in the files directory. * Index: misc/tabledrag.js =================================================================== RCS file: misc/tabledrag.js diff -N misc/tabledrag.js --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ misc/tabledrag.js 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,602 @@ +// $Id $ + +Drupal.behaviors.tableDrag = function(context) { + for (var base in Drupal.settings.tabledrag) { + if (!$('#' + base + '.tabledrag-processed').size(), context) { + var tableSettings = Drupal.settings.tabledrag[base]; + + $('#' + base + ':not(.tabledrag-processed)').each(function() { + // Create the new tabledrag instance. Save in the Drupal variable + // to allow other scripts access to the object. + Drupal.tabledrag[base] = new Drupal.tabledrag(this, tableSettings); + }); + + $('#' + base).addClass('tabledrag-processed'); + } + } +} + +Drupal.tabledrag = function(table, tableSettings) { + var self = this; + + this.table = table; + this.tableSettings = tableSettings; + this.dragObject = null; // Keep hold of the current drag object if any. + this.oldY = 0; // Save processing by remembering x, y positions. + + // Check if this table contains indentations. + var indents = $('div.indentation', table); + this.indentEnabled = indents.size() > 0 ? true : false; + if (this.indentEnabled) { + var indentSize = indents.css('width'); + this.oldX = 0; + this.indentCount = 1; // Total width of indents, set in makeDraggable. + this.indentIncrement = indentSize.replace(/[0-9\.]*/, ''); + this.indentAmount = parseInt(indentSize); + } + + // Make each applicable row draggable. + $('tr.draggable', table).each(function() { self.makeDraggable(this); }); + + // Hide columns containing affected form elements. + this.hideColumns(); + + // Add mouse bindings to the document. The self variable is passed along + // as event handlers do not have direct access to the tabledrag object. + $(document).bind('mousemove', { tabledrag: self }, self.dragRow); + $(document).bind('mouseup', { tabledrag: self }, self.dropRow); +} + +/** + * Hide the columns containing form elements according to the settings for + * this tabledrag instance. + */ +Drupal.tabledrag.prototype.hideColumns = function() { + for (group in this.tableSettings) { + var hidden = this.tableSettings[group][0]['hidden']; + var field = $('.' + this.tableSettings[group][0]['target'] + ':first', this.table); + var cell = field.parents('td:first, th:first'); + if (hidden && cell[0] && cell.css('display') != 'none') { + var columnIndex = $('td, th', field.parents('tr:first')).index(cell.get(0)); + columnIndex++; // nth-child selector is 1 based, not 0 based. + $('tr', this.table).each(function() { + // Find and hide the cell in the table body. + var cell = $('td:nth-child('+ columnIndex +')', this); + if (cell.size()) { + cell.css('display', 'none'); + } + // We might be dealing with a row spanning the entire table. + // Reduce the colspan on the first cell to prevent the cell from + // overshooting the table. + else { + cell = $('td:first', this); + cell.attr('colspan', cell.attr('colspan') - 1); + } + // Remove table header cells entirely (Safari doesn't hide properly). + var th = $('th:nth-child('+ columnIndex +')', this); + if (th.size()) { + th.remove(); + } + }); + } + } +} + +/** + * Find the target used within a particular row and group. + */ +Drupal.tabledrag.prototype.rowSettings = function(group, row) { + var field = $('.' + group, row); + for (delta in this.tableSettings[group]) { + var targetClass = this.tableSettings[group][delta]['target']; + if (field.is('.' + targetClass)) { + // Return a copy of the settings. + var rowSettings = new Object(); + rowSettings.__proto__ = this.tableSettings[group][delta]; + return rowSettings; + } + } +}; + +/** + * Take an item and add an onmousedown method so that we can make it draggable. + */ +Drupal.tabledrag.prototype.makeDraggable = function(item) { + if (!item) return; + var self = this; // Keep the context of the tabledrag object. + // Create the handle. + handle = $('
'); + // Insert the handle after indentations (if any). + if ($('td:first .indentation:last', item).after(handle).size()) { + // Update the total width of indentation in this entire table. + self.indentCount = Math.max($('.indentation', item).size(), self.indentCount); + } + else { + $('td:first', item).prepend(handle); + } + // Add hover action for the handle. + handle.hover(function() { + self.dragObject == null ? $(this).addClass('tabledrag-handle-hover') : null; + }, function() { + self.dragObject == null ? $(this).removeClass('tabledrag-handle-hover') : null; + }); + // Add the mousedown action for the handle. + handle.mousedown(function(event) { + self.dragObject = new Object(); + self.dragObject.element = item; + self.dragObject.group = new Array(item); + self.dragObject.initMouseOffset = self.getMouseOffset(item, event); + self.dragObject.initMousePos = self.mouseCoords(event); + if (self.indentEnabled) { + self.dragObject.indentMousePos = self.dragObject.initMousePos; + self.dragObject.indents = $('.indentation', item).size(); + self.dragObject.children = self.rowChildren(item, true); + self.dragObject.group = $.merge(self.dragObject.group, self.dragObject.children); + } + $(this).addClass('tabledrag-handle-hover'); + $(item).addClass('highlight drag'); + return false; + }); +} + +/** + * Mousemove event handler, bound to document. + */ +Drupal.tabledrag.prototype.dragRow = function(event) { + var self = event.data.tabledrag; + if (self.dragObject) { + var mousePos = self.mouseCoords(event); + var y = mousePos.y - self.dragObject.initMouseOffset.y; + var x = mousePos.x - self.dragObject.initMouseOffset.x; + + // Handle swapping rows. + if (y != self.oldY) { + // Going up or down... + var movingDown = y > self.oldY; + // Update the old value. + self.oldY = y; + // Check if we need to move any rows. + var currentRow = self.findDropTargetRow(x, y); + + if (currentRow && self.dragObject.element != currentRow) { + if (movingDown) { + $(currentRow).after($(self.dragObject.group)); + } + else if (!movingDown) { + $(currentRow).before($(self.dragObject.group)); + } + self.restripeTable(self.table); + } + } + + // Similar to row swapping, handle indentations. + if (self.indentEnabled && x != self.oldX) { + self.oldX = x; + var xDiff = mousePos.x - self.dragObject.indentMousePos.x; + // Set the number of indentations the mouse has been moved left or right. + var indentDiff = parseInt(xDiff / self.indentAmount); + // Limit the indentation to no less than the left edge of the table and no + // more than the total amount of indentation in the table. + indentDiff = indentDiff > 0 ? Math.min(indentDiff, self.indentCount - self.dragObject.indents + 1) : Math.max(indentDiff, -self.dragObject.indents); + if (indentDiff != 0) { + // Further limit the diff according to surrounding rows. + if (indentDiff > 0) { + var prevIndent = $('.indentation', $(self.dragObject.group).filter(':first').prev()).size(); + indentDiff = Math.min(prevIndent - self.dragObject.indents + 1, indentDiff); + } + else { + var nextIndent = $('.indentation', $(self.dragObject.group).filter(':last').next()).size(); + indentDiff = Math.max(nextIndent - self.dragObject.indents, indentDiff); + } + + for (var n = 1; n <= Math.abs(indentDiff); n++) { + // Add or remove indentations. + if (indentDiff < 0) { + $('.indentation:first', self.dragObject.group).remove(); + self.dragObject.indents--; + } + else { + $('td:first', self.dragObject.group).prepend(Drupal.theme('tabledragIndentation')); + self.dragObject.indents++; + } + } + // Update indentation for this actual row. + self.dragObject.indentMousePos.x += self.indentAmount * indentDiff; + // Update table total indentations. + self.indentCount = Math.max(self.indentCount, self.dragObject.indents); + } + } + + return false; + } +}; + +/** + * Mouseup event handler, bound to document. + */ +Drupal.tabledrag.prototype.dropRow = function(event) { + var self = event.data.tabledrag; + if (self.dragObject != null) { + var droppedRow = self.dragObject.element; + // The row is already in the right place so we just release it. + $(droppedRow).removeClass('highlight'); + $('.tabledrag-handle', droppedRow).removeClass('tabledrag-handle-hover'); + self.updateFields(droppedRow); + self.markChanged(droppedRow); + if (self.indentEnabled) { + self.removeIndentClasses(); + } + self.dragObject = null; + self.onDrop(droppedRow); + } +}; + +/** + * Stub function. Allows custom javascript to add a handler when the row is + * dropped. + */ +Drupal.tabledrag.prototype.onDrop = function(droppedRow) { + return null; +} + +/** + * Get the position of an element by going up the DOM tree and adding up all + * the offsets. + */ +Drupal.tabledrag.prototype.getPosition = function(event){ + var left = 0; + var top = 0; + // Safari fix -- thanks to Luis Chato for this! + if (event.offsetHeight == 0) { + /* Safari 2 doesn't correctly grab the offsetTop of a table row + this is detailed here: + http://jacob.peargrove.com/blog/2006/technical/table-row-offsettop-bug-in-safari/ + the solution is likewise noted there, grab the offset of a table cell + in the row - the firstChild. note that firefox will return a text node + as a first child, so designing a more thorough solution may need to + take that into account, this seems to work in Firefox, Safari, and IE. + */ + event = event.firstChild; // a table cell + } + + while (event.offsetParent){ + left += event.offsetLeft; + top += event.offsetTop; + event = event.offsetParent; + } + + left += event.offsetLeft; + top += event.offsetTop; + + return {x:left, y:top}; +} + +/** + * Get the mouse coordinates from the event (allowing for browser differences). + */ +Drupal.tabledrag.prototype.mouseCoords = function(event){ + if (event.pageX || event.pageY) { + return {x:event.pageX, y:event.pageY}; + } + return { + x:event.clientX + document.body.scrollLeft - document.body.clientLeft, + y:event.clientY + document.body.scrollTop - document.body.clientTop + }; +} + +/** + * Given a target element and a mouse event, get the mouse offset from that + * element. To do this we need the element's position and the mouse position. + */ +Drupal.tabledrag.prototype.getMouseOffset = function(target, event){ + event = event || window.event; + + var docPos = this.getPosition(target); + var mousePos = this.mouseCoords(event); + return {x:mousePos.x - docPos.x, y:mousePos.y - docPos.y}; +} + +/** + * Find the row the mouse is currently over. This row is then taken and swapped + * with the one being dragged. + */ +Drupal.tabledrag.prototype.findDropTargetRow = function(x, y) { + var rows = this.table.tBodies[0].rows; + for (var n=0; n (rowY - rowHeight)) && (y < (rowY + rowHeight))) { + + if (this.indentEnabled) { + // Check that this row is not a child of the row being dragged. + for (n in this.dragObject.children) { + if (this.dragObject.children[n] == row) { + return null; + } + } + // Check that moving this row will not cause an invalid parent + // relationship with the row below. + var thisIndents = $('.indentation', row).size(); + var prevIndents = $('.indentation', $(row).prev()).size(); + var nextIndents = $('.indentation', $(row).next()).size(); + var dragIndents = $('.indentation', this.dragObject.element).size(); + var lastDragIndents = $('.indentation', $(this.dragObject.group).filter(':last')).size(); + if (lastDragIndents < thisIndents || (lastDragIndents == thisIndents && nextIndents > thisIndents) || prevIndents + 1 < dragIndents) { + return null; + } + } + + // We've found the row the mouse just passed over, but it doesn't take + // into account hidden rows. Skip backwards until we find a draggable + // row. + while ($(row).is(':not(.draggable)') && $(row).prev().is(':not(.draggable)')) { + row = $(row).prev().get(0); + } + // Special case for the first row, if it's not draggable, do not allow + // another row to be put before it. + if (n == 0 && $(row).is(':not(.draggable)')) { + return null; + } + return row; + } + } + return null; +} + +/** + * After the row is dropped, update the fields inside of it according to the + * dragtable settings. + */ +Drupal.tabledrag.prototype.updateFields = function(changedRow) { + changedRow = changedRow || this.dragObject.element; + for (group in this.tableSettings) { + // Each group may have a different setting for relationship, so we find + // the source rows for each seperately. + var rowSettings = this.rowSettings(group, changedRow); + + // Siblings are easy, check previous and next rows. + if (rowSettings.relationship == 'sibling') { + var previousRow = $(changedRow).prev().get(0); + var nextRow = $(changedRow).next().get(0); + var sourceRow = changedRow; + if ($(previousRow).is('.draggable') && $('.' + group, previousRow).length) { + if (this.indentEnabled) { + if ($('.indentations', previousRow).size() == $('.indentations', changedRow)) { + sourceRow = previousRow; + } + } + else { + sourceRow = previousRow; + } + } + else if ($(nextRow).is('.draggable') && $('.' + group, nextRow).length) { + if (this.indentEnabled) { + if ($('.indentations', nextRow).size() == $('.indentations', changedRow)) { + sourceRow = nextRow; + } + } + else { + sourceRow = nextRow; + } + } + } + // Parents, look up the tree until we find a field not in this group. + // Go up as many parents as indentations in the changed row. + else if (rowSettings.relationship == 'parent') { + var previousRow = $(changedRow).prev(); + while (previousRow.length && $('.indentation', previousRow).length >= this.dragObject.indents) { + previousRow = previousRow.prev(); + } + // If we found a row. + if (previousRow.length) { + sourceRow = previousRow[0]; + } + // Otherwise we went all the way to the top of the table. + // Assume that the first item has no indentions. + else { + sourceRow = $('tr.draggable:first')[0]; + // Basically copy first item's value. + var originalSource = rowSettings.source; + var originalRelationship = rowSettings.relationship; + rowSettings.source = rowSettings.target; + rowSettings.relationship = 'sibling'; + } + } + + // Because we may have moved the row from one category to another, + // take a look at our sibling and borrow its sources and targets. + this.copyDragClasses(sourceRow, changedRow, group); + rowSettings = this.rowSettings(group, changedRow); + + var targetClass = '.' + rowSettings.target; + var targetElement = $(targetClass, changedRow).get(0); + // Check if a target element exists in this row. + if (targetElement) { + var sourceClass = '.' + rowSettings.source; + var sourceElement = $(sourceClass, sourceRow).get(0); + switch (rowSettings.action) { + case 'match': + // Update the value. + targetElement.value = sourceElement.value; + break; + case 'order': + var siblings = this.rowSiblings(group, changedRow); + if ($(targetElement).is('select')) { + // Get a list of acceptable values. + var values = new Array(); + $('option', targetElement).each(function() { + values.push(this.value); + }); + // Populate the values in the siblings. + $(targetClass, siblings).each(function() { + this.value = values.shift(); + }); + } + else { + // Assume a numeric input field. + var weight = 0; + $(targetClass, siblings).each(function() { + this.value = weight; + weight++; + }); + } + break; + } + } + } +} + +/** + * Find all children of a row by indentation. + * @param row + * DOM object for the row we're investingating children. + * @param addClasses + * Whether we want to add classes to this row to indicate the relationship. + */ +Drupal.tabledrag.prototype.rowChildren = function(row, addClasses) { + var parentIndentation = $('.indentation', row).length; + var currentRow = $(row, this.table).next('tr'); + var rows = new Array(); + var child = 0; + while (currentRow.length) { + var rowIndentation = $('.indentation', currentRow).length; + // A greater indentation indicates this is a child. + if (rowIndentation > parentIndentation) { + child++; + rows.push(currentRow[0]); + if (addClasses) { + $('.indentation', currentRow).each(function(indentNum) { + if (child == 1 && (indentNum == parentIndentation)) { + $(this).addClass('tree-child-first'); + } + if (indentNum == parentIndentation) { + $(this).addClass('tree-child'); + } + else if (indentNum > parentIndentation) { + $(this).addClass('tree-horizontal'); + } + }); + } + } + else { + if (addClasses) { + $('.indentation:nth-child(' + (parentIndentation + 1) + ')', currentRow.prev('tr.draggable')).addClass('tree-child-last'); + } + break; + } + currentRow = currentRow.next('tr.draggable'); + } + return rows; +} + +/** + * Find all siblings for a row, either according to its subgroup or indentation. + * Note that the passed in row is included in the list of siblings. + * + * @param group + * The field group we're checking siblings against. + * @param row + * The row for which we're finding siblings. + */ +Drupal.tabledrag.prototype.rowSiblings = function (group, row) { + var siblings = new Array(); + var rowSettings = this.rowSettings(group, row); + var directions = new Array('prev', 'next'); + var rowIndentation = $('.indentation', row).length; + for (d in directions) { + var checkRow = $(row)[directions[d]](); + while (checkRow.length) { + // Check that the sibling contains a similar target field. + if ($('.' + rowSettings.target, checkRow)) { + // Either add immediately if this is a flat table, or check to ensure + // that this row has the same level of indentaiton. + if (this.indentEnabled) { + var checkRowIndentation = $('.indentation', checkRow).length + } + + if (!(this.indentEnabled) || (checkRowIndentation == rowIndentation)) { + siblings.push(checkRow[0]); + } + else if (checkRowIndentation < rowIndentation) { + // No need to keep looking for siblings when we get to a parent. + break; + } + } + else { + break; + } + checkRow = $(checkRow)[directions[d]](); + } + // Since siblings are added in reverse order for previous, reverse the + // completed list of previous siblings. Add the current row and continue. + if (directions[d] == 'prev') { + siblings.reverse(); + siblings.push(row); + } + } + return siblings; +} + +/** + * Remove indentation helper classes. + */ +Drupal.tabledrag.prototype.removeIndentClasses = function() { + for (n in this.dragObject.children) { + $('.indentation', this.dragObject.children[n]) + .removeClass('tree-child') + .removeClass('tree-child-first') + .removeClass('tree-child-last') + .removeClass('tree-horizontal') + } +} + +/** + * Copy all special tabledrag classes from one row's form elements to a + * different one, removing any special classes that the destination row + * may have had. + */ +Drupal.tabledrag.prototype.copyDragClasses = function(sourceRow, targetRow, group) { + var sourceElement = $('.' + group, sourceRow); + var targetElement = $('.' + group, targetRow); + if (sourceElement.length && targetElement.length) { + targetElement[0].className = sourceElement[0].className; + } +} + +/** + * Add an asterisk or other marker to the changed row. + */ +Drupal.tabledrag.prototype.markChanged = function(changedRow) { + var marker = $('
' + Drupal.theme('tabledragChangedMarker') + '
'); + var cell = $('td:first', changedRow); + // Converting marker to a DOM object and using .html() to do string + // comparision ensures consitency across browsers. + if (cell.html().indexOf(marker.html()) == -1) { + cell.append(marker.html()); + } +} + +Drupal.tabledrag.prototype.restripeTable = function(table) { + // :even and :odd are reversed because jquery counts from 0 and + // we count from 1, so we're out of sync. + $('tbody tr:not(:hidden)', table) + .filter(':odd').filter('.odd') + .removeClass('odd').addClass('even') + .end().end() + .filter(':even').filter('.even') + .removeClass('even').addClass('odd'); +} + +Drupal.theme.prototype.tabledragChangedMarker = function () { + return '*'; +}; + +Drupal.theme.prototype.tabledragIndentation = function () { + return '
 
'; +}; Index: misc/tree.png =================================================================== RCS file: misc/tree.png diff -N misc/tree.png --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ misc/tree.png 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,5 @@ +âPNG + +
region_title; ?>
block_title; ?>block_modified ? '*' : ''; ?> region_select; ?> weight_select; ?>